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