C++ main module for stochmagnet Package  1.0
functions.h
Go to the documentation of this file.
1 #ifndef FUNCTIONS_H
2 #define FUNCTIONS_H
3 
4 //types of the code
5 #include "types.h"
6 
7 //standart library
8 #include <stdlib.h>
9 
10 //for type name
11 #include <cxxabi.h>
12 
13 //include the Template condition
14 #include<type_traits>
15 
16 //regex header fro string manipulation
17 #include <regex>
18 
19 //template type verification header
20 #include<type_traits>
21 #include<concepts>
22 
23 //math header isnan
24 #include <math.h>
25 
26 #ifdef DEBUG
27 //define the assert method
28 #include<cassert>
29 #endif
30 
31 //bitwise operators
32 //================
33 // a | b | iand
34 // 0 | 0 | 0
35 // 0 | 1 | 0
36 // 1 | 0 | 0
37 // 1 | 1 | 1
38 #define iand(a,b) ( (a) & (b) )
39 
40 // a | b | ior
41 // 0 | 0 | 0
42 // 0 | 1 | 1
43 // 1 | 0 | 1
44 // 1 | 1 | 1
45 #define ior(a,b) ( (a) | (b) )
46 
47 // a | b | inor
48 // 0 | 0 | 1
49 // 0 | 1 | 0
50 // 1 | 0 | 0
51 // 1 | 1 | 0
52 #define inor(a,b) ( ~( (a) | (b) ) )
53 
54 // a | b | ixor
55 // 0 | 0 | 0
56 // 0 | 1 | 1
57 // 1 | 0 | 1
58 // 1 | 1 | 0
59 #define ixor(a,b) ( (a) ^ (b) ) )
60 
61 // a | inot
62 // 0 | 1
63 // 1 | 0
64 #define inot(a) ( 256 - (~(a)) )
65 
66 namespace core_functions {
67 
68  //template type verifications
69  //============================
70 
71  //is complex
72  template <typename T>
73  struct is_complex : std::false_type {};
74 
75  template <std::floating_point T>
76  struct is_complex<std::complex<T>> : std::true_type {};
77 
78  template <typename T>
79  concept isComplexType =
80  ( std::floating_point<T> || is_complex<T>::value);
81 
82 
83  //type verification for real or integer type
84  template<typename T>
86  (std::is_arithmetic_v<T> || isComplexType<T> );
87 
88  //orded type
89  template<typename T>
90  concept isOrderedType =
91  (std::totally_ordered<T>);
92 
93  //type verification string type
94  template<typename T>
95  concept isStringType=
96  (std::is_constructible<std::string,T>::value);
97 
98  //type verification for real type
99  template<typename T>
100  concept isRealType=
101  (std::is_floating_point_v<T>);
102 
103 
104  //type verification for integer type
105  template<typename T>
106  concept isIntegerType=
107  (std::is_integral_v<T>);
108 
109  // type verification for signed integer type
110  template<typename T>
112  (std::is_integral_v<T>) && (std::is_signed_v<T>);
113 
114  // type verification for unsigned integer type
115  template<typename T>
117  (std::is_integral_v<T>) && (!std::is_signed_v<T>);
118 
119 
120  // type verification that the class Derived inherits from the class base
121  template<class Base, class Derived>
122  concept isBaseOf= std::is_base_of_v<Base, Derived>;
123 
124  // type verification that the class T is an array of type T
125  template<class T>
126  concept isArray= std::is_array<T>();
127 
128  // type verification that the class T is a bounded array of type T
129  template<class T>
130  concept isBoundedArray= std::is_bounded_array<T>::value;
131 
132  // type verification that the class T is a bounded array of type T
133  template<class T>
134  concept isUnboundedArray= std::is_unbounded_array<T>::value;
135 
136  // type verification that the class T is an double array of type T
137  template<class T>
138  concept isDoubleArray= std::is_pointer_v<T**>;
139 
140  // type verification that the class T is a pointer
141  template<class T>
142  concept isPointer= std::is_pointer_v<T>;
143 
144  // type verification that the class T is a not null pointer
145  template<class T>
146  concept iNotNullPointer= not std::is_pointer<std::nullptr_t>::value;
147 
148  // type verification that the class T and Q are same
149  template<class T,class Q>
150  concept isSameType= std::is_same<T,Q>::value;
151 
152 
153  //type accessors
154  //==============
155 
156  //type id operator
160  template <class T>
161  inline static tString getTypeName(const T& v) {
162  return typeid(v).name();
163  };
164 
168  template <class T>
169  inline static tString getTypeName() {
170  tString tname=typeid(T).name();
171  int status;
172  char *demangled_name = abi::__cxa_demangle(tname.c_str(), NULL, NULL, &status);
173  if(status == 0) {
174  tname = demangled_name;
175  std::free(demangled_name);
176  }
177  return tname;
178  };
182  template <class T>
183  inline tString pointerToString(const T * ptr) {
184  std::stringstream cstr;
185  cstr<<ptr;
186  return cstr.str();
187  };
188  //equal operators
189  //================
195  template<typename T>
196  inline tBoolean areEquals(const T& e1, const T& e2 )
197  { return (e1==e2) ; }
198 
206  template<>
207  inline tBoolean areEquals(const tReal& e1,const tReal& e2 ) {
208  return abs(e1-e2)<std::numeric_limits<tReal>::epsilon();
209  }
217  template<>
218  inline tBoolean areEquals(const tComplex& e1,const tComplex &e2 ) {
219  return abs(e1-e2)<std::numeric_limits<tReal>::epsilon();
220  }
221 
229  template<>
230  inline tBoolean areEquals(const tString& e1,const tString & e2 ) {
231  return (e1.compare(e2)==0);
232  }
233 
234 
235  //bit manipulation
236 
237  //a[p]=1
238  template<typename T> requires isIntegerType<T>
239  inline static T& bitSet(T& a,const tUCInt& p) {
240  a |= (1ULL<<p);
241  return a;
242  }
243 
244  //a[p]=0
245  template<typename T> requires isIntegerType<T>
246  inline static T& bitClear(T& a,const tUCInt& p) {
247  a &= ~(1ULL<<p);
248  return a;
249  }
250  //a[p]=!a[p]
251  template<typename T> requires isIntegerType<T>
252  inline static T& bitFlip(T& a,const tUCInt& p) {
253  a ^= (1ULL<<p);
254  return a;
255  }
256 
257  //b=a[p];a[n-1]...a[2]a[1]a[0]
258  template<typename T> requires isIntegerType<T>
259  inline static tBoolean bitGet(const T& a,const tUCInt& p) {
260  return (!!(a & (1ULL<<p)));
261  }
262 
263 
264  //a[p]=1 for all p=1 for all p were mask[p]=1
265  template<typename T> requires isIntegerType<T>
266  inline static T& bitMaskSet(T& x, const T& mask) {
267  x |= (mask);
268  return x;
269  }
270 
271  //a[p]=0 for all p=1 for all p where mask[p]=1
272  template<typename T> requires isIntegerType<T>
273  inline static T& bitMaskClear(T& x, const T& mask) {
274  x &= (~(mask));
275  return x;
276  };
277 
278  //a[p]=!a[p] for all for all p where mask[p]=1
279  template<typename T> requires isIntegerType<T>
280  inline static T& bitMaskFlip(T& x, const T& mask) {
281  x ^= mask;
282  return x;
283  }
284 
285  //return true is all a[p]=1 for all p where mask[p]=1
286  template<typename T> requires isIntegerType<T>
287  inline static tBoolean bitMaskCheckAll(const T& x, const T& mask) {
288  return (!(~(x) & (mask)));
289  }
290 
291  //return false (0) if it does not exist any p such that a[p]=1 for all p where mask[p]=1
292  //return true is all a[p]=1 for all p where mask[p]=1
293  template<typename T> requires isIntegerType<T>
294  inline static tBoolean bitMaskCheckAny(const T& x, const T& mask) {
295  return ((x) & (mask));
296  }
297 
298 
299 
300  //string manipulations
301  //====================
305  template<typename T,std::enable_if_t<std::is_integral_v<T>> * = nullptr>
306  inline tString toString(const T& v,const tUCInt& d) {
307  tString ret=std::to_string(v);
308  for (tUCInt k=ret.length();k<d;k++) {
309  ret="0"+ret;
310  }
311  return ret;
312  }
316  template<typename T,std::enable_if_t<std::is_arithmetic_v<T>> * = nullptr>
317  inline tString toString(const T& v) {
318  return std::to_string(v);
319  }
320 
324  inline tString booleanToString(const tBoolean& v) {
325  return (v)?"true":"false";
326  }
327 
332  inline tString& ltrim(tString& s) {
333  s=std::regex_replace(s, std::regex("^\\s+"), std::string(""));
334  return s;
335  }
340  inline tString& rtrim(tString& s) {
341  s=std::regex_replace(s, std::regex("\\s+$"), std::string(""));
342  return s;
343  }
344 
349  inline tString& trim(tString& s) {
350  return ltrim(rtrim(s));
351  }
352 
359  inline void replaceAll(const tString& word,const tString& rWord,tString& str) {
360  tIndex pos = str.find(word);
361  tIndex lWord=word.length();
362  tIndex lrWord=rWord.length();
363  // Repeat till end is reached
364  while( pos != std::string::npos)
365  {
366  // Replace this occurrence of Sub String
367  str.replace(pos, lWord , rWord);
368  // Get the next occurrence from the current position
369  pos =str.find(word, pos + lrWord);
370  }
371  }
375  inline void toLower(tString& s) {
376  std::transform(s.cbegin(), s.cend(), s.begin(),
377  [](unsigned char c){ return std::tolower(c);});
378  }
382  inline void toUpper(tString& s) {
383  tString r=s;//returned string
384  std::transform(s.cbegin(), s.cend(), s.begin(),
385  [](unsigned char c){ return std::toupper(c);});
386  }
387 
394  inline void tokenize(tString& str,
395  const tString& delim,
396  std::vector<tString> words) {
397  char *rest=&str[0];
398  char *token;
399  words.clear();
400  while ( (token = strtok_r(rest,delim.c_str(),&rest))) words.push_back(tString(token));
401  }
402 
403 
404 
405  //Numeric functions
406  //======================
407 
413  template<typename T>
414  requires core_functions::isArithmeticType<T>
415  inline const T& min(const T& a,const T&b) {
416  return (a<b)?a:b;
417  }
423  template<typename T>
424  requires core_functions::isArithmeticType<T>
425  inline const T& max(const T& a,const T&b) {
426  return (a>b)?a:b;
427  }
428 
433  template<typename T>
434  requires core_functions::isRealType<T>
435  inline tBoolean isNAN(const T& s) {
436  return std::isnan(s);
437  }
438 
443  inline tBoolean isInteger(const tString& s) {
444  char* p;
445  strtol(s.c_str(), &p, 10);
446  if (*p) {
447  // conversion failed because the input wasn't a number
448  return false;
449  }
450  else {
451  // use converted
452  return true;
453  }
454  }
459  inline tBoolean isNumeric(const tString& s) {
460  char* p;
461  strtod(s.c_str(), &p);
462  if (*p) {
463  // conversion failed because the input wasn't a number
464  return false;
465  }
466  else {
467  // use converted
468  return true;
469  }
470  }
475  inline tBoolean isString(const tString& s) {
476  return ( (!isInteger(s)) && (!isNumeric(s)));
477  }
478 
479  /* \brief get min epsilon
480  * @return the minimum positive significant value
481  */
482  template<class T>
483  requires core_functions::isArithmeticType<T>
484  inline static T getEpsilon() {
485  return std::numeric_limits<T>::epsilon();
486  }
487  /* \brief get infinity value
488  * @return the infinity value
489  */
490  template<class T>
491  requires core_functions::isArithmeticType<T>
492  inline static T getInfinity() {
493  return std::numeric_limits<T>::infinity();
494  }
495  /* \brief get max value
496  * @return the maximum positive value
497  */
498  template<class T>
499  requires core_functions::isIntegerType<T>
500  inline static T getMax() {
501  return std::numeric_limits<T>::max();
502  }
503  /* \brief get min value
504  * @return the minimum positive value
505  */
506  template<class T>
507  requires core_functions::isIntegerType<T>
508  inline static T getMin() {
509  return std::numeric_limits<T>::min();
510  }
511 
512 
517  template<typename T>
518  inline void parse(const tString& s,T& v) {
519  std::stringstream ss;
520  ss<<s;
521  ss>>v;
522  }
527  template<>
528  inline void parse(const tString& s,tBoolean& v) {
529  tString ss(s);
531  if (ss.compare("true")==0) v=true;
532  v=false;
533  }
534 
537  template<typename T,size_t D>
538  inline tString toString(const std::array<T,D>& a) {
539  std::stringstream ss;
540  ss<<"{";
541  for(const auto & ai : a) {
542  ss<<ai<<",";
543  }
544  ss.seekp(-1, std::ios_base::end);
545  ss<<"}";
546  return ss.str();
547  }
550  template<typename T>
551  inline tString toString(const std::valarray<T>& a) {
552  std::stringstream ss;
553  ss<<"{";
554  for(const auto & ai : a) {
555  ss<<ai<<",";
556  }
557  ss.seekp(-1, std::ios_base::end);
558  ss<<"}";
559  return ss.str();
560  }
561 
562  //complex multiplicator
563  template<typename Q>
564  inline const std::pair<Q,Q>& complexMultiply(const std::pair<Q,Q>& alpha,std::pair<Q,Q>& beta) {
565  Q betaImg=beta.second;
566  beta.second=alpha.first*betaImg+alpha.second*beta.first;
567  beta.first=alpha.first*beta.first-alpha.second*betaImg;
568  return beta;
569 
570  }
571 
572  //complex add
573  template<typename Q>
574  inline const std::pair<Q,Q>& complexAdd(const std::pair<Q,Q>& alpha,std::pair<Q,Q>& beta) {
575  beta.first+=alpha.first;
576  beta.second+=alpha.second;
577  return beta;
578 
579  }
580 
581 }//end core_functions namespace
582 
583 
584 
585 //class function
586 
587 
588 //debug command
589 //==============
590 //if the program has been compiled with -DDEBUG, the assert command is executed
591 #ifdef DEBUG
592 
593 #define ASSERT(a) {assert(a);}
594 #define ASSERT_IN(a) {assert(a);}
595 #define ASSERT_OUT(a) {assert(a);}
596 #define ASSERT_EXCEPTION(T,P,F,C) { if (!(T)) throw CORE_Exception(P,F,C);}
597 
598 #else
599 
600 #define ASSERT(a) {}
601 #define ASSERT_IN(a) {}
602 #define ASSERT_OUT(a) {}
603 #define ASSERT_EXCEPTION(T,P,F,C)
604 
605 //endif DEBUG
606 #endif
607 
608 
609 //endif FUNCTION_H
610 #endif
Definition: functions.h:66
concept isIntegerType
Definition: functions.h:106
concept isSameType
Definition: functions.h:150
concept isOrderedType
Definition: functions.h:90
tBoolean areEquals(const T &e1, const T &e2)
tests if two elements are equals
Definition: functions.h:196
tString pointerToString(const T *ptr)
return the pointer of the class as a string
Definition: functions.h:183
concept isBoundedArray
Definition: functions.h:130
concept isUnsignedIntegerType
Definition: functions.h:116
concept isBaseOf
Definition: functions.h:122
const std::pair< Q, Q > & complexMultiply(const std::pair< Q, Q > &alpha, std::pair< Q, Q > &beta)
Definition: functions.h:564
concept isSignedIntegerType
Definition: functions.h:111
tString booleanToString(const tBoolean &v)
Definition: functions.h:324
requires core_functions::isArithmeticType< T > const T & min(const T &a, const T &b)
min function
Definition: functions.h:415
tString toString(const T &v, const tUCInt &d)
Definition: functions.h:306
tBoolean isInteger(const tString &s)
return true if the argument is an integer number
Definition: functions.h:443
concept isStringType
Definition: functions.h:95
concept isComplexType
Definition: functions.h:79
void replaceAll(const tString &word, const tString &rWord, tString &str)
replace all instnce of word by replace word in string
Definition: functions.h:359
tString & trim(tString &s)
remove all the empty characters at the begining and end of the string
Definition: functions.h:349
requires core_functions::isArithmeticType< T > const T & max(const T &a, const T &b)
max function
Definition: functions.h:425
void tokenize(tString &str, const tString &delim, std::vector< tString > words)
tokenize
Definition: functions.h:394
concept iNotNullPointer
Definition: functions.h:146
void toUpper(tString &s)
turn a string to lower value
Definition: functions.h:382
concept isUnboundedArray
Definition: functions.h:134
concept isArithmeticType
Definition: functions.h:85
const std::pair< Q, Q > & complexAdd(const std::pair< Q, Q > &alpha, std::pair< Q, Q > &beta)
Definition: functions.h:574
requires core_functions::isRealType< T > tBoolean isNAN(const T &s)
return true if the argument is an integer number
Definition: functions.h:435
concept isArray
Definition: functions.h:126
concept isPointer
Definition: functions.h:142
tString & ltrim(tString &s)
remove all the empty characters at the beginning of the string
Definition: functions.h:332
concept isDoubleArray
Definition: functions.h:138
void parse(const tString &s, T &v)
parse the string
Definition: functions.h:518
tBoolean isString(const tString &s)
return true if the argument is a string
Definition: functions.h:475
void toLower(tString &s)
turn a string to lower value
Definition: functions.h:375
tBoolean isNumeric(const tString &s)
return true if the argument is an integer number
Definition: functions.h:459
tString & rtrim(tString &s)
remove all the empty characters at the end of the string
Definition: functions.h:340
concept isRealType
Definition: functions.h:100
Definition: functions.h:73
#define tIndex
Definition: types.h:157
#define tString
Definition: types.h:147
#define tComplex
Definition: types.h:139
#define tUCInt
Definition: types.h:26
#define tBoolean
Definition: types.h:151
#define tReal
Definition: types.h:137