-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCounter.h
67 lines (55 loc) · 1.26 KB
/
Counter.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#ifndef _COUNTER_H_
#define _COUNTER_H_
#include <stdexcept>
#include <vector>
class Counter
{
public:
Counter() { }
Counter(const std::vector<unsigned>& max)
{
reset(max);
}
unsigned size() const { return counter.size(); }
/// Resets the counter with the new maximum
void reset(const std::vector<unsigned>& max)
{
this->max = max;
counter = std::vector<unsigned>(max.size(), 0);
}
/// Resets the counter
void reset()
{
counter = std::vector<unsigned>(max.size(), 0);
}
/// Checks to see whether the counter is full
bool full() const
{
for (unsigned i=0; i< max.size(); i++)
if (max[i] != counter[i])
return false;
return true;
}
Counter& operator++(int)
{
int idx = counter.size()-1;
while (true)
{
counter[idx]++;
if (counter[idx] > max[idx])
{
counter[idx] = 0;
idx--;
if (idx == -1)
throw std::runtime_error("Attempted to increment a full counter!");
}
else
break;
}
}
const std::vector<unsigned>& get() const { return counter; }
private:
std::vector<unsigned> counter;
std::vector<unsigned> max;
};
#endif