CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ArrayNDScanner.cc
Go to the documentation of this file.
2 #include <cassert>
3 
5 
6 namespace npstat {
7  void ArrayNDScanner::initialize(const unsigned* shape, const unsigned lenShape) {
8  // Check argument validity
9  if (lenShape == 0U || lenShape >= CHAR_BIT * sizeof(unsigned long))
10  throw npstat::NpstatInvalidArgument("In npstat::ArrayNDScanner::initialize: invalid scan shape");
11  assert(shape);
12  for (unsigned j = 0; j < lenShape; ++j)
13  if (!shape[j])
15  "In npstat::ArrayNDScanner::initialize: "
16  "number of scans must be positive in each dimension");
17 
18  // Initialize the scanner data
19  state_ = 0UL;
20  dim_ = lenShape;
21  strides_[dim_ - 1] = 1UL;
22  for (unsigned j = dim_ - 1; j > 0; --j)
23  strides_[j - 1] = strides_[j] * shape[j];
24  maxState_ = strides_[0] * shape[0];
25  }
26 
27  void ArrayNDScanner::getIndex(unsigned* ix, const unsigned indexBufferLen) const {
28  if (indexBufferLen < dim_)
30  "In npstat::ArrayNDScanner::getIndex: "
31  "insufficient length of the output buffer");
32  if (state_ >= maxState_)
33  throw npstat::NpstatRuntimeError("In npstat::ArrayNDScanner::getIndex: invalid scanner state");
34  assert(ix);
35 
36  unsigned long l = state_;
37  for (unsigned i = 0; i < dim_; ++i) {
38  unsigned long idx = l / strides_[i];
39  ix[i] = static_cast<unsigned>(idx);
40  l -= (idx * strides_[i]);
41  }
42  }
43 } // namespace npstat
void initialize(const unsigned *shape, unsigned lenShape)
assert(be >=bs)
unsigned long maxState_
Exceptions for the npstat namespace.
unsigned long strides_[CHAR_BIT *sizeof(unsigned long)]
void getIndex(unsigned *index, unsigned indexBufferLen) const
Iteration over indices of a multidimensional array.