TBTK
Need a break? Support the development by playing Polarity Puzzles
MultiCounter.h
1 /* Copyright 2017 Kristofer Björnson
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
23 #ifndef COM_DAFER45_TBTK_MULTI_COUNTER
24 #define COM_DAFER45_TBTK_MULTI_COUNTER
25 
26 #include "TBTK/TBTKMacros.h"
27 
28 #include <vector>
29 
30 namespace TBTK{
31 
41 template<typename DataType>
43 public:
52  const std::vector<DataType> &begin,
53  const std::vector<DataType> &end,
54  const std::vector<DataType> &increment
55  );
56 
62 
69  const DataType operator[](unsigned int n) const;
70 
72  operator std::vector<DataType>() const;
73 
75  void reset();
76 
81  bool done() const;
82 
86  unsigned int getSize() const;
87 private:
89  std::vector<DataType> begin;
90 
92  std::vector<DataType> end;
93 
95  std::vector<DataType> increment;
96 
98  std::vector<DataType> counter;
99 };
100 
101 template<typename DataType>
103  const std::vector<DataType> &begin,
104  const std::vector<DataType> &end,
105  const std::vector<DataType> &increment
106 ){
107  TBTKAssert(
108  begin.size() == end.size(),
109  "MultiCounter::MultiCounter()",
110  "'begin' and 'end' must have the same number of elements.",
111  ""
112  );
113  TBTKAssert(
114  begin.size() == increment.size(),
115  "MultiCounter::MultiCounter()",
116  "'begin' and 'increment' must have the same number of"
117  << " elements.",
118  ""
119  );
120 
121  this->begin = begin;
122  this->end = end;
123  this->increment = increment;
124 
125  for(unsigned int n = 0; n < begin.size(); n++){
126  TBTKAssert(
127  this->begin[n] <= this->end[n],
128  "MultiCounter::MultiCounter()",
129  "Only forward iteration is supported, but entry '" << n
130  << "' in 'end' is smaller than entry '" << n << "' in"
131  << " 'begin'.",
132  ""
133  );
134 
135  TBTKAssert(
136  this->increment[n] > 0,
137  "MultiCounter::MultiCounter()",
138  "Only positive increments are supported, but entry '"
139  << n << "' in 'increment' is '" << this->increment[n]
140  << "'.",
141  ""
142  );
143  }
144 
145  reset();
146 }
147 
148 template<typename DataType>
150  for(int n = counter.size()-1; n > 0; n--){
151  counter[n] += increment[n];
152  if(counter[n] < end[n])
153  return *this;
154 
155  counter[n] = begin[n];
156  }
157 
158  counter[0] += increment[0];
159 
160  return *this;
161 }
162 
163 template<typename DataType>
164 inline const DataType MultiCounter<DataType>::operator[](unsigned int n) const{
165  return counter[n];
166 }
167 
168 template<typename DataType>
169 inline MultiCounter<DataType>::operator std::vector<DataType>() const{
170  return counter;
171 }
172 
173 template<typename DataType>
175  counter = begin;
176 }
177 
178 template<typename DataType>
179 inline bool MultiCounter<DataType>::done() const{
180  return counter[0] >= end[0];
181 }
182 
183 template<typename DataType>
184 inline unsigned int MultiCounter<DataType>::getSize() const{
185  return begin.size();
186 }
187 
188 }; //End of namesapce TBTK
189 
190 #endif
MultiCounter & operator++()
Definition: MultiCounter.h:149
Precompiler macros.
const DataType operator[](unsigned int n) const
Definition: MultiCounter.h:164
void reset()
Definition: MultiCounter.h:174
MultiCounter(const std::vector< DataType > &begin, const std::vector< DataType > &end, const std::vector< DataType > &increment)
Definition: MultiCounter.h:102
unsigned int getSize() const
Definition: MultiCounter.h:184
Definition: Boolean.h:32
Helper class for flattening nested loops.
Definition: MultiCounter.h:42
bool done() const
Definition: MultiCounter.h:179