aimstil  5.0.5
Accumulator.h
Go to the documentation of this file.
1 #ifndef TIL_ACCUMULATOR_H_
2 #define TIL_ACCUMULATOR_H_
3 
4 // includes from TIL
5 #include "til/cat2type.h"
6 #include "til/functors.h"
7 //#include "globalTraits.h"
8 
9 
10 namespace til
11 {
12 
13  //-----------------------------------------------------------------------------------------------------
14 
15  //---------------------------------//
16  // Standard accumulator policies //
17  //---------------------------------//
18 
19  namespace policy
20  {
23  {
24  template < typename T >
25  void value(T) {}
26 
27  template < typename TIterator >
28  void range(TIterator, TIterator) {}
29 
30  template < typename TContainer >
31  void container(TContainer &) {}
32  };
33 
37  {
38  public: // constructors & destructor
39 
40  AccumulatorRecord_Sum() { m_count = 0; }
41 
42  public: // set & get
43 
44  unsigned int get() const { return m_count; }
45 
46  public: // functions
47 
48  template < typename T >
49  void value(T)
50  { ++m_count; }
51 
52  template < typename TIterator >
53  void range(const TIterator & start, const TIterator & finish)
54  { m_count += std::distance(start, finish); }
55 
56  template < typename TContainer >
57  void container(const TContainer & c)
58  { m_count += size(c); }
59 
60  private:
61  unsigned int m_count;
62  };
63  }
64 
65 
66  //-----------------------------------------------------------------------------------------------------
67 
68  //---------------//
69  // Accumulator //
70  //---------------//
71 
80  template < typename T, typename TAccumulation, typename AccumulationPolicy, typename RecordPolicy = policy::AccumulatorRecord_None>
82  {
83  public: // constructors
84 
86  m_accumulation()
87  , m_accumulationPolicy()
88  , m_recordPolicy()
89  {}
90 
91  public: // set & get
92 
94  const TAccumulation & get() const { return m_accumulation; }
96  const RecordPolicy & recordPolicy() const { return m_recordPolicy; }
97 
99  void clear() { m_accumulation.clear(); }
100 
101  public: // functions
102 
104  void accumulate(typename boost::call_traits<T>::param_type value)
105  { m_accumulationPolicy(m_accumulation, value); }
106 
108  template < typename TIterator >
109  // ensure that the iterator points on the same type as the histo type
110  typename boost::enable_if<boost::is_same<typename value_type_of<TIterator>::type, T> >::type
111  accumulate(TIterator begin, TIterator end)
112  {
113  m_recordPolicy.range(begin, end);
114  for (; begin != end; ++begin)
115  {
116  // TODO: We could actually propose to do something when the containee is also a container, also
117  // it is not necessarily a good idea.
118  //this->_accumulateValue(*begin);
119  this->accumulate(*begin);
120  }
121  }
122 
123  private: // data
124 
125  TAccumulation m_accumulation;
126  AccumulationPolicy m_accumulationPolicy;
127  RecordPolicy m_recordPolicy;
128  };
129 
130 
131  //-----------------------------------------------------------------------------------------------------
132 
133  //-------------------//
134  // MeanAccumulator //
135  //-------------------//
136 
138  template < typename T, typename TAccumulation >
140  public Accumulator<T, TAccumulation, functor::AddTo<TAccumulation, T>, policy::AccumulatorRecord_Sum >
141  {
142  public: // typedefs
143 
146 
147  public: // set & get
148 
150  TAccumulation get()
151  {
152  return this->Base::get() / this->recordPolicy().get();
153  }
154  };
155 
156 } // namespace til
157 
158 #endif /*ACCUMULATOR_H_*/
void range(TIterator, TIterator)
Definition: Accumulator.h:28
Accumulator< T, TAccumulation, functor::AddTo< TAccumulation, T >, policy::AccumulatorRecord_Sum > Base
Definition: Accumulator.h:145
boost::enable_if< boost::is_same< typename value_type_of< TIterator >::type, T > >::type accumulate(TIterator begin, TIterator end)
Accumulate values spanned by given range.
Definition: Accumulator.h:111
This Record policy counts the number of elements that have been added.
Definition: Accumulator.h:36
Belongs to package Box Do not include directly, include til/Box.h instead.
Definition: Accumulator.h:10
numeric_array< T, D > size(const Box< T, D > &box)
Return the size of a box.
Definition: boxTools.h:56
const TAccumulation & get() const
Get accumulation result.
Definition: Accumulator.h:94
void accumulate(typename boost::call_traits< T >::param_type value)
Accumulate a value.
Definition: Accumulator.h:104
void clear()
clear accumulated values
Definition: Accumulator.h:99
A class to accumulate values and return their mean.
Definition: Accumulator.h:139
Record nothing while accumulating values.
Definition: Accumulator.h:22
A class to accumulate value.
Definition: Accumulator.h:81
const RecordPolicy & recordPolicy() const
Get record policy.
Definition: Accumulator.h:96
MeanAccumulator< T, TAccumulation > Self
Definition: Accumulator.h:144
void container(const TContainer &c)
Definition: Accumulator.h:57
void range(const TIterator &start, const TIterator &finish)
Definition: Accumulator.h:53