Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

shuffle.hpp 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /// \file
  2. // Range v3 library
  3. //
  4. // Copyright Filip Matzner 2015
  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_ACTION_SHUFFLE_HPP
  14. #define RANGES_V3_ACTION_SHUFFLE_HPP
  15. #include <range/v3/range_fwd.hpp>
  16. #include <range/v3/action/action.hpp>
  17. #include <range/v3/algorithm/shuffle.hpp>
  18. #include <range/v3/functional/bind.hpp>
  19. #include <range/v3/functional/bind_back.hpp>
  20. #include <range/v3/functional/invoke.hpp>
  21. #include <range/v3/iterator/concepts.hpp>
  22. #include <range/v3/iterator/traits.hpp>
  23. #include <range/v3/utility/static_const.hpp>
  24. namespace ranges
  25. {
  26. /// \addtogroup group-actions
  27. /// @{
  28. namespace actions
  29. {
  30. struct shuffle_fn
  31. {
  32. private:
  33. friend action_access;
  34. template<typename Gen>
  35. static auto CPP_fun(bind)(shuffle_fn shuffle, Gen && gen)( //
  36. requires uniform_random_bit_generator<Gen>)
  37. {
  38. return bind_back(shuffle, static_cast<Gen &&>(gen));
  39. }
  40. #ifdef RANGES_WORKAROUND_MSVC_OLD_LAMBDA
  41. template<typename Gen>
  42. struct lamduh
  43. {
  44. Gen & gen_;
  45. template<typename Rng>
  46. auto operator()(Rng && rng) const
  47. -> invoke_result_t<shuffle_fn, Rng, Gen &>
  48. {
  49. return shuffle_fn{}(static_cast<Rng &&>(rng), gen_);
  50. }
  51. };
  52. template<typename Gen>
  53. static lamduh<Gen> CPP_fun(bind)(shuffle_fn, Gen & gen)( //
  54. requires uniform_random_bit_generator<Gen>)
  55. {
  56. return {gen};
  57. }
  58. #else // ^^^ workaround / no workaround vvv
  59. template<typename Gen>
  60. static auto CPP_fun(bind)(shuffle_fn, Gen & gen)( //
  61. requires uniform_random_bit_generator<Gen>)
  62. {
  63. return [&gen](auto && rng)
  64. -> invoke_result_t<shuffle_fn, decltype(rng), Gen &> {
  65. return shuffle_fn{}(static_cast<decltype(rng)>(rng), gen);
  66. };
  67. }
  68. #endif // RANGES_WORKAROUND_MSVC_OLD_LAMBDA
  69. public:
  70. template<typename Rng, typename Gen>
  71. auto operator()(Rng && rng, Gen && gen) const -> CPP_ret(Rng)( //
  72. requires random_access_range<Rng> && permutable<iterator_t<Rng>> &&
  73. uniform_random_bit_generator<std::remove_reference_t<Gen>> &&
  74. convertible_to<invoke_result_t<Gen &>, range_difference_t<Rng>>)
  75. {
  76. ranges::shuffle(rng, static_cast<Gen &&>(gen));
  77. return static_cast<Rng &&>(rng);
  78. }
  79. };
  80. /// \ingroup group-actions
  81. /// \relates shuffle_fn
  82. /// \sa `action`
  83. RANGES_INLINE_VARIABLE(action<shuffle_fn>, shuffle)
  84. } // namespace actions
  85. /// @}
  86. } // namespace ranges
  87. #endif