gambit is hosted by Hepforge, IPPP Durham
GAMBIT  v1.5.0-252-gf9a3f78
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::map<std::string,double> map_str_dbl;
70  typedef std::map<std::string,int> map_str_int;
72  typedef std::map<std::string,std::map<std::string,double> > map_str_map_str_dbl;
74  typedef std::map<std::string,std::string> map_str_str;
76  typedef std::map<std::string,std::map<std::string,std::string> > map_str_map_str_str;
78  typedef std::map< std::pair < int, int >, double> map_intpair_dbl;
79 
81  typedef void (*fptr_void)();
82 
83  // Useful unqualified functions
84  using std::cout;
85  using std::cerr;
86  using std::endl;
87 
88  // A simple triplet class for holding a central value and aysmmetric +/- variations
89  template<typename TYPE>
90  struct triplet
91  {
95  : central(0.),
96  upper(0.),
97  lower(0.)
98  {}
100  triplet(TYPE centralval)
101  : central(centralval),
102  upper(0.),
103  lower(0.)
104  {}
106  triplet(TYPE centralval, TYPE upperval, TYPE lowerval)
107  : central(centralval),
108  upper(upperval),
109  lower(lowerval)
110  {}
113  : central(in.central),
114  upper(in.upper),
115  lower(in.lower)
116  {}
119  central = in.central;
120  upper = in.upper;
121  lower = in.lower;
122  return *this;
123  }
124  };
125 
127  typedef std::map<int,triplet<double> > map_int_triplet_dbl;
128 
129  // a tuple containg two doubles and a bool
131  {
132  double first, second;
133  bool flag;
136  : first(0.),
137  second(0.),
138  flag(0.)
139  {}
140 
142  dbl_dbl_bool(double firstval, double secondval, bool flagval)
143  : first(firstval),
144  second(secondval),
145  flag(flagval)
146  {}
149  : first(in.first),
150  second(in.second),
151  flag(in.flag)
152  {}
155  first = in.first;
156  second = in.second;
157  flag = in.flag;
158  return *this;
159  }
160  };
161 
162 
163 
166  template <typename TYPE>
167  class safe_ptr
168  {
169 
170  public:
171 
173  safe_ptr(TYPE* in_ptr = NULL) { ptr = in_ptr; }
174 
176  virtual void set(TYPE* in_ptr) { ptr = in_ptr; }
177 
179  virtual const TYPE& operator*() const
180  {
181  if (ptr == NULL) dieGracefully();
182  return *ptr;
183  }
184 
186  virtual const TYPE& operator[](int index) const
187  {
188  if (ptr == NULL) dieGracefully();
189  return *(ptr+index);
190  }
191 
193  virtual const TYPE* operator->() const
194  {
195  if (ptr == NULL) dieGracefully();
196  return ptr;
197  }
198 
199  protected:
200 
202  const TYPE* ptr;
203 
205  static void dieGracefully()
206  {
207  str errmsg = "You just tried to dereference a GAMBIT safe pointer that has value";
208  errmsg += "\nNULL. Bad idea. Probably you tried to retrieve a conditional"
209  "\ndependency that has not been activated because the necessary condition"
210  "\nhas not been met, or you tried to access a model parameter for a model"
211  "\nthat you are not actually scanning. This means there is a bug in one"
212  "\nof your module functions.";
213  utils_error().raise(LOCAL_INFO,errmsg);
214  }
215 
216  };
217 
218 
221  template <typename TYPE>
222  class omp_safe_ptr : public safe_ptr<TYPE>
223  {
224 
225  public:
226 
228  omp_safe_ptr(TYPE* in_ptr = NULL) : safe_ptr<TYPE>(in_ptr) {}
229 
231  virtual const TYPE& operator*() const
232  {
233  if (this->ptr == NULL) safe_ptr<TYPE>::dieGracefully();
234  return *(this->ptr+omp_get_thread_num());
235  }
236 
237  };
238 
243  template <typename TYPE>
245  {
246 
247  public:
248 
250  safe_variable_ptr & operator=(const safe_variable_ptr&) = delete;
251  safe_variable_ptr(const safe_variable_ptr&) = delete;
252 
254  safe_variable_ptr(TYPE* in_ptr = NULL) { ptr = in_ptr; }
255 
257  void set(TYPE* in_ptr) { ptr = in_ptr; }
258 
260  TYPE* get() { return ptr; }
261 
263  TYPE& operator*()
264  {
265  if (ptr == NULL) dieGracefully();
266  return *ptr;
267  }
268 
270  TYPE& operator[](int index)
271  {
272  if (ptr == NULL) dieGracefully();
273  return *(ptr+index);
274  }
275 
277  TYPE* operator->()
278  {
279  if (ptr == NULL) dieGracefully();
280  return ptr;
281  }
282 
283  protected:
284 
286  TYPE* ptr;
287 
289  static void dieGracefully()
290  {
291  str errmsg = "You just tried to dereference a GAMBIT safe variable pointer that has value";
292  errmsg += "\nNULL. Bad idea. Probably you tried to retrieve a conditional"
293  "\ndependency that has not been activated because the necessary condition"
294  "\nhas not been met.";
295  utils_error().raise(LOCAL_INFO,errmsg);
296  }
297 
298  };
299 
300 
305  template <typename T, int... lims>
306  class Farray
307  {
308  protected:
309  static_assert(sizeof...(lims)%2==0, "Farray error: Odd number of index limits.");
310  static_assert(sizeof...(lims)!=0, "Farray error: No array index limits given.");
311  // Allowed array access types (expand if necessary)
312  typedef mult_types< short, const short, short&, const short&,
313  unsigned short, const unsigned short, unsigned short&, const unsigned short&,
314  int, const int, int&, const int&,
315  unsigned, const unsigned, unsigned&, const unsigned&,
316  long, const long, long&, const long&,
317  unsigned long, const unsigned long, unsigned long&, const unsigned long&,
318  long long , const long long, long long&, const long long&,
319  unsigned long long, const unsigned long long, unsigned long long&, const unsigned long long&> allowed_types;
320  // Helper structs for calculating number of elements
321  template<int... _lims>
322  struct calc_nElem{};
323  template<int limL, int limU, int... _lims>
324  struct calc_nElem<limL,limU,_lims...>
325  {
326  enum{val= (limU-limL+1)*calc_nElem<_lims... >::val};
327  static_assert(limU>=limL, "Farray error: Upper array index limit is lower than lower limit.");
328  };
329  template<int limL, int limU>
330  struct calc_nElem<limL,limU>
331  {
332  enum{val=(limU-limL+1)};
333  static_assert(limU>=limL, "Farray error: Upper array index limit is lower than lower limit.");
334  };
335  public:
336  typedef calc_nElem<lims... > nElem;
337  T array[nElem::val];
338  Farray(){}
339  Farray(Farray<T,lims... > &in){*this = in;}
340  template <typename ... Args>
341  typename enable_if_all_member<allowed_types, T&, Args...>::type::type
342  operator () (Args ... a)
343  {
344  static_assert(2*sizeof...(a)==sizeof...(lims), "Farray error: Invalid number of arguments passed to () operator.");
345  int indices[] = {int(a)...};
346  int limits[] = {lims...};
347  int idx = 0;
348  // Calculate index for array access
349  for (int i = 0; i < int(sizeof...(lims)/2); ++i)
350  {
351  int idx_i = indices[i];
352  if(idx_i<limits[2*i] || idx_i>limits[2*i+1])
353  {
354  str errmsg = "Farray error: Array index out of bounds.";
355  utils_error().raise(LOCAL_INFO,errmsg);
356  }
357  idx_i -= limits[2*i];
358  for (int j=0; j<i; j++) idx_i *= (limits[2*j+1]-limits[2*j]+1);
359  idx += idx_i;
360  }
361  return array[idx];
362  }
363  template <typename ... Args>
364  typename enable_if_all_member<allowed_types, const T&, Args...>::type::type
365  operator () (Args ... a) const
366  {
367  static_assert(2*sizeof...(a)==sizeof...(lims), "Farray error: Invalid number of arguments passed to () operator.");
368  int indices[] = {int(a)...};
369  int limits[] = {lims...};
370  int idx = 0;
371  // Calculate index for array access
372  for (int i = 0; i < (sizeof...(lims)/2); ++i)
373  {
374  int idx_i = indices[i];
375  if(idx_i<limits[2*i] || idx_i>limits[2*i+1])
376  {
377  str errmsg = "Farray error: Array index out of bounds.";
378  utils_error().raise(LOCAL_INFO,errmsg);
379  }
380  idx_i -= limits[2*i];
381  for (int j=0; j<i; j++) idx_i *= limits[2*j+1]-limits[2*j]+1;
382  idx += idx_i;
383  }
384  return array[idx];
385  }
386  Farray<T,lims... >& operator= (const Farray<T,lims... > &orig)
387  {
388  if (this == &orig) return *this;
389  for (int i=0; i<nElem::val; ++i)
390  {
391  array[i] = orig.array[i];
392  }
393  return *this;
394  }
395  Farray(const T val)
396  {
397  for (int i=0; i<nElem::val; i++)
398  {
399  array[i] = val;
400  }
401  }
402  Farray<T,lims... >& operator=(const T val)
403  {
404  for (int i=0; i<nElem::val; ++i)
405  {
406  array[i] = val;
407  }
408  return *this;
409  }
410  };
411 
417  template <int len>
418  class Fstring : public Farray<char,1,len>
419  {
420  public:
422  Fstring(const std::string &in) {*this = in;}
423  Fstring(const char* in) {*this = in;}
424  Fstring(char in) {*this = in;}
425  template<int ilen>
426  Fstring(const Fstring<ilen> &in){*this = in;}
427  Fstring& operator= (const std::string &in)
428  {
429  for(unsigned int i=0; i<len; i++)
430  {
431  Farray<char,1,len>::array[i] = (i<in.length()) ? in[i] : ' ';
432  }
433  return *this;
434  }
435  Fstring& operator= (const char* in)
436  {
437  for(unsigned int i=0; i<len; i++)
438  {
439  Farray<char,1,len>::array[i] = (i<std::strlen(in)) ? in[i] : ' ';
440  }
441  return *this;
442  }
443  Fstring& operator= (char in)
444  {
446  for(int i=1; i<len; i++)
447  {
448  Farray<char,1,len>::array[i] = ' ';
449  }
450  return *this;
451  }
452  template<int ilen>
454  {
455  if (reinterpret_cast<const void*>(this) == reinterpret_cast<const void*>(&in)) return *this;
456  for(int i=0; i<len; i++)
457  {
458  Farray<char,1,len>::array[i] = (i<ilen) ? in.array[i] : ' ';
459  }
460  return *this;
461  }
463  std::string str() const
464  {
465  return std::string(Farray<char,1,len>::array,len);
466  }
468  std::string trimmed_str() const
469  {
470  int idx;
471  for(int i=1; i<=len; i++)
472  {
473  idx=len-i;
474  if(Farray<char,1,len>::array[idx] != ' ') break;
475  }
476  return std::string(Farray<char,1,len>::array,idx+1);
477  }
478  // Overloaded == operator with std::strings
479  bool operator== (std::string str)
480  {
481  return this->trimmed_str() == str;
482  }
483  };
484 
492  template <int len, int... lims>
493  class FstringArray : public Farray<char,1,len, lims... >
494  {
495  public:
496  template <typename ... Args>
497  typename enable_if_all_member<typename Farray<char,1,len, lims... >::allowed_types, Fstring<len>*, Args...>::type::type
498  operator () (Args ... a)
499  {
500  static_assert(2*sizeof...(a)==sizeof...(lims), "FstringArray error: Invalid number of arguments passed to () operator");
501  int indices[] = {1,int(a)...};
502  int limits[] = {1,len,lims...};
503  int idx = 0;
504  // Calculate index for array access
505  for (int i = 0; i < int((sizeof...(lims)+2)/2); ++i)
506  {
507  int idx_i = indices[i];
508  if(idx_i<limits[2*i] || idx_i>limits[2*i+1])
509  {
510  str errmsg = "FstringArray error: Array index out of bounds.";
511  utils_error().raise(LOCAL_INFO,errmsg);
512  }
513  idx_i -= limits[2*i];
514  for (int j=0; j<i; j++) idx_i *= (limits[2*j+1]-limits[2*j]+1);
515  idx += idx_i;
516  }
517  return reinterpret_cast<Fstring<len>*>(&Farray<char,1,len, lims... >::array[idx]);
518  }
519  };
520 
523  template <typename T>
524  class FcomplexT
525  {
526  public:
527  T re;
528  T im;
534  template<typename T2>
536  {
537  re = in.re;
538  im = in.im;
539  }
541  FcomplexT(const std::complex<T>& in)
542  {
543  re = in.real();
544  im = in.imag();
545  }
547  FcomplexT(const T& in)
548  {
549  re = in;
550  im = 0.0;
551  }
553  template<typename T2>
555  {
556  re = in.re;
557  im = in.im;
558  return *this;
559  }
561  FcomplexT& operator = (const std::complex<T> &in)
562  {
563  re = in.real();
564  im = in.imag();
565  return *this;
566  }
568  FcomplexT& operator = (const T &in)
569  {
570  re = in;
571  im = 0.0;
572  return *this;
573  }
575  template<typename T2>
576  operator FcomplexT<T2>()
577  {
578  return FcomplexT<T2>(std::complex<T2>(re, im));
579  }
581  operator std::complex<T>()
582  {
583  return std::complex<T>(re, im);
584  }
585  // Abs of a Fortran complex type
586  T abs() const
587  {
588  return sqrt(re*re + im*im);
589  }
590  // Overloaded * operator
591  template<typename T2>
593  {
594  FcomplexT out;
595 
596  out.re = re*in.re - im*in.im;
597  out.im = re*in.im + im*in.re;
598 
599  return out;
600  }
601  // Overloaded / operator
602  template<typename T2>
604  {
605  FcomplexT out;
606 
607  if(in.abs() != 0)
608  {
609  out.re = (re*in.re + im*in.im) / in.abs();
610  out.im = (im*in.re - re*in.im) / in.abs();
611  }
612  else
613  {
614  out.re = 0;
615  out.im = 0;
616  }
617 
618  return out;
619  }
620  };
621 
629  typedef char Fcharacter;
630  typedef double Fdouble;
631  typedef double Fdouble_precision;
632  typedef double Fdoubleprecision;
633  typedef int Finteger;
634  typedef short Finteger2;
635  typedef long int Finteger4;
636  typedef long long Finteger8;
637  typedef int32_t Flogical; // 4-byte integers, compiler independent
638  typedef bool Flogical1;
639  typedef float Freal;
640  typedef float Freal4;
641  typedef double Freal8;
642  typedef long double Freal16;
643 
645  typedef void MVoid;
646  typedef int MInteger;
647  typedef double MReal;
648  typedef bool MBool;
649  typedef char MChar;
650  typedef std::string MString;
651  template <typename T> using MList = std::vector<T>;
652 
653 
654 }
655 #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:306
TYPE * ptr
The actual underlying pointer.
Definition: util_types.hpp:286
double Freal8
Definition: util_types.hpp:641
double Fdouble_precision
Definition: util_types.hpp:631
EXPORT_SYMBOLS error & utils_error()
Utility errors.
std::string MString
Definition: util_types.hpp:650
LOCAL_INFO macro.
A safe pointer that throws an informative error if you try to dereference it when nullified...
Definition: util_types.hpp:167
int Finteger
Definition: util_types.hpp:633
char MChar
Definition: util_types.hpp:649
bool Flogical1
Definition: util_types.hpp:638
triplet()
Default constructor.
Definition: util_types.hpp:94
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:309
virtual const TYPE * operator->() const
Access is allowed to const member functions only.
Definition: util_types.hpp:193
#define LOCAL_INFO
Definition: local_info.hpp:34
Fortran complex type.
Definition: util_types.hpp:524
FcomplexT(const FcomplexT< T2 > &in)
Default copy constructor.
Definition: util_types.hpp:535
virtual const TYPE & operator[](int index) const
Dereference pointer as if it is an array.
Definition: util_types.hpp:186
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:72
bool MBool
Definition: util_types.hpp:648
Fstring(const std::string &in)
Definition: util_types.hpp:422
double Fdouble
Definition: util_types.hpp:630
FcomplexT(const T &in)
Constructor from a single instance of some type.
Definition: util_types.hpp:547
Farray specialization for Fortran arrays of strings.
Definition: util_types.hpp:493
long double Freal16
Definition: util_types.hpp:642
omp_safe_ptr(TYPE *in_ptr=NULL)
Constructor.
Definition: util_types.hpp:228
~FcomplexT()
Default destructor.
Definition: util_types.hpp:532
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:100
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:423
static void dieGracefully()
Failure message invoked when the user tries to dereference a null safe_ptr.
Definition: util_types.hpp:205
FcomplexT(const std::complex< T > &in)
Constructor from a C++ complex type.
Definition: util_types.hpp:541
A safe pointer designed to point at an array, and return the entry in that array corresponding to the...
Definition: util_types.hpp:222
virtual const TYPE & operator*() const
Dereference pointer.
Definition: util_types.hpp:231
FcomplexT< float > Fcomplex
Fortran type typedefs TODO: Implement compiler dependent macros ensuring that these are always correc...
Definition: util_types.hpp:624
safe_ptr(TYPE *in_ptr=NULL)
Construct-o-safe_ptr.
Definition: util_types.hpp:173
Farray(const T val)
Definition: util_types.hpp:395
float Freal
Definition: util_types.hpp:639
int MInteger
Definition: util_types.hpp:646
Farray(Farray< T, lims... > &in)
Definition: util_types.hpp:339
virtual const TYPE & operator*() const
Dereference pointer.
Definition: util_types.hpp:179
TYPE & operator*()
Dereference pointer.
Definition: util_types.hpp:263
static void dieGracefully()
Failure message invoked when the user tries to dereference a null safe_variable_ptr.
Definition: util_types.hpp:289
std::map< std::pair< int, int >, double > map_intpair_dbl
Shorthand for an int-int pair to double map.
Definition: util_types.hpp:78
A safe variable pointer that throws an informative error if you try to dereference it when nullified...
Definition: util_types.hpp:244
TYPE & operator[](int index)
Dereference pointer as if it is an array.
Definition: util_types.hpp:270
dbl_dbl_bool(const dbl_dbl_bool &in)
Copy constructor.
Definition: util_types.hpp:148
FcomplexT< double > Fcomplex16
Definition: util_types.hpp:626
Variadic utilty functions.
dbl_dbl_bool(double firstval, double secondval, bool flagval)
Three-value constructor.
Definition: util_types.hpp:142
hb_ModelParameters void
std::vector< T > MList
Definition: util_types.hpp:651
T array[nElem::val]
Definition: util_types.hpp:337
std::string str
Shorthand for a standard string.
Definition: Analysis.hpp:35
dbl_dbl_bool()
Default constructor.
Definition: util_types.hpp:135
float Freal4
Definition: util_types.hpp:640
void(* fptr_void)()
Shorthand for a pointer to a void function with no arguments.
Definition: util_types.hpp:81
triplet< TYPE > & operator=(const triplet< TYPE > &in)
Copy assignment operator.
Definition: util_types.hpp:118
long int Finteger4
Definition: util_types.hpp:635
DS5_MSPCTM DS_INTDOF int
long long Finteger8
Definition: util_types.hpp:636
std::map< std::string, int > map_str_int
Shorthand for a string-to-int map.
Definition: util_types.hpp:70
std::string str() const
Get std::string copy of the Fstring, including all trailing spaces.
Definition: util_types.hpp:463
safe_variable_ptr(TYPE *in_ptr=NULL)
Constructor.
Definition: util_types.hpp:254
std::map< std::string, std::string > map_str_str
Shorthand for a string-to-string map.
Definition: util_types.hpp:74
FcomplexT()
Default constructor.
Definition: util_types.hpp:530
Exception objects required for standalone compilation.
void MVoid
Types used for Mathematica backends.
Definition: util_types.hpp:645
calc_nElem< lims... > nElem
Definition: util_types.hpp:336
FcomplexT< double > Fdouble_complex
Definition: util_types.hpp:627
char Fcharacter
Definition: util_types.hpp:629
std::string trimmed_str() const
Get std::string copy of the Fstring without trailing spaces.
Definition: util_types.hpp:468
Fstring(char in)
Definition: util_types.hpp:424
int32_t Flogical
Definition: util_types.hpp:637
FcomplexT< long double > Flongdouble_complex
Definition: util_types.hpp:628
std::map< std::string, double > map_str_dbl
Shorthand for a string-to-double map.
Farray specialization for Fortran strings.
Definition: util_types.hpp:418
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:76
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:127
TYPE * operator->()
Access member functions.
Definition: util_types.hpp:277
double Fdoubleprecision
Definition: util_types.hpp:632
short Finteger2
Definition: util_types.hpp:634
TODO: see if we can use this one:
Definition: Analysis.hpp:33
Farray< T, lims... > & operator=(const T val)
Definition: util_types.hpp:402
Fstring(const Fstring< ilen > &in)
Definition: util_types.hpp:426
double MReal
Definition: util_types.hpp:647
FcomplexT< float > Fcomplex8
Definition: util_types.hpp:625
triplet(TYPE centralval, TYPE upperval, TYPE lowerval)
Three-value constructor.
Definition: util_types.hpp:106
const TYPE * ptr
The actual underlying pointer, interpreted as a pointer to constant value.
Definition: util_types.hpp:202
triplet(const triplet< TYPE > &in)
Copy constructor.
Definition: util_types.hpp:112