libstdc++
algorithm
Go to the documentation of this file.
00001 // <experimental/algorithm> -*- C++ -*-
00002 
00003 // Copyright (C) 2014-2016 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 <random>
00040 #include <experimental/bits/lfts_config.h>
00041 
00042 namespace std _GLIBCXX_VISIBILITY(default)
00043 {
00044 namespace experimental
00045 {
00046 inline namespace fundamentals_v1
00047 {
00048 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00049 
00050   template<typename _ForwardIterator, typename _Searcher>
00051     inline _ForwardIterator
00052     search(_ForwardIterator __first, _ForwardIterator __last,
00053            const _Searcher& __searcher)
00054     { return __searcher(__first, __last); }
00055 
00056 #define __cpp_lib_experimental_sample 201402
00057 
00058   /// Reservoir sampling algorithm.
00059   template<typename _InputIterator, typename _RandomAccessIterator,
00060            typename _Size, typename _UniformRandomNumberGenerator>
00061     _RandomAccessIterator
00062     __sample(_InputIterator __first, _InputIterator __last, input_iterator_tag,
00063              _RandomAccessIterator __out, random_access_iterator_tag,
00064              _Size __n, _UniformRandomNumberGenerator&& __g)
00065     {
00066       using __distrib_type = std::uniform_int_distribution<_Size>;
00067       using __param_type = typename __distrib_type::param_type;
00068       __distrib_type __d{};
00069       _Size __sample_sz = 0;
00070       while (__first != __last && __sample_sz != __n)
00071         {
00072           __out[__sample_sz++] = *__first;
00073           ++__first;
00074         }
00075       for (auto __pop_sz = __sample_sz; __first != __last;
00076           ++__first, (void)++__pop_sz)
00077         {
00078           const auto __k = __d(__g, __param_type{0, __pop_sz});
00079           if (__k < __n)
00080             __out[__k] = *__first;
00081         }
00082       return __out + __sample_sz;
00083     }
00084 
00085   /// Selection sampling algorithm.
00086   template<typename _ForwardIterator, typename _OutputIterator, typename _Cat,
00087            typename _Size, typename _UniformRandomNumberGenerator>
00088     _OutputIterator
00089     __sample(_ForwardIterator __first, _ForwardIterator __last,
00090              forward_iterator_tag,
00091              _OutputIterator __out, _Cat,
00092              _Size __n, _UniformRandomNumberGenerator&& __g)
00093     {
00094       using __distrib_type = std::uniform_int_distribution<_Size>;
00095       using __param_type = typename __distrib_type::param_type;
00096       __distrib_type __d{};
00097       _Size __unsampled_sz = std::distance(__first, __last);
00098       for (__n = std::min(__n, __unsampled_sz); __n != 0; ++__first)
00099         if (__d(__g, __param_type{0, --__unsampled_sz}) < __n)
00100           {
00101             *__out++ = *__first;
00102             --__n;
00103           }
00104       return __out;
00105     }
00106 
00107   /// Take a random sample from a population.
00108   template<typename _PopulationIterator, typename _SampleIterator,
00109            typename _Distance, typename _UniformRandomNumberGenerator>
00110     _SampleIterator
00111     sample(_PopulationIterator __first, _PopulationIterator __last,
00112            _SampleIterator __out, _Distance __n,
00113            _UniformRandomNumberGenerator&& __g)
00114     {
00115       using __pop_cat = typename
00116         std::iterator_traits<_PopulationIterator>::iterator_category;
00117       using __samp_cat = typename
00118         std::iterator_traits<_SampleIterator>::iterator_category;
00119 
00120       static_assert(
00121           __or_<is_convertible<__pop_cat, forward_iterator_tag>,
00122                 is_convertible<__samp_cat, random_access_iterator_tag>>::value,
00123           "output range must use a RandomAccessIterator when input range"
00124           " does not meet the ForwardIterator requirements");
00125 
00126       static_assert(is_integral<_Distance>::value,
00127                     "sample size must be an integer type");
00128 
00129       typename iterator_traits<_PopulationIterator>::difference_type __d = __n;
00130       return std::experimental::__sample(
00131           __first, __last, __pop_cat{}, __out, __samp_cat{},
00132           __d, std::forward<_UniformRandomNumberGenerator>(__g));
00133     }
00134 
00135 _GLIBCXX_END_NAMESPACE_VERSION
00136 } // namespace fundamentals_v1
00137 } // namespace experimental
00138 } // namespace std
00139 
00140 #endif // C++14
00141 
00142 #endif // _GLIBCXX_EXPERIMENTAL_ALGORITHM