TBTK
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 <initializer_list>
29 
30 namespace TBTK{
31 
32 template<typename DataType>
34 public:
37  const std::initializer_list<DataType> &begin,
38  const std::initializer_list<DataType> &end,
39  const std::initializer_list<DataType> &increment
40  );
41 
43  ~MultiCounter();
44 
47 
49  const DataType operator[](unsigned int n) const;
50 
52  void reset();
53 
55  bool done() const;
56 private:
58  std::vector<DataType> begin;
59 
61  std::vector<DataType> end;
62 
64  std::vector<DataType> increment;
65 
67  std::vector<DataType> counter;
68 };
69 
70 template<typename DataType>
72  const std::initializer_list<DataType> &begin,
73  const std::initializer_list<DataType> &end,
74  const std::initializer_list<DataType> &increment
75 ){
76  TBTKAssert(
77  begin.size() == end.size(),
78  "MultiCounter::MultiCounter()",
79  "'begin' and 'end' must have the same number of elements.",
80  ""
81  );
82  TBTKAssert(
83  begin.size() == increment.size(),
84  "MultiCounter::MultiCounter()",
85  "'begin' and 'increment' must have the same number of"
86  << " elements.",
87  ""
88  );
89 
90  this->begin = begin;
91  this->end = end;
92  this->increment = increment;
93 
94  for(unsigned int n = 0; n < begin.size(); n++){
95  TBTKAssert(
96  this->begin[n] <= this->end[n],
97  "MultiCounter::MultiCounter()",
98  "Only forward iteration is supported, but entry '" << n
99  << "' in 'end' is smaller than entry '" << n << "' in"
100  << " 'begin'.",
101  ""
102  );
103 
104  TBTKAssert(
105  this->increment[n] > 0,
106  "MultiCounter::MultiCounter()",
107  "Only positive increments are supported, but entry '"
108  << n << "' in 'increment' is '" << this->increment[n]
109  << "'.",
110  ""
111  );
112  }
113 
114  reset();
115 }
116 
117 template<typename DataType>
119 }
120 
121 template<typename DataType>
123  for(int n = counter.size()-1; n > 0; n--){
124  counter[n] += increment[n];
125  if(counter[n] < end[n])
126  return *this;
127 
128  counter[n] = begin[n];
129  }
130 
131  counter[0] += increment[0];
132 
133  return *this;
134 }
135 
136 template<typename DataType>
137 inline const DataType MultiCounter<DataType>::operator[](unsigned int n) const{
138  return counter[n];
139 }
140 
141 template<typename DataType>
143  counter = begin;
144 }
145 
146 template<typename DataType>
147 inline bool MultiCounter<DataType>::done() const{
148  return counter[0] >= end[0];
149 }
150 
151 }; //End of namesapce TBTK
152 
153 #endif
MultiCounter(const std::initializer_list< DataType > &begin, const std::initializer_list< DataType > &end, const std::initializer_list< DataType > &increment)
Definition: MultiCounter.h:71
~MultiCounter()
Definition: MultiCounter.h:118
MultiCounter & operator++()
Definition: MultiCounter.h:122
Precompiler macros.
const DataType operator[](unsigned int n) const
Definition: MultiCounter.h:137
void reset()
Definition: MultiCounter.h:142
Definition: ModelFactory.h:35
bool done() const
Definition: MultiCounter.h:147
Definition: MultiCounter.h:33