2 Copyright (c) Marshall Clow 2008-2012.
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 28 Sep 2015 mtc First version
12 /// \file sort_subrange.hpp
13 /// \brief Sort a subrange
14 /// \author Marshall Clow
16 /// Suggested by Sean Parent in his CppCon 2015 keynote
18 #ifndef BOOST_ALGORITHM_SORT_SUBRANGE_HPP
19 #define BOOST_ALGORITHM_SORT_SUBRANGE_HPP
21 #include <functional> // For std::less
22 #include <iterator> // For std::iterator_traits
23 #include <algorithm> // For nth_element and partial_sort
25 #include <boost/range/begin.hpp>
26 #include <boost/range/end.hpp>
28 namespace boost { namespace algorithm {
30 /// \fn sort_subrange ( T const& val,
31 /// Iterator first, Iterator last,
32 /// Iterator sub_first, Iterator sub_last,
34 /// \brief Sort the subrange [sub_first, sub_last) that is inside
35 /// the range [first, last) as if you had sorted the entire range.
37 /// \param first The start of the larger range
38 /// \param last The end of the larger range
39 /// \param sub_first The start of the sub range
40 /// \param sub_last The end of the sub range
41 /// \param p A predicate to use to compare the values.
42 /// p ( a, b ) returns a boolean.
44 template<typename Iterator, typename Pred>
46 Iterator first, Iterator last,
47 Iterator sub_first, Iterator sub_last,
50 if (sub_first == sub_last) return; // the empty sub-range is already sorted.
52 if (sub_first != first) { // sub-range is at the start, don't need to partition
53 (void) std::nth_element(first, sub_first, last, p);
56 std::partial_sort(sub_first, sub_last, last, p);
61 template<typename Iterator>
62 void sort_subrange (Iterator first, Iterator last, Iterator sub_first, Iterator sub_last)
64 typedef typename std::iterator_traits<Iterator>::value_type value_type;
65 return sort_subrange(first, last, sub_first, sub_last, std::less<value_type>());
71 /// \fn partition_subrange ( T const& val,
72 /// Iterator first, Iterator last,
73 /// Iterator sub_first, Iterator sub_last,
75 /// \brief Gather the elements of the subrange [sub_first, sub_last) that is
76 /// inside the range [first, last) as if you had sorted the entire range.
78 /// \param first The start of the larger range
79 /// \param last The end of the larger range
80 /// \param sub_first The start of the sub range
81 /// \param sub_last The end of the sub range
82 /// \param p A predicate to use to compare the values.
83 /// p ( a, b ) returns a boolean.
85 template<typename Iterator, typename Pred>
86 void partition_subrange (
87 Iterator first, Iterator last,
88 Iterator sub_first, Iterator sub_last,
91 if (sub_first != first) {
92 (void) std::nth_element(first, sub_first, last, p);
97 (void) std::nth_element(sub_first, sub_last, last, p);
100 template<typename Iterator>
101 void partition_subrange (Iterator first, Iterator last, Iterator sub_first, Iterator sub_last)
103 typedef typename std::iterator_traits<Iterator>::value_type value_type;
104 return partition_subrange(first, last, sub_first, sub_last, std::less<value_type>());
109 #endif // BOOST_ALGORITHM_SORT_SUBRANGE_HPP