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