| 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 |
|
| 14 |
#ifndef RANGES_V3_ACTION_SHUFFLE_HPP |
| 15 |
#define RANGES_V3_ACTION_SHUFFLE_HPP |
| 16 |
|
| 17 |
#include <range/v3/range_fwd.hpp> |
| 18 |
|
| 19 |
#include <range/v3/action/action.hpp> |
| 20 |
#include <range/v3/algorithm/shuffle.hpp> |
| 21 |
#include <range/v3/functional/bind.hpp> |
| 22 |
#include <range/v3/functional/bind_back.hpp> |
| 23 |
#include <range/v3/functional/invoke.hpp> |
| 24 |
#include <range/v3/iterator/concepts.hpp> |
| 25 |
#include <range/v3/iterator/traits.hpp> |
| 26 |
#include <range/v3/utility/static_const.hpp> |
| 27 |
|
| 28 |
#include <range/v3/detail/prologue.hpp> |
| 29 |
|
| 30 |
namespace ranges |
| 31 |
{ |
| 32 |
/// \addtogroup group-actions |
| 33 |
/// @{ |
| 34 |
namespace actions |
| 35 |
{ |
| 36 |
struct shuffle_fn |
| 37 |
{ |
| 38 |
template(typename Gen)( |
| 39 |
requires uniform_random_bit_generator<Gen>) |
| 40 |
constexpr auto operator()(Gen & gen) const |
| 41 |
{ |
| 42 |
return make_action_closure( |
| 43 |
bind_back(shuffle_fn{}, detail::reference_wrapper_<Gen>(gen))); |
| 44 |
} |
| 45 |
|
| 46 |
template(typename Gen)( |
| 47 |
requires uniform_random_bit_generator<Gen>) |
| 48 |
constexpr auto operator()(Gen && gen) const |
| 49 |
{ |
| 50 |
return make_action_closure( |
| 51 |
bind_back(shuffle_fn{}, static_cast<Gen &&>(gen))); |
| 52 |
} |
| 53 |
|
| 54 |
template(typename Rng, typename Gen)( |
| 55 |
requires random_access_range<Rng> AND permutable<iterator_t<Rng>> AND |
| 56 |
uniform_random_bit_generator<std::remove_reference_t<Gen>> AND |
| 57 |
convertible_to<invoke_result_t<Gen &>, range_difference_t<Rng>>) |
| 58 |
Rng operator()(Rng && rng, Gen && gen) const |
| 59 |
{ |
| 60 |
ranges::shuffle(rng, static_cast<Gen &&>(gen)); |
| 61 |
return static_cast<Rng &&>(rng); |
| 62 |
} |
| 63 |
|
| 64 |
/// \cond |
| 65 |
template<typename Rng, typename T> |
| 66 |
invoke_result_t<shuffle_fn, Rng, T &> // |
| 67 |
operator()(Rng && rng, detail::reference_wrapper_<T> r) const |
| 68 |
{ |
| 69 |
return (*this)(static_cast<Rng &&>(rng), r.get()); |
| 70 |
} |
| 71 |
/// \endcond |
| 72 |
}; |
| 73 |
|
| 74 |
/// \relates actions::shuffle_fn |
| 75 |
/// \sa `action_closure` |
| 76 |
RANGES_INLINE_VARIABLE(shuffle_fn, shuffle) |
| 77 |
} // namespace actions |
| 78 |
/// @} |
| 79 |
} // namespace ranges |
| 80 |
|
| 81 |
#include <range/v3/detail/epilogue.hpp> |
| 82 |
|
| 83 |
#endif |