clamp-hpp.qbk 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. [/ QuickBook Document version 1.5 ]
  2. [section:clamp clamp]
  3. [/license
  4. Copyright (c) 2010-2012 Marshall Clow
  5. Distributed under the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE_1_0.txt or copy at
  7. http://www.boost.org/LICENSE_1_0.txt)
  8. ]
  9. The header file clamp.hpp contains two functions for "clamping" a value between a pair of boundary values.
  10. [heading clamp]
  11. The function `clamp (v, lo, hi)` returns:
  12. * lo if v < lo
  13. * hi if hi < v
  14. * otherwise, v
  15. Note: using `clamp` with floating point numbers may give unexpected results if one of the values is `NaN`.
  16. There is also a version that allows the caller to specify a comparison predicate to use instead of `operator <`.
  17. ``
  18. template<typename T>
  19. const T& clamp ( const T& val, const T& lo, const T& hi );
  20. template<typename T, typename Pred>
  21. const T& clamp ( const T& val, const T& lo, const T& hi, Pred p );
  22. ``
  23. The following code: ``
  24. int foo = 23;
  25. foo = clamp ( foo, 1, 10 );
  26. ``
  27. will leave `foo` with a value of 10
  28. Complexity:
  29. `clamp` will make either one or two calls to the comparison predicate before returning one of the three parameters.
  30. [heading clamp_range]
  31. There are also four range-based versions of clamp, that apply clamping to a series of values. You could write them yourself with std::transform and bind, like this: `std::transform ( first, last, out, bind ( clamp ( _1, lo, hi )))`, but they are provided here for your convenience.
  32. ``
  33. template<typename InputIterator, typename OutputIterator>
  34. OutputIterator clamp_range ( InputIterator first, InputIterator last, OutputIterator out,
  35. typename std::iterator_traits<InputIterator>::value_type lo,
  36. typename std::iterator_traits<InputIterator>::value_type hi );
  37. template<typename Range, typename OutputIterator>
  38. OutputIterator clamp_range ( const Range &r, OutputIterator out,
  39. typename std::iterator_traits<typename boost::range_iterator<const Range>::type>::value_type lo,
  40. typename std::iterator_traits<typename boost::range_iterator<const Range>::type>::value_type hi );
  41. template<typename InputIterator, typename OutputIterator, typename Pred>
  42. OutputIterator clamp_range ( InputIterator first, InputIterator last, OutputIterator out,
  43. typename std::iterator_traits<InputIterator>::value_type lo,
  44. typename std::iterator_traits<InputIterator>::value_type hi, Pred p );
  45. template<typename Range, typename OutputIterator, typename Pred>
  46. OutputIterator clamp_range ( const Range &r, OutputIterator out,
  47. typename std::iterator_traits<typename boost::range_iterator<const Range>::type>::value_type lo,
  48. typename std::iterator_traits<typename boost::range_iterator<const Range>::type>::value_type hi,
  49. Pred p );
  50. ``
  51. [endsect]