algorithm 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // <experimental/algorithm> -*- C++ -*-
  2. // Copyright (C) 2014-2018 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file experimental/algorithm
  21. * This is a TS C++ Library header.
  22. */
  23. #ifndef _GLIBCXX_EXPERIMENTAL_ALGORITHM
  24. #define _GLIBCXX_EXPERIMENTAL_ALGORITHM 1
  25. #pragma GCC system_header
  26. #if __cplusplus >= 201402L
  27. #include <algorithm>
  28. #include <experimental/bits/lfts_config.h>
  29. #include <experimental/random>
  30. namespace std _GLIBCXX_VISIBILITY(default)
  31. {
  32. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  33. namespace experimental
  34. {
  35. inline namespace fundamentals_v2
  36. {
  37. template<typename _ForwardIterator, typename _Searcher>
  38. inline _ForwardIterator
  39. search(_ForwardIterator __first, _ForwardIterator __last,
  40. const _Searcher& __searcher)
  41. { return __searcher(__first, __last); }
  42. #define __cpp_lib_experimental_sample 201402
  43. /// Take a random sample from a population.
  44. template<typename _PopulationIterator, typename _SampleIterator,
  45. typename _Distance, typename _UniformRandomNumberGenerator>
  46. _SampleIterator
  47. sample(_PopulationIterator __first, _PopulationIterator __last,
  48. _SampleIterator __out, _Distance __n,
  49. _UniformRandomNumberGenerator&& __g)
  50. {
  51. using __pop_cat = typename
  52. std::iterator_traits<_PopulationIterator>::iterator_category;
  53. using __samp_cat = typename
  54. std::iterator_traits<_SampleIterator>::iterator_category;
  55. static_assert(
  56. __or_<is_convertible<__pop_cat, forward_iterator_tag>,
  57. is_convertible<__samp_cat, random_access_iterator_tag>>::value,
  58. "output range must use a RandomAccessIterator when input range"
  59. " does not meet the ForwardIterator requirements");
  60. static_assert(is_integral<_Distance>::value,
  61. "sample size must be an integer type");
  62. typename iterator_traits<_PopulationIterator>::difference_type __d = __n;
  63. return std::__sample(__first, __last, __pop_cat{}, __out, __samp_cat{},
  64. __d,
  65. std::forward<_UniformRandomNumberGenerator>(__g));
  66. }
  67. template<typename _PopulationIterator, typename _SampleIterator,
  68. typename _Distance>
  69. inline _SampleIterator
  70. sample(_PopulationIterator __first, _PopulationIterator __last,
  71. _SampleIterator __out, _Distance __n)
  72. {
  73. return experimental::sample(__first, __last, __out, __n,
  74. _S_randint_engine());
  75. }
  76. template<typename _RandomAccessIterator>
  77. inline void
  78. shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
  79. { return std::shuffle(__first, __last, _S_randint_engine()); }
  80. } // namespace fundamentals_v2
  81. } // namespace experimental
  82. _GLIBCXX_END_NAMESPACE_VERSION
  83. } // namespace std
  84. #endif // C++14
  85. #endif // _GLIBCXX_EXPERIMENTAL_ALGORITHM