You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
3.6KB

  1. // <experimental/algorithm> -*- C++ -*-
  2. // Copyright (C) 2014-2020 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. * @ingroup libfund-ts
  23. */
  24. #ifndef _GLIBCXX_EXPERIMENTAL_ALGORITHM
  25. #define _GLIBCXX_EXPERIMENTAL_ALGORITHM 1
  26. #pragma GCC system_header
  27. #if __cplusplus >= 201402L
  28. #include <algorithm>
  29. #include <experimental/bits/lfts_config.h>
  30. #include <experimental/random>
  31. namespace std _GLIBCXX_VISIBILITY(default)
  32. {
  33. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  34. namespace experimental
  35. {
  36. inline namespace fundamentals_v2
  37. {
  38. template<typename _ForwardIterator, typename _Searcher>
  39. inline _ForwardIterator
  40. search(_ForwardIterator __first, _ForwardIterator __last,
  41. const _Searcher& __searcher)
  42. { return __searcher(__first, __last); }
  43. #define __cpp_lib_experimental_sample 201402
  44. /// Take a random sample from a population.
  45. template<typename _PopulationIterator, typename _SampleIterator,
  46. typename _Distance, typename _UniformRandomNumberGenerator>
  47. _SampleIterator
  48. sample(_PopulationIterator __first, _PopulationIterator __last,
  49. _SampleIterator __out, _Distance __n,
  50. _UniformRandomNumberGenerator&& __g)
  51. {
  52. using __pop_cat = typename
  53. std::iterator_traits<_PopulationIterator>::iterator_category;
  54. using __samp_cat = typename
  55. std::iterator_traits<_SampleIterator>::iterator_category;
  56. static_assert(
  57. __or_<is_convertible<__pop_cat, forward_iterator_tag>,
  58. is_convertible<__samp_cat, random_access_iterator_tag>>::value,
  59. "output range must use a RandomAccessIterator when input range"
  60. " does not meet the ForwardIterator requirements");
  61. static_assert(is_integral<_Distance>::value,
  62. "sample size must be an integer type");
  63. typename iterator_traits<_PopulationIterator>::difference_type __d = __n;
  64. return _GLIBCXX_STD_A::
  65. __sample(__first, __last, __pop_cat{}, __out, __samp_cat{}, __d,
  66. std::forward<_UniformRandomNumberGenerator>(__g));
  67. }
  68. template<typename _PopulationIterator, typename _SampleIterator,
  69. typename _Distance>
  70. inline _SampleIterator
  71. sample(_PopulationIterator __first, _PopulationIterator __last,
  72. _SampleIterator __out, _Distance __n)
  73. {
  74. return experimental::sample(__first, __last, __out, __n,
  75. _S_randint_engine());
  76. }
  77. template<typename _RandomAccessIterator>
  78. inline void
  79. shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
  80. { return std::shuffle(__first, __last, _S_randint_engine()); }
  81. } // namespace fundamentals_v2
  82. } // namespace experimental
  83. _GLIBCXX_END_NAMESPACE_VERSION
  84. } // namespace std
  85. #endif // C++14
  86. #endif // _GLIBCXX_EXPERIMENTAL_ALGORITHM