minmax.hpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // (C) Copyright Herve Bronnimann 2004.
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. /*
  6. Revision history:
  7. 1 July 2004
  8. Split the code into two headers to lessen dependence on
  9. Boost.tuple. (Herve)
  10. 26 June 2004
  11. Added the code for the boost minmax library. (Herve)
  12. */
  13. #ifndef BOOST_ALGORITHM_MINMAX_HPP
  14. #define BOOST_ALGORITHM_MINMAX_HPP
  15. /* PROPOSED STANDARD EXTENSIONS:
  16. *
  17. * minmax(a, b)
  18. * Effect: (b<a) ? std::make_pair(b,a) : std::make_pair(a,b);
  19. *
  20. * minmax(a, b, comp)
  21. * Effect: comp(b,a) ? std::make_pair(b,a) : std::make_pair(a,b);
  22. *
  23. */
  24. #include <boost/config.hpp>
  25. #include <boost/tuple/tuple.hpp> // for using pairs with boost::cref
  26. #include <boost/ref.hpp>
  27. namespace boost {
  28. template <typename T>
  29. tuple< T const&, T const& >
  30. minmax(T const& a, T const& b) {
  31. return (b<a) ? make_tuple(cref(b),cref(a)) : make_tuple(cref(a),cref(b));
  32. }
  33. template <typename T, class BinaryPredicate>
  34. tuple< T const&, T const& >
  35. minmax(T const& a, T const& b, BinaryPredicate comp) {
  36. return comp(b,a) ? make_tuple(cref(b),cref(a)) : make_tuple(cref(a),cref(b));
  37. }
  38. } // namespace boost
  39. #endif // BOOST_ALGORITHM_MINMAX_HPP