CMS 3D CMS Logo

List of all members | Public Member Functions | Private Member Functions | Private Attributes
npstat::ArrayNDScanner Class Reference

#include <ArrayNDScanner.h>

Public Member Functions

unsigned dim () const
 
void getIndex (unsigned *index, unsigned indexBufferLen) const
 
bool isValid () const
 
unsigned long maxState () const
 
ArrayNDScanneroperator++ ()
 
void operator++ (int)
 
void reset ()
 
void setState (const unsigned long state)
 
unsigned long state () const
 
 ArrayNDScanner (const unsigned *shape, const unsigned lenShape)
 
 ArrayNDScanner (const std::vector< unsigned > &shape)
 
 ArrayNDScanner ()=delete
 

Private Member Functions

void initialize (const unsigned *shape, unsigned lenShape)
 

Private Attributes

unsigned dim_
 
unsigned long maxState_
 
unsigned long state_
 
unsigned long strides_ [CHAR_BIT *sizeof(unsigned long)]
 

Detailed Description

This class can be used to iterate over array indices without actually building the array or requesting any memory from the heap. Typical use:

for (ArrayNDScanner scanner(shape); scanner.isValid(); ++scanner)
{
scanner.getIndex(indexArray, indexArrayLen);
.... Do what is necessary with multidimensional index ....
.... Extract linear index: ...............................
scanner.state();
}

This can be useful, for example, in case one needs to iterate over slices of some array (so that the array itself can not be used to obtain similar information easily).

Definition at line 36 of file ArrayNDScanner.h.

Constructor & Destructor Documentation

◆ ArrayNDScanner() [1/3]

npstat::ArrayNDScanner::ArrayNDScanner ( const unsigned *  shape,
const unsigned  lenShape 
)
inline

Constructor from a multidimensional array shape

Definition at line 40 of file ArrayNDScanner.h.

References initialize().

40 { initialize(shape, lenShape); }
void initialize(const unsigned *shape, unsigned lenShape)

◆ ArrayNDScanner() [2/3]

npstat::ArrayNDScanner::ArrayNDScanner ( const std::vector< unsigned > &  shape)
inlineexplicit

Definition at line 42 of file ArrayNDScanner.h.

References initialize().

42  {
43  initialize(shape.empty() ? static_cast<unsigned*>(nullptr) : &shape[0], shape.size());
44  }
void initialize(const unsigned *shape, unsigned lenShape)

◆ ArrayNDScanner() [3/3]

npstat::ArrayNDScanner::ArrayNDScanner ( )
delete

Member Function Documentation

◆ dim()

unsigned npstat::ArrayNDScanner::dim ( ) const
inline

Dimensionality of the scan

Definition at line 50 of file ArrayNDScanner.h.

References dim_.

50 { return dim_; }

◆ getIndex()

void npstat::ArrayNDScanner::getIndex ( unsigned *  index,
unsigned  indexBufferLen 
) const

Retrieve current multidimensional index

Definition at line 27 of file ArrayNDScanner.cc.

References cms::cuda::assert(), dim_, mps_fire::i, heavyIonCSV_trainingSettings::idx, cmsLHEtoEOSManager::l, maxState_, state_, and strides_.

27  {
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  }
assert(be >=bs)
unsigned long maxState_
unsigned long strides_[CHAR_BIT *sizeof(unsigned long)]

◆ initialize()

void npstat::ArrayNDScanner::initialize ( const unsigned *  shape,
unsigned  lenShape 
)
private

Definition at line 7 of file ArrayNDScanner.cc.

References cms::cuda::assert(), dim_, dqmiolumiharvest::j, maxState_, state_, strides_, and mitigatedMETSequence_cff::U.

Referenced by ArrayNDScanner().

7  {
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  }
assert(be >=bs)
unsigned long maxState_
unsigned long strides_[CHAR_BIT *sizeof(unsigned long)]

◆ isValid()

bool npstat::ArrayNDScanner::isValid ( void  ) const
inline

◆ maxState()

unsigned long npstat::ArrayNDScanner::maxState ( ) const
inline

Maximum possible state (i.e., linear index of the scan)

Definition at line 56 of file ArrayNDScanner.h.

References maxState_.

56 { return maxState_; }
unsigned long maxState_

◆ operator++() [1/2]

ArrayNDScanner& npstat::ArrayNDScanner::operator++ ( void  )
inline

Prefix increment

Definition at line 68 of file ArrayNDScanner.h.

References maxState_, and state_.

68  {
69  if (state_ < maxState_)
70  ++state_;
71  return *this;
72  }
unsigned long maxState_

◆ operator++() [2/2]

void npstat::ArrayNDScanner::operator++ ( int  )
inline

Postfix increment (distinguished by the dummy "int" parameter)

Definition at line 75 of file ArrayNDScanner.h.

References maxState_, and state_.

75  {
76  if (state_ < maxState_)
77  ++state_;
78  }
unsigned long maxState_

◆ reset()

void npstat::ArrayNDScanner::reset ( void  )
inline

Reset the state (as if the object has just been constructed)

Definition at line 65 of file ArrayNDScanner.h.

References state_.

65 { state_ = 0UL; }

◆ setState()

void npstat::ArrayNDScanner::setState ( const unsigned long  state)
inline

Set the state directly

Definition at line 81 of file ArrayNDScanner.h.

References maxState_, state(), and state_.

81 { state_ = state <= maxState_ ? state : maxState_; }
unsigned long maxState_
unsigned long state() const

◆ state()

unsigned long npstat::ArrayNDScanner::state ( ) const
inline

Retrieve current state (i.e., linear index of the scan)

Definition at line 53 of file ArrayNDScanner.h.

References state_.

Referenced by setState().

53 { return state_; }

Member Data Documentation

◆ dim_

unsigned npstat::ArrayNDScanner::dim_
private

Definition at line 89 of file ArrayNDScanner.h.

Referenced by dim(), getIndex(), and initialize().

◆ maxState_

unsigned long npstat::ArrayNDScanner::maxState_
private

Definition at line 88 of file ArrayNDScanner.h.

Referenced by getIndex(), initialize(), isValid(), maxState(), operator++(), and setState().

◆ state_

unsigned long npstat::ArrayNDScanner::state_
private

Definition at line 87 of file ArrayNDScanner.h.

Referenced by getIndex(), initialize(), isValid(), operator++(), reset(), setState(), and state().

◆ strides_

unsigned long npstat::ArrayNDScanner::strides_[CHAR_BIT *sizeof(unsigned long)]
private

Definition at line 86 of file ArrayNDScanner.h.

Referenced by getIndex(), and initialize().