1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___ALGORITHM_SAMPLE_H
10 #define _LIBCPP___ALGORITHM_SAMPLE_H
12 #include <__algorithm/iterator_operations.h>
13 #include <__algorithm/min.h>
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>
22 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
23 # pragma GCC system_header
27 #include <__undef_macros>
29 _LIBCPP_BEGIN_NAMESPACE_STD
31 template <class _AlgPolicy
,
32 class _PopulationIterator
,
33 class _PopulationSentinel
,
34 class _SampleIterator
,
36 class _UniformRandomNumberGenerator
>
37 _LIBCPP_HIDE_FROM_ABI _SampleIterator
__sample(
38 _PopulationIterator __first
,
39 _PopulationSentinel __last
,
40 _SampleIterator __output_iter
,
42 _UniformRandomNumberGenerator
& __g
,
45 for (; __first
!= __last
&& __k
< __n
; ++__first
, (void)++__k
)
46 __output_iter
[__k
] = *__first
;
48 for (; __first
!= __last
; ++__first
, (void)++__k
) {
49 _Distance __r
= uniform_int_distribution
<_Distance
>(0, __k
)(__g
);
51 __output_iter
[__r
] = *__first
;
53 return __output_iter
+ std::min(__n
, __k
);
56 template <class _AlgPolicy
,
57 class _PopulationIterator
,
58 class _PopulationSentinel
,
59 class _SampleIterator
,
61 class _UniformRandomNumberGenerator
>
62 _LIBCPP_HIDE_FROM_ABI _SampleIterator
__sample(
63 _PopulationIterator __first
,
64 _PopulationSentinel __last
,
65 _SampleIterator __output_iter
,
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
);
73 *__output_iter
++ = *__first
;
80 template <class _AlgPolicy
,
81 class _PopulationIterator
,
82 class _PopulationSentinel
,
83 class _SampleIterator
,
85 class _UniformRandomNumberGenerator
>
86 _LIBCPP_HIDE_FROM_ABI _SampleIterator
__sample(
87 _PopulationIterator __first
,
88 _PopulationSentinel __last
,
89 _SampleIterator __output_iter
,
91 _UniformRandomNumberGenerator
& __g
) {
92 _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__n
>= 0, "N must be a positive number.");
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
;
98 return std::__sample
<_AlgPolicy
>(
99 std::move(__first
), std::move(__last
), std::move(__output_iter
), _CommonType(__n
), __g
, _PopIterCategory());
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
,
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");
114 return std::__sample
<_ClassicAlgPolicy
>(std::move(__first
), std::move(__last
), std::move(__output_iter
), __n
, __g
);
117 #endif // _LIBCPP_STD_VER >= 17
119 _LIBCPP_END_NAMESPACE_STD
123 #endif // _LIBCPP___ALGORITHM_SAMPLE_H