Chombo + EB  3.0
Misc.H
Go to the documentation of this file.
1 #ifdef CH_LANG_CC
2 /*
3  * _______ __
4  * / ___/ / ___ __ _ / / ___
5  * / /__/ _ \/ _ \/ V \/ _ \/ _ \
6  * \___/_//_/\___/_/_/_/_.__/\___/
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>
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
80 inline int ipow(int a, int b)
81 {
82  CH_assert(b>=0);
83  int rtn = 1;
84  for (; b>0; b--)
85  {
86  rtn *= a;
87  }
88  return rtn;
89  // return std::pow(a,b);
90 }
91
92 inline Real ipow(const Real& a, const int& b)
93 {
94  return std::pow(a, b);
95
96 // double rtn = 1.0;
97 // int r=Abs(b);
98 // for (;r>0; r--)
99 // rtn*=a;
100 // return (b>0) ? rtn : 1.0/rtn;
101 }
102
103
104
105 #include "BaseNamespaceFooter.H"
106 #endif
#define CH_assert(cond)
Definition: CHArray.H:37
double Real
Definition: REAL.H:33
T Abs(const T &a_a)
Definition: Misc.H:53
T Min(const T &a_a, const T &a_b)
Definition: Misc.H:26
int ipow(int a, int b)
Definition: Misc.H:80
T Max(const T &a_a, const T &a_b)
Definition: Misc.H:39
void Swap(T &a_a, T &a_b)
Definition: Misc.H:62