npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
BoxNDScanner.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_BOXNDSCANNER_HH_
2 #define NPSTAT_BOXNDSCANNER_HH_
3 
4 /*!
5 // \file BoxNDScanner.hh
6 //
7 // \brief Iteration over uniformly spaced coordinates inside
8 // a multidimensional box
9 //
10 // Author: I. Volobouev
11 //
12 // March 2010
13 */
14 
15 #include "npstat/nm/BoxND.hh"
16 
17 namespace npstat {
18  /**
19  * A class for iterating over all coordinates in a multidimensional box
20  * (but not a full-fledeged iterator). The expected usage pattern is as
21  * follows:
22  *
23  * @code
24  * double* coords = ... (the buffer size should be at least box.dim())
25  * for (BoxNDScanner<double> scan(box,shape); scan.isValid(); ++scan)
26  * {
27  * scan.getCoords(coords, coordsBufferSize);
28  * .... Do what is necessary with coordinates ....
29  * .... Extract linear bin number: ..............
30  * scan.state();
31  * }
32  * @endcode
33  *
34  * The coordinates will be in the middle of the bins (imagine
35  * a multivariate histogram with boundaries defined by the given box).
36  */
37  template <typename Numeric>
39  {
40  public:
41  //@{
42  /**
43  // Constructor from a bounding box and a multidimensional
44  // array shape
45  */
46  inline BoxNDScanner(const BoxND<Numeric>& box,
47  const std::vector<unsigned>& shape)
48  : box_(box), state_(0UL)
49  {initialize(shape.empty() ? static_cast<unsigned*>(0) :
50  &shape[0], shape.size());}
51 
52  inline BoxNDScanner(const BoxND<Numeric>& box,
53  const unsigned* shape, const unsigned lenShape)
54  : box_(box), state_(0UL) {initialize(shape, lenShape);}
55  //@}
56 
57  /** Dimensionality of the scan */
58  inline unsigned dim() const {return box_.dim();}
59 
60  /** Retrieve current state (i.e., linear index of the scan) */
61  inline unsigned long state() const {return state_;}
62 
63  /** Maximum possible state (i.e., linear index of the scan) plus one */
64  inline unsigned long maxState() const {return maxState_;}
65 
66  /** Returns false when iteration is complete */
67  inline bool isValid() const {return state_ < maxState_;}
68 
69  /** Retrieve current coordinates inside the box */
70  void getCoords(Numeric* x, unsigned nx) const;
71 
72  /** Retrieve current multidimensional index */
73  void getIndex(unsigned* index, unsigned indexBufferLen) const;
74 
75  /** Reset the state (as if the object has just been constructed) */
76  inline void reset() {state_ = 0UL;}
77 
78  /** Prefix increment */
80  {if (state_ < maxState_) ++state_; return *this;}
81 
82  /** Postfix increment (distinguished by the dummy "int" parameter) */
83  inline void operator++(int) {if (state_ < maxState_) ++state_;}
84 
85  /** Set the state directly */
86  inline void setState(const unsigned long state)
87  {state_ = state <= maxState_ ? state : maxState_;}
88 
89  private:
90  BoxNDScanner();
91 
92  void initialize(const unsigned* shape, unsigned lenShape);
93 
94  BoxND<Numeric> box_;
95  std::vector<unsigned long> strides_;
96  std::vector<double> bw_;
97  unsigned long state_;
98  unsigned long maxState_;
99 
100 #ifdef SWIG
101  public:
102  inline std::vector<Numeric> getCoords_2() const
103  {
104  const unsigned ndim = strides_.size();
105  std::vector<Numeric> out(ndim);
106  getCoords(&out[0], ndim);
107  return out;
108  }
109 
110  inline std::vector<unsigned> getIndex_2() const
111  {
112  const unsigned ndim = strides_.size();
113  std::vector<unsigned> out(ndim);
114  getIndex(&out[0], ndim);
115  return out;
116  }
117 
118  inline void next() {if (state_ < maxState_) ++state_;}
119 #endif // SWIG
120  };
121 }
122 
123 #include "npstat/nm/BoxNDScanner.icc"
124 
125 #endif // NPSTAT_BOXNDSCANNER_HH_
Template to represent rectangles, boxes, and hyperboxes.
Definition: BoxNDScanner.hh:39
void setState(const unsigned long state)
Definition: BoxNDScanner.hh:86
bool isValid() const
Definition: BoxNDScanner.hh:67
void getCoords(Numeric *x, unsigned nx) const
unsigned long maxState() const
Definition: BoxNDScanner.hh:64
void operator++(int)
Definition: BoxNDScanner.hh:83
void getIndex(unsigned *index, unsigned indexBufferLen) const
unsigned dim() const
Definition: BoxNDScanner.hh:58
unsigned long state() const
Definition: BoxNDScanner.hh:61
void reset()
Definition: BoxNDScanner.hh:76
BoxNDScanner(const BoxND< Numeric > &box, const std::vector< unsigned > &shape)
Definition: BoxNDScanner.hh:46
BoxNDScanner & operator++()
Definition: BoxNDScanner.hh:79
Definition: AbsArrayProjector.hh:14
Definition: BoxND.hh:25