Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
25 lines
518 B
C++
25 lines
518 B
C++
#pragma once
|
|
|
|
#include <cmath>
|
|
|
|
namespace mln {
|
|
namespace util {
|
|
|
|
/// Constrains n to the given range (including min, excluding max) via modular
|
|
/// arithmetic.
|
|
template <typename T>
|
|
T wrap(T value, T min, T max) {
|
|
if (value >= min && value < max) {
|
|
return value;
|
|
} else if (value == max) {
|
|
return min;
|
|
}
|
|
|
|
const T delta = max - min;
|
|
const T wrapped = min + std::fmod(value - min, delta);
|
|
return value < min ? wrapped + delta : wrapped;
|
|
}
|
|
|
|
} // namespace util
|
|
} // namespace mln
|