NSCL DDAS  1.0
Support for XIA DDAS at the NSCL
 All Classes Namespaces Files Functions Variables Macros Pages
TrIterator.hpp
1 // TrIterator.hpp
2 //
3 // Author : Jeromy Tompkins
4 // Date : 8/14/2013
5 
6 #ifndef TRITERATOR_H
7 #define TRITERATOR_H
8 
9 #include <cstddef>
10 #include "TObject.h"
11 
12 namespace TrAnal
13 {
14 
16 
23 template<class T>
24 class TrIterator // functions as though it inherits from std::iterator< ::std::input_iterator_tag,T>
25 {
26  public:
27  typedef T value_type;
28  typedef ptrdiff_t difference_type;
29  typedef T* pointer;
30  typedef T& reference;
31  typedef std::input_iterator_tag iterator_category;
32 
33  private:
34  const T* m_iter;
35 
36  public:
38 
44  TrIterator(const T* iter=0) : m_iter(iter) {}
45 
47 
51  TrIterator(const TrIterator& iter) : m_iter(iter.m_iter) {}
52 
54 
60  if (this != &iter) {
61  m_iter = iter.m_iter;
62  }
63  return *this;
64  }
65 
66  // support boolean comparison
68  bool operator==(const TrIterator& iter) const { return m_iter==iter.m_iter;}
70  bool operator!=(const TrIterator& iter) const { return m_iter!=iter.m_iter;}
71 
73  bool operator<(const TrIterator& iter) const { return m_iter<iter.m_iter;}
75  bool operator<=(const TrIterator& iter) const { return m_iter<=iter.m_iter;}
77  bool operator>(const TrIterator& iter) const { return m_iter>iter.m_iter;}
79  bool operator>=(const TrIterator& iter) const { return m_iter>=iter.m_iter;}
80 
81  // Incrementation support
83  TrIterator& operator++() { ++m_iter; return *this;}
85  TrIterator operator++(int) { TrIterator tmp(*this); operator++(); return tmp;}
86 
87  // Support for decrementing
89  TrIterator& operator--() { --m_iter; return *this;}
91  TrIterator operator--(int) { TrIterator tmp(*this); operator--(); return tmp;}
92 
93  // Support basic random access
95  TrIterator operator+(int n) const { TrIterator tmp(m_iter+n); return tmp;}
97  TrIterator& operator+=(int n) { m_iter=m_iter+n; return *this;}
99  TrIterator operator-(int n) const { TrIterator tmp(m_iter-n); return tmp;}
101  TrIterator& operator-=(int n) { m_iter=m_iter-n; return *this;}
102 
103  // Subtraction
105 
108  difference_type operator-(const TrIterator& that) const
109  {
110  return (m_iter-that.m_iter);
111  }
112 
113  // support value access
115 
118  value_type operator*() const { return *m_iter;}
119 
120  // ROOT dictionary generation
122  ClassDef(TrIterator,0);
124 
125 }; // end TrIterator class
126 
128 
132 template<class T>
133 TrIterator<T> operator+(int n, const TrIterator<T>& iter)
134 {
135  return iter.operator+(n);
136 }
137 
139 
143 template<class T>
144 TrIterator<T> operator-(int n, const TrIterator<T>& iter)
145 {
146  return iter.operator-(n);
147 }
148 
149 //-------------------------------------------------------------------
151 //
152 // TrRange class
153 //___________________________________________________________________
154 
155 
157 
172 template<class T>
173 class TrRange
174 {
175  private:
176  TrIterator<T> m_begin;
177  TrIterator<T> m_end;
178 
179  public:
181 
189  const TrIterator<T>& e=TrIterator<T>())
190  : m_begin(b), m_end(e) {}
191 
193 
202  TrRange(const TrIterator<T>& ref, int range) : m_begin(ref), m_end(ref+range)
203  {
204  if (m_begin>m_end) invert();
205  }
206 
208  TrRange(const TrRange& that) : m_begin(that.m_begin), m_end(that.m_end) {}
209 
211  TrRange& operator=(const TrRange& that)
212  {
213  if (this != &that) {
214  m_begin = that.m_begin;
215  m_end = that.m_end;
216  }
217  return *this;
218  }
219 
220  // Member access operators
222  TrIterator<T> begin() const { return m_begin;}
224  TrIterator<T> end() const { return m_end;}
225 
227 
232  {
233  m_begin.operator++();
234  m_end.operator++();
235 
236  return *this;
237  }
238 
240 
246  {
247  TrIterator<T> tmpb = m_begin++;
248  TrIterator<T> tmpe = m_end++;
249  TrRange tmp (tmpb,tmpe);
250 
251  return tmp;
252  }
253 
255 
260  {
261  m_begin.operator--();
262  m_end.operator--();
263 
264  return *this;
265  }
266 
268 
274  {
275  TrIterator<T> tmpb = m_begin--;
276  TrIterator<T> tmpe = m_end--;
277  TrRange tmp (tmpb,tmpe);
278 
279  return tmp;
280  }
281 
282  // Basic random access
284 
288  TrRange operator+(int n) const
289  {
290  TrRange tmp(m_begin.operator+(n),
291  m_end.operator+(n));
292 
293  return tmp;
294  }
295 
297 
301  TrRange operator-(int n) const
302  {
303  TrRange tmp(m_begin.operator-(n),
304  m_end.operator-(n));
305 
306  return tmp;
307  }
308 
310 
315  {
316  m_begin.operator+=(n);
317  m_end.operator+=(n);
318 
319  return *this;
320  }
321 
323 
328  {
329  m_begin.operator-=(n);
330  m_end.operator-=(n);
331 
332  return *this;
333  }
334 
336 
339  typename TrIterator<T>::difference_type range() const { return (m_end - m_begin);}
340 
342  void invert()
343  {
344  TrIterator<T> tmp=m_begin;
345  m_begin=m_end;
346  m_end=tmp;
347 
348  }
349 
350  // Equality operators
352 
355  bool operator==(const TrRange& that) const { return ((m_begin==that.m_begin)&&(m_end==that.m_end));}
357  bool operator!=(const TrRange& that) const { return (! operator==(that));}
358 
359  // Comparison operators
361 
368  bool operator<(const TrRange& that) const { return ((m_begin<that.m_begin) && (m_end<that.m_end));}
370 
376  bool operator>(const TrRange& that) const { return ((m_begin>that.m_begin) && (m_end>that.m_end));}
377 
379  bool operator<=(const TrRange& that) const { return (operator<(that) || operator==(that)); }
381  bool operator>=(const TrRange& that) const { return (operator>(that) || operator==(that)); }
382 
384  ClassDef(TrRange,0);
386 
387 }; // end of TrRange class
388 
400 template<class T>
401 TrRange<T> operator+(int n, const TrRange<T>& range)
402 {
403  TrRange<T> tmp = range.operator+(n);
404  return tmp;
405 }
406 
407 
420 template<class T>
421 TrRange<T> operator-(int n, const TrRange<T>& range)
422 {
423  TrRange<T> tmp = range.operator-(n);
424  return tmp;
425 }
426 
427 } // end namespace
428 
429 
430 #endif
TrIterator operator--(int)
iter– type decremenation
Definition: TrIterator.hpp:91
bool operator<=(const TrIterator &iter) const
Less than or equal to operator.
Definition: TrIterator.hpp:75
bool operator==(const TrRange &that) const
Equality operator.
Definition: TrIterator.hpp:355
TrIterator template class.
Definition: TrIterator.hpp:24
TrIterator< T > end() const
Get value of end iterator.
Definition: TrIterator.hpp:224
TrRange & operator-=(int n)
range -=n style decrement
Definition: TrIterator.hpp:327
TrRange(const TrRange &that)
Copy constructor.
Definition: TrIterator.hpp:208
bool operator>(const TrRange &that) const
Greater than operator.
Definition: TrIterator.hpp:376
TrRange class.
Definition: TrIterator.hpp:173
TrRange & operator--()
–range style decrementation
Definition: TrIterator.hpp:259
void invert()
Swap begin and end iterators.
Definition: TrIterator.hpp:342
TrIterator operator+(int n) const
iter+n
Definition: TrIterator.hpp:95
TrRange operator-(int n) const
range-n style decrement
Definition: TrIterator.hpp:301
TrRange & operator++()
++range style incrementation
Definition: TrIterator.hpp:231
TrRange(const TrIterator< T > &ref, int range)
Range specified constructor.
Definition: TrIterator.hpp:202
TrRange(const TrIterator< T > &b=TrIterator< T >(), const TrIterator< T > &e=TrIterator< T >())
Default constructor.
Definition: TrIterator.hpp:188
bool operator!=(const TrIterator &iter) const
Inequality operator.
Definition: TrIterator.hpp:70
TrIterator(const T *iter=0)
Default constructor.
Definition: TrIterator.hpp:44
TrIterator & operator+=(int n)
iter += n
Definition: TrIterator.hpp:97
TrRange & operator+=(int n)
range += n support
Definition: TrIterator.hpp:314
TrIterator< T >::difference_type range() const
Compute distance between begin and end.
Definition: TrIterator.hpp:339
difference_type operator-(const TrIterator &that) const
Distance between two iterators.
Definition: TrIterator.hpp:108
TrIterator operator-(int n) const
iter-n
Definition: TrIterator.hpp:99
TrIterator & operator++()
++iter type incrementation
Definition: TrIterator.hpp:83
TrIterator(const TrIterator &iter)
Copy constructor.
Definition: TrIterator.hpp:51
TrIterator< T > begin() const
Get value of begin iterator.
Definition: TrIterator.hpp:222
Definition: AlgoIterator.hpp:12
TrRange operator+(int n) const
range+n style increment
Definition: TrIterator.hpp:288
value_type operator*() const
Dereference operator.
Definition: TrIterator.hpp:118
TrRange & operator=(const TrRange &that)
Assignment operator.
Definition: TrIterator.hpp:211
TrIterator & operator-=(int n)
iter -= n
Definition: TrIterator.hpp:101
TrIterator & operator=(const TrIterator &iter)
Assignment operator.
Definition: TrIterator.hpp:59
bool operator<(const TrRange &that) const
Less than operator.
Definition: TrIterator.hpp:368
TrRange operator++(int)
range++ style incrementation
Definition: TrIterator.hpp:245
TrIterator & operator--()
–iter type decrementation
Definition: TrIterator.hpp:89
bool operator!=(const TrRange &that) const
Inequality operator.
Definition: TrIterator.hpp:357
bool operator<(const TrIterator &iter) const
Less than operator.
Definition: TrIterator.hpp:73
TrRange operator--(int)
Decrementation range–;.
Definition: TrIterator.hpp:273
bool operator>=(const TrIterator &iter) const
Greater than or equal to operator.
Definition: TrIterator.hpp:79
bool operator==(const TrIterator &iter) const
Equality operator.
Definition: TrIterator.hpp:68
bool operator<=(const TrRange &that) const
Greater than or equal operator.
Definition: TrIterator.hpp:379
TrIterator operator++(int)
iter++ type incremenation
Definition: TrIterator.hpp:85
bool operator>=(const TrRange &that) const
Less than or equal operator.
Definition: TrIterator.hpp:381
bool operator>(const TrIterator &iter) const
Greater than operator.
Definition: TrIterator.hpp:77