File: sample.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 (250 lines) | stat: -rw-r--r-- 9,809 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
/// \file
// Range v3 library
//
//  Copyright Eric Niebler 2014-present
//  Copyright Casey Carter 2016
//
//  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
//
#ifndef RANGES_V3_ALGORITHM_SAMPLE_HPP
#define RANGES_V3_ALGORITHM_SAMPLE_HPP

#include <utility>

#include <range/v3/range_fwd.hpp>

#include <range/v3/algorithm/copy_n.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/random.hpp>
#include <range/v3/utility/static_const.hpp>

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

namespace ranges
{
    /// \addtogroup group-algorithms
    /// @{
    template<typename I, typename O>
    using sample_result = detail::in_out_result<I, O>;

    /// \cond
    namespace detail
    {
        template<typename I, typename S, typename O, typename Gen>
        sample_result<I, O> sample_sized_impl(I first,
                                              S last,
                                              iter_difference_t<I> pop_size,
                                              O out,
                                              iter_difference_t<O> sample_size,
                                              Gen && gen)
        {
            if(pop_size > 0 && sample_size > 0)
            {
                std::uniform_int_distribution<iter_difference_t<I>> dist;
                using param_t = typename decltype(dist)::param_type;
                for(; first != last; ++first)
                {
                    if(sample_size >= pop_size)
                        return copy_n(std::move(first), pop_size, std::move(out));

                    if(dist(gen, param_t{0, --pop_size}) < sample_size)
                    {
                        *out = *first;
                        ++out;
                        if(--sample_size == 0)
                            break;
                    }
                }
            }

            return {std::move(first), std::move(out)};
        }
    } // namespace detail
    /// \endcond

    RANGES_FUNC_BEGIN(sample)

        /// \brief function template \c sample
        template(typename I,
                 typename S,
                 typename O,
                 typename Gen = detail::default_random_engine &)(
            requires input_iterator<I> AND sentinel_for<S, I> AND
                weakly_incrementable<O> AND indirectly_copyable<I, O> AND
                uniform_random_bit_generator<std::remove_reference_t<Gen>> AND
                (random_access_iterator<O> || forward_iterator<I> ||
                 sized_sentinel_for<S, I>))
        sample_result<I, O> RANGES_FUNC(sample)(I first,
                                                S last,
                                                O out,
                                                iter_difference_t<O> const n,
                                                Gen && gen = detail::get_random_engine())
        {
            if(RANGES_CONSTEXPR_IF(forward_iterator<I> || sized_sentinel_for<S, I>)) //
            {
                auto const k = distance(first, last);
                return detail::sample_sized_impl(std::move(first),
                                                 std::move(last),
                                                 k,
                                                 std::move(out),
                                                 n,
                                                 static_cast<Gen &&>(gen));
            }
            else
            {
                // out is random-access here; calls to advance(out,n) and
                // next(out,n) are O(1).
                if(n > 0)
                {
                    for(iter_difference_t<O> i = 0; i < n; (void)++i, ++first)
                    {
                        if(first == last)
                        {
                            advance(out, i);
                            goto done;
                        }
                        *next(out, i) = *first;
                    }

                    std::uniform_int_distribution<iter_difference_t<O>> dist;
                    using param_t = typename decltype(dist)::param_type;
                    for(auto pop_size = n; first != last; (void)++first, ++pop_size)
                    {
                        auto const i = dist(gen, param_t{0, pop_size});
                        if(i < n)
                            *next(out, i) = *first;
                    }

                    advance(out, n);
                }
            done:
                return {std::move(first), std::move(out)};
            }
        }

        /// \overload
        template(typename I,
                 typename S,
                 typename ORng,
                 typename Gen = detail::default_random_engine &)(
            requires input_iterator<I> AND sentinel_for<S, I> AND
                weakly_incrementable<iterator_t<ORng>> AND
                indirectly_copyable<I, iterator_t<ORng>> AND
                uniform_random_bit_generator<std::remove_reference_t<Gen>> AND
                (forward_range<ORng> || sized_range<ORng>) AND
                (random_access_iterator<iterator_t<ORng>> || forward_iterator<I> ||
                    sized_sentinel_for<S, I>))
        sample_result<I, borrowed_iterator_t<ORng>> RANGES_FUNC(sample)(
            I first,
            S last,
            ORng && out,
            Gen && gen = detail::get_random_engine()) //
        {
            if(RANGES_CONSTEXPR_IF(forward_iterator<I> || sized_sentinel_for<S, I>)) //
            {
                auto k = distance(first, last);
                return detail::sample_sized_impl(std::move(first),
                                                 std::move(last),
                                                 k,
                                                 begin(out),
                                                 distance(out),
                                                 static_cast<Gen &&>(gen));
            }
            else
            {
                return (*this)(std::move(first),
                               std::move(last),
                               begin(out),
                               distance(out),
                               static_cast<Gen &&>(gen));
            }
        }

        /// \overload
        template(typename Rng,
                 typename O,
                 typename Gen = detail::default_random_engine &)(
            requires input_range<Rng> AND weakly_incrementable<O> AND
                indirectly_copyable<iterator_t<Rng>, O> AND
                uniform_random_bit_generator<std::remove_reference_t<Gen>> AND
                (random_access_iterator<O> || forward_range<Rng> || sized_range<Rng>))
        sample_result<borrowed_iterator_t<Rng>, O> RANGES_FUNC(sample)(
            Rng && rng,
            O out,
            iter_difference_t<O> const n,
            Gen && gen = detail::get_random_engine()) //
        {
            if(RANGES_CONSTEXPR_IF(forward_range<Rng> || sized_range<Rng>)) //
            {
                return detail::sample_sized_impl(begin(rng),
                                                 end(rng),
                                                 distance(rng),
                                                 std::move(out),
                                                 n,
                                                 static_cast<Gen &&>(gen));
            }
            else
            {
                return (*this)(
                    begin(rng), end(rng), std::move(out), n, static_cast<Gen &&>(gen));
            }
        }

        /// \overload
        template(typename IRng,
                 typename ORng,
                 typename Gen = detail::default_random_engine &)(
            requires input_range<IRng> AND range<ORng> AND
                indirectly_copyable<iterator_t<IRng>, iterator_t<ORng>> AND
                uniform_random_bit_generator<std::remove_reference_t<Gen>> AND
                (random_access_iterator<iterator_t<ORng>> || forward_range<IRng> ||
                    sized_range<IRng>) AND
                (forward_range<ORng> || sized_range<ORng>))
        sample_result<borrowed_iterator_t<IRng>, borrowed_iterator_t<ORng>> //
        RANGES_FUNC(sample)(IRng && rng,
                            ORng && out,
                            Gen && gen = detail::get_random_engine())
        {
            if(RANGES_CONSTEXPR_IF(forward_range<IRng> || sized_range<IRng>)) //
            {
                return detail::sample_sized_impl(begin(rng),
                                                 end(rng),
                                                 distance(rng),
                                                 begin(out),
                                                 distance(out),
                                                 static_cast<Gen &&>(gen));
            }
            else
            {
                return (*this)(begin(rng),
                               end(rng),
                               begin(out),
                               distance(out),
                               static_cast<Gen &&>(gen));
            }
        }

    RANGES_FUNC_END(sample)

    namespace cpp20
    {
        using ranges::sample_result;
        using ranges::sample;
    }
    /// @}
} // namespace ranges

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

#endif