NSCL DDAS  1.0
Support for XIA DDAS at the NSCL
 All Classes Namespaces Files Functions Variables Macros Pages
SumIterator.hpp
1 // SumIterator.hpp
2 //
3 // Author : Jeromy Tompkins
4 // Date : 8/14/2013
5 
6 #ifndef SUMITERATOR_H
7 #define SUMITERATOR_H
8 
9 #include "AlgoIterator.hpp"
10 #include "TrIterator.hpp"
11 #include "TObject.h"
12 
13 namespace TrAnal
14 {
15 
17 
26  template<class T>
27  class SumIterator : public AlgoIterator<T> {
28  public:
29  typedef T value_type;
30  typedef ptrdiff_t difference_type;
31  typedef T* pointer;
32  typedef T& reference;
33  typedef std::input_iterator_tag iterator_category;
34 
35  private:
36  TrRange<T> m_range;
37  double m_sum;
38 
39  public:
40  // Canonicals
41  SumIterator(const TrRange<T>& range)
42  : AlgoIterator<T>(), m_range(range), m_sum(T())
43  { m_sum = accumulate(range); }
44 
45  SumIterator(const SumIterator& that)
46  : AlgoIterator<T>(), m_range(that.m_range), m_sum(that.m_sum)
47  {}
48 
49  virtual ~SumIterator() {}
50 
51  SumIterator& operator=(const SumIterator& that)
52  {
53  if (this!=&that) {
54  m_range = that.m_range;
55  m_sum = that.m_sum;
56  }
57  return *this;
58  }
59 
60  // Equality operators
61  // Note that this will compare both the range
62  bool operator==(const SumIterator& that) const
63  {
64  return (m_range==that.m_range);
65  }
66  bool operator!=(const SumIterator& that) const
67  {
68  return (! operator==(that));
69  }
70 
72 
78  virtual bool operator<(const TrIterator<T>& it) const
79  {
80  return ( max_extent() < it );
81  }
82 
83  // Comparison operators...note that these do not
84  // check that the iterators even correspond to the same
85  // composite object
86  bool operator<(const SumIterator& that) const
87  {
88  return (m_range<that.m_range);
89  }
90  bool operator>(const SumIterator& that) const
91  {
92  return !(m_range<that.m_range);
93  }
94  bool operator<=(const SumIterator& that) const
95  {
96  return (!operator>(that));
97  }
98  bool operator>=(const SumIterator& that) const
99  {
100  return !(m_range<that.m_range);
101  }
102 
103  // Incrementation ++iter;
105  {
106  IncrementalUpdate();
107 
108  return *this;
109  }
110 
111  // Incrementation ++iter;
112  SumIterator operator++(int)
113  {
114  SumIterator tmp(*this);
115  operator++();
116  return tmp;
117  }
118 
120 
123  virtual double operator*() const { return m_sum;}
124 
125  virtual TrIterator<T> min_extent() const
126  { return m_range.begin() < m_range.end() ? m_range.begin() : m_range.end(); }
127 
128  virtual TrIterator<T> max_extent() const
129  { return m_range.end() > m_range.begin() ? m_range.end() : m_range.begin(); }
130 
131  private:
132  void IncrementalUpdate()
133  {
134  // Because ranges are semi inclusive { i.e. [beg,end) }, we
135  // already have all of the information needed to update the
136  // sum. The end is currently "out-of-range" but will become
137  // the newest in-range value once the range in incremented.
138  // The value pointed to by begin will not be in the range any
139  // more once incremented.
140  T old_begin_val = *(m_range.begin());
141  T old_end_val = *(m_range.end());
142 
143  m_sum -= old_begin_val;
144  m_sum += old_end_val;
145 
146  ++m_range;
147  }
148 
149  // Sum all values within the range
150  T accumulate(const TrRange<T>& range)
151  {
152  T sum=T();
153  TrIterator<T> it = range.begin();
154  while (it<range.end()) {
155  sum += *it;
156  ++it;
157  }
158  return sum;
159  }
160 
161  public:
162  // ROOT dictionary generation
164  ClassDef(SumIterator,0);
166  };
167 
168 
169 } // end TrAnal namespace
170 #endif
TrIterator template class.
Definition: TrIterator.hpp:24
TrIterator< T > end() const
Get value of end iterator.
Definition: TrIterator.hpp:224
virtual SumIterator & operator++()
Prefixed incrementation Supports ++x style incrementing. Default implementation does nothing...
Definition: SumIterator.hpp:104
TrRange class.
Definition: TrIterator.hpp:173
virtual double operator*() const
Dereference operator.
Definition: SumIterator.hpp:123
virtual TrIterator< T > min_extent() const
Min extent To implement the minimum iterator extent for the range This default implementation returns...
Definition: SumIterator.hpp:125
TrIterator< T > begin() const
Get value of begin iterator.
Definition: TrIterator.hpp:222
Definition: AlgoIterator.hpp:12
virtual bool operator<(const TrIterator< T > &it) const
Comparison operator.
Definition: SumIterator.hpp:78
virtual TrIterator< T > max_extent() const
Max extent To implement the maximum iterator extent for the range This default implementation returns...
Definition: SumIterator.hpp:128
Abstract AlgoIterator base class.
Definition: AlgoIterator.hpp:17
Summing iterator.
Definition: SumIterator.hpp:27