File: stable_partition.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 (331 lines) | stat: -rw-r--r-- 13,415 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/// \file
// Range v3 library
//
//  Copyright Eric Niebler 2014-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
//
//===-------------------------- algorithm ---------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef RANGES_V3_ALGORITHM_STABLE_PARTITION_HPP
#define RANGES_V3_ALGORITHM_STABLE_PARTITION_HPP

#include <functional>
#include <memory>
#include <type_traits>

#include <meta/meta.hpp>

#include <range/v3/range_fwd.hpp>

#include <range/v3/algorithm/move.hpp>
#include <range/v3/algorithm/partition_copy.hpp>
#include <range/v3/algorithm/rotate.hpp>
#include <range/v3/functional/identity.hpp>
#include <range/v3/functional/invoke.hpp>
#include <range/v3/iterator/concepts.hpp>
#include <range/v3/iterator/move_iterators.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/memory.hpp>
#include <range/v3/utility/static_const.hpp>
#include <range/v3/utility/swap.hpp>

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

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

    /// \cond
    namespace detail
    {
        template<typename I, typename C, typename P, typename D, typename Pair>
        I stable_partition_impl(I first, I last, C pred, P proj, D len, Pair const p,
                                std::forward_iterator_tag fi)
        {
            // *first is known to be false
            // len >= 1
            if(len == 1)
                return first;
            if(len == 2)
            {
                I tmp = first;
                if(invoke(pred, invoke(proj, *++tmp)))
                {
                    ranges::iter_swap(first, tmp);
                    return tmp;
                }
                return first;
            }
            if(len <= p.second)
            { // The buffer is big enough to use
                // Move the falses into the temporary buffer, and the trues to the front
                // of the line Update first to always point to the last of the trues
                auto tmpbuf = make_raw_buffer(p.first);
                auto buf = tmpbuf.begin();
                *buf = iter_move(first);
                ++buf;
                auto res = partition_copy(make_move_iterator(next(first)),
                                          make_move_sentinel(last),
                                          first,
                                          buf,
                                          std::ref(pred),
                                          std::ref(proj));
                // All trues now at start of range, all falses in buffer
                // Move falses back into range, but don't mess up first which points to
                // first false
                ranges::move(p.first, res.out2.base().base(), res.out1);
                // h destructs moved-from values out of the temp buffer, but doesn't
                // deallocate buffer
                return res.out1;
            }
            // Else not enough buffer, do in place
            // len >= 3
            D half = len / 2; // half >= 2
            I middle = next(first, half);
            // recurse on [first, middle), *first know to be false
            // F?????????????????
            // f       m         l
            I begin_false =
                detail::stable_partition_impl(first, middle, pred, proj, half, p, fi);
            // TTTFFFFF??????????
            // f  ff   m         l
            // recurse on [middle, last], except increase middle until *(middle) is false,
            // *last know to be true
            I m1 = middle;
            D len_half = len - half;
            while(invoke(pred, invoke(proj, *m1)))
            {
                if(++m1 == last)
                    return ranges::rotate(begin_false, middle, last).begin();
                --len_half;
            }
            // TTTFFFFFTTTF??????
            // f  ff   m  m1     l
            I end_false =
                detail::stable_partition_impl(m1, last, pred, proj, len_half, p, fi);
            // TTTFFFFFTTTTTFFFFF
            // f  ff   m    sf   l
            return ranges::rotate(begin_false, middle, end_false).begin();
            // TTTTTTTTFFFFFFFFFF
            //         |
        }

        template<typename I, typename S, typename C, typename P>
        I stable_partition_impl(I first, S last, C pred, P proj,
                                std::forward_iterator_tag fi)
        {
            using difference_type = iter_difference_t<I>;
            difference_type const alloc_limit = 3; // might want to make this a function
                                                   // of trivial assignment.
            // Either prove all true and return first or point to first false
            while(true)
            {
                if(first == last)
                    return first;
                if(!invoke(pred, invoke(proj, *first)))
                    break;
                ++first;
            }
            // We now have a reduced range [first, last)
            // *first is known to be false
            using value_type = iter_value_t<I>;
            auto len_end = enumerate(first, last);
            auto p = len_end.first >= alloc_limit
                         ? detail::get_temporary_buffer<value_type>(len_end.first)
                         : detail::value_init{};
            std::unique_ptr<value_type, detail::return_temporary_buffer> const h{p.first};
            return detail::stable_partition_impl(
                first, len_end.second, pred, proj, len_end.first, p, fi);
        }

        template<typename I, typename C, typename P, typename D, typename Pair>
        I stable_partition_impl(I first, I last, C pred, P proj, D len, Pair p,
                                std::bidirectional_iterator_tag bi)
        {
            // *first is known to be false
            // *last is known to be true
            // len >= 2
            if(len == 2)
            {
                ranges::iter_swap(first, last);
                return last;
            }
            if(len == 3)
            {
                I tmp = first;
                if(invoke(pred, invoke(proj, *++tmp)))
                {
                    ranges::iter_swap(first, tmp);
                    ranges::iter_swap(tmp, last);
                    return last;
                }
                ranges::iter_swap(tmp, last);
                ranges::iter_swap(first, tmp);
                return tmp;
            }
            if(len <= p.second)
            { // The buffer is big enough to use
                // Move the falses into the temporary buffer, and the trues to the front
                // of the line Update first to always point to the last of the trues
                auto tmpbuf = ranges::make_raw_buffer(p.first);
                auto buf = tmpbuf.begin();
                *buf = iter_move(first);
                ++buf;
                auto res = partition_copy(make_move_iterator(next(first)),
                                          make_move_sentinel(last),
                                          first,
                                          buf,
                                          std::ref(pred),
                                          std::ref(proj));
                first = res.out1;
                // move *last, known to be true
                *first = iter_move(res.in);
                ++first;
                // All trues now at start of range, all falses in buffer
                // Move falses back into range, but don't mess up first which points to
                // first false
                ranges::move(p.first, res.out2.base().base(), first);
                // h destructs moved-from values out of the temp buffer, but doesn't
                // deallocate buffer
                return first;
            }
            // Else not enough buffer, do in place
            // len >= 4
            I middle = first;
            D half = len / 2; // half >= 2
            advance(middle, half);
            // recurse on [first, middle-1], except reduce middle-1 until *(middle-1) is
            // true, *first know to be false F????????????????T f       m        l
            I m1 = middle;
            I begin_false = first;
            D len_half = half;
            while(!invoke(pred, invoke(proj, *--m1)))
            {
                if(m1 == first)
                    goto first_half_done;
                --len_half;
            }
            // F???TFFF?????????T
            // f   m1  m        l
            begin_false =
                detail::stable_partition_impl(first, m1, pred, proj, len_half, p, bi);
        first_half_done:
            // TTTFFFFF?????????T
            // f  ff   m        l
            // recurse on [middle, last], except increase middle until *(middle) is false,
            // *last know to be true
            m1 = middle;
            len_half = len - half;
            while(invoke(pred, invoke(proj, *m1)))
            {
                if(++m1 == last)
                    return ranges::rotate(begin_false, middle, ++last).begin();
                --len_half;
            }
            // TTTFFFFFTTTF?????T
            // f  ff   m  m1    l
            I end_false =
                detail::stable_partition_impl(m1, last, pred, proj, len_half, p, bi);
            // TTTFFFFFTTTTTFFFFF
            // f  ff   m    sf  l
            return ranges::rotate(begin_false, middle, end_false).begin();
            // TTTTTTTTFFFFFFFFFF
            //         |
        }

        template<typename I, typename S, typename C, typename P>
        I stable_partition_impl(I first, S end_, C pred, P proj,
                                std::bidirectional_iterator_tag bi)
        {
            using difference_type = iter_difference_t<I>;
            using value_type = iter_value_t<I>;
            difference_type const alloc_limit =
                4; // might want to make this a function of trivial assignment
            // Either prove all true and return first or point to first false
            while(true)
            {
                if(first == end_)
                    return first;
                if(!invoke(pred, invoke(proj, *first)))
                    break;
                ++first;
            }
            // first points to first false, everything prior to first is already set.
            // Either prove [first, last) is all false and return first, or point last to
            // last true
            I last = ranges::next(first, end_);
            do
            {
                if(first == --last)
                    return first;
            } while(!invoke(pred, invoke(proj, *last)));
            // We now have a reduced range [first, last]
            // *first is known to be false
            // *last is known to be true
            // len >= 2
            auto len = distance(first, last) + 1;
            auto p = len >= alloc_limit ? detail::get_temporary_buffer<value_type>(len)
                                        : detail::value_init{};
            std::unique_ptr<value_type, detail::return_temporary_buffer> const h{p.first};
            return detail::stable_partition_impl(first, last, pred, proj, len, p, bi);
        }
    } // namespace detail
    /// \endcond

    RANGES_FUNC_BEGIN(stable_partition)

        /// \brief function template \c stable_partition
        template(typename I, typename S, typename C, typename P = identity)(
            requires bidirectional_iterator<I> AND sentinel_for<S, I> AND
            indirect_unary_predicate<C, projected<I, P>> AND permutable<I>)
        I RANGES_FUNC(stable_partition)(I first, S last, C pred, P proj = P{})
        {
            return detail::stable_partition_impl(std::move(first),
                                                 std::move(last),
                                                 std::ref(pred),
                                                 std::ref(proj),
                                                 iterator_tag_of<I>());
        }

        // BUGBUG Can this be optimized if Rng has O1 size?
        /// \overload
        template(typename Rng, typename C, typename P = identity)(
            requires bidirectional_range<Rng> AND
            indirect_unary_predicate<C, projected<iterator_t<Rng>, P>> AND
            permutable<iterator_t<Rng>>)
        borrowed_iterator_t<Rng> //
        RANGES_FUNC(stable_partition)(Rng && rng, C pred, P proj = P{}) //
        {
            return (*this)(begin(rng), end(rng), std::move(pred), std::move(proj));
        }

    RANGES_FUNC_END(stable_partition)

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

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

#endif