Sprout/sprout/algorithm/fixed/random_swap_result.hpp

113 lines
5.1 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
2014-01-08 07:48:12 +00:00
Copyright (c) 2011-2014 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_RESULT_HPP
#define SPROUT_ALGORITHM_FIXED_RANDOM_SWAP_RESULT_HPP
#include <cstddef>
#include <type_traits>
#include <sprout/config.hpp>
2013-02-07 14:12:57 +00:00
#include <sprout/array/array.hpp>
#include <sprout/tuple/tuple/tuple.hpp>
#include <sprout/tuple/tuple/get.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-02-07 15:49:47 +00:00
#include <sprout/utility/pair/access.hpp>
#include <sprout/algorithm/fixed/results.hpp>
2012-07-25 14:10:01 +00:00
#include <sprout/algorithm/fixed/swap_element.hpp>
#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 UniformRandomNumberGenerator, typename Random>
2013-02-03 16:10:26 +00:00
inline SPROUT_CONSTEXPR sprout::pair<sprout::array<std::ptrdiff_t, 2>, typename std::decay<UniformRandomNumberGenerator>::type>
make_random_swap_result_indexes_2(Random const& rnd, std::ptrdiff_t i0) {
2013-02-03 16:10:26 +00:00
typedef sprout::pair<sprout::array<std::ptrdiff_t, 2>, typename std::decay<UniformRandomNumberGenerator>::type> result_type;
2012-07-25 14:10:01 +00:00
return result_type(
2014-01-16 09:59:25 +00:00
sprout::array<std::ptrdiff_t, 2>{{i0, sprout::random::result(rnd)}},
sprout::random::next(rnd).engine()
2012-07-25 14:10:01 +00:00
);
}
template<typename UniformRandomNumberGenerator, typename Random>
2013-02-03 16:10:26 +00:00
inline SPROUT_CONSTEXPR sprout::pair<sprout::array<std::ptrdiff_t, 2>, typename std::decay<UniformRandomNumberGenerator>::type>
make_random_swap_result_indexes_1(Random const& rnd) {
2012-07-25 14:10:01 +00:00
return sprout::fixed::detail::make_random_swap_result_indexes_2<UniformRandomNumberGenerator>(
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>
2013-02-03 16:10:26 +00:00
inline SPROUT_CONSTEXPR sprout::pair<sprout::array<std::ptrdiff_t, 2>, typename std::decay<UniformRandomNumberGenerator>::type>
make_random_swap_result_indexes(std::ptrdiff_t n, UniformRandomNumberGenerator&& g) {
2013-02-03 16:10:26 +00:00
typedef sprout::pair<sprout::array<std::ptrdiff_t, 2>, typename std::decay<UniformRandomNumberGenerator>::type> result_type;
2012-07-25 14:10:01 +00:00
return n > 1
? sprout::fixed::detail::make_random_swap_result_indexes_1<UniformRandomNumberGenerator>(
2013-02-03 16:10:26 +00:00
SPROUT_WORKAROUND_DETAIL_UNIFORM_INT_DISTRIBUTION<std::ptrdiff_t>(0, n - 1)(
SPROUT_FORWARD(UniformRandomNumberGenerator, g)
2013-02-03 16:10:26 +00:00
)
2012-07-25 14:10:01 +00:00
)
: result_type(
sprout::array<std::ptrdiff_t, 2>{{}},
SPROUT_FORWARD(UniformRandomNumberGenerator, g)
2012-07-25 14:10:01 +00:00
)
;
}
template<typename UniformRandomNumberGenerator, typename Container, typename RandomSwapped>
inline SPROUT_CONSTEXPR typename sprout::fixed::results::shuffle<Container, UniformRandomNumberGenerator>::type
random_swap_result_impl_1(Container const& cont, RandomSwapped const& random_swapped) {
typedef typename sprout::fixed::results::shuffle<Container, UniformRandomNumberGenerator>::type result_type;
2013-02-03 16:10:26 +00:00
return sprout::first(random_swapped)[0] == sprout::first(random_swapped)[1]
? result_type(sprout::deep_copy(cont), sprout::second(random_swapped))
2012-07-25 14:10:01 +00:00
: result_type(
sprout::fixed::swap_element(
cont,
2013-02-03 16:10:26 +00:00
sprout::next(sprout::begin(cont), sprout::first(random_swapped)[0]),
sprout::next(sprout::begin(cont), sprout::first(random_swapped)[1])
2012-07-25 14:10:01 +00:00
),
2013-02-03 16:10:26 +00:00
sprout::second(random_swapped)
2012-07-25 14:10:01 +00:00
)
;
}
template<typename Container, typename UniformRandomNumberGenerator>
inline SPROUT_CONSTEXPR typename sprout::fixed::results::shuffle<Container, UniformRandomNumberGenerator>::type
random_swap_result_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_result_impl_1<UniformRandomNumberGenerator>(
cont,
sprout::fixed::detail::make_random_swap_result_indexes(
size,
SPROUT_FORWARD(UniformRandomNumberGenerator, g)
2012-07-25 14:10:01 +00:00
)
);
}
} // namespace detail
//
// random_swap_result
//
template<typename Container, typename UniformRandomNumberGenerator>
inline SPROUT_CONSTEXPR typename sprout::fixed::results::shuffle<Container, UniformRandomNumberGenerator>::type
random_swap_result(Container const& cont, UniformRandomNumberGenerator&& g) {
2012-07-25 14:10:01 +00:00
return sprout::fixed::detail::random_swap_result_impl(
cont, SPROUT_FORWARD(UniformRandomNumberGenerator, g),
2012-07-25 14:10:01 +00:00
sprout::size(cont)
);
}
} // namespace fixed
using sprout::fixed::random_swap_result;
} // namespace sprout
#endif // #ifndef SPROUT_ALGORITHM_FIXED_RANDOM_SWAP_RESULT_HPP