blob: 3b2439939f922e4d83c6de406997c2aa5583985e (
plain)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
#ifndef CYCLICINT_H
#define CYCLICINT_H
//! Class containing an integer which ranges from 0..N-1.
//!
//! Exceeding the ends will overflow to the other end. Implicit cast to/from int is provided.
template<int N>
class CyclicInt {
private:
int value;
CyclicInt set(int v) {
value = v < N ? v < 0 ? N - -v % N : v : v % N;
return *this;
}
public:
CyclicInt() {
}
CyclicInt(int v) {
set(v);
}
CyclicInt operator=(int v) {
return set(v);
}
CyclicInt operator+(CyclicInt v) {
return value + v.value;
}
CyclicInt operator+(int v) {
return value + v;
}
CyclicInt operator+=(CyclicInt v) {
return set(value + v.value);
}
CyclicInt operator++() {
return set(value + 1);
}
CyclicInt operator++(int) {
int v = value;
set(value + 1);
return v;
}
CyclicInt operator-(CyclicInt v) {
return value - v.value;
}
CyclicInt operator-(int v) {
return value - v;
}
CyclicInt operator-=(CyclicInt v) {
return set(value - v.value);
}
CyclicInt operator--() {
return set(value - 1);
}
CyclicInt operator--(int) {
int v = value;
set(value - 1);
return v;
}
operator int() {
return value;
}
};
#endif
|