libstdc++
algorithm
Go to the documentation of this file.
00001 // <experimental/algorithm> -*- C++ -*-
00002 
00003 // Copyright (C) 2014-2019 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file experimental/algorithm
00026  *  This is a TS C++ Library header.
00027  */
00028 
00029 #ifndef _GLIBCXX_EXPERIMENTAL_ALGORITHM
00030 #define _GLIBCXX_EXPERIMENTAL_ALGORITHM 1
00031 
00032 #pragma GCC system_header
00033 
00034 #if __cplusplus >= 201402L
00035 
00036 #include <algorithm>
00037 #include <experimental/bits/lfts_config.h>
00038 #include <experimental/random>
00039 
00040 namespace std _GLIBCXX_VISIBILITY(default)
00041 {
00042 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00043 
00044 namespace experimental
00045 {
00046 inline namespace fundamentals_v2
00047 {
00048   template<typename _ForwardIterator, typename _Searcher>
00049     inline _ForwardIterator
00050     search(_ForwardIterator __first, _ForwardIterator __last,
00051            const _Searcher& __searcher)
00052     { return __searcher(__first, __last); }
00053 
00054 #define __cpp_lib_experimental_sample 201402
00055 
00056   /// Take a random sample from a population.
00057   template<typename _PopulationIterator, typename _SampleIterator,
00058            typename _Distance, typename _UniformRandomNumberGenerator>
00059     _SampleIterator
00060     sample(_PopulationIterator __first, _PopulationIterator __last,
00061            _SampleIterator __out, _Distance __n,
00062            _UniformRandomNumberGenerator&& __g)
00063     {
00064       using __pop_cat = typename
00065         std::iterator_traits<_PopulationIterator>::iterator_category;
00066       using __samp_cat = typename
00067         std::iterator_traits<_SampleIterator>::iterator_category;
00068 
00069       static_assert(
00070           __or_<is_convertible<__pop_cat, forward_iterator_tag>,
00071                 is_convertible<__samp_cat, random_access_iterator_tag>>::value,
00072           "output range must use a RandomAccessIterator when input range"
00073           " does not meet the ForwardIterator requirements");
00074 
00075       static_assert(is_integral<_Distance>::value,
00076                     "sample size must be an integer type");
00077 
00078       typename iterator_traits<_PopulationIterator>::difference_type __d = __n;
00079       return std::__sample(__first, __last, __pop_cat{}, __out, __samp_cat{},
00080                            __d,
00081                            std::forward<_UniformRandomNumberGenerator>(__g));
00082     }
00083 
00084   template<typename _PopulationIterator, typename _SampleIterator,
00085            typename _Distance>
00086     inline _SampleIterator
00087     sample(_PopulationIterator __first, _PopulationIterator __last,
00088            _SampleIterator __out, _Distance __n)
00089     {
00090       return experimental::sample(__first, __last, __out, __n,
00091                                   _S_randint_engine());
00092     }
00093 
00094   template<typename _RandomAccessIterator>
00095     inline void
00096     shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
00097     { return std::shuffle(__first, __last, _S_randint_engine()); }
00098 
00099 } // namespace fundamentals_v2
00100 } // namespace experimental
00101 
00102 _GLIBCXX_END_NAMESPACE_VERSION
00103 } // namespace std
00104 
00105 #endif // C++14
00106 
00107 #endif // _GLIBCXX_EXPERIMENTAL_ALGORITHM