2013-08-08 09:54:33 +00:00
|
|
|
/*=============================================================================
|
2019-01-07 08:47:17 +00:00
|
|
|
Copyright (c) 2011-2019 Bolero MURAKAMI
|
2013-08-08 09:54:33 +00:00
|
|
|
https://github.com/bolero-MURAKAMI/Sprout
|
|
|
|
|
|
|
|
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
=============================================================================*/
|
2012-07-25 14:10:01 +00:00
|
|
|
#ifndef SPROUT_ALGORITHM_FIXED_RANDOM_SWAP_HPP
|
|
|
|
#define SPROUT_ALGORITHM_FIXED_RANDOM_SWAP_HPP
|
|
|
|
|
|
|
|
#include <sprout/config.hpp>
|
2014-04-30 07:30:26 +00:00
|
|
|
#include <sprout/workaround/std/cstddef.hpp>
|
2013-02-07 14:12:57 +00:00
|
|
|
#include <sprout/array/array.hpp>
|
2012-07-25 14:10:01 +00:00
|
|
|
#include <sprout/container/traits.hpp>
|
|
|
|
#include <sprout/container/functions.hpp>
|
|
|
|
#include <sprout/iterator/operation.hpp>
|
|
|
|
#include <sprout/utility/forward.hpp>
|
2013-02-07 14:12:57 +00:00
|
|
|
#include <sprout/utility/pair/pair.hpp>
|
2013-11-20 13:04:11 +00:00
|
|
|
#include <sprout/algorithm/fixed/results.hpp>
|
2012-07-25 14:10:01 +00:00
|
|
|
#include <sprout/algorithm/fixed/swap_element.hpp>
|
2014-01-17 13:09:08 +00:00
|
|
|
#include <sprout/random/results.hpp>
|
2013-02-03 16:10:26 +00:00
|
|
|
#include <sprout/workaround/detail/uniform_int_distribution.hpp>
|
2012-07-25 14:10:01 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
namespace fixed {
|
|
|
|
namespace detail {
|
|
|
|
template<typename Random>
|
2012-09-29 08:10:46 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::array<std::ptrdiff_t, 2>
|
|
|
|
make_random_swap_indexes_2(Random const& rnd, std::ptrdiff_t i0) {
|
2014-01-16 09:59:25 +00:00
|
|
|
return sprout::array<std::ptrdiff_t, 2>{{i0, sprout::random::result(rnd)}};
|
2012-07-25 14:10:01 +00:00
|
|
|
}
|
|
|
|
template<typename Random>
|
2012-09-29 08:10:46 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::array<std::ptrdiff_t, 2>
|
|
|
|
make_random_swap_indexes_1(Random const& rnd) {
|
2012-07-25 14:10:01 +00:00
|
|
|
return sprout::fixed::detail::make_random_swap_indexes_2(
|
2014-01-16 09:59:25 +00:00
|
|
|
sprout::random::next(rnd)(),
|
|
|
|
sprout::random::result(rnd)
|
2012-07-25 14:10:01 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
template<typename UniformRandomNumberGenerator>
|
2012-09-29 08:10:46 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::array<std::ptrdiff_t, 2>
|
|
|
|
make_random_swap_indexes(std::ptrdiff_t n, UniformRandomNumberGenerator&& g) {
|
2012-07-25 14:10:01 +00:00
|
|
|
return n > 1
|
|
|
|
? sprout::fixed::detail::make_random_swap_indexes_1(
|
2013-02-03 16:10:26 +00:00
|
|
|
SPROUT_WORKAROUND_DETAIL_UNIFORM_INT_DISTRIBUTION<std::ptrdiff_t>(0, n - 1)(
|
2014-02-22 07:32:51 +00:00
|
|
|
SPROUT_FORWARD(UniformRandomNumberGenerator, g)
|
2013-02-03 16:10:26 +00:00
|
|
|
)
|
2012-07-25 14:10:01 +00:00
|
|
|
)
|
|
|
|
: sprout::array<std::ptrdiff_t, 2>{{}}
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename Container, typename RandomSwapped>
|
2013-11-20 13:04:11 +00:00
|
|
|
inline SPROUT_CONSTEXPR typename sprout::fixed::results::algorithm<Container>::type
|
2012-09-29 08:10:46 +00:00
|
|
|
random_swap_impl_1(Container const& cont, RandomSwapped const& random_swapped) {
|
2012-07-25 14:10:01 +00:00
|
|
|
return random_swapped[0] == random_swapped[1]
|
|
|
|
? sprout::deep_copy(cont)
|
|
|
|
: sprout::fixed::swap_element(
|
|
|
|
cont,
|
|
|
|
sprout::next(sprout::begin(cont), random_swapped[0]),
|
|
|
|
sprout::next(sprout::begin(cont), random_swapped[1])
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename Container, typename UniformRandomNumberGenerator>
|
2013-11-20 13:04:11 +00:00
|
|
|
inline SPROUT_CONSTEXPR typename sprout::fixed::results::algorithm<Container>::type
|
2012-09-29 08:10:46 +00:00
|
|
|
random_swap_impl(
|
|
|
|
Container const& cont, UniformRandomNumberGenerator&& g,
|
2012-07-25 14:10:01 +00:00
|
|
|
typename sprout::container_traits<Container>::size_type size
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::fixed::detail::random_swap_impl_1(
|
|
|
|
cont,
|
|
|
|
sprout::fixed::detail::make_random_swap_indexes(
|
|
|
|
size,
|
2014-02-22 07:32:51 +00:00
|
|
|
SPROUT_FORWARD(UniformRandomNumberGenerator, g)
|
2012-07-25 14:10:01 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} // namespace detail
|
|
|
|
//
|
|
|
|
// random_swap
|
|
|
|
//
|
|
|
|
template<typename Container, typename UniformRandomNumberGenerator>
|
2013-11-20 13:04:11 +00:00
|
|
|
inline SPROUT_CONSTEXPR typename sprout::fixed::results::algorithm<Container>::type
|
2012-09-29 08:10:46 +00:00
|
|
|
random_swap(Container const& cont, UniformRandomNumberGenerator&& g) {
|
2012-07-25 14:10:01 +00:00
|
|
|
return sprout::fixed::detail::random_swap_impl(
|
2014-02-22 07:32:51 +00:00
|
|
|
cont, SPROUT_FORWARD(UniformRandomNumberGenerator, g),
|
2012-07-25 14:10:01 +00:00
|
|
|
sprout::size(cont)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} // namespace fixed
|
|
|
|
|
|
|
|
using sprout::fixed::random_swap;
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_ALGORITHM_FIXED_RANDOM_SWAP_HPP
|