Limbo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Math.h
Go to the documentation of this file.
1 
8 #ifndef _LIMBO_MATH_MATH
9 #define _LIMBO_MATH_MATH
10 
11 #include <iterator>
12 #include <limits>
13 
15 namespace limbo
16 {
17 
20 template <typename T>
21 inline T abs(T const& t)
22 {
23  return (t > 0)? t : -t;
24 }
30 template <typename Iterator>
31 inline typename std::iterator_traits<Iterator>::value_type sum(Iterator first, Iterator last)
32 {
33  typename std::iterator_traits<Iterator>::value_type v = 0;
34  for (; first != last; ++first)
35  v += *first;
36  return v;
37 }
43 template <typename Iterator>
44 inline typename std::iterator_traits<Iterator>::value_type average(Iterator first, Iterator last)
45 {
46  typename std::iterator_traits<Iterator>::value_type v = 0;
47  size_t cnt = 0;
48  for (; first != last; ++first)
49  {
50  v += *first;
51  cnt += 1;
52  }
53  return v/cnt;
54 }
60 template <typename Iterator>
61 inline typename std::iterator_traits<Iterator>::value_type max(Iterator first, Iterator last)
62 {
63  typename std::iterator_traits<Iterator>::value_type v = *first;
64  for (; first != last; ++first)
65  {
66  if (v < *first)
67  v = *first;
68  }
69  return v;
70 }
76 template <typename Iterator>
77 inline typename std::iterator_traits<Iterator>::value_type min(Iterator first, Iterator last)
78 {
79  typename std::iterator_traits<Iterator>::value_type v = *first;
80  for (; first != last; ++first)
81  {
82  if (v > *first)
83  v = *first;
84  }
85  return v;
86 }
87 
92 template <typename T>
93 inline T lowest();
94 
96 template <>
97 inline char lowest<char>()
98 {
100 }
102 template <>
103 inline unsigned char lowest<unsigned char>()
104 {
106 }
108 template <>
109 inline short lowest<short>()
110 {
112 }
114 template <>
115 inline unsigned short lowest<unsigned short>()
116 {
118 }
120 template <>
121 inline int lowest<int>()
122 {
124 }
126 template <>
127 inline unsigned int lowest<unsigned int>()
128 {
130 }
132 template <>
133 inline long lowest<long>()
134 {
136 }
138 template <>
139 inline unsigned long lowest<unsigned long>()
140 {
142 }
144 template <>
145 inline long long lowest<long long>()
146 {
148 }
150 template <>
151 inline unsigned long long lowest<unsigned long long>()
152 {
154 }
156 template <>
157 inline float lowest<float>()
158 {
160 }
162 template <>
163 inline double lowest<double>()
164 {
166 }
168 template <>
169 inline long double lowest<long double>()
170 {
172 }
174 
175 } // namespace limbo
176 
177 #endif
int lowest< int >()
specialization for integer types
Definition: Math.h:121
long long lowest< long long >()
specialization for integer types
Definition: Math.h:145
unsigned int lowest< unsigned int >()
specialization for integer types
Definition: Math.h:127
T abs(T const &t)
generalized api can handle both integer and floating points
Definition: Math.h:21
char lowest< char >()
specialization for integer types
Definition: Math.h:97
std::iterator_traits< Iterator >::value_type average(Iterator first, Iterator last)
get average of an array
Definition: Math.h:44
unsigned long lowest< unsigned long >()
specialization for integer types
Definition: Math.h:139
std::iterator_traits< Iterator >::value_type sum(Iterator first, Iterator last)
get summation of an array
Definition: Math.h:31
long double lowest< long double >()
specialization for floating point types
Definition: Math.h:169
namespace for Limbo
Definition: GraphUtility.h:22
std::iterator_traits< Iterator >::value_type max(Iterator first, Iterator last)
get max of an array
Definition: Math.h:61
unsigned short lowest< unsigned short >()
specialization for integer types
Definition: Math.h:115
short lowest< short >()
specialization for integer types
Definition: Math.h:109
double lowest< double >()
specialization for floating point types
Definition: Math.h:163
float lowest< float >()
specialization for floating point types
Definition: Math.h:157
unsigned char lowest< unsigned char >()
specialization for integer types
Definition: Math.h:103
long lowest< long >()
specialization for integer types
Definition: Math.h:133
std::iterator_traits< Iterator >::value_type min(Iterator first, Iterator last)
get min of an array
Definition: Math.h:77
unsigned long long lowest< unsigned long long >()
specialization for integer types
Definition: Math.h:151