From e05cb11bb003d80781006152d97b4dc35e4275c6 Mon Sep 17 00:00:00 2001 From: Paul Davis Date: Tue, 13 Sep 2016 13:54:02 -0500 Subject: [PATCH] extend Evoral::Range to offer ::length() and ::squish() The latter maps a T into a range, using loop semantics --- libs/evoral/evoral/Range.hpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/libs/evoral/evoral/Range.hpp b/libs/evoral/evoral/Range.hpp index 9d47faf30d..e2ac87e909 100644 --- a/libs/evoral/evoral/Range.hpp +++ b/libs/evoral/evoral/Range.hpp @@ -138,6 +138,18 @@ struct /*LIBEVORAL_API*/ Range { T from; ///< start of the range T to; ///< end of the range (inclusive: to lies inside the range) bool empty() const { return from == to; } + T length() const { return to - from + 1; } + + /** for a T, return a mapping of it into the range (used for + * looping). If the argument is earlier than or equal to the end of + * this range, do nothing. + */ + T squish (T t) const { + if (t > to) { + t = (from + ((t - from) % length())); + } + return t; + } }; template