xref: /dflybsd-src/contrib/gcc-8.0/libstdc++-v3/include/experimental/algorithm (revision 95059079af47f9a66a175f374f2da1a5020e3255)
138fd1498Szrj// <experimental/algorithm> -*- C++ -*-
238fd1498Szrj
338fd1498Szrj// Copyright (C) 2014-2018 Free Software Foundation, Inc.
438fd1498Szrj//
538fd1498Szrj// This file is part of the GNU ISO C++ Library.  This library is free
638fd1498Szrj// software; you can redistribute it and/or modify it under the
738fd1498Szrj// terms of the GNU General Public License as published by the
838fd1498Szrj// Free Software Foundation; either version 3, or (at your option)
938fd1498Szrj// any later version.
1038fd1498Szrj
1138fd1498Szrj// This library is distributed in the hope that it will be useful,
1238fd1498Szrj// but WITHOUT ANY WARRANTY; without even the implied warranty of
1338fd1498Szrj// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1438fd1498Szrj// GNU General Public License for more details.
1538fd1498Szrj
1638fd1498Szrj// Under Section 7 of GPL version 3, you are granted additional
1738fd1498Szrj// permissions described in the GCC Runtime Library Exception, version
1838fd1498Szrj// 3.1, as published by the Free Software Foundation.
1938fd1498Szrj
2038fd1498Szrj// You should have received a copy of the GNU General Public License and
2138fd1498Szrj// a copy of the GCC Runtime Library Exception along with this program;
2238fd1498Szrj// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
2338fd1498Szrj// <http://www.gnu.org/licenses/>.
2438fd1498Szrj
2538fd1498Szrj/** @file experimental/algorithm
2638fd1498Szrj *  This is a TS C++ Library header.
2738fd1498Szrj */
2838fd1498Szrj
2938fd1498Szrj#ifndef _GLIBCXX_EXPERIMENTAL_ALGORITHM
3038fd1498Szrj#define _GLIBCXX_EXPERIMENTAL_ALGORITHM 1
3138fd1498Szrj
3238fd1498Szrj#pragma GCC system_header
3338fd1498Szrj
3438fd1498Szrj#if __cplusplus >= 201402L
3538fd1498Szrj
3638fd1498Szrj#include <algorithm>
3738fd1498Szrj#include <experimental/bits/lfts_config.h>
38*58e805e6Szrj#include <experimental/random>
3938fd1498Szrj
4038fd1498Szrjnamespace std _GLIBCXX_VISIBILITY(default)
4138fd1498Szrj{
4238fd1498Szrj_GLIBCXX_BEGIN_NAMESPACE_VERSION
4338fd1498Szrj
4438fd1498Szrjnamespace experimental
4538fd1498Szrj{
46*58e805e6Szrjinline namespace fundamentals_v2
4738fd1498Szrj{
4838fd1498Szrj  template<typename _ForwardIterator, typename _Searcher>
4938fd1498Szrj    inline _ForwardIterator
5038fd1498Szrj    search(_ForwardIterator __first, _ForwardIterator __last,
5138fd1498Szrj	   const _Searcher& __searcher)
5238fd1498Szrj    { return __searcher(__first, __last); }
5338fd1498Szrj
5438fd1498Szrj#define __cpp_lib_experimental_sample 201402
5538fd1498Szrj
5638fd1498Szrj  /// Take a random sample from a population.
5738fd1498Szrj  template<typename _PopulationIterator, typename _SampleIterator,
5838fd1498Szrj           typename _Distance, typename _UniformRandomNumberGenerator>
5938fd1498Szrj    _SampleIterator
6038fd1498Szrj    sample(_PopulationIterator __first, _PopulationIterator __last,
6138fd1498Szrj	   _SampleIterator __out, _Distance __n,
6238fd1498Szrj	   _UniformRandomNumberGenerator&& __g)
6338fd1498Szrj    {
6438fd1498Szrj      using __pop_cat = typename
6538fd1498Szrj	std::iterator_traits<_PopulationIterator>::iterator_category;
6638fd1498Szrj      using __samp_cat = typename
6738fd1498Szrj	std::iterator_traits<_SampleIterator>::iterator_category;
6838fd1498Szrj
6938fd1498Szrj      static_assert(
7038fd1498Szrj	  __or_<is_convertible<__pop_cat, forward_iterator_tag>,
7138fd1498Szrj		is_convertible<__samp_cat, random_access_iterator_tag>>::value,
7238fd1498Szrj	  "output range must use a RandomAccessIterator when input range"
7338fd1498Szrj	  " does not meet the ForwardIterator requirements");
7438fd1498Szrj
7538fd1498Szrj      static_assert(is_integral<_Distance>::value,
7638fd1498Szrj		    "sample size must be an integer type");
7738fd1498Szrj
7838fd1498Szrj      typename iterator_traits<_PopulationIterator>::difference_type __d = __n;
7938fd1498Szrj      return std::__sample(__first, __last, __pop_cat{}, __out, __samp_cat{},
8038fd1498Szrj			   __d,
8138fd1498Szrj			   std::forward<_UniformRandomNumberGenerator>(__g));
8238fd1498Szrj    }
83*58e805e6Szrj
84*58e805e6Szrj  template<typename _PopulationIterator, typename _SampleIterator,
85*58e805e6Szrj           typename _Distance>
86*58e805e6Szrj    inline _SampleIterator
87*58e805e6Szrj    sample(_PopulationIterator __first, _PopulationIterator __last,
88*58e805e6Szrj	   _SampleIterator __out, _Distance __n)
89*58e805e6Szrj    {
90*58e805e6Szrj      return experimental::sample(__first, __last, __out, __n,
91*58e805e6Szrj				  _S_randint_engine());
92*58e805e6Szrj    }
93*58e805e6Szrj
94*58e805e6Szrj  template<typename _RandomAccessIterator>
95*58e805e6Szrj    inline void
96*58e805e6Szrj    shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
97*58e805e6Szrj    { return std::shuffle(__first, __last, _S_randint_engine()); }
98*58e805e6Szrj
99*58e805e6Szrj} // namespace fundamentals_v2
10038fd1498Szrj} // namespace experimental
10138fd1498Szrj
10238fd1498Szrj_GLIBCXX_END_NAMESPACE_VERSION
10338fd1498Szrj} // namespace std
10438fd1498Szrj
10538fd1498Szrj#endif // C++14
10638fd1498Szrj
10738fd1498Szrj#endif // _GLIBCXX_EXPERIMENTAL_ALGORITHM
108