-
Notifications
You must be signed in to change notification settings - Fork 0
/
CyclicCounter.h
53 lines (40 loc) · 950 Bytes
/
CyclicCounter.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
/*
Only safe for integer variables
Counts like the range operator in Python but cyclically: (0,3) -> 0,1,2, 0,1,2, ...
*/
#ifndef CyclicCounter_h__
#define CyclicCounter_h__
#include "custom_assert.h"
template <class Int>
class CyclicCounter
{
public:
CyclicCounter(Int max);
CyclicCounter(Int min, Int max);
inline Int value() { return _value; }
Int operator++();
private:
Int _value;
const Int _min, _max;
};
template <class Int>
CyclicCounter<Int>::CyclicCounter(Int max) : _value(0), _min(0), _max(max)
{
Assert(max>0, "Invalid max! Must be greater than 0");
}
template <class Int>
CyclicCounter<Int>::CyclicCounter(Int min, Int max) : _value(min), _min(min), _max(max)
{
Assert(min<max, "Invalid min and max!");
}
template <class Int>
Int CyclicCounter<Int>::operator++()
{
static Int max = _max-1;
if (_value < max)
++_value;
else
_value = _min;
return _value;
}
#endif // CyclicCounter_h__