File indexing completed on 2024-04-06 12:19:19
0001 #ifndef NPSTAT_ARRAYNDSCANNER_HH_
0002 #define NPSTAT_ARRAYNDSCANNER_HH_
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #include <vector>
0015 #include <climits>
0016
0017 namespace npstat {
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036 class ArrayNDScanner {
0037 public:
0038
0039
0040 inline ArrayNDScanner(const unsigned* shape, const unsigned lenShape) { initialize(shape, lenShape); }
0041
0042 inline explicit ArrayNDScanner(const std::vector<unsigned>& shape) {
0043 initialize(shape.empty() ? static_cast<unsigned*>(nullptr) : &shape[0], shape.size());
0044 }
0045
0046 ArrayNDScanner() = delete;
0047
0048
0049
0050 inline unsigned dim() const { return dim_; }
0051
0052
0053 inline unsigned long state() const { return state_; }
0054
0055
0056 inline unsigned long maxState() const { return maxState_; }
0057
0058
0059 inline bool isValid() const { return state_ < maxState_; }
0060
0061
0062 void getIndex(unsigned* index, unsigned indexBufferLen) const;
0063
0064
0065 inline void reset() { state_ = 0UL; }
0066
0067
0068 inline ArrayNDScanner& operator++() {
0069 if (state_ < maxState_)
0070 ++state_;
0071 return *this;
0072 }
0073
0074
0075 inline void operator++(int) {
0076 if (state_ < maxState_)
0077 ++state_;
0078 }
0079
0080
0081 inline void setState(const unsigned long state) { state_ = state <= maxState_ ? state : maxState_; }
0082
0083 private:
0084 void initialize(const unsigned* shape, unsigned lenShape);
0085
0086 unsigned long strides_[CHAR_BIT * sizeof(unsigned long)];
0087 unsigned long state_;
0088 unsigned long maxState_;
0089 unsigned dim_;
0090 };
0091 }
0092
0093 #endif