From 0980e48d8d4db20da92684dea42de871b153cd7e Mon Sep 17 00:00:00 2001 From: Yury Melnichek Date: Sat, 15 Jan 2011 23:50:13 +0200 Subject: [PATCH] Add NextModN(), PrevModN(), NextIterInCycle(), PrevIterInCycle(). --- base/math.hpp | 10 ++++++++++ base/stl_add.hpp | 15 +++++++++++++++ 2 files changed, 25 insertions(+) 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; +} +