| 1 | /* |
| 2 | Copyright (c) Marshall Clow 2008-2012. |
| 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 | Revision history: |
| 8 | 27 June 2009 mtc First version |
| 9 | 23 Oct 2010 mtc Added predicate version |
| 10 | |
| 11 | */ |
| 12 | |
| 13 | /// \file clamp.hpp |
| 14 | /// \brief Clamp algorithm |
| 15 | /// \author Marshall Clow |
| 16 | /// |
| 17 | /// Suggested by olafvdspek in https://svn.boost.org/trac/boost/ticket/3215 |
| 18 | |
| 19 | #ifndef BOOST_ALGORITHM_CLAMP_HPP |
| 20 | #define BOOST_ALGORITHM_CLAMP_HPP |
| 21 | |
| 22 | #include <functional> // For std::less |
| 23 | #include <iterator> // For std::iterator_traits |
| 24 | #include <cassert> |
| 25 | |
| 26 | #include <boost/config.hpp> |
| 27 | #include <boost/range/begin.hpp> |
| 28 | #include <boost/range/end.hpp> |
| 29 | #include <boost/type_traits/type_identity.hpp> // for boost::type_identity |
| 30 | #include <boost/core/enable_if.hpp> // for boost::disable_if |
| 31 | |
| 32 | namespace boost { namespace algorithm { |
| 33 | |
| 34 | /// \fn clamp ( T const& val, |
| 35 | /// typename boost::type_identity<T>::type const & lo, |
| 36 | /// typename boost::type_identity<T>::type const & hi, Pred p ) |
| 37 | /// \return the value "val" brought into the range [ lo, hi ] |
| 38 | /// using the comparison predicate p. |
| 39 | /// If p ( val, lo ) return lo. |
| 40 | /// If p ( hi, val ) return hi. |
| 41 | /// Otherwise, return the original value. |
| 42 | /// |
| 43 | /// \param val The value to be clamped |
| 44 | /// \param lo The lower bound of the range to be clamped to |
| 45 | /// \param hi The upper bound of the range to be clamped to |
| 46 | /// \param p A predicate to use to compare the values. |
| 47 | /// p ( a, b ) returns a boolean. |
| 48 | /// |
| 49 | template<typename T, typename Pred> |
| 50 | BOOST_CXX14_CONSTEXPR T const & clamp ( T const& val, |
| 51 | typename boost::type_identity<T>::type const & lo, |
| 52 | typename boost::type_identity<T>::type const & hi, Pred p ) |
| 53 | { |
| 54 | // assert ( !p ( hi, lo )); // Can't assert p ( lo, hi ) b/c they might be equal |
| 55 | return p ( val, lo ) ? lo : p ( hi, val ) ? hi : val; |
| 56 | } |
| 57 | |
| 58 | |
| 59 | /// \fn clamp ( T const& val, |
| 60 | /// typename boost::identity<T>::type const & lo, |
| 61 | /// typename boost::identity<T>::type const & hi ) |
| 62 | /// \return the value "val" brought into the range [ lo, hi ]. |
| 63 | /// If the value is less than lo, return lo. |
| 64 | /// If the value is greater than "hi", return hi. |
| 65 | /// Otherwise, return the original value. |
| 66 | /// |
| 67 | /// \param val The value to be clamped |
| 68 | /// \param lo The lower bound of the range to be clamped to |
| 69 | /// \param hi The upper bound of the range to be clamped to |
| 70 | /// |
| 71 | template<typename T> |
| 72 | BOOST_CXX14_CONSTEXPR T const& clamp ( const T& val, |
| 73 | typename boost::type_identity<T>::type const & lo, |
| 74 | typename boost::type_identity<T>::type const & hi ) |
| 75 | { |
| 76 | return boost::algorithm::clamp ( val, lo, hi, std::less<T>()); |
| 77 | } |
| 78 | |
| 79 | /// \fn clamp_range ( InputIterator first, InputIterator last, OutputIterator out, |
| 80 | /// std::iterator_traits<InputIterator>::value_type const & lo, |
| 81 | /// std::iterator_traits<InputIterator>::value_type const & hi ) |
| 82 | /// \return clamp the sequence of values [first, last) into [ lo, hi ] |
| 83 | /// |
| 84 | /// \param first The start of the range of values |
| 85 | /// \param last One past the end of the range of input values |
| 86 | /// \param out An output iterator to write the clamped values into |
| 87 | /// \param lo The lower bound of the range to be clamped to |
| 88 | /// \param hi The upper bound of the range to be clamped to |
| 89 | /// |
| 90 | template<typename InputIterator, typename OutputIterator> |
| 91 | BOOST_CXX14_CONSTEXPR OutputIterator clamp_range ( InputIterator first, InputIterator last, OutputIterator out, |
| 92 | typename std::iterator_traits<InputIterator>::value_type const & lo, |
| 93 | typename std::iterator_traits<InputIterator>::value_type const & hi ) |
| 94 | { |
| 95 | // this could also be written with bind and std::transform |
| 96 | while ( first != last ) |
| 97 | *out++ = boost::algorithm::clamp ( *first++, lo, hi ); |
| 98 | return out; |
| 99 | } |
| 100 | |
| 101 | /// \fn clamp_range ( const Range &r, OutputIterator out, |
| 102 | /// typename std::iterator_traits<typename boost::range_iterator<const Range>::type>::value_type const & lo, |
| 103 | /// typename std::iterator_traits<typename boost::range_iterator<const Range>::type>::value_type const & hi ) |
| 104 | /// \return clamp the sequence of values [first, last) into [ lo, hi ] |
| 105 | /// |
| 106 | /// \param r The range of values to be clamped |
| 107 | /// \param out An output iterator to write the clamped values into |
| 108 | /// \param lo The lower bound of the range to be clamped to |
| 109 | /// \param hi The upper bound of the range to be clamped to |
| 110 | /// |
| 111 | template<typename Range, typename OutputIterator> |
| 112 | BOOST_CXX14_CONSTEXPR typename boost::disable_if_c<boost::is_same<Range, OutputIterator>::value, OutputIterator>::type |
| 113 | clamp_range ( const Range &r, OutputIterator out, |
| 114 | typename std::iterator_traits<typename boost::range_iterator<const Range>::type>::value_type const & lo, |
| 115 | typename std::iterator_traits<typename boost::range_iterator<const Range>::type>::value_type const & hi ) |
| 116 | { |
| 117 | return boost::algorithm::clamp_range ( boost::begin ( r ), boost::end ( r ), out, lo, hi ); |
| 118 | } |
| 119 | |
| 120 | |
| 121 | /// \fn clamp_range ( InputIterator first, InputIterator last, OutputIterator out, |
| 122 | /// std::iterator_traits<InputIterator>::value_type const & lo, |
| 123 | /// std::iterator_traits<InputIterator>::value_type const & hi, Pred p ) |
| 124 | /// \return clamp the sequence of values [first, last) into [ lo, hi ] |
| 125 | /// using the comparison predicate p. |
| 126 | /// |
| 127 | /// \param first The start of the range of values |
| 128 | /// \param last One past the end of the range of input values |
| 129 | /// \param out An output iterator to write the clamped values into |
| 130 | /// \param lo The lower bound of the range to be clamped to |
| 131 | /// \param hi The upper bound of the range to be clamped to |
| 132 | /// \param p A predicate to use to compare the values. |
| 133 | /// p ( a, b ) returns a boolean. |
| 134 | |
| 135 | /// |
| 136 | template<typename InputIterator, typename OutputIterator, typename Pred> |
| 137 | BOOST_CXX14_CONSTEXPR OutputIterator clamp_range ( InputIterator first, InputIterator last, OutputIterator out, |
| 138 | typename std::iterator_traits<InputIterator>::value_type const & lo, |
| 139 | typename std::iterator_traits<InputIterator>::value_type const & hi, Pred p ) |
| 140 | { |
| 141 | // this could also be written with bind and std::transform |
| 142 | while ( first != last ) |
| 143 | *out++ = boost::algorithm::clamp ( *first++, lo, hi, p ); |
| 144 | return out; |
| 145 | } |
| 146 | |
| 147 | /// \fn clamp_range ( const Range &r, OutputIterator out, |
| 148 | /// typename std::iterator_traits<typename boost::range_iterator<const Range>::type>::value_type const & lo, |
| 149 | /// typename std::iterator_traits<typename boost::range_iterator<const Range>::type>::value_type const & hi, |
| 150 | /// Pred p ) |
| 151 | /// \return clamp the sequence of values [first, last) into [ lo, hi ] |
| 152 | /// using the comparison predicate p. |
| 153 | /// |
| 154 | /// \param r The range of values to be clamped |
| 155 | /// \param out An output iterator to write the clamped values into |
| 156 | /// \param lo The lower bound of the range to be clamped to |
| 157 | /// \param hi The upper bound of the range to be clamped to |
| 158 | /// \param p A predicate to use to compare the values. |
| 159 | /// p ( a, b ) returns a boolean. |
| 160 | // |
| 161 | // Disable this template if the first two parameters are the same type; |
| 162 | // In that case, the user will get the two iterator version. |
| 163 | template<typename Range, typename OutputIterator, typename Pred> |
| 164 | BOOST_CXX14_CONSTEXPR typename boost::disable_if_c<boost::is_same<Range, OutputIterator>::value, OutputIterator>::type |
| 165 | clamp_range ( const Range &r, OutputIterator out, |
| 166 | typename std::iterator_traits<typename boost::range_iterator<const Range>::type>::value_type const & lo, |
| 167 | typename std::iterator_traits<typename boost::range_iterator<const Range>::type>::value_type const & hi, |
| 168 | Pred p ) |
| 169 | { |
| 170 | return boost::algorithm::clamp_range ( boost::begin ( r ), boost::end ( r ), out, lo, hi, p ); |
| 171 | } |
| 172 | |
| 173 | |
| 174 | }} |
| 175 | |
| 176 | #endif // BOOST_ALGORITHM_CLAMP_HPP |
| 177 | |