stable_sort.hpp 2.1 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_STABLE_SORT_HPP_INCLUDED
  10. #define BOOST_RANGE_ALGORITHM_STABLE_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 stable_sort
  21. ///
  22. /// range-based version of the stable_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& stable_sort(RandomAccessRange& rng)
  28. {
  29. BOOST_RANGE_CONCEPT_ASSERT(( RandomAccessRangeConcept<RandomAccessRange> ));
  30. std::stable_sort(boost::begin(rng), boost::end(rng));
  31. return rng;
  32. }
  33. /// \overload
  34. template<class RandomAccessRange>
  35. inline const RandomAccessRange& stable_sort(const RandomAccessRange& rng)
  36. {
  37. BOOST_RANGE_CONCEPT_ASSERT(( RandomAccessRangeConcept<const RandomAccessRange> ));
  38. std::stable_sort(boost::begin(rng), boost::end(rng));
  39. return rng;
  40. }
  41. /// \overload
  42. template<class RandomAccessRange, class BinaryPredicate>
  43. inline RandomAccessRange& stable_sort(RandomAccessRange& rng, BinaryPredicate sort_pred)
  44. {
  45. BOOST_RANGE_CONCEPT_ASSERT(( RandomAccessRangeConcept<RandomAccessRange> ));
  46. std::stable_sort(boost::begin(rng), boost::end(rng), sort_pred);
  47. return rng;
  48. }
  49. /// \overload
  50. template<class RandomAccessRange, class BinaryPredicate>
  51. inline const RandomAccessRange& stable_sort(const RandomAccessRange& rng, BinaryPredicate sort_pred)
  52. {
  53. BOOST_RANGE_CONCEPT_ASSERT(( RandomAccessRangeConcept<const RandomAccessRange> ));
  54. std::stable_sort(boost::begin(rng), boost::end(rng), sort_pred);
  55. return rng;
  56. }
  57. } // namespace range
  58. using range::stable_sort;
  59. } // namespace boost
  60. #endif // include guard