libstdc++
algorithm
Go to the documentation of this file.
00001 // <experimental/algorithm> -*- C++ -*-
00002 
00003 // Copyright (C) 2014-2018 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 
00039 namespace std _GLIBCXX_VISIBILITY(default)
00040 {
00041 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00042 
00043 namespace experimental
00044 {
00045 inline namespace fundamentals_v1
00046 {
00047   template<typename _ForwardIterator, typename _Searcher>
00048     inline _ForwardIterator
00049     search(_ForwardIterator __first, _ForwardIterator __last,
00050            const _Searcher& __searcher)
00051     { return __searcher(__first, __last); }
00052 
00053 #define __cpp_lib_experimental_sample 201402
00054 
00055   /// Take a random sample from a population.
00056   template<typename _PopulationIterator, typename _SampleIterator,
00057            typename _Distance, typename _UniformRandomNumberGenerator>
00058     _SampleIterator
00059     sample(_PopulationIterator __first, _PopulationIterator __last,
00060            _SampleIterator __out, _Distance __n,
00061            _UniformRandomNumberGenerator&& __g)
00062     {
00063       using __pop_cat = typename
00064         std::iterator_traits<_PopulationIterator>::iterator_category;
00065       using __samp_cat = typename
00066         std::iterator_traits<_SampleIterator>::iterator_category;
00067 
00068       static_assert(
00069           __or_<is_convertible<__pop_cat, forward_iterator_tag>,
00070                 is_convertible<__samp_cat, random_access_iterator_tag>>::value,
00071           "output range must use a RandomAccessIterator when input range"
00072           " does not meet the ForwardIterator requirements");
00073 
00074       static_assert(is_integral<_Distance>::value,
00075                     "sample size must be an integer type");
00076 
00077       typename iterator_traits<_PopulationIterator>::difference_type __d = __n;
00078       return std::__sample(__first, __last, __pop_cat{}, __out, __samp_cat{},
00079                            __d,
00080                            std::forward<_UniformRandomNumberGenerator>(__g));
00081     }
00082 } // namespace fundamentals_v1
00083 } // namespace experimental
00084 
00085 _GLIBCXX_END_NAMESPACE_VERSION
00086 } // namespace std
00087 
00088 #endif // C++14
00089 
00090 #endif // _GLIBCXX_EXPERIMENTAL_ALGORITHM