Jpp  19.1.0-rc.1
the software that should make you happy
JArrayIterator.hh
Go to the documentation of this file.
1 #ifndef __JTOOLS__JARRAY_ITERATOR__
2 #define __JTOOLS__JARRAY_ITERATOR__
3 
4 #include "JTools/JArray.hh"
6 
7 
8 /**
9  * \author mdejong
10  */
11 
12 namespace JTOOLS {}
13 namespace JPP { using namespace JTOOLS; }
14 
15 namespace JTOOLS {
16 
17  /**
18  * ND array iterator.
19  */
20  template<int N, class T>
21  struct JArrayIterator :
22  public JArrayIterator<N - 1, T>
23  {
24  /**
25  * Constructor.
26  *
27  * \param collection abscissa values
28  */
30  JArrayIterator<N - 1, T>(collection)
31  {
32  i = 0;
33  }
34 
35 
36  /**
37  * Check validity of iterator.
38  *
39  * \return true if valid; else false
40  */
41  operator bool() const
42  {
43  return i != this->collection.getSize() && JArrayIterator<N-1, T>::operator bool();
44  }
45 
46 
47  /**
48  * Get current value.
49  *
50  * \return value
51  */
53  {
54  return JArray<N, double>(JArrayIterator<N-1, T>::operator *(), this->collection.getX(i));
55  }
56 
57 
58  /**
59  * Prefix increment.
60  *
61  * \return this array sampler
62  */
64  {
65  if (++i == this->collection.getSize()) {
66 
67  i = 0; // rewind
68 
70  }
71 
72  return *this;
73  }
74 
75 
76  /**
77  * Postfix increment.
78  *
79  * \return previous array sampler
80  */
82  {
83  const JArrayIterator previous(*this);
84 
85  ++(*this);
86 
87  return previous;
88  }
89 
90  private:
91  int i;
92  };
93 
94 
95  /**
96  * 1D array iterator.
97  */
98  template<class T>
99  struct JArrayIterator<1, T>
100  {
101  /**
102  * Constructor.
103  *
104  * \param collection abscissa values
105  */
107  collection(collection)
108  {
109  i = 0;
110  }
111 
112 
113  /**
114  * Check validity of iterator.
115  *
116  * \return true if valid; else false
117  */
118  operator bool() const
119  {
120  return i != this->collection.getSize();
121  }
122 
123 
124  /**
125  * Get current value.
126  *
127  * \return value
128  */
130  {
131  return JArray<1, double>(this->collection.getX(i));
132  }
133 
134 
135  /**
136  * Prefix increment.
137  *
138  * \return this array sampler
139  */
141  {
142  if (i != this->collection.getSize()) {
143  ++i;
144  }
145 
146  return *this;
147  }
148 
149 
150  /**
151  * Postfix increment.
152  *
153  * \return previous array sampler
154  */
156  {
157  const JArrayIterator previous(*this);
158 
159  ++(*this);
160 
161  return previous;
162  }
163 
164 
166 
167  private:
168  int i;
169  };
170 }
171 
172 #endif
One dimensional array of template objects with fixed length.
Definition: JArray.hh:43
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for multi-dimensional interpolations and histograms.
JArrayIterator(const JAbstractCollection< T > &collection)
Constructor.
const JAbstractCollection< T > & collection
ND array iterator.
JArray< N, double > operator*() const
Get current value.
JArrayIterator(const JAbstractCollection< T > &collection)
Constructor.
JArrayIterator & operator++()
Prefix increment.