From b37921ee938712687e22ffac96e52d4fedcf59bc Mon Sep 17 00:00:00 2001 From: q66 Date: Sun, 26 Apr 2015 18:45:10 +0100 Subject: [PATCH] add octa::clamp --- octa/algorithm.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/octa/algorithm.h b/octa/algorithm.h index c279fd0..2e82e46 100644 --- a/octa/algorithm.h +++ b/octa/algorithm.h @@ -206,6 +206,18 @@ namespace octa { return max_element(il.range(), compare).first(); } + /* clamp */ + + template + inline T clamp(const T &v, const U &lo, const U &hi) { + return max(T(lo), min(v, T(hi))); + } + + template + inline T clamp(const T &v, const U &lo, const U &hi, C compare) { + return max(T(lo), min(v, T(hi), compare), compare); + } + /* algos that don't change the range */ template