00001
00007 #ifndef __GENERICALGS_H__
00008 #define __GENERICALGS_H__
00009
00010 #include <algorithm>
00011
00018 #ifndef ISEQ
00019 #define ISEQ(container) (container).begin(),(container).end()
00020 #endif
00021
00022
00028 #ifndef callMemberFunction
00029 #define callMemberFunction(object,ptrToMember) ((object).*(ptrToMember))
00030 #endif
00031
00032
00034 namespace Generic {
00035
00036
00037
00039 template <class T>
00040 inline const T crop(const T lower, const T upper, const T x) {
00041 return std::max(lower,std::min(upper,x));
00042 }
00043
00044
00045
00053 template <class T>
00054 inline const T wrap(const T lower, const T upper, const T x) {
00055 const T range=upper-lower;
00056 return lower + (T)fmod(x-lower + 2*range*(1-floor(x/(2*range))),
00057 range);
00058 }
00059
00060
00061
00063 template <class T>
00064 inline int round(const T x) { return int(x+0.5); }
00065
00066
00067
00072 template<class Container>
00073 void delete_contents(Container& c)
00074 {
00075 while(!c.empty()) {
00076 delete c.back();
00077 c.pop_back();
00078 }
00079 }
00080
00081
00082
00088 template<class Container>
00089 void nested_delete_contents(Container& c)
00090 {
00091 while(!c.empty()) {
00092 delete_contents(*c.back());
00093 delete c.back();
00094 c.pop_back();
00095 }
00096 }
00097
00098
00099
00102 template <class ForwardIterator, class T>
00103 void lower_threshold(ForwardIterator first, ForwardIterator last,
00104 const T& threshold, const T& new_value)
00105 { for (; first != last; ++first) if (*first < threshold) *first=new_value; }
00106
00107
00108
00110 template <class InputIterator, class ForwardIterator, class T>
00111 void lower_threshold(InputIterator ifirst, InputIterator ilast,
00112 ForwardIterator ofirst,
00113 const T& threshold, const T& new_value) {
00114 for (; ifirst != ilast; ++ifirst,++ofirst)
00115 *ofirst = (*ifirst < threshold) ? new_value : *ifirst;
00116 }
00117
00118
00119
00122 template <class ForwardIterator, class T>
00123 void upper_threshold(ForwardIterator first, ForwardIterator last,
00124 const T& threshold, const T& new_value)
00125 { for (; first != last; ++first) if (*first > threshold) *first=new_value; }
00126
00127
00128
00130 template <class InputIterator, class ForwardIterator, class T>
00131 void upper_threshold(InputIterator ifirst, InputIterator ilast,
00132 ForwardIterator ofirst,
00133 const T& threshold, const T& new_value) {
00134 for (; ifirst != ilast; ++ifirst,++ofirst)
00135 *ofirst = (*ifirst > threshold) ? new_value : *ifirst;
00136 }
00137
00138
00139
00144 template <class InputIterator, class T, class Operation >
00145 T accumulate(InputIterator beg, InputIterator end, T init, Operation op)
00146 {
00147 for ( ; beg != end; ++beg)
00148 init = init + op(*beg);
00149 return init;
00150 }
00151
00157 template <class InputIterator, class T, class Operation, class BinaryOperation >
00158 T accumulate(InputIterator beg, InputIterator end, T init, Operation op, BinaryOperation binop)
00159 {
00160 for ( ; beg != end; ++beg)
00161 init = binop(init,op(*beg));
00162 return init;
00163 }
00164
00165
00167 template <class T>
00168 inline T hypot(T x, T y)
00169 { return sqrt(x*x+y*y); }
00170
00171
00172 }
00173 #endif