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