xref: /freebsd/contrib/llvm-project/libcxx/include/__algorithm/sample.h (revision aa1a8ff2d6dbc51ef058f46f3db5a8bb77967145)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef _LIBCPP___ALGORITHM_SAMPLE_H
10 #define _LIBCPP___ALGORITHM_SAMPLE_H
11 
12 #include <__algorithm/iterator_operations.h>
13 #include <__algorithm/min.h>
14 #include <__assert>
15 #include <__config>
16 #include <__iterator/distance.h>
17 #include <__iterator/iterator_traits.h>
18 #include <__random/uniform_int_distribution.h>
19 #include <__type_traits/common_type.h>
20 #include <__utility/move.h>
21 
22 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
23 #  pragma GCC system_header
24 #endif
25 
26 _LIBCPP_PUSH_MACROS
27 #include <__undef_macros>
28 
29 _LIBCPP_BEGIN_NAMESPACE_STD
30 
31 template <class _AlgPolicy,
32           class _PopulationIterator,
33           class _PopulationSentinel,
34           class _SampleIterator,
35           class _Distance,
36           class _UniformRandomNumberGenerator>
37 _LIBCPP_HIDE_FROM_ABI _SampleIterator __sample(
38     _PopulationIterator __first,
39     _PopulationSentinel __last,
40     _SampleIterator __output_iter,
41     _Distance __n,
42     _UniformRandomNumberGenerator& __g,
43     input_iterator_tag) {
44   _Distance __k = 0;
45   for (; __first != __last && __k < __n; ++__first, (void)++__k)
46     __output_iter[__k] = *__first;
47   _Distance __sz = __k;
48   for (; __first != __last; ++__first, (void)++__k) {
49     _Distance __r = uniform_int_distribution<_Distance>(0, __k)(__g);
50     if (__r < __sz)
51       __output_iter[__r] = *__first;
52   }
53   return __output_iter + std::min(__n, __k);
54 }
55 
56 template <class _AlgPolicy,
57           class _PopulationIterator,
58           class _PopulationSentinel,
59           class _SampleIterator,
60           class _Distance,
61           class _UniformRandomNumberGenerator>
62 _LIBCPP_HIDE_FROM_ABI _SampleIterator __sample(
63     _PopulationIterator __first,
64     _PopulationSentinel __last,
65     _SampleIterator __output_iter,
66     _Distance __n,
67     _UniformRandomNumberGenerator& __g,
68     forward_iterator_tag) {
69   _Distance __unsampled_sz = _IterOps<_AlgPolicy>::distance(__first, __last);
70   for (__n = std::min(__n, __unsampled_sz); __n != 0; ++__first) {
71     _Distance __r = uniform_int_distribution<_Distance>(0, --__unsampled_sz)(__g);
72     if (__r < __n) {
73       *__output_iter++ = *__first;
74       --__n;
75     }
76   }
77   return __output_iter;
78 }
79 
80 template <class _AlgPolicy,
81           class _PopulationIterator,
82           class _PopulationSentinel,
83           class _SampleIterator,
84           class _Distance,
85           class _UniformRandomNumberGenerator>
86 _LIBCPP_HIDE_FROM_ABI _SampleIterator __sample(
87     _PopulationIterator __first,
88     _PopulationSentinel __last,
89     _SampleIterator __output_iter,
90     _Distance __n,
91     _UniformRandomNumberGenerator& __g) {
92   _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__n >= 0, "N must be a positive number.");
93 
94   using _PopIterCategory = typename _IterOps<_AlgPolicy>::template __iterator_category<_PopulationIterator>;
95   using _Difference      = typename _IterOps<_AlgPolicy>::template __difference_type<_PopulationIterator>;
96   using _CommonType      = typename common_type<_Distance, _Difference>::type;
97 
98   return std::__sample<_AlgPolicy>(
99       std::move(__first), std::move(__last), std::move(__output_iter), _CommonType(__n), __g, _PopIterCategory());
100 }
101 
102 #if _LIBCPP_STD_VER >= 17
103 template <class _PopulationIterator, class _SampleIterator, class _Distance, class _UniformRandomNumberGenerator>
104 inline _LIBCPP_HIDE_FROM_ABI _SampleIterator
105 sample(_PopulationIterator __first,
106        _PopulationIterator __last,
107        _SampleIterator __output_iter,
108        _Distance __n,
109        _UniformRandomNumberGenerator&& __g) {
110   static_assert(__has_forward_iterator_category<_PopulationIterator>::value ||
111                     __has_random_access_iterator_category<_SampleIterator>::value,
112                 "SampleIterator must meet the requirements of RandomAccessIterator");
113 
114   return std::__sample<_ClassicAlgPolicy>(std::move(__first), std::move(__last), std::move(__output_iter), __n, __g);
115 }
116 
117 #endif // _LIBCPP_STD_VER >= 17
118 
119 _LIBCPP_END_NAMESPACE_STD
120 
121 _LIBCPP_POP_MACROS
122 
123 #endif // _LIBCPP___ALGORITHM_SAMPLE_H
124