From 1a11c579d66677911ef5a2942208efc51fc68b47 Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Sat, 27 Jun 2009 19:53:55 +0000 Subject: [PATCH] Intial checkin of 'clamp' algorithm [SVN r54414] --- clamp/test/Jamfile.v2 | 18 +++++ clamp/test/clamp_test.cpp | 114 ++++++++++++++++++++++++++++++ include/boost/algorithm/clamp.hpp | 42 +++++++++++ 3 files changed, 174 insertions(+) create mode 100644 clamp/test/Jamfile.v2 create mode 100644 clamp/test/clamp_test.cpp create mode 100644 include/boost/algorithm/clamp.hpp diff --git a/clamp/test/Jamfile.v2 b/clamp/test/Jamfile.v2 new file mode 100644 index 0000000..0b87e6b --- /dev/null +++ b/clamp/test/Jamfile.v2 @@ -0,0 +1,18 @@ +# Boost.All Library test Jamfile +# +# Copyright (C) 2009 Jesse Williamson +# +# Use, modification, and distribution is subject to the Boost Software +# License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at +# http://www.boost.org/LICENSE_1_0.txt) +# + +import testing ; + +{ + test-suite algorithm/clamp: + : [ run clamp_test.cpp + : : : : clamp ] + ; +} + diff --git a/clamp/test/clamp_test.cpp b/clamp/test/clamp_test.cpp new file mode 100644 index 0000000..50019cf --- /dev/null +++ b/clamp/test/clamp_test.cpp @@ -0,0 +1,114 @@ +// (C) Copyright Jesse Williamson 2009 +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include + +#include +#include + +#include + +namespace ba = boost::algorithm; + +void test_ints() +{ + +// Inside the range, equal to the endpoints, and outside the endpoints. + BOOST_CHECK_EQUAL ( 3, ba::clamp ( 1, 10, 3 )); + BOOST_CHECK_EQUAL ( 1, ba::clamp ( 1, 10, 1 )); + BOOST_CHECK_EQUAL ( 1, ba::clamp ( 1, 10, 0 )); + BOOST_CHECK_EQUAL ( 10, ba::clamp ( 1, 10, 10 )); + BOOST_CHECK_EQUAL ( 10, ba::clamp ( 1, 10, 11 )); + +// Negative numbers + BOOST_CHECK_EQUAL ( -3, ba::clamp ( -10, -1, -3 )); + BOOST_CHECK_EQUAL ( -1, ba::clamp ( -10, -1, -1 )); + BOOST_CHECK_EQUAL ( -1, ba::clamp ( -10, -1, 0 )); + BOOST_CHECK_EQUAL ( -10, ba::clamp ( -10, -1, -10 )); + BOOST_CHECK_EQUAL ( -10, ba::clamp ( -10, -1, -11 )); + +// Mixed positive and negative numbers + BOOST_CHECK_EQUAL ( 5, ba::clamp ( -10, 10, 5 )); + BOOST_CHECK_EQUAL ( -10, ba::clamp ( -10, 10, -10 )); + BOOST_CHECK_EQUAL ( -10, ba::clamp ( -10, 10, -15 )); + BOOST_CHECK_EQUAL ( 10, ba::clamp ( -10, 10, 10 )); + BOOST_CHECK_EQUAL ( 10, ba::clamp ( -10, 10, 15 )); + +// Unsigned + BOOST_CHECK_EQUAL ( 5U, ba::clamp ( 1U, 10U, 5U )); + BOOST_CHECK_EQUAL ( 1U, ba::clamp ( 1U, 10U, 1U )); + BOOST_CHECK_EQUAL ( 1U, ba::clamp ( 1U, 10U, 0U )); + BOOST_CHECK_EQUAL ( 10U, ba::clamp ( 1U, 10U, 10U )); + BOOST_CHECK_EQUAL ( 10U, ba::clamp ( 1U, 10U, 15U )); +} + + +void test_floats() +{ + +// Inside the range, equal to the endpoints, and outside the endpoints. + BOOST_CHECK_EQUAL ( 3.0, ba::clamp ( 1.0, 10.0, 3.0 )); + BOOST_CHECK_EQUAL ( 1.0, ba::clamp ( 1.0, 10.0, 1.0 )); + BOOST_CHECK_EQUAL ( 1.0, ba::clamp ( 1.0, 10.0, 0.0 )); + BOOST_CHECK_EQUAL ( 10.0, ba::clamp ( 1.0, 10.0, 10.0 )); + BOOST_CHECK_EQUAL ( 10.0, ba::clamp ( 1.0, 10.0, 11.0 )); + +// Negative numbers + BOOST_CHECK_EQUAL ( -3.f, ba::clamp ( -10.f, -1.f, -3.f )); + BOOST_CHECK_EQUAL ( -1.f, ba::clamp ( -10.f, -1.f, -1.f )); + BOOST_CHECK_EQUAL ( -1.f, ba::clamp ( -10.f, -1.f, 0.f )); + BOOST_CHECK_EQUAL ( -10.f, ba::clamp ( -10.f, -1.f, -10.f )); + BOOST_CHECK_EQUAL ( -10.f, ba::clamp ( -10.f, -1.f, -11.f )); + +// Mixed positive and negative numbers + BOOST_CHECK_EQUAL ( 5.f, ba::clamp ( -10.f, 10.f, 5.f )); + BOOST_CHECK_EQUAL ( -10.f, ba::clamp ( -10.f, 10.f, -10.f )); + BOOST_CHECK_EQUAL ( -10.f, ba::clamp ( -10.f, 10.f, -15.f )); + BOOST_CHECK_EQUAL ( 10.f, ba::clamp ( -10.f, 10.f, 10.f )); + BOOST_CHECK_EQUAL ( 10.f, ba::clamp ( -10.f, 10.f, 15.f )); + +} + +class custom { +public: + custom ( int x ) : v(x) {} + custom ( const custom &rhs ) : v(rhs.v) {} + ~custom () {} + custom & operator = ( const custom &rhs ) { v = rhs.v; return *this; } + + bool operator < ( const custom &rhs ) const { return v < rhs.v; } + bool operator == ( const custom &rhs ) const { return v == rhs.v; } + bool operator <= ( const custom &rhs ) const { return v <= rhs.v; } + bool operator >= ( const custom &rhs ) const { return v >= rhs.v; } + + std::ostream & print ( std::ostream &os ) const { return os << v; } + +private: + int v; + }; + +std::ostream & operator << ( std::ostream & os, const custom &x ) { return x.print ( os ); } + +void test_custom() +{ + +// Inside the range, equal to the endpoints, and outside the endpoints. + BOOST_CHECK_EQUAL ( custom( 3), ba::clamp ( custom(1), custom(10), custom( 3))); + BOOST_CHECK_EQUAL ( custom( 1), ba::clamp ( custom(1), custom(10), custom( 1))); + BOOST_CHECK_EQUAL ( custom( 1), ba::clamp ( custom(1), custom(10), custom( 0))); + BOOST_CHECK_EQUAL ( custom(10), ba::clamp ( custom(1), custom(10), custom(10))); + BOOST_CHECK_EQUAL ( custom(10), ba::clamp ( custom(1), custom(10), custom(11))); + +// Fail!! +// BOOST_CHECK_EQUAL ( custom(1), ba::clamp ( custom(1), custom(10), custom(11))); +} + +int test_main( int , char* [] ) +{ + test_ints (); + test_floats (); + test_custom (); + return 0; +} diff --git a/include/boost/algorithm/clamp.hpp b/include/boost/algorithm/clamp.hpp new file mode 100644 index 0000000..8cb57f0 --- /dev/null +++ b/include/boost/algorithm/clamp.hpp @@ -0,0 +1,42 @@ +/* + Copyright (c) Marshall Clow 2008. + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + + Revision history: + 27 June 2009 mtc First version + +*/ + +/// \file clamp.hpp +/// \brief Clamp algorithm +/// \author Marshall Clow +/// +/// Suggested by olafvdspek in https://svn.boost.org/trac/boost/ticket/3215 + +#ifndef BOOST_ALGORITHM_CLAMP_HPP +#define BOOST_ALGORITHM_CLAMP_HPP + +namespace boost { namespace algorithm { + +/// \fn clamp ( V lo, V hi, V val ) +/// \brief Returns the value "val" brought into the range [ lo, hi ] +/// If the value is greater than "hi", return hi. If the value is +/// less than "lo", return lo. Otherwise, return the original value. +/// +/// \param lo The low point of the range to be clamped to +/// \param hi The high point of the range to be clamped to +/// \param val The value to be clamped +/// + template + V clamp ( V lo, V hi, V val ) + { + return val >= hi ? hi : val <= lo ? lo : val; +// Alternately, +// return std::max ( std::min ( val, hi ), lo ); + } + +}} + +#endif // BOOST_ALGORITHM_CLAMP_HPP