gambit is hosted by Hepforge, IPPP Durham
GAMBIT  v1.5.0-2191-ga4742ac
a Global And Modular Bsm Inference Tool
util_functions.hpp
Go to the documentation of this file.
1 // GAMBIT: Global and Modular BSM Inference Tool
2 // *********************************************
22 
23 
24 #ifndef __util_functions_hpp__
25 #define __util_functions_hpp__
26 
27 #include <vector>
28 #include <chrono>
29 #include <cmath>
30 
32 #include "gambit/cmake/cmake_variables.hpp"
33 
34 #include <boost/algorithm/string/split.hpp>
35 #include <boost/algorithm/string/classification.hpp>
36 
37 extern "C"
38 {
39  #include "mkpath/mkpath.h"
40 }
41 
42 namespace Gambit
43 {
44 
48  template <typename T>
49  T byVal(T t) { return t; }
50 
51  template <typename T>
52  int sgn(T val) { return (T(0) < val) - (val < T(0)); }
53 
55  using std::abs;
56 
60  template<typename T>
61  uintptr_t memaddress_to_uint(T* ptr)
62  {
63  return reinterpret_cast<uintptr_t>(ptr);
64  }
65 
66  namespace Utils
67  {
68 
70  const str buildtime_scratch = GAMBIT_DIR "/scratch/build_time/";
71 
75 
78  EXPORT_SYMBOLS str construct_runtime_scratch(bool fail_on_mpi_uninitialised=true);
79 
82  EXPORT_SYMBOLS std::vector<str> delimiterSplit(str s, str delim);
83 
86 
89 
93 
96 
98  EXPORT_SYMBOLS bool sspairset_contains(const str&, const std::set<sspair>&);
99 
101  EXPORT_SYMBOLS bool sspairset_contains(const str&, const str&, const std::set<sspair>&);
102 
104  EXPORT_SYMBOLS bool sspairset_contains(const sspair&, const std::set<sspair>&);
105 
108 
110  EXPORT_SYMBOLS void strcpy2f(char*, int, str);
111 
113  EXPORT_SYMBOLS bool endsWith(const std::string& str, const std::string& suffix);
114 
116  EXPORT_SYMBOLS bool startsWith(const std::string& str, const std::string& prefix, bool case_sensitive=true);
117 
119  EXPORT_SYMBOLS bool iequals(const std::string& a, const std::string& b, bool case_sensitive=false);
120 
122  EXPORT_SYMBOLS std::vector<std::string> split(const std::string& input, const std::string& delimiter);
123 
125  EXPORT_SYMBOLS std::string strtolower(const std::string& a);
126 
127  /************************************************************************/
128  /* Comparator for case-insensitive comparison in STL assos. containers */
129  /************************************************************************/
130  // From: https://stackoverflow.com/a/1801913/1447953
131  struct EXPORT_SYMBOLS ci_less : std::binary_function<std::string, std::string, bool>
132  {
133  // case-independent (ci) compare_less binary function
134  struct nocase_compare : public std::binary_function<unsigned char,unsigned char,bool>
135  {
136  bool operator() (const unsigned char& c1, const unsigned char& c2) const {
137  return tolower (c1) < tolower (c2);
138  }
139  };
140  bool operator() (const std::string & s1, const std::string & s2) const {
141  return std::lexicographical_compare
142  (s1.begin (), s1.end (), // source range
143  s2.begin (), s2.end (), // dest range
144  nocase_compare ()); // comparison
145  }
146  };
147 
148 
150  // Useful for initialising vectors with arrays, e.g.
151  // int vv[] = { 12,43 };
152  // std::vector<int> v(beginA(vv), endA(vv));
153  // Though 'begin' is unnecessary, can just do
154  // std::vector<int> v(vv, endA(vv));
155  template <typename T, size_t N>
156  T* beginA(T(&arr)[N]) { return &arr[0]; }
157  template <typename T, size_t N>
158  T* endA(T(&arr)[N]) { return &arr[0]+N; }
159 
161  // From http://stackoverflow.com/questions/1964150/c-test-if-2-sets-are-disjoint
162  template<class Set1, class Set2>
163  bool is_disjoint(const Set1 &set1, const Set2 &set2)
164  {
165  if(set1.empty() || set2.empty()) return true;
166 
167  typename Set1::const_iterator
168  it1 = set1.begin(),
169  it1End = set1.end();
170  typename Set2::const_iterator
171  it2 = set2.begin(),
172  it2End = set2.end();
173 
174  if(*it1 > *set2.rbegin() || *it2 > *set1.rbegin()) return true;
175 
176  while(it1 != it1End && it2 != it2End)
177  {
178  if(*it1 == *it2) return false;
179  if(*it1 < *it2) { it1++; }
180  else { it2++; }
181  }
182 
183  return true;
184  }
185 
187  EXPORT_SYMBOLS const str& ensure_path_exists(const str&);
188 
190  EXPORT_SYMBOLS bool file_exists(const std::string& filename);
191 
193  EXPORT_SYMBOLS std::vector<str> ls_dir(const str& dir);
194 
196  EXPORT_SYMBOLS str dir_name(const str& path);
197 
199  EXPORT_SYMBOLS str base_name(const str& path);
200 
202  EXPORT_SYMBOLS int remove_all_files_in(const str& dirname, bool error_if_absent = true);
203 
204 
205  typedef std::chrono::time_point<std::chrono::system_clock> time_point;
206 
208  EXPORT_SYMBOLS time_point get_clock_now();
209 
211  EXPORT_SYMBOLS str return_time_and_date(const time_point& in);
212 
215  EXPORT_SYMBOLS bool are_similar(const str& s1, const str& s2);
216 
219  bool check1(const str& s1, const str& s2);
220 
223  bool check2(const str& s1, const str& s2);
224 
226  EXPORT_SYMBOLS double sqr(double a);
227 
229  using std::isnan;
230 
232  using std::isinf;
233 
236  EXPORT_SYMBOLS bool isInteger(const std::string&);
237 
239  template <typename T> int sgn(T val) { return (T(0) < val) - (val < T(0)); }
240 
241  // Dummy functions for variadic variables to avoid compiler warnings
242  template<typename... T> void dummy_function() {}
243  template<typename T> void dummy_function(T one)
244  {
245  (void)one;
246  }
247 
248  template<typename T1, typename... T> void dummy_function(T1 first, T... args)
249  {
250  (void)first;
251  dummy_function(args...);
252  }
253 
254  }
255 
256 }
257 
258 #endif //defined __util_functions_hpp__
const str buildtime_scratch
Return the path to the build-time scratch directory.
EXPORT_SYMBOLS str dir_name(const str &path)
Get directory name from full path+filename (POSIX)
EXPORT_SYMBOLS std::vector< std::string > split(const std::string &input, const std::string &delimiter)
Split string into vector of strings, using a delimiter string.
EXPORT_SYMBOLS str replace_leading_namespace(str s, str ns, str ns_new)
Replaces a namespace at the start of a string, or after "const".
#define EXPORT_SYMBOLS
std::chrono::time_point< std::chrono::system_clock > time_point
Definition: logging.hpp:46
EXPORT_SYMBOLS std::string strtolower(const std::string &a)
Convert a whole string to lowercase.
EXPORT_SYMBOLS void strip_parentheses(str &)
Strips leading and/or trailing parentheses from a string.
EXPORT_SYMBOLS bool sspairset_contains(const str &, const std::set< sspair > &)
Test if a set of str,str pairs contains any entry with first element matching a given string...
General small utility classes, typedefs, etc.
EXPORT_SYMBOLS bool isInteger(const std::string &)
Check if a string represents an integer From: http://stackoverflow.com/a/2845275/1447953.
EXPORT_SYMBOLS str construct_runtime_scratch(bool fail_on_mpi_uninitialised=true)
Construct the path to the run-specific scratch directory This version is safe to call from a destruct...
START_MODEL b
Definition: demo.hpp:270
EXPORT_SYMBOLS std::vector< str > ls_dir(const str &dir)
Return a vector of strings listing the contents of a directory (POSIX)
EXPORT_SYMBOLS double sqr(double a)
returns square of double - saves tedious repetition
std::pair< str, str > sspair
Shorthand for a pair of standard strings.
Definition: util_types.hpp:64
EXPORT_SYMBOLS bool endsWith(const std::string &str, const std::string &suffix)
Checks whether `str&#39; ends with `suffix&#39;.
EXPORT_SYMBOLS str return_time_and_date(const time_point &in)
Get date and time.
EXPORT_SYMBOLS void strcpy2f(char *, int, str)
Copy a str to a character array, stripping the null termination character.
int sgn(T val)
Get the sign of a (hopefully numeric type)
EXPORT_SYMBOLS bool startsWith(const std::string &str, const std::string &prefix, bool case_sensitive=true)
Checks whether `str&#39; begins with `prefix&#39;.
bool check2(const str &s1, const str &s2)
Sub-check for are_similar.
EXPORT_SYMBOLS bool file_exists(const std::string &filename)
Check if a file exists.
EXPORT_SYMBOLS int remove_all_files_in(const str &dirname, bool error_if_absent=true)
Delete all files in a directory (does not act recursively)
EXPORT_SYMBOLS void strip_whitespace_except_after_const(str &)
Strip all whitespace except that following "const", in which case the whitespace is replaced by a sin...
EXPORT_SYMBOLS bool iequals(const std::string &a, const std::string &b, bool case_sensitive=false)
Perform a (possibly) case-insensitive string comparison.
EXPORT_SYMBOLS str base_name(const str &path)
Get file name from full path+filename (POSIX)
T * beginA(T(&arr)[N])
Get pointers to beginning and end of array.
bool is_disjoint(const Set1 &set1, const Set2 &set2)
Test if two sets are disjoint (works on any sorted std container I think)
hb_ModelParameters void
std::string str
Shorthand for a standard string.
Definition: Analysis.hpp:35
EXPORT_SYMBOLS const str & ensure_path_exists(const str &)
Ensure that a path exists (and then return the path, for chaining purposes)
EXPORT_SYMBOLS const str & runtime_scratch()
Return the path to the run-specific scratch directory Don&#39;t call this from a destructor, as the internal static str may have already been destroyed.
EXPORT_SYMBOLS std::vector< str > delimiterSplit(str s, str delim)
Split a string into a vector of strings, using a delimiter, and removing any whitespace around the de...
bool check1(const str &s1, const str &s2)
Sub-check for are_similar.
EXPORT_SYMBOLS str strip_leading_namespace(str s, str ns)
Strips namespace from the start of a string, or after "const".
uintptr_t memaddress_to_uint(T *ptr)
Convert the memory address a pointer points to to an unsigned integer (The size of uintptr_t depends ...
T byVal(T t)
Redirection function to turn an lvalue into an rvalue, so that it is correctly passed by value when d...
EXPORT_SYMBOLS str str_fixed_len(str, int)
Created a str of a specified length.
T * endA(T(&arr)[N])
int sgn(T val)
TODO: see if we can use this one:
Definition: Analysis.hpp:33
time_point get_clock_now()
Get clock time.
EXPORT_SYMBOLS bool are_similar(const str &s1, const str &s2)
Check if two strings are a "close" match Used for "did you mean?" type checking during command line a...
Funk one(Args... argss)
Definition: daFunk.hpp:602