Home
Install
Use
Develop
Support
News
Credits
hosted by
|
8#ifndef _Util_genutil_templates_h
9#define _Util_genutil_templates_h
20template< typename C, typename M>
21void foreach (C& container, M method)
23 std::for_each (container.begin(), container.end(),
24 std::mem_fun(method));
28template< typename C, typename M, typename A>
29void foreach (C& container, M method, const A& a)
31 std::for_each (container.begin(), container.end(),
32 std::bind2nd( std::mem_fun(method), a ));
36void scrunch (std::vector<T>& vals, unsigned factor, bool mean = true)
38 typename std::vector<T>::iterator into = vals.begin();
39 typename std::vector<T>::iterator val;
43 for (val = vals.begin(); val != vals.end(); into++) {
45 for (fi=1; fi<factor && val != vals.end(); (val++, fi++))
50 vals.resize (vals.size()/factor);
55void shift ( unsigned size, unsigned shift, T* array)
57 T* temp = new T[shift];
58 memcpy (temp, array, shift* sizeof(T));
59 memmove (array, array+shift, (size-shift)* sizeof(T));
60 memcpy (array+size-shift, temp, shift* sizeof(T));
66void shift ( unsigned size, unsigned shift, T* result, const T* input)
68 memcpy (result, input+shift, (size-shift)* sizeof(T));
69 memcpy (result+size-shift, input, shift* sizeof(T));
74T histomean ( const std::vector<T>& vals)
79 T total = vals.size();
82 typename std::vector<T>::const_iterator val;
84 for (val = vals.begin(); val != vals.end(); val++) {
85 valcount += *val * bin;
86 totcount += *val * total;
90 return valcount/totcount;
102template < class I, class T>
103T sum ( const I& it1, const I& it2, T& the_sum)
107 for (I it=it1; it != it2; it++)
114template < class I> typename std::iterator_traits<I>::value_type
115sum ( const I& it1, const I& it2)
117 typename std::iterator_traits<I>::value_type t;
118 return sum (it1, it2, t);
122template < class C> typename C::value_type
125 return sum (x.begin(), x.end());
131template < class I, class T> T
132mean ( const I& it1, const I& it2, T& the_mean)
134 the_mean = sum(it1, it2, the_mean) / (it2 - it1);
139template < class I> typename std::iterator_traits<I>::value_type
140mean ( const I& it1, const I& it2)
142 typename std::iterator_traits<I>::value_type t;
143 return mean (it1, it2, t);
147template < class C> typename C::value_type
150 return mean (x.begin(), x.end());
156template < class I, class T> T
157variance ( const I& it1, const I& it2, T& the_variance)
159 T the_mean = mean (it1, it2, the_mean);
162 for (I it=it1; it != it2; it++)
163 the_variance += sqr( T(*it) - the_mean );
165 the_variance /= (it2 - it1 - 1);
171template < class I> typename std::iterator_traits<I>::value_type
172variance ( const I& it1, const I& it2)
174 typename std::iterator_traits<I>::value_type t;
175 return variance (it1, it2, t);
179template < class C> typename C::value_type
182 return variance (x.begin(), x.end());
187void normalize (std::vector<T>& x)
190 assert( the_sum != 0 );
191 for ( unsigned i=0; i<x.size(); i++)
196template < class T, class I>
197void minmax ( const I& it1, const I& it2, T& min, T& max, bool follow = false)
199 if (!follow && (it1 != it2))
202 for (I it=it1; it != it2; it++) {
211template < class T, class C>
212void minmax ( const C& c, T& min, T& max, bool follow = false)
214 minmax (c.begin(), c.end(), min, max, follow);
218template < class T, class C>
219 void cyclic_minmax ( const C& c, unsigned i1, unsigned i2, T& min, T& max,
223 minmax (c.begin()+i1, c.begin()+i2, min, max, follow);
226 minmax (c.begin()+i1, c.end(), min, max, follow);
227 minmax (c.begin(), c.begin()+i2, min, max, true);
232template < class T, class C>
233bool found ( const T& x, const C& c)
235 return std::find (c.begin(), c.end(), x) != c.end();
239template< class T, class C>
240int index ( const T& x, const C& c)
242 typename C::const_iterator f = std::find (c.begin(), c.end(), x);
246 return f - c.begin();
250template< class T, class C>
251bool remove ( const T& x, C& c)
253 typename C::iterator f = std::find (c.begin(), c.end(), x);
274void remove (C& c, unsigned first, unsigned last)
276 c.erase (c.begin()+first, c.begin()+last+1);
280template< typename Big, typename Small>
281inline Big multiple_greater (Big big, Small small)
283 Big divides = big / small;
287 return divides * small;
292template< typename C1, typename C2>
293void set_difference (C1& B, const C2& A)
295 typename C1::iterator newlast = B.end();
296 for ( typename C2::const_iterator it=A.begin(); it != A.end(); it++)
297 newlast = std::remove (B.begin(), newlast, *it);
298 B.erase (newlast, B.end());
Generated using doxygen 1.14.0
|