選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

shuffle.hpp 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /// \file
  2. // Range v3 library
  3. //
  4. // Copyright Eric Niebler 2014-present
  5. //
  6. // Use, modification and distribution is subject to the
  7. // Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. // Project home: https://github.com/ericniebler/range-v3
  12. //
  13. #ifndef RANGES_V3_ALGORITHM_SHUFFLE_HPP
  14. #define RANGES_V3_ALGORITHM_SHUFFLE_HPP
  15. #include <cstdint>
  16. #include <utility>
  17. #include <range/v3/range_fwd.hpp>
  18. #include <range/v3/functional/invoke.hpp>
  19. #include <range/v3/iterator/concepts.hpp>
  20. #include <range/v3/iterator/traits.hpp>
  21. #include <range/v3/range/access.hpp>
  22. #include <range/v3/range/concepts.hpp>
  23. #include <range/v3/range/dangling.hpp>
  24. #include <range/v3/range/traits.hpp>
  25. #include <range/v3/utility/random.hpp>
  26. #include <range/v3/utility/static_const.hpp>
  27. #include <range/v3/utility/swap.hpp>
  28. namespace ranges
  29. {
  30. /// \addtogroup group-algorithms
  31. /// @{
  32. RANGES_BEGIN_NIEBLOID(shuffle)
  33. /// \brief function template \c shuffle
  34. template<typename I, typename S, typename Gen = detail::default_random_engine &>
  35. auto RANGES_FUN_NIEBLOID(shuffle)(I const first,
  36. S const last,
  37. Gen && gen = detail::get_random_engine()) //
  38. ->CPP_ret(I)( //
  39. requires random_access_iterator<I> && sentinel_for<S, I> &&
  40. permutable<I> &&
  41. uniform_random_bit_generator<std::remove_reference_t<Gen>> &&
  42. convertible_to<invoke_result_t<Gen &>, iter_difference_t<I>>)
  43. {
  44. auto mid = first;
  45. if(mid == last)
  46. return mid;
  47. using D1 = iter_difference_t<I>;
  48. using D2 = detail::if_then_t<std::is_integral<D1>::value, D1, std::ptrdiff_t>;
  49. std::uniform_int_distribution<D2> uid{};
  50. using param_t = typename decltype(uid)::param_type;
  51. while(++mid != last)
  52. {
  53. RANGES_ENSURE(mid - first <= PTRDIFF_MAX);
  54. if(auto const i = uid(gen, param_t{0, D2(mid - first)}))
  55. ranges::iter_swap(mid - i, mid);
  56. }
  57. return mid;
  58. }
  59. /// \overload
  60. template<typename Rng, typename Gen = detail::default_random_engine &>
  61. auto RANGES_FUN_NIEBLOID(shuffle)(Rng && rng,
  62. Gen && rand = detail::get_random_engine()) //
  63. ->CPP_ret(safe_iterator_t<Rng>)( //
  64. requires random_access_range<Rng> && permutable<iterator_t<Rng>> &&
  65. uniform_random_bit_generator<std::remove_reference_t<Gen>> &&
  66. convertible_to<invoke_result_t<Gen &>,
  67. iter_difference_t<iterator_t<Rng>>>)
  68. {
  69. return (*this)(begin(rng), end(rng), static_cast<Gen &&>(rand));
  70. }
  71. RANGES_END_NIEBLOID(shuffle)
  72. namespace cpp20
  73. {
  74. using ranges::shuffle;
  75. }
  76. /// @}
  77. } // namespace ranges
  78. #endif // include guard