RUSH
Reusable Utilities in Single Headers is a collection of header-only utilities for C++
Loading...
Searching...
No Matches
counter.hpp
Go to the documentation of this file.
1
8#ifndef RUSH_COUNTER_HPP
9#define RUSH_COUNTER_HPP
10
11#include <limits>
12#include <utility>
13
14namespace rush {
15
21template <typename T = unsigned long>
22class Counter {
23public:
30 explicit Counter(const T init = 0, const T step = 1) : init_{init}, step_{step}, counter_{init} {}
31
38 return std::exchange(counter_, counter_ + step_);
39 }
40
47 return counter_ += step_;
48 }
49
55 T operator++(int) {
56 return std::exchange(counter_, counter_ + step_);
57 }
58
65 T operator+=(const int n) {
66 return std::exchange(counter_, counter_ + n * step_);
67 }
68
75 return counter_ -= step_;
76 }
77
83 T operator--(int) {
84 return std::exchange(counter_, counter_ - step_);
85 }
86
93 T operator-=(const int n) {
94 return std::exchange(counter_, counter_ - n * step_);
95 }
96
102 void set(const T value) {
103 counter_ = value;
104 }
105
109 void reset() {
110 counter_ = init_;
111 }
112
113private:
114 const T init_;
115 const T step_;
116 T counter_;
117};
118
124template <typename T = unsigned long>
125class RangeCounter : public Counter<T> {
126public:
134 explicit RangeCounter(const T init = 0, const T reset = std::numeric_limits<T>::max(), const T step = 1) : Counter<T>(0, step), offset_{init}, reset_{reset - init} {}
135
142 return Counter<T>::operator()() % reset_ + offset_;
143 }
144
151 return Counter<T>::operator++() % reset_ + offset_;
152 }
153
159 T operator++(int) {
160 return Counter<T>::operator++(0) % reset_ + offset_;
161 }
162
169 T operator+=(const int n) {
170 return Counter<T>::operator+=(n) % reset_ + offset_;
171 }
172
174 T operator--() = delete;
175 T operator--(int) = delete;
176 T operator-=(const int n) = delete;
179private:
180 const T offset_;
181 const T reset_;
182};
183
184} // namespace rush
185
186#endif // RUSH_COUNTER_HPP
A generic counter class with customizable initial value and step.
Definition counter.hpp:22
void reset()
Reset the counter to its initial value.
Definition counter.hpp:109
T operator+=(const int n)
Increment the counter by n steps.
Definition counter.hpp:65
T operator()()
Increment the counter by the step value and return the previous value.
Definition counter.hpp:37
Counter(const T init=0, const T step=1)
Constructor.
Definition counter.hpp:30
void set(const T value)
Set the counter to a specific value.
Definition counter.hpp:102
T operator--()
Pre-decrement the counter by the step value.
Definition counter.hpp:74
T operator++(int)
Post-increment the counter by the step value.
Definition counter.hpp:55
T operator-=(const int n)
Decrement the counter by n steps.
Definition counter.hpp:93
T operator--(int)
Post-decrement the counter by the step value.
Definition counter.hpp:83
T operator++()
Pre-increment the counter by the step value.
Definition counter.hpp:46
A counter class that resets after reaching a specified limit.
Definition counter.hpp:125
RangeCounter(const T init=0, const T reset=std::numeric_limits< T >::max(), const T step=1)
Constructor.
Definition counter.hpp:134
T operator++()
Pre-increment the counter by the step value, modulo the reset value.
Definition counter.hpp:150
T operator()()
Increment the counter by the step value and return the previous value, modulo the reset value.
Definition counter.hpp:141
T operator++(int)
Post-increment the counter by the step value, modulo the reset value.
Definition counter.hpp:159
T operator+=(const int n)
Increment the counter by n steps, modulo the reset value.
Definition counter.hpp:169