gambit is hosted by Hepforge, IPPP Durham
GAMBIT  v1.5.0-2191-ga4742ac
a Global And Modular Bsm Inference Tool
util_types.hpp
Go to the documentation of this file.
1 // GAMBIT: Global and Modular BSM Inference Tool
2 // *********************************************
42 
43 #ifndef __util_types_hpp__
44 #define __util_types_hpp__
45 
46 #include <map>
47 #include <string>
48 #include <sstream>
49 #include <omp.h>
50 #include <cstring>
51 #include <complex>
52 
56 
57 
58 namespace Gambit
59 {
60 
62  typedef std::string str;
64  typedef std::pair<str, str> sspair;
66  typedef std::pair<double, double> ddpair;
68  typedef std::pair<int, int> iipair;
70  typedef std::pair<str, double> sdpair;
72  typedef std::map<std::string,double> map_str_dbl;
74  typedef std::map<std::string,int> map_str_int;
76  typedef std::map<std::string,std::map<std::string,double> > map_str_map_str_dbl;
78  typedef std::map<std::string,std::string> map_str_str;
80  typedef std::map<std::string,bool> map_str_bool;
82  typedef std::map<int,double> map_int_dbl;
84  typedef std::map<std::string,std::map<std::string,std::string> > map_str_map_str_str;
86  typedef std::map< std::pair < int, int >, double> map_intpair_dbl;
87 
89  typedef void (*fptr_void)();
90 
91  // Useful unqualified functions
92  using std::cout;
93  using std::cerr;
94  using std::endl;
95 
96  // A simple triplet class for holding a central value and aysmmetric +/- variations
97  template<typename TYPE>
98  struct triplet
99  {
103  : central(0.),
104  upper(0.),
105  lower(0.)
106  {}
108  triplet(TYPE centralval)
109  : central(centralval),
110  upper(0.),
111  lower(0.)
112  {}
114  triplet(TYPE centralval, TYPE upperval, TYPE lowerval)
115  : central(centralval),
116  upper(upperval),
117  lower(lowerval)
118  {}
121  : central(in.central),
122  upper(in.upper),
123  lower(in.lower)
124  {}
127  central = in.central;
128  upper = in.upper;
129  lower = in.lower;
130  return *this;
131  }
132  };
133 
135  typedef std::map<int,triplet<double> > map_int_triplet_dbl;
136 
137  // a tuple containg two doubles and a bool
139  {
140  double first, second;
141  bool flag;
144  : first(0.),
145  second(0.),
146  flag(0.)
147  {}
148 
150  dbl_dbl_bool(double firstval, double secondval, bool flagval)
151  : first(firstval),
152  second(secondval),
153  flag(flagval)
154  {}
157  : first(in.first),
158  second(in.second),
159  flag(in.flag)
160  {}
163  first = in.first;
164  second = in.second;
165  flag = in.flag;
166  return *this;
167  }
168  };
169 
170 
171 
174  template <typename TYPE>
175  class safe_ptr
176  {
177 
178  public:
179 
181  safe_ptr(TYPE* in_ptr = NULL) { ptr = in_ptr; }
182 
184  virtual void set(TYPE* in_ptr) { ptr = in_ptr; }
185 
187  virtual const TYPE& operator*() const
188  {
189  if (ptr == NULL) dieGracefully();
190  return *ptr;
191  }
192 
194  virtual const TYPE& operator[](int index) const
195  {
196  if (ptr == NULL) dieGracefully();
197  return *(ptr+index);
198  }
199 
201  virtual const TYPE* operator->() const
202  {
203  if (ptr == NULL) dieGracefully();
204  return ptr;
205  }
206 
207  protected:
208 
210  const TYPE* ptr;
211 
213  static void dieGracefully()
214  {
215  str errmsg = "You just tried to dereference a GAMBIT safe pointer that has value";
216  errmsg += "\nNULL. Bad idea. Probably you tried to retrieve a conditional"
217  "\ndependency that has not been activated because the necessary condition"
218  "\nhas not been met, or you tried to access a model parameter for a model"
219  "\nthat you are not actually scanning. This means there is a bug in one"
220  "\nof your module functions.";
221  utils_error().raise(LOCAL_INFO,errmsg);
222  }
223 
224  };
225 
226 
229  template <typename TYPE>
230  class omp_safe_ptr : public safe_ptr<TYPE>
231  {
232 
233  public:
234 
236  omp_safe_ptr(TYPE* in_ptr = NULL) : safe_ptr<TYPE>(in_ptr) {}
237 
239  virtual const TYPE& operator*() const
240  {
241  if (this->ptr == NULL) safe_ptr<TYPE>::dieGracefully();
242  return *(this->ptr+omp_get_thread_num());
243  }
244 
245  };
246 
251  template <typename TYPE>
253  {
254 
255  public:
256 
258  safe_variable_ptr & operator=(const safe_variable_ptr&) = delete;
259  safe_variable_ptr(const safe_variable_ptr&) = delete;
260 
262  safe_variable_ptr(TYPE* in_ptr = NULL) { ptr = in_ptr; }
263 
265  void set(TYPE* in_ptr) { ptr = in_ptr; }
266 
268  TYPE* get() { return ptr; }
269 
271  TYPE& operator*()
272  {
273  if (ptr == NULL) dieGracefully();
274  return *ptr;
275  }
276 
278  TYPE& operator[](int index)
279  {
280  if (ptr == NULL) dieGracefully();
281  return *(ptr+index);
282  }
283 
285  TYPE* operator->()
286  {
287  if (ptr == NULL) dieGracefully();
288  return ptr;
289  }
290 
291  protected:
292 
294  TYPE* ptr;
295 
297  static void dieGracefully()
298  {
299  str errmsg = "You just tried to dereference a GAMBIT safe variable pointer that has value";
300  errmsg += "\nNULL. Bad idea. Probably you tried to retrieve a conditional"
301  "\ndependency that has not been activated because the necessary condition"
302  "\nhas not been met.";
303  utils_error().raise(LOCAL_INFO,errmsg);
304  }
305 
306  };
307 
309  typedef std::map<std::string, safe_ptr<const double> > param_map_type;
310 
311 
312 
317  template <typename T, int... lims>
318  class Farray
319  {
320  protected:
321  static_assert(sizeof...(lims)%2==0, "Farray error: Odd number of index limits.");
322  static_assert(sizeof...(lims)!=0, "Farray error: No array index limits given.");
323  // Allowed array access types (expand if necessary)
324  typedef mult_types< short, const short, short&, const short&,
325  unsigned short, const unsigned short, unsigned short&, const unsigned short&,
326  int, const int, int&, const int&,
327  unsigned, const unsigned, unsigned&, const unsigned&,
328  long, const long, long&, const long&,
329  unsigned long, const unsigned long, unsigned long&, const unsigned long&,
330  long long , const long long, long long&, const long long&,
331  unsigned long long, const unsigned long long, unsigned long long&, const unsigned long long&> allowed_types;
332  // Helper structs for calculating number of elements
333  template<int... _lims>
334  struct calc_nElem{};
335  template<int limL, int limU, int... _lims>
336  struct calc_nElem<limL,limU,_lims...>
337  {
338  enum{val= (limU-limL+1)*calc_nElem<_lims... >::val};
339  static_assert(limU>=limL, "Farray error: Upper array index limit is lower than lower limit.");
340  };
341  template<int limL, int limU>
342  struct calc_nElem<limL,limU>
343  {
344  enum{val=(limU-limL+1)};
345  static_assert(limU>=limL, "Farray error: Upper array index limit is lower than lower limit.");
346  };
347  public:
348  typedef calc_nElem<lims... > nElem;
349  T array[nElem::val];
350  Farray(){}
351  Farray(Farray<T,lims... > &in){*this = in;}
352  template <typename ... Args>
353  typename enable_if_all_member<allowed_types, T&, Args...>::type::type
354  operator () (Args ... a)
355  {
356  static_assert(2*sizeof...(a)==sizeof...(lims), "Farray error: Invalid number of arguments passed to () operator.");
357  int indices[] = {int(a)...};
358  int limits[] = {lims...};
359  int idx = 0;
360  // Calculate index for array access
361  for (int i = 0; i < int(sizeof...(lims)/2); ++i)
362  {
363  int idx_i = indices[i];
364  if(idx_i<limits[2*i] || idx_i>limits[2*i+1])
365  {
366  str errmsg = "Farray error: Array index out of bounds.";
367  utils_error().raise(LOCAL_INFO,errmsg);
368  }
369  idx_i -= limits[2*i];
370  for (int j=0; j<i; j++) idx_i *= (limits[2*j+1]-limits[2*j]+1);
371  idx += idx_i;
372  }
373  return array[idx];
374  }
375  template <typename ... Args>
376  typename enable_if_all_member<allowed_types, const T&, Args...>::type::type
377  operator () (Args ... a) const
378  {
379  static_assert(2*sizeof...(a)==sizeof...(lims), "Farray error: Invalid number of arguments passed to () operator.");
380  int indices[] = {int(a)...};
381  int limits[] = {lims...};
382  int idx = 0;
383  // Calculate index for array access
384  for (int i = 0; i < (sizeof...(lims)/2); ++i)
385  {
386  int idx_i = indices[i];
387  if(idx_i<limits[2*i] || idx_i>limits[2*i+1])
388  {
389  str errmsg = "Farray error: Array index out of bounds.";
390  utils_error().raise(LOCAL_INFO,errmsg);
391  }
392  idx_i -= limits[2*i];
393  for (int j=0; j<i; j++) idx_i *= limits[2*j+1]-limits[2*j]+1;
394  idx += idx_i;
395  }
396  return array[idx];
397  }
398  Farray<T,lims... >& operator= (const Farray<T,lims... > &orig)
399  {
400  if (this == &orig) return *this;
401  for (int i=0; i<nElem::val; ++i)
402  {
403  array[i] = orig.array[i];
404  }
405  return *this;
406  }
407  Farray(const T val)
408  {
409  for (int i=0; i<nElem::val; i++)
410  {
411  array[i] = val;
412  }
413  }
414  Farray<T,lims... >& operator=(const T val)
415  {
416  for (int i=0; i<nElem::val; ++i)
417  {
418  array[i] = val;
419  }
420  return *this;
421  }
422  };
423 
429  template <int len>
430  class Fstring : public Farray<char,1,len>
431  {
432  public:
434  Fstring(const std::string &in) {*this = in;}
435  Fstring(const char* in) {*this = in;}
436  Fstring(char in) {*this = in;}
437  template<int ilen>
438  Fstring(const Fstring<ilen> &in){*this = in;}
439  Fstring& operator= (const std::string &in)
440  {
441  for(unsigned int i=0; i<len; i++)
442  {
443  Farray<char,1,len>::array[i] = (i<in.length()) ? in[i] : ' ';
444  }
445  return *this;
446  }
447  Fstring& operator= (const char* in)
448  {
449  for(unsigned int i=0; i<len; i++)
450  {
451  Farray<char,1,len>::array[i] = (i<std::strlen(in)) ? in[i] : ' ';
452  }
453  return *this;
454  }
455  Fstring& operator= (char in)
456  {
458  for(int i=1; i<len; i++)
459  {
460  Farray<char,1,len>::array[i] = ' ';
461  }
462  return *this;
463  }
464  template<int ilen>
466  {
467  if (reinterpret_cast<const void*>(this) == reinterpret_cast<const void*>(&in)) return *this;
468  for(int i=0; i<len; i++)
469  {
470  Farray<char,1,len>::array[i] = (i<ilen) ? in.array[i] : ' ';
471  }
472  return *this;
473  }
475  std::string str() const
476  {
477  return std::string(Farray<char,1,len>::array,len);
478  }
480  std::string trimmed_str() const
481  {
482  int idx;
483  for(int i=1; i<=len; i++)
484  {
485  idx=len-i;
486  if(Farray<char,1,len>::array[idx] != ' ') break;
487  }
488  return std::string(Farray<char,1,len>::array,idx+1);
489  }
490  // Overloaded == operator with std::strings
491  bool operator== (std::string str)
492  {
493  return this->trimmed_str() == str;
494  }
495  };
496 
504  template <int len, int... lims>
505  class FstringArray : public Farray<char,1,len, lims... >
506  {
507  public:
508  template <typename ... Args>
509  typename enable_if_all_member<typename Farray<char,1,len, lims... >::allowed_types, Fstring<len>*, Args...>::type::type
510  operator () (Args ... a)
511  {
512  static_assert(2*sizeof...(a)==sizeof...(lims), "FstringArray error: Invalid number of arguments passed to () operator");
513  int indices[] = {1,int(a)...};
514  int limits[] = {1,len,lims...};
515  int idx = 0;
516  // Calculate index for array access
517  for (int i = 0; i < int((sizeof...(lims)+2)/2); ++i)
518  {
519  int idx_i = indices[i];
520  if(idx_i<limits[2*i] || idx_i>limits[2*i+1])
521  {
522  str errmsg = "FstringArray error: Array index out of bounds.";
523  utils_error().raise(LOCAL_INFO,errmsg);
524  }
525  idx_i -= limits[2*i];
526  for (int j=0; j<i; j++) idx_i *= (limits[2*j+1]-limits[2*j]+1);
527  idx += idx_i;
528  }
529  return reinterpret_cast<Fstring<len>*>(&Farray<char,1,len, lims... >::array[idx]);
530  }
531  };
532 
535  template <typename T>
536  class FcomplexT
537  {
538  public:
539  T re;
540  T im;
546  template<typename T2>
548  {
549  re = in.re;
550  im = in.im;
551  }
553  FcomplexT(const std::complex<T>& in)
554  {
555  re = in.real();
556  im = in.imag();
557  }
559  FcomplexT(const T& in)
560  {
561  re = in;
562  im = 0.0;
563  }
565  template<typename T2>
567  {
568  re = in.re;
569  im = in.im;
570  return *this;
571  }
573  FcomplexT& operator = (const std::complex<T> &in)
574  {
575  re = in.real();
576  im = in.imag();
577  return *this;
578  }
580  FcomplexT& operator = (const T &in)
581  {
582  re = in;
583  im = 0.0;
584  return *this;
585  }
587  template<typename T2>
588  operator FcomplexT<T2>()
589  {
590  return FcomplexT<T2>(std::complex<T2>(re, im));
591  }
593  operator std::complex<T>()
594  {
595  return std::complex<T>(re, im);
596  }
597  // Abs of a Fortran complex type
598  T abs() const
599  {
600  return sqrt(re*re + im*im);
601  }
602  // Overloaded * operator
603  template<typename T2>
605  {
606  FcomplexT out;
607 
608  out.re = re*in.re - im*in.im;
609  out.im = re*in.im + im*in.re;
610 
611  return out;
612  }
613  // Overloaded / operator
614  template<typename T2>
616  {
617  FcomplexT out;
618 
619  if(in.abs() != 0)
620  {
621  out.re = (re*in.re + im*in.im) / in.abs();
622  out.im = (im*in.re - re*in.im) / in.abs();
623  }
624  else
625  {
626  out.re = 0;
627  out.im = 0;
628  }
629 
630  return out;
631  }
632  };
633 
641  typedef char Fcharacter;
642  typedef double Fdouble;
643  typedef double Fdouble_precision;
644  typedef double Fdoubleprecision;
645  typedef int Finteger;
646  typedef short Finteger2;
647  typedef long int Finteger4;
648  typedef long long Finteger8;
649  typedef int32_t Flogical; // 4-byte integers, compiler independent
650  typedef bool Flogical1;
651  typedef float Freal;
652  typedef float Freal4;
653  typedef double Freal8;
654  typedef long double Freal16;
655 
657  typedef void MVoid;
658  typedef int MInteger;
659  typedef double MReal;
660  typedef bool MBool;
661  typedef char MChar;
662  typedef std::string MString;
663  template <typename T> using MList = std::vector<T>;
664 
665 }
666 #endif //defined __util_types_hpp__
Array class that matches the memory structure and functionality of arrays in Fortran codes Syntax: Fa...
Definition: util_types.hpp:318
TYPE * ptr
The actual underlying pointer.
Definition: util_types.hpp:294
std::map< int, double > map_int_dbl
Shorthand for an int to double map.
Definition: util_types.hpp:82
double Freal8
Definition: util_types.hpp:653
std::map< std::string, safe_ptr< const double > > param_map_type
Shorthand for the type of the &#39;Param&#39; map (string-to-double-safe_ptr map)
Definition: util_types.hpp:309
double Fdouble_precision
Definition: util_types.hpp:643
EXPORT_SYMBOLS error & utils_error()
Utility errors.
std::string MString
Definition: util_types.hpp:662
std::pair< str, double > sdpair
Shorthand for a pair of string and double.
Definition: util_types.hpp:70
LOCAL_INFO macro.
std::map< std::string, bool > map_str_bool
Shorthand for a string-to-bool map.
Definition: util_types.hpp:80
A safe pointer that throws an informative error if you try to dereference it when nullified...
Definition: util_types.hpp:175
int Finteger
Definition: util_types.hpp:645
char MChar
Definition: util_types.hpp:661
bool Flogical1
Definition: util_types.hpp:650
triplet()
Default constructor.
Definition: util_types.hpp:102
mult_types< short, const short, short &, const short &, unsigned short, const unsigned short, unsigned short &, const unsigned short &, int, const int, int &, const int &, unsigned, const unsigned, unsigned &, const unsigned &, long, const long, long &, const long &, unsigned long, const unsigned long, unsigned long &, const unsigned long &, long long, const long long, long long &, const long long &, unsigned long long, const unsigned long long, unsigned long long &, const unsigned long long & > allowed_types
Definition: util_types.hpp:321
virtual const TYPE * operator->() const
Access is allowed to const member functions only.
Definition: util_types.hpp:201
#define LOCAL_INFO
Definition: local_info.hpp:34
Fortran complex type.
Definition: util_types.hpp:536
FcomplexT(const FcomplexT< T2 > &in)
Default copy constructor.
Definition: util_types.hpp:547
virtual const TYPE & operator[](int index) const
Dereference pointer as if it is an array.
Definition: util_types.hpp:194
P2 operator*(const P2 &a, double f)
std::map< std::string, std::map< std::string, double > > map_str_map_str_dbl
Shorthand for a string-to-string-to-double map.
Definition: util_types.hpp:76
bool MBool
Definition: util_types.hpp:660
Fstring(const std::string &in)
Definition: util_types.hpp:434
double Fdouble
Definition: util_types.hpp:642
FcomplexT(const T &in)
Constructor from a single instance of some type.
Definition: util_types.hpp:559
Farray specialization for Fortran arrays of strings.
Definition: util_types.hpp:505
long double Freal16
Definition: util_types.hpp:654
omp_safe_ptr(TYPE *in_ptr=NULL)
Constructor.
Definition: util_types.hpp:236
~FcomplexT()
Default destructor.
Definition: util_types.hpp:544
std::pair< double, double > ddpair
Shorthand for a pair of doubles.
Definition: util_types.hpp:66
triplet(TYPE centralval)
One-value constructor.
Definition: util_types.hpp:108
std::pair< str, str > sspair
Shorthand for a pair of standard strings.
Definition: util_types.hpp:64
Fstring(const char *in)
Definition: util_types.hpp:435
static void dieGracefully()
Failure message invoked when the user tries to dereference a null safe_ptr.
Definition: util_types.hpp:213
FcomplexT(const std::complex< T > &in)
Constructor from a C++ complex type.
Definition: util_types.hpp:553
A safe pointer designed to point at an array, and return the entry in that array corresponding to the...
Definition: util_types.hpp:230
virtual const TYPE & operator*() const
Dereference pointer.
Definition: util_types.hpp:239
FcomplexT< float > Fcomplex
Fortran type typedefs TODO: Implement compiler dependent macros ensuring that these are always correc...
Definition: util_types.hpp:636
safe_ptr(TYPE *in_ptr=NULL)
Construct-o-safe_ptr.
Definition: util_types.hpp:181
Farray(const T val)
Definition: util_types.hpp:407
float Freal
Definition: util_types.hpp:651
int MInteger
Definition: util_types.hpp:658
Farray(Farray< T, lims... > &in)
Definition: util_types.hpp:351
virtual const TYPE & operator*() const
Dereference pointer.
Definition: util_types.hpp:187
TYPE & operator*()
Dereference pointer.
Definition: util_types.hpp:271
static void dieGracefully()
Failure message invoked when the user tries to dereference a null safe_variable_ptr.
Definition: util_types.hpp:297
std::map< std::pair< int, int >, double > map_intpair_dbl
Shorthand for an int-int pair to double map.
Definition: util_types.hpp:86
A safe variable pointer that throws an informative error if you try to dereference it when nullified...
Definition: util_types.hpp:252
TYPE & operator[](int index)
Dereference pointer as if it is an array.
Definition: util_types.hpp:278
dbl_dbl_bool(const dbl_dbl_bool &in)
Copy constructor.
Definition: util_types.hpp:156
FcomplexT< double > Fcomplex16
Definition: util_types.hpp:638
Variadic utilty functions.
dbl_dbl_bool(double firstval, double secondval, bool flagval)
Three-value constructor.
Definition: util_types.hpp:150
hb_ModelParameters void
std::vector< T > MList
Definition: util_types.hpp:663
T array[nElem::val]
Definition: util_types.hpp:349
std::string str
Shorthand for a standard string.
Definition: Analysis.hpp:35
dbl_dbl_bool()
Default constructor.
Definition: util_types.hpp:143
float Freal4
Definition: util_types.hpp:652
void(* fptr_void)()
Shorthand for a pointer to a void function with no arguments.
Definition: util_types.hpp:89
triplet< TYPE > & operator=(const triplet< TYPE > &in)
Copy assignment operator.
Definition: util_types.hpp:126
long int Finteger4
Definition: util_types.hpp:647
DS5_MSPCTM DS_INTDOF int
long long Finteger8
Definition: util_types.hpp:648
std::map< std::string, int > map_str_int
Shorthand for a string-to-int map.
Definition: util_types.hpp:74
std::string str() const
Get std::string copy of the Fstring, including all trailing spaces.
Definition: util_types.hpp:475
safe_variable_ptr(TYPE *in_ptr=NULL)
Constructor.
Definition: util_types.hpp:262
std::map< std::string, std::string > map_str_str
Shorthand for a string-to-string map.
Definition: util_types.hpp:78
FcomplexT()
Default constructor.
Definition: util_types.hpp:542
Exception objects required for standalone compilation.
void MVoid
Types used for Mathematica backends.
Definition: util_types.hpp:657
calc_nElem< lims... > nElem
Definition: util_types.hpp:348
FcomplexT< double > Fdouble_complex
Definition: util_types.hpp:639
char Fcharacter
Definition: util_types.hpp:641
std::string trimmed_str() const
Get std::string copy of the Fstring without trailing spaces.
Definition: util_types.hpp:480
Fstring(char in)
Definition: util_types.hpp:436
int32_t Flogical
Definition: util_types.hpp:649
FcomplexT< long double > Flongdouble_complex
Definition: util_types.hpp:640
std::map< std::string, double > map_str_dbl
Shorthand for a string-to-double map.
Farray specialization for Fortran strings.
Definition: util_types.hpp:430
std::map< std::string, std::map< std::string, std::string > > map_str_map_str_str
Shorthand for a string-to-string-to-string map.
Definition: util_types.hpp:84
P2 operator/(const P2 &a, double f)
std::map< int, triplet< double > > map_int_triplet_dbl
Shorthand for an int-to-double triplet map.
Definition: util_types.hpp:135
std::pair< int, int > iipair
Shorthand for a pair of integers.
Definition: util_types.hpp:68
TYPE * operator->()
Access member functions.
Definition: util_types.hpp:285
double Fdoubleprecision
Definition: util_types.hpp:644
short Finteger2
Definition: util_types.hpp:646
TODO: see if we can use this one:
Definition: Analysis.hpp:33
Farray< T, lims... > & operator=(const T val)
Definition: util_types.hpp:414
Fstring(const Fstring< ilen > &in)
Definition: util_types.hpp:438
double MReal
Definition: util_types.hpp:659
FcomplexT< float > Fcomplex8
Definition: util_types.hpp:637
triplet(TYPE centralval, TYPE upperval, TYPE lowerval)
Three-value constructor.
Definition: util_types.hpp:114
const TYPE * ptr
The actual underlying pointer, interpreted as a pointer to constant value.
Definition: util_types.hpp:210
triplet(const triplet< TYPE > &in)
Copy constructor.
Definition: util_types.hpp:120