npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
ArrayNDScanner.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_ARRAYNDSCANNER_HH_
2 #define NPSTAT_ARRAYNDSCANNER_HH_
3 
4 /*!
5 // \file ArrayNDScanner.hh
6 //
7 // \brief Iteration over indices of a multidimensional array
8 //
9 // Author: I. Volobouev
10 //
11 // July 2012
12 */
13 
14 #include <vector>
15 #include <climits>
16 
17 namespace npstat {
18  /**
19  * This class can be used to iterate over array indices without actually
20  * building the array or requesting any memory from the heap. Typical use:
21  *
22  * @code
23  * for (ArrayNDScanner scanner(shape); scanner.isValid(); ++scanner)
24  * {
25  * scanner.getIndex(indexArray, indexArrayLen);
26  * .... Do what is necessary with multidimensional index ....
27  * .... Extract linear index: ...............................
28  * scanner.state();
29  * }
30  * @endcode
31  *
32  * This can be useful, for example, in case one needs to iterate over
33  * slices of some array (so that the array itself can not be used
34  * to obtain similar information easily).
35  */
37  {
38  public:
39  //@{
40  /** Constructor from a multidimensional array shape */
41  inline ArrayNDScanner(const unsigned* shape, const unsigned lenShape)
42  {initialize(shape, lenShape);}
43 
44  inline explicit ArrayNDScanner(const std::vector<unsigned>& shape)
45  {initialize(shape.empty() ? static_cast<unsigned*>(0) :
46  &shape[0], shape.size());}
47  //@}
48 
49  /** Dimensionality of the scan */
50  inline unsigned dim() const {return dim_;}
51 
52  /** Retrieve current state (i.e., linear index of the scan) */
53  inline unsigned long state() const {return state_;}
54 
55  /** Maximum possible state (i.e., linear index of the scan) */
56  inline unsigned long maxState() const {return maxState_;}
57 
58  /** Returns false when iteration is complete */
59  inline bool isValid() const {return state_ < maxState_;}
60 
61  /** Retrieve current multidimensional index */
62  void getIndex(unsigned* index, unsigned indexBufferLen) const;
63 
64  /** Reset the state (as if the object has just been constructed) */
65  inline void reset() {state_ = 0UL;}
66 
67  /** Prefix increment */
69  {if (state_ < maxState_) ++state_; return *this;}
70 
71  /** Postfix increment (distinguished by the dummy "int" parameter) */
72  inline void operator++(int) {if (state_ < maxState_) ++state_;}
73 
74  /** Set the state directly */
75  inline void setState(const unsigned long state)
76  {state_ = state <= maxState_ ? state : maxState_;}
77 
78  private:
80 
81  void initialize(const unsigned* shape, unsigned lenShape);
82 
83  unsigned long strides_[CHAR_BIT*sizeof(unsigned long)];
84  unsigned long state_;
85  unsigned long maxState_;
86  unsigned dim_;
87  };
88 }
89 
90 #endif // NPSTAT_ARRAYSCANNER_HH_
Definition: ArrayNDScanner.hh:37
unsigned dim() const
Definition: ArrayNDScanner.hh:50
unsigned long state() const
Definition: ArrayNDScanner.hh:53
ArrayNDScanner & operator++()
Definition: ArrayNDScanner.hh:68
bool isValid() const
Definition: ArrayNDScanner.hh:59
unsigned long maxState() const
Definition: ArrayNDScanner.hh:56
void operator++(int)
Definition: ArrayNDScanner.hh:72
void reset()
Definition: ArrayNDScanner.hh:65
ArrayNDScanner(const unsigned *shape, const unsigned lenShape)
Definition: ArrayNDScanner.hh:41
void getIndex(unsigned *index, unsigned indexBufferLen) const
void setState(const unsigned long state)
Definition: ArrayNDScanner.hh:75
Definition: AbsArrayProjector.hh:14