Chombo + EB + MF  3.2
Misc.H
Go to the documentation of this file.
1 #ifdef CH_LANG_CC
2 /*
3  * _______ __
4  * / ___/ / ___ __ _ / / ___
5  * / /__/ _ \/ _ \/ V \/ _ \/ _ \
6  * \___/_//_/\___/_/_/_/_.__/\___/
7  * Please refer to Copyright.txt, in Chombo's root directory.
8  */
9 #endif
10 
11 #ifndef _MISC_H_
12 #define _MISC_H_
13 
14 #include "CH_assert.H"
15 #include "REAL.H"
16 #include <cmath>
17 #include "BaseNamespaceHeader.H"
18 
19 /**
20  Returns a copy of the minimum of the two values. Will work with
21  any type T that supplies a T::operator<(const T, const T), or an
22  equivalent construct, as well as a copy constructor. In
23  particular, it will work with any integral or floating-point
24  intrinsic type.
25 */
26 template <class T> inline T Min(const T& a_a,
27  const T& a_b)
28 {
29  return (a_a < a_b) ? a_a : a_b;
30 }
31 
32 /**
33  Returns a copy of the maximum of the two values. Will work with
34  any type T that supplies a T::operator>(const T, const T), or an
35  equivalent construct, as well as a copy constructor. In
36  particular, it will work with any integral or floating-point
37  intrinsic type.
38 */
39 template <class T> inline T Max (const T& a_a,
40  const T& a_b)
41 {
42  return (a_a > a_b) ? a_a : a_b;
43 }
44 
45 /**
46  Returns a copy of the absolute value of the value. Will work with
47  any type T that can be compared against zero with
48  T::operator>(const T, const T), or an equivalent construct, as
49  well as a copy constructor, and an T::operator-() or equivalent.
50  In particular, it will work with any integral or floating-point
51  intrinsic type.
52 */
53 template <class T> inline T Abs(const T& a_a)
54 {
55  return (a_a > 0) ? a_a : -a_a;
56 }
57 
58 /**
59  Swaps the two values. Type T must have a copy constructor and
60  an assignment operator.
61 */
62 template <class T> inline void Swap(T& a_a,
63  T& a_b)
64 {
65  T tmp = a_a;
66  a_a = a_b;
67  a_b = tmp;
68 }
69 
70 //inline int pow(int a, int b)
71 //{
72 // CH_assert(a>=0);
73 // CH_assert(b>=0);
74 // int rtn = 1;
75 // for (; b>0; b--)
76 // rtn*=a;
77 // return rtn;
78 //}
79 template <unsigned int P>
80 constexpr inline int ipow(int M) { return M*ipow<P-1>(M);}
81 template<>
82 constexpr inline int ipow<0>(int M) {return 1;}
83 
84 inline int ipow(int a, int b)
85 {
86  CH_assert(b>=0);
87  int rtn = 1;
88  for (; b>0; b--)
89  {
90  rtn *= a;
91  }
92  return rtn;
93  // return std::pow(a,b);
94 }
95 
96 inline Real ipow(const Real& a, const int& b)
97 {
98  return std::pow(a, b);
99 
100 // double rtn = 1.0;
101 // int r=Abs(b);
102 // for (;r>0; r--)
103 // rtn*=a;
104 // return (b>0) ? rtn : 1.0/rtn;
105 }
106 
107 /*------------------------------------------------------------------------------
108  * The following rarely used functions are in namespace Misc to avoid conficts
109  * and help identify their origin
110  *----------------------------------------------------------------------------*/
111 
112 namespace Misc
113 {
114 
115 /// Test if integer is a power of 2
116 inline bool isPower2(const int a_i)
117 {
118  if (a_i <= 0) return false; // Catch <= 0
119  unsigned i = a_i;
120  while (!(i & 1)) // Find first bit
121  {
122  i >>= 1;
123  }
124  return (i >> 1) ? false : true; // Can't have any other bits
125 }
126 
127 /// Test for class type
128 /**
129  Use the SFINAE principle as described in 15.2.2 "Determining
130  Class Types" in Vandevoorde and Josuttis "C++ Templates" book to
131  see if T is class type. E.g.,
132  TypeTr<int>::IsClass == false
133  TypeTr<Box>::IsClass == true
134 */
135 template <typename T>
136 class TypeTr
137 {
138  private:
139  typedef char One;
140  typedef struct
141  {
142  char a[2];
143  } Two;
144  template <typename C> static One test(int C::*);
145  template <typename C> static Two test(...);
146  public:
147  enum
148  {
149  IsClass = sizeof(TypeTr<T>::template test<T>(0)) == 1
150  };
151 };
152 
153 } // namespace Misc
154 
155 #include "BaseNamespaceFooter.H"
156 
157 #endif
#define CH_assert(cond)
Definition: CHArray.H:37
bool isPower2(const int a_i)
Test if integer is a power of 2.
Definition: Misc.H:116
static One test(int C::*)
void const char const int const int const int const Real const Real const int const Real const int const Real Real * C
Definition: Lapack.H:83
double Real
Definition: REAL.H:33
T Abs(const T &a_a)
Definition: Misc.H:53
Definition: Misc.H:140
Test for class type.
Definition: Misc.H:136
Definition: Misc.H:149
T Min(const T &a_a, const T &a_b)
Definition: Misc.H:26
void const char const int * M
Definition: Lapack.H:83
T Max(const T &a_a, const T &a_b)
Definition: Misc.H:39
constexpr int ipow(int M)
Definition: Misc.H:80
char One
Definition: Misc.H:139
constexpr int ipow< 0 >(int M)
Definition: Misc.H:82
void Swap(T &a_a, T &a_b)
Definition: Misc.H:62