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/min.h>
15 #include <__random/uniform_int_distribution.h>
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 #pragma GCC system_header
23 #include <__undef_macros>
25 _LIBCPP_BEGIN_NAMESPACE_STD
27 template <class _PopulationIterator, class _SampleIterator, class _Distance,
28 class _UniformRandomNumberGenerator>
29 _LIBCPP_INLINE_VISIBILITY
30 _SampleIterator __sample(_PopulationIterator __first,
31 _PopulationIterator __last, _SampleIterator __output_iter,
33 _UniformRandomNumberGenerator & __g,
37 for (; __first != __last && __k < __n; ++__first, (void) ++__k)
38 __output_iter[__k] = *__first;
40 for (; __first != __last; ++__first, (void) ++__k) {
41 _Distance __r = uniform_int_distribution<_Distance>(0, __k)(__g);
43 __output_iter[__r] = *__first;
45 return __output_iter + _VSTD::min(__n, __k);
48 template <class _PopulationIterator, class _SampleIterator, class _Distance,
49 class _UniformRandomNumberGenerator>
50 _LIBCPP_INLINE_VISIBILITY
51 _SampleIterator __sample(_PopulationIterator __first,
52 _PopulationIterator __last, _SampleIterator __output_iter,
54 _UniformRandomNumberGenerator& __g,
55 forward_iterator_tag) {
56 _Distance __unsampled_sz = _VSTD::distance(__first, __last);
57 for (__n = _VSTD::min(__n, __unsampled_sz); __n != 0; ++__first) {
58 _Distance __r = uniform_int_distribution<_Distance>(0, --__unsampled_sz)(__g);
60 *__output_iter++ = *__first;
67 template <class _PopulationIterator, class _SampleIterator, class _Distance,
68 class _UniformRandomNumberGenerator>
69 _LIBCPP_INLINE_VISIBILITY
70 _SampleIterator __sample(_PopulationIterator __first,
71 _PopulationIterator __last, _SampleIterator __output_iter,
72 _Distance __n, _UniformRandomNumberGenerator& __g) {
73 typedef typename iterator_traits<_PopulationIterator>::iterator_category
75 typedef typename iterator_traits<_PopulationIterator>::difference_type
77 static_assert(__is_cpp17_forward_iterator<_PopulationIterator>::value ||
78 __is_cpp17_random_access_iterator<_SampleIterator>::value,
79 "SampleIterator must meet the requirements of RandomAccessIterator");
80 typedef typename common_type<_Distance, _Difference>::type _CommonType;
81 _LIBCPP_ASSERT(__n >= 0, "N must be a positive number.");
82 return _VSTD::__sample(
83 __first, __last, __output_iter, _CommonType(__n),
87 #if _LIBCPP_STD_VER > 14
88 template <class _PopulationIterator, class _SampleIterator, class _Distance,
89 class _UniformRandomNumberGenerator>
90 inline _LIBCPP_INLINE_VISIBILITY
91 _SampleIterator sample(_PopulationIterator __first,
92 _PopulationIterator __last, _SampleIterator __output_iter,
93 _Distance __n, _UniformRandomNumberGenerator&& __g) {
94 return _VSTD::__sample(__first, __last, __output_iter, __n, __g);
96 #endif // _LIBCPP_STD_VER > 14
98 _LIBCPP_END_NAMESPACE_STD
102 #endif // _LIBCPP___ALGORITHM_SAMPLE_H