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

 ArrayNDScanner ()=delete
 
 ArrayNDScanner (const std::vector< unsigned > &shape)
 
 ArrayNDScanner (const unsigned *shape, const unsigned lenShape)
 
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
 

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.

40 { initialize(shape, lenShape); }

References initialize().

◆ ArrayNDScanner() [2/3]

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

Definition at line 42 of file ArrayNDScanner.h.

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

References initialize().

◆ 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.

50 { return dim_; }

References dim_.

◆ getIndex()

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

Retrieve current multidimensional index

Definition at line 27 of file ArrayNDScanner.cc.

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  }

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

◆ initialize()

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

Definition at line 7 of file ArrayNDScanner.cc.

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  }

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

Referenced by ArrayNDScanner().

◆ 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.

56 { return maxState_; }

References maxState_.

◆ operator++() [1/2]

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

Prefix increment

Definition at line 68 of file ArrayNDScanner.h.

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

References maxState_, and state_.

◆ 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.

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

References maxState_, and state_.

◆ 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.

65 { state_ = 0UL; }

References state_.

◆ setState()

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

Set the state directly

Definition at line 81 of file ArrayNDScanner.h.

81 { state_ = state <= maxState_ ? state : maxState_; }

References maxState_, state(), and state_.

◆ 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.

53 { return state_; }

References state_.

Referenced by setState().

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().

npstat::ArrayNDScanner::strides_
unsigned long strides_[CHAR_BIT *sizeof(unsigned long)]
Definition: ArrayNDScanner.h:86
mps_fire.i
i
Definition: mps_fire.py:428
npstat::ArrayNDScanner::initialize
void initialize(const unsigned *shape, unsigned lenShape)
Definition: ArrayNDScanner.cc:7
cms::cuda::assert
assert(be >=bs)
npstat::ArrayNDScanner::maxState_
unsigned long maxState_
Definition: ArrayNDScanner.h:88
npstat::ArrayNDScanner::ArrayNDScanner
ArrayNDScanner()=delete
heavyIonCSV_trainingSettings.idx
idx
Definition: heavyIonCSV_trainingSettings.py:5
npstat::NpstatRuntimeError
Definition: NpstatException.h:46
npstat::NpstatInvalidArgument
Definition: NpstatException.h:38
mitigatedMETSequence_cff.U
U
Definition: mitigatedMETSequence_cff.py:36
npstat::ArrayNDScanner::dim_
unsigned dim_
Definition: ArrayNDScanner.h:89
npstat::ArrayNDScanner::state
unsigned long state() const
Definition: ArrayNDScanner.h:53
npstat::ArrayNDScanner::state_
unsigned long state_
Definition: ArrayNDScanner.h:87
cmsLHEtoEOSManager.l
l
Definition: cmsLHEtoEOSManager.py:204
linear
float linear(float x)
Definition: OccupancyPlotMacros.cc:26
AlignmentPI::index
index
Definition: AlignmentPayloadInspectorHelper.h:46
dqmiolumiharvest.j
j
Definition: dqmiolumiharvest.py:66
findQualityFiles.size
size
Write out results.
Definition: findQualityFiles.py:443