Skip to content
This repository has been archived by the owner on May 14, 2021. It is now read-only.

Commit

Permalink
add clamp function
Browse files Browse the repository at this point in the history
  • Loading branch information
thestinger committed Oct 11, 2012
1 parent d8e91b8 commit f028ce5
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions clamp.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef CLAMP_HH
#define CLAMP_HH

#include <functional>

template<typename T, typename Pred>
T const &clamp(T const &val, T const &lo, T const &hi, Pred p) {
return p(val, lo) ? lo : p(hi, val) ? hi : val;
}

template<typename T>
T const &clamp(const T &val, T const &lo, T const &hi) {
return clamp(val, lo, hi, std::less<T>());
}

#endif

0 comments on commit f028ce5

Please sign in to comment.