xref: /netbsd-src/external/gpl3/gcc/dist/libstdc++-v3/include/experimental/algorithm (revision b1e838363e3c6fc78a55519254d99869742dd33c)
14d5abbe8Smrg// <experimental/algorithm> -*- C++ -*-
24d5abbe8Smrg
3*b1e83836Smrg// Copyright (C) 2014-2022 Free Software Foundation, Inc.
44d5abbe8Smrg//
54d5abbe8Smrg// This file is part of the GNU ISO C++ Library.  This library is free
64d5abbe8Smrg// software; you can redistribute it and/or modify it under the
74d5abbe8Smrg// terms of the GNU General Public License as published by the
84d5abbe8Smrg// Free Software Foundation; either version 3, or (at your option)
94d5abbe8Smrg// any later version.
104d5abbe8Smrg
114d5abbe8Smrg// This library is distributed in the hope that it will be useful,
124d5abbe8Smrg// but WITHOUT ANY WARRANTY; without even the implied warranty of
134d5abbe8Smrg// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
144d5abbe8Smrg// GNU General Public License for more details.
154d5abbe8Smrg
164d5abbe8Smrg// Under Section 7 of GPL version 3, you are granted additional
174d5abbe8Smrg// permissions described in the GCC Runtime Library Exception, version
184d5abbe8Smrg// 3.1, as published by the Free Software Foundation.
194d5abbe8Smrg
204d5abbe8Smrg// You should have received a copy of the GNU General Public License and
214d5abbe8Smrg// a copy of the GCC Runtime Library Exception along with this program;
224d5abbe8Smrg// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
234d5abbe8Smrg// <http://www.gnu.org/licenses/>.
244d5abbe8Smrg
254d5abbe8Smrg/** @file experimental/algorithm
264d5abbe8Smrg *  This is a TS C++ Library header.
27fb8a8121Smrg *  @ingroup libfund-ts
284d5abbe8Smrg */
294d5abbe8Smrg
304d5abbe8Smrg#ifndef _GLIBCXX_EXPERIMENTAL_ALGORITHM
314d5abbe8Smrg#define _GLIBCXX_EXPERIMENTAL_ALGORITHM 1
324d5abbe8Smrg
334d5abbe8Smrg#pragma GCC system_header
344d5abbe8Smrg
35b17d1066Smrg#if __cplusplus >= 201402L
364d5abbe8Smrg
374d5abbe8Smrg#include <algorithm>
38f9a78e0eSmrg#include <experimental/bits/lfts_config.h>
39a3e9eb18Smrg#include <experimental/random>
404d5abbe8Smrg
414d5abbe8Smrgnamespace std _GLIBCXX_VISIBILITY(default)
424d5abbe8Smrg{
438b6133e5Smrg_GLIBCXX_BEGIN_NAMESPACE_VERSION
448b6133e5Smrg
45a3e9eb18Smrgnamespace experimental
46a3e9eb18Smrg{
47a3e9eb18Smrginline namespace fundamentals_v2
48a3e9eb18Smrg{
494d5abbe8Smrg  template<typename _ForwardIterator, typename _Searcher>
504d5abbe8Smrg    inline _ForwardIterator
514d5abbe8Smrg    search(_ForwardIterator __first, _ForwardIterator __last,
524d5abbe8Smrg	   const _Searcher& __searcher)
534d5abbe8Smrg    { return __searcher(__first, __last); }
544d5abbe8Smrg
554d5abbe8Smrg#define __cpp_lib_experimental_sample 201402
564d5abbe8Smrg
574d5abbe8Smrg  /// Take a random sample from a population.
584d5abbe8Smrg  template<typename _PopulationIterator, typename _SampleIterator,
594d5abbe8Smrg           typename _Distance, typename _UniformRandomNumberGenerator>
604d5abbe8Smrg    _SampleIterator
614d5abbe8Smrg    sample(_PopulationIterator __first, _PopulationIterator __last,
624d5abbe8Smrg	   _SampleIterator __out, _Distance __n,
634d5abbe8Smrg	   _UniformRandomNumberGenerator&& __g)
644d5abbe8Smrg    {
654d5abbe8Smrg      using __pop_cat = typename
664d5abbe8Smrg	std::iterator_traits<_PopulationIterator>::iterator_category;
674d5abbe8Smrg      using __samp_cat = typename
684d5abbe8Smrg	std::iterator_traits<_SampleIterator>::iterator_category;
694d5abbe8Smrg
704d5abbe8Smrg      static_assert(
714d5abbe8Smrg	  __or_<is_convertible<__pop_cat, forward_iterator_tag>,
724d5abbe8Smrg		is_convertible<__samp_cat, random_access_iterator_tag>>::value,
734d5abbe8Smrg	  "output range must use a RandomAccessIterator when input range"
744d5abbe8Smrg	  " does not meet the ForwardIterator requirements");
754d5abbe8Smrg
764d5abbe8Smrg      static_assert(is_integral<_Distance>::value,
774d5abbe8Smrg		    "sample size must be an integer type");
784d5abbe8Smrg
793f4ceed9Smrg      typename iterator_traits<_PopulationIterator>::difference_type __d = __n;
80fb8a8121Smrg      return _GLIBCXX_STD_A::
81fb8a8121Smrg	__sample(__first, __last, __pop_cat{}, __out, __samp_cat{}, __d,
82b17d1066Smrg		 std::forward<_UniformRandomNumberGenerator>(__g));
834d5abbe8Smrg    }
844d5abbe8Smrg
85a3e9eb18Smrg  template<typename _PopulationIterator, typename _SampleIterator,
86a3e9eb18Smrg           typename _Distance>
87a3e9eb18Smrg    inline _SampleIterator
88a3e9eb18Smrg    sample(_PopulationIterator __first, _PopulationIterator __last,
89a3e9eb18Smrg	   _SampleIterator __out, _Distance __n)
90a3e9eb18Smrg    {
91a3e9eb18Smrg      return experimental::sample(__first, __last, __out, __n,
92a3e9eb18Smrg				  _S_randint_engine());
93a3e9eb18Smrg    }
94a3e9eb18Smrg
95a3e9eb18Smrg  template<typename _RandomAccessIterator>
96a3e9eb18Smrg    inline void
97a3e9eb18Smrg    shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
98a3e9eb18Smrg    { return std::shuffle(__first, __last, _S_randint_engine()); }
99a3e9eb18Smrg
100a3e9eb18Smrg} // namespace fundamentals_v2
1018b6133e5Smrg} // namespace experimental
102a3e9eb18Smrg
103a3e9eb18Smrg_GLIBCXX_END_NAMESPACE_VERSION
1044d5abbe8Smrg} // namespace std
1054d5abbe8Smrg
1064d5abbe8Smrg#endif // C++14
1074d5abbe8Smrg
1084d5abbe8Smrg#endif // _GLIBCXX_EXPERIMENTAL_ALGORITHM
109