-
Notifications
You must be signed in to change notification settings - Fork 1
/
RingIndex.hpp
233 lines (188 loc) · 5.15 KB
/
RingIndex.hpp
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
* \author Ivan "Kyb Sledgehammer" Kuvaldin <i.kyb[2]ya,ru>
* \brief
* Реализация кольцевого буффера с расширенными возможностями.
* Похожего по функционалу на std::vector
*/
/// TODO Перевести RingBuffer::Index_t на RingIndex
/// TODO How to move to std::vector?
/// TODO Iterators
#pragma once
#include <cstddef>
/*#include <memory>
#include <cmath>
#include <limits>
#include <stdexcept>*/
namespace Sledge {
using std::size_t;
/// RingIndex is a wrapped unsigned integer used to convert
/// relative index of RingBuffer to absolute index of underlaying raw data array.
/// Max value of index is LIMIT_MAX
///@note read more about optimizations <http://stackoverflow.com/questions/43731203/is-the-if-statement-redundant-before-modulo-and-before-assign-operations>
//template<typename RawType_=std::size_t>
class RingIndex
{
public:
using RawType = std::size_t; //RawType_;
using RawT = RawType;
//using SRrawType = signed RawType; //FIXME Clang and CcArm stacks here.
static const RawType LIMIT_MAX = std::numeric_limits<RawType>::max();
//static const RawType INDEX_MAX = LIMIT_MAX -1;
protected:
RawType idx_max = LIMIT_MAX; // 0 causes undef.beh.
RawType idx = 0;
public: // == CONSTRUCTOR ==
RingIndex( RawType idx = 0
, RawType idx_max = LIMIT_MAX )
: idx_max(idx_max) // idx_max == 0 ? LIMIT_MAX : idx_max OR better throw an exception
, idx(idx % idx_max) //(idx % this->idx_max)
{
// if( idx_max == 0 ) throw std::runtime_error("Illegal argument"); // but we won't spend resources
/*//if( idx >= idx_max )
this->idx %= idx_max;*/
}
RingIndex( const RingIndex& ri )
: idx(ri.idx)
, idx_max(ri.idx_max)
{
/* Do not spend time for this sanity check. We believe input `ri` is normalized. There is no legal way to set idx_max >= idx
if( idx_max >= idx )
this->idx %= idx_max;*/
}
public: // == GETERS and SETTERS and CAST operators and ASSIGN OPERATORS ==
/// Cast
explicit operator RawType() const {
return idx;
}
RawType get() const {
return idx;
}
RingIndex& set(RawT i){
idx = i % idx_max;
return *this;
}
RawType get_max() const {
return idx_max;
}
RingIndex& set_max(RawT max){
idx_max = max;
idx %= idx_max;
return *this;
}
RingIndex & operator=(RawT r){
return set(r);
}
RingIndex & operator=(RingIndex const & ri){
idx = ri.idx;
idx_max = ri.idx_max;
return *this;
}
public: // == ARITHMETIC OPERATORS ==
/// Pre-increment
RingIndex& operator++(){
if( ++idx >= idx_max )
idx = 0; //idx = ++idx % idx_max;
return *this;
}
/// Post-increment. Create copy to return non-incremented.
RingIndex operator++(int){
auto copy = *this; //auto copy = RingIndex(idx_max,idx); //
this->operator++(); //ToDo check vs simple void function without `return statement`
return copy;
}
/// Pre-decrement
RingIndex& operator--(){
/*idx = idx == 0
? idx_max -1
: idx-1;*/
if( idx == 0 )
idx = idx_max -1;
else
--idx;
return *this;
}
/// Post-decrement. Create copy to return non-Increment.
RingIndex operator--(int){
auto copy = *this; //auto copy = RingIndex(idx_max,idx);
this->operator--(); //idx = ++idx % idx_max;
return copy;
}
RingIndex& operator+=( RawType inc )
{
//inc %= idx_max;
idx += inc;
if( idx < inc ) // overflow
idx += LIMIT_MAX - idx_max +1;
//if( idx >= idx_max ) // not overflowed over `LIMIT_MAX`, but over `idx_max`
idx %= idx_max;
/*if( inc > (LIMIT_MAX - idx) ) // would overflow
idx += inc + (LIMIT_MAX - idx_max);
else{
idx += inc;
idx %= idx_max;
}*/
/*if( inc <=
idx += inc;
if( asm_check_overflow_flag() )
idx -= LIMIT_MAX - idx_max;*/
return *this;
}
RingIndex & operator-=( RawType dec )
{
if( idx < dec ) // would underflow
idx = idx_max - (dec-idx);
else
idx -= dec;
return *this;
}
RingIndex operator+( RawType inc ) const
{
RingIndex ret = *this;
ret += inc;
return ret;
}
RingIndex operator-( RawType dec ) const
{
RingIndex ret = *this;
ret -= dec;
return ret;
}
public: // == COMPARISON OPERATORS ==
bool operator==(RingIndex const &ri) const{
return idx == ri.idx;
}
bool operator!=(RingIndex const &ri) const{
return idx != ri.idx;
}
bool operator<(RingIndex const &ri) const {
return idx < ri.idx;
}
bool operator>(RingIndex const &ri) const {
return idx > ri.idx;
}
bool operator<=(RingIndex const &ri) const {
return idx <= ri.idx;
}
bool operator>=(RingIndex const &ri) const {
return idx >= ri.idx;
}
bool operator==(RawT i) const {
return idx == i;
}
bool operator!=(RawT i) const {
return idx != i;
}
bool operator<(RawT i) const {
return idx < i;
}
bool operator>(RawT i) const {
return idx > i;
}
bool operator<=(RawT i) const {
return idx <= i;
}
bool operator>=(RawT i) const {
return idx >= i;
}
};
} // namespace Sledge