File indexing completed on 2023-03-17 11:10:42
0001 #include "JetMETCorrections/InterpolationTables/interface/NpstatException.h"
0002 #include <cassert>
0003
0004 #include "JetMETCorrections/InterpolationTables/interface/ArrayNDScanner.h"
0005
0006 namespace npstat {
0007 void ArrayNDScanner::initialize(const unsigned* shape, const unsigned lenShape) {
0008
0009 if (lenShape == 0U || lenShape >= CHAR_BIT * sizeof(unsigned long))
0010 throw npstat::NpstatInvalidArgument("In npstat::ArrayNDScanner::initialize: invalid scan shape");
0011 assert(shape);
0012 for (unsigned j = 0; j < lenShape; ++j)
0013 if (!shape[j])
0014 throw npstat::NpstatInvalidArgument(
0015 "In npstat::ArrayNDScanner::initialize: "
0016 "number of scans must be positive in each dimension");
0017
0018
0019 state_ = 0UL;
0020 dim_ = lenShape;
0021 strides_[dim_ - 1] = 1UL;
0022 for (unsigned j = dim_ - 1; j > 0; --j)
0023 strides_[j - 1] = strides_[j] * shape[j];
0024 maxState_ = strides_[0] * shape[0];
0025 }
0026
0027 void ArrayNDScanner::getIndex(unsigned* ix, const unsigned indexBufferLen) const {
0028 if (indexBufferLen < dim_)
0029 throw npstat::NpstatInvalidArgument(
0030 "In npstat::ArrayNDScanner::getIndex: "
0031 "insufficient length of the output buffer");
0032 if (state_ >= maxState_)
0033 throw npstat::NpstatRuntimeError("In npstat::ArrayNDScanner::getIndex: invalid scanner state");
0034 assert(ix);
0035
0036 unsigned long l = state_;
0037 for (unsigned i = 0; i < dim_; ++i) {
0038 unsigned long idx = l / strides_[i];
0039 ix[i] = static_cast<unsigned>(idx);
0040 l -= (idx * strides_[i]);
0041 }
0042 }
0043 }