is_clamped.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. Copyright (c) Ivan Matek, Marshall Clow 2021.
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. */
  6. /// \file is_clamped.hpp
  7. /// \brief IsClamped algorithm
  8. /// \authors Ivan Matek, Marshall Clow
  9. ///
  10. #ifndef BOOST_ALGORITHM_IS_CLAMPED_HPP
  11. #define BOOST_ALGORITHM_IS_CLAMPED_HPP
  12. #include <functional> // for std::less
  13. #include <cassert>
  14. #include <boost/type_traits/type_identity.hpp> // for boost::type_identity
  15. namespace boost { namespace algorithm {
  16. /// \fn is_clamped ( T const& val,
  17. /// typename boost::type_identity<T>::type const & lo,
  18. /// typename boost::type_identity<T>::type const & hi, Pred p )
  19. /// \returns true if value "val" is in the range [ lo, hi ]
  20. /// using the comparison predicate p.
  21. /// If p ( val, lo ) return false.
  22. /// If p ( hi, val ) return false.
  23. /// Otherwise, returns true.
  24. ///
  25. /// \param val The value to be checked
  26. /// \param lo The lower bound of the range
  27. /// \param hi The upper bound of the range
  28. /// \param p A predicate to use to compare the values.
  29. /// p ( a, b ) returns a boolean.
  30. ///
  31. template <typename T, typename Pred>
  32. BOOST_CXX14_CONSTEXPR bool is_clamped(
  33. T const& val, typename boost::type_identity<T>::type const& lo,
  34. typename boost::type_identity<T>::type const& hi, Pred p) {
  35. // assert ( !p ( hi, lo )); // Can't assert p ( lo, hi ) b/c they
  36. // might be equal
  37. return p(val, lo) ? false : p(hi, val) ? false : true;
  38. }
  39. /// \fn is_clamped ( T const& val,
  40. /// typename boost::type_identity<T>::type const & lo,
  41. /// typename boost::type_identity<T>::type const & hi)
  42. /// \returns true if value "val" is in the range [ lo, hi ]
  43. /// using operator < for comparison.
  44. /// If the value is less than lo, return false.
  45. /// If the value is greater than hi, return false.
  46. /// Otherwise, returns true.
  47. ///
  48. /// \param val The value to be checked
  49. /// \param lo The lower bound of the range
  50. /// \param hi The upper bound of the range
  51. ///
  52. template<typename T>
  53. BOOST_CXX14_CONSTEXPR bool is_clamped ( const T& val,
  54. typename boost::type_identity<T>::type const & lo,
  55. typename boost::type_identity<T>::type const & hi )
  56. {
  57. return boost::algorithm::is_clamped ( val, lo, hi, std::less<T>());
  58. }
  59. }}
  60. #endif // BOOST_ALGORITHM_CLAMP_HPP