8#ifndef RUSH_COUNTER_HPP
9#define RUSH_COUNTER_HPP
21template <
typename T =
unsigned long>
30 explicit Counter(
const T init = 0,
const T step = 1) : init_{init}, step_{step}, counter_{init} {}
38 return std::exchange(counter_, counter_ + step_);
47 return counter_ += step_;
56 return std::exchange(counter_, counter_ + step_);
66 return std::exchange(counter_, counter_ + n * step_);
75 return counter_ -= step_;
84 return std::exchange(counter_, counter_ - step_);
94 return std::exchange(counter_, counter_ - n * step_);
124template <
typename T =
unsigned long>
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} {}
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