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