diff --git a/cpp/algorithm.h b/cpp/algorithm.h new file mode 100644 index 0000000..4067138 --- /dev/null +++ b/cpp/algorithm.h @@ -0,0 +1,28 @@ +// +// algorithm_17.h +// 2Term +// +// Created by Kelvin Sherlock on 7/12/2016. +// +// + +#ifndef algorithm_17_h +#define algorithm_17_h + +#include + + +// c++17 + +template +constexpr const T& clamp( const T& v, const T& lo, const T& hi, Compare comp ) { + return comp(v, hi) ? std::max(v, lo, comp) : std::min(v, hi, comp); +} + +template +constexpr const T& clamp( const T& v, const T& lo, const T& hi ) { + return clamp( v, lo, hi, std::less() ); +} + + +#endif /* algorithm_17_h */