diff --git a/base/math.hpp b/base/math.hpp index 096fe94f1c..7e98ed501e 100644 --- a/base/math.hpp +++ b/base/math.hpp @@ -105,4 +105,14 @@ template inline T PowUint(T x, uint64_t n) return res; } +template inline T NextModN(T x, T n) +{ + return x + 1 == n ? 0 : x + 1; +} + +template inline T PrevModN(T x, T n) +{ + return x == 0 ? n - 1 : x - 1; +} + } diff --git a/base/stl_add.hpp b/base/stl_add.hpp index a17dd0e023..9f9d5437b9 100644 --- a/base/stl_add.hpp +++ b/base/stl_add.hpp @@ -93,3 +93,18 @@ struct NoopFunctor UNUSED_VALUE(value); } }; + +template IterT NextIterInCycle(IterT it, IterT beg, IterT end) +{ + if (++it == end) + return beg; + return it; +} + +template IterT PrevIterInCycle(IterT it, IterT beg, IterT end) +{ + if (it == beg) + it = end; + return --it; +} +