ModuloCounter.h
3.96 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* ModuloCounter.h
*
* Created on: 8 mar 2019
* Author: mariuszo
*/
#ifndef MODULOCOUNTER_H_
#define MODULOCOUNTER_H_
/*
#include <type_traits>
#include <limits>
#include <atomic>
#include <assert.h>
*/
template <typename T, class Enable = void> class ModuloCounter {};
template <typename T> class ModuloCounter<T, typename std::enable_if<std::numeric_limits<T>::is_integer>::type> {
T m_value;
T m_limit;
public:
ModuloCounter(const T& value=-1, const T& modulo=0):m_value(value), m_limit(modulo-1){
assert(m_limit>=-1);
assert(m_limit==-1 || (m_value>=0 && m_value<=m_limit));
}
inline void operator()(const T& value, const T& modulo) {
m_value=value;
m_limit=modulo-1;
assert(m_limit>=0);
assert(m_value>=0 && m_value<=m_limit);
}
T& operator=(const T& value){
assert(m_limit>=0);
assert(m_value>=0 && m_value<=m_limit);
m_value=value;
return m_value;
}
inline T& operator++(){
assert(m_limit>=0);
if (m_value!=m_limit)
m_value++;
else
m_value=0;
return m_value;
}
inline T& operator--(){
assert(m_limit>=0);
if (m_value!=0)
m_value--;
else
m_value=m_limit;
return m_value;
}
inline T operator++(int){
assert(m_limit>=0);
const T orgvalue(m_value);
++(*this);
return orgvalue;
}
inline T operator--(int){
assert(m_limit>=0);
const T orgvalue(m_value);
--(*this);
return orgvalue;
}
inline const T& val() const{
return m_value;
}
inline T prev() const {
assert(m_limit>=0);
if (m_value!=0)
return m_value-1;
else
return m_limit;
}
inline static T prev(const T& value, const T& modulo) {
assert(modulo>0);
assert(value>=0 && value<modulo);
if (value!=0)
return value-1;
else
return modulo-1;
}
inline T next() const {
assert(m_limit>=0);
if (m_value!=m_limit)
return m_value+1;
else
return 0;
}
inline static T next(const T& value, const T& modulo) {
assert(modulo>0);
assert(value>=0 && value<modulo);
if (value!=modulo-1)
return value+1;
else
return 0;
}
};
template <typename T, class Enable = void> class AtomicModuloCounter {};
template <typename T> class AtomicModuloCounter<T, typename std::enable_if<std::numeric_limits<T>::is_integer>::type> {
std::atomic<T> m_value;
T m_limit;
AtomicModuloCounter& operator=(const AtomicModuloCounter&) = delete;
AtomicModuloCounter& operator=(const AtomicModuloCounter&) volatile = delete;
public:
AtomicModuloCounter(const T& value=-1, const T& modulo=0):m_value(value), m_limit(modulo-1){
assert(m_limit>=-1);
assert(m_limit==-1 || (m_value>=0 && m_value<=m_limit));
}
inline void operator()(const T& value, const T& modulo) {
m_value=value;
m_limit=modulo-1;
assert(m_limit>=0);
assert(m_value>=0 && m_value<=m_limit);
}
T& operator=(const T& value){
assert(m_limit>=0);
assert(m_value>=0 && m_value<=m_limit);
return m_value=value;
}
inline T operator++(){
assert(m_limit>=0);
T val=m_value.load(std::memory_order_relaxed);
T newval;
do {
newval=(val!=m_limit)?val+1:0;
} while (!m_value.compare_exchange_weak(val, newval, std::memory_order_release, std::memory_order_relaxed));
return newval;
}
inline T operator--(){
assert(m_limit>=0);
T val=m_value.load(std::memory_order_relaxed);
T newval;
do {
newval=(val!=0)?val-1:m_limit;
} while (!m_value.compare_exchange_weak(val, newval, std::memory_order_release, std::memory_order_relaxed));
return newval;
}
inline T operator++(T){
assert(m_limit>=0);
T val=m_value.load(std::memory_order_relaxed);
do {
} while (!m_value.compare_exchange_weak(val, (val!=m_limit)?val+1:0, std::memory_order_release, std::memory_order_relaxed));
return val;
}
inline T operator--(T){
assert(m_limit>=0);
T val=m_value.load(std::memory_order_relaxed);
do {
} while (!m_value.compare_exchange_weak(val, (val!=0)?val-1:m_limit, std::memory_order_release, std::memory_order_relaxed));
return val;
}
inline const T val() const{
return m_value.load(std::memory_order_acquire);
}
};
#endif /* MODULOCOUNTER_H_ */