Go to the documentation of this file.00001 #include "JetMETCorrections/InterpolationTables/interface/NpstatException.h"
00002 #include <cassert>
00003
00004 #include "JetMETCorrections/InterpolationTables/interface/ArrayNDScanner.h"
00005
00006 namespace npstat {
00007 void ArrayNDScanner::initialize(
00008 const unsigned* shape, const unsigned lenShape)
00009 {
00010
00011 if (lenShape == 0U || lenShape >= CHAR_BIT*sizeof(unsigned long))
00012 throw npstat::NpstatInvalidArgument(
00013 "In npstat::ArrayNDScanner::initialize: invalid scan shape");
00014 assert(shape);
00015 for (unsigned j=0; j<lenShape; ++j)
00016 if (!shape[j]) throw npstat::NpstatInvalidArgument(
00017 "In npstat::ArrayNDScanner::initialize: "
00018 "number of scans must be positive in each dimension");
00019
00020
00021 state_ = 0UL;
00022 dim_ = lenShape;
00023 strides_[dim_ - 1] = 1UL;
00024 for (unsigned j=dim_ - 1; j>0; --j)
00025 strides_[j - 1] = strides_[j]*shape[j];
00026 maxState_ = strides_[0]*shape[0];
00027 }
00028
00029 void ArrayNDScanner::getIndex(
00030 unsigned* ix, const unsigned indexBufferLen) const
00031 {
00032 if (indexBufferLen < dim_) throw npstat::NpstatInvalidArgument(
00033 "In npstat::ArrayNDScanner::getIndex: "
00034 "insufficient length of the output buffer");
00035 if (state_ >= maxState_) throw npstat::NpstatRuntimeError(
00036 "In npstat::ArrayNDScanner::getIndex: invalid scanner state");
00037 assert(ix);
00038
00039 unsigned long l = state_;
00040 for (unsigned i=0; i<dim_; ++i)
00041 {
00042 unsigned long idx = l / strides_[i];
00043 ix[i] = static_cast<unsigned>(idx);
00044 l -= (idx * strides_[i]);
00045 }
00046 }
00047 }