File: sort.hpp

package info (click to toggle)
range-v3 0.12.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,652 kB
  • sloc: cpp: 76,839; xml: 226; sh: 89; python: 34; makefile: 19; perl: 15
file content (234 lines) | stat: -rw-r--r-- 8,514 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/// \file
// Range v3 library
//
//  Copyright Eric Niebler 2013-present
//
//  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)
//
// Project home: https://github.com/ericniebler/range-v3
//
//  Copyright (c) 1994
//  Hewlett-Packard Company
//
//  Permission to use, copy, modify, distribute and sell this software
//  and its documentation for any purpose is hereby granted without fee,
//  provided that the above copyright notice appear in all copies and
//  that both that copyright notice and this permission notice appear
//  in supporting documentation.  Hewlett-Packard Company makes no
//  representations about the suitability of this software for any
//  purpose.  It is provided "as is" without express or implied warranty.
//
//  Copyright (c) 1996
//  Silicon Graphics Computer Systems, Inc.
//
//  Permission to use, copy, modify, distribute and sell this software
//  and its documentation for any purpose is hereby granted without fee,
//  provided that the above copyright notice appear in all copies and
//  that both that copyright notice and this permission notice appear
//  in supporting documentation.  Silicon Graphics makes no
//  representations about the suitability of this software for any
//  purpose.  It is provided "as is" without express or implied warranty.
//

#ifndef RANGES_V3_ALGORITHM_SORT_HPP
#define RANGES_V3_ALGORITHM_SORT_HPP

#include <range/v3/range_fwd.hpp>

#include <range/v3/algorithm/heap_algorithm.hpp>
#include <range/v3/algorithm/move_backward.hpp>
#include <range/v3/algorithm/partial_sort.hpp>
#include <range/v3/functional/comparisons.hpp>
#include <range/v3/functional/identity.hpp>
#include <range/v3/functional/invoke.hpp>
#include <range/v3/iterator/concepts.hpp>
#include <range/v3/iterator/operations.hpp>
#include <range/v3/iterator/traits.hpp>
#include <range/v3/range/access.hpp>
#include <range/v3/range/concepts.hpp>
#include <range/v3/range/dangling.hpp>
#include <range/v3/range/traits.hpp>
#include <range/v3/utility/static_const.hpp>

#include <range/v3/detail/prologue.hpp>

namespace ranges
{
    /// \cond
    namespace detail
    {
        template<typename I, typename C, typename P>
        inline constexpr I unguarded_partition(I first, I last, C & pred, P & proj)
        {
            I mid = first + (last - first) / 2, penultimate = ranges::prev(last);
            auto &&x = *first, &&y = *mid, &&z = *penultimate;
            auto &&a = invoke(proj, (decltype(x) &&)x),
                 &&b = invoke(proj, (decltype(y) &&)y),
                 &&c = invoke(proj, (decltype(z) &&)z);

            // Find the median:
            I pivot_pnt =
                invoke(pred, a, b)
                    ? (invoke(pred, b, c) ? mid
                                          : (invoke(pred, a, c) ? penultimate : first))
                    : (invoke(pred, a, c) ? first
                                          : (invoke(pred, b, c) ? penultimate : mid));

            // Do the partition:
            while(true)
            {
                auto && v = *pivot_pnt;
                auto && pivot = invoke(proj, (decltype(v) &&)v);
                while(invoke(pred, invoke(proj, *first), pivot))
                    ++first;
                --last;
                while(invoke(pred, pivot, invoke(proj, *last)))
                    --last;
                if(!(first < last))
                    return first;
                ranges::iter_swap(first, last);
                pivot_pnt =
                    pivot_pnt == first ? last : (pivot_pnt == last ? first : pivot_pnt);
                ++first;
            }
        }

        template<typename I, typename C, typename P>
        inline constexpr void unguarded_linear_insert(I last, 
                                                      iter_value_t<I> val, 
                                                      C & pred,
                                                      P & proj)
        {
            I next_ = prev(last);
            while(invoke(pred, invoke(proj, val), invoke(proj, *next_)))
            {
                *last = iter_move(next_);
                last = next_;
                --next_;
            }
            *last = std::move(val);
        }

        template<typename I, typename C, typename P>
        inline constexpr void linear_insert(I first, I last, C & pred, P & proj)
        {
            iter_value_t<I> val = iter_move(last);
            if(invoke(pred, invoke(proj, val), invoke(proj, *first)))
            {
                move_backward(first, last, last + 1);
                *first = std::move(val);
            }
            else
                detail::unguarded_linear_insert(last, std::move(val), pred, proj);
        }

        template<typename I, typename C, typename P>
        inline constexpr void insertion_sort(I first, I last, C & pred, P & proj)
        {
            if(first == last)
                return;
            for(I i = next(first); i != last; ++i)
                detail::linear_insert(first, i, pred, proj);
        }

        template<typename I, typename C, typename P>
        inline constexpr void unguarded_insertion_sort(I first, I last, C & pred, P & proj)
        {
            for(I i = first; i != last; ++i)
                detail::unguarded_linear_insert(i, iter_move(i), pred, proj);
        }

        constexpr int introsort_threshold()
        {
            return 16;
        }

        template<typename I, typename C, typename P>
        inline constexpr void final_insertion_sort(I first, I last, C & pred, P & proj)
        {
            if(last - first > detail::introsort_threshold())
            {
                detail::insertion_sort(
                    first, first + detail::introsort_threshold(), pred, proj);
                detail::unguarded_insertion_sort(
                    first + detail::introsort_threshold(), last, pred, proj);
            }
            else
                detail::insertion_sort(first, last, pred, proj);
        }

        template<typename Size>
        inline constexpr Size log2(Size n)
        {
            Size k = 0;
            for(; n != 1; n >>= 1)
                ++k;
            return k;
        }

        template<typename I, typename Size, typename C, typename P>
        inline constexpr void introsort_loop(I first, I last, Size depth_limit, C & pred, P & proj)
        {
            while(last - first > detail::introsort_threshold())
            {
                if(depth_limit == 0)
                    return partial_sort(
                               first, last, last, std::ref(pred), std::ref(proj)),
                           void();
                I cut = detail::unguarded_partition(first, last, pred, proj);
                detail::introsort_loop(cut, last, --depth_limit, pred, proj);
                last = cut;
            }
        }
    } // namespace detail
    /// \endcond

    /// \addtogroup group-algorithms
    /// @{

    // Introsort: Quicksort to a certain depth, then Heapsort. Insertion
    // sort below a certain threshold.
    // TODO Forward iterators, like EoP?

    RANGES_FUNC_BEGIN(sort)

        /// \brief function template \c sort
        template(typename I, typename S, typename C = less, typename P = identity)(
            requires sortable<I, C, P> AND random_access_iterator<I> AND
                sentinel_for<S, I>)
        constexpr I RANGES_FUNC(sort)(I first, S end_, C pred = C{}, P proj = P{})
        {
            I last = ranges::next(first, std::move(end_));
            if(first != last)
            {
                detail::introsort_loop(
                    first, last, detail::log2(last - first) * 2, pred, proj);
                detail::final_insertion_sort(first, last, pred, proj);
            }
            return last;
        }

        /// \overload
        template(typename Rng, typename C = less, typename P = identity)(
            requires sortable<iterator_t<Rng>, C, P> AND random_access_range<Rng>)
        constexpr borrowed_iterator_t<Rng> //
        RANGES_FUNC(sort)(Rng && rng, C pred = C{}, P proj = P{}) //
        {
            return (*this)(begin(rng), end(rng), std::move(pred), std::move(proj));
        }

    RANGES_FUNC_END(sort)

    namespace cpp20
    {
        using ranges::sort;
    }
    /// @}
} // namespace ranges

#include <range/v3/detail/epilogue.hpp>

#endif