sort.hpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright Neil Groves 2009. Use, modification and
  2. // distribution is subject to the Boost Software License, Version
  3. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. //
  6. //
  7. // For more information, see http://www.boost.org/libs/range/
  8. //
  9. #ifndef BOOST_RANGE_ALGORITHM_SORT_HPP_INCLUDED
  10. #define BOOST_RANGE_ALGORITHM_SORT_HPP_INCLUDED
  11. #include <boost/concept_check.hpp>
  12. #include <boost/range/begin.hpp>
  13. #include <boost/range/end.hpp>
  14. #include <boost/range/concepts.hpp>
  15. #include <algorithm>
  16. namespace boost
  17. {
  18. namespace range
  19. {
  20. /// \brief template function sort
  21. ///
  22. /// range-based version of the sort std algorithm
  23. ///
  24. /// \pre RandomAccessRange is a model of the RandomAccessRangeConcept
  25. /// \pre BinaryPredicate is a model of the BinaryPredicateConcept
  26. template<class RandomAccessRange>
  27. inline RandomAccessRange& sort(RandomAccessRange& rng)
  28. {
  29. BOOST_RANGE_CONCEPT_ASSERT(( RandomAccessRangeConcept<RandomAccessRange> ));
  30. std::sort(boost::begin(rng), boost::end(rng));
  31. return rng;
  32. }
  33. /// \overload
  34. template<class RandomAccessRange>
  35. inline const RandomAccessRange& sort(const RandomAccessRange& rng)
  36. {
  37. BOOST_RANGE_CONCEPT_ASSERT(( RandomAccessRangeConcept<const RandomAccessRange> ));
  38. std::sort(boost::begin(rng), boost::end(rng));
  39. return rng;
  40. }
  41. /// \overload
  42. template<class RandomAccessRange, class BinaryPredicate>
  43. inline RandomAccessRange& sort(RandomAccessRange& rng, BinaryPredicate pred)
  44. {
  45. BOOST_RANGE_CONCEPT_ASSERT(( RandomAccessRangeConcept<RandomAccessRange> ));
  46. std::sort(boost::begin(rng), boost::end(rng), pred);
  47. return rng;
  48. }
  49. /// \overload
  50. template<class RandomAccessRange, class BinaryPredicate>
  51. inline const RandomAccessRange& sort(const RandomAccessRange& rng, BinaryPredicate pred)
  52. {
  53. BOOST_RANGE_CONCEPT_ASSERT(( RandomAccessRangeConcept<const RandomAccessRange> ));
  54. std::sort(boost::begin(rng), boost::end(rng), pred);
  55. return rng;
  56. }
  57. } // namespace range
  58. using range::sort;
  59. } // namespace boost
  60. #endif // include guard