2013-10-31 09:57:41 +00:00
|
|
|
/*=============================================================================
|
2019-01-07 08:47:17 +00:00
|
|
|
Copyright (c) 2011-2019 Bolero MURAKAMI
|
2013-10-31 09:57:41 +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)
|
|
|
|
=============================================================================*/
|
2014-04-05 09:31:09 +00:00
|
|
|
#ifndef SPROUT_ALGORITHM_CXX14_SHUFFLE_HPP
|
|
|
|
#define SPROUT_ALGORITHM_CXX14_SHUFFLE_HPP
|
2013-10-31 09:57:41 +00:00
|
|
|
|
2013-11-04 09:50:48 +00:00
|
|
|
#include <iterator>
|
2013-10-31 09:57:41 +00:00
|
|
|
#include <sprout/config.hpp>
|
2014-04-30 07:30:26 +00:00
|
|
|
#include <sprout/workaround/std/cstddef.hpp>
|
2013-11-04 09:50:48 +00:00
|
|
|
#include <sprout/algorithm/cxx14/iter_swap.hpp>
|
|
|
|
#include <sprout/workaround/detail/uniform_int_distribution.hpp>
|
2013-10-31 09:57:41 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
//
|
|
|
|
// 25.3.12 Random shuffle
|
|
|
|
//
|
|
|
|
template<typename RandomAccessIterator, typename UniformRandomNumberGenerator>
|
|
|
|
inline SPROUT_CXX14_CONSTEXPR void
|
2013-11-04 09:50:48 +00:00
|
|
|
shuffle(RandomAccessIterator first, RandomAccessIterator last, UniformRandomNumberGenerator&& g) {
|
|
|
|
typedef typename std::iterator_traits<RandomAccessIterator>::difference_type difference_type;
|
|
|
|
typedef SPROUT_WORKAROUND_DETAIL_UNIFORM_INT_DISTRIBUTION<std::ptrdiff_t> distribution_type;
|
|
|
|
typedef typename distribution_type::param_type param_type;
|
|
|
|
difference_type d = last - first;
|
|
|
|
if (d > 1) {
|
|
|
|
distribution_type dist;
|
|
|
|
for (--last, --d; first < last; ++first, --d) {
|
|
|
|
difference_type i = dist(g, param_type(0, d));
|
2014-04-05 12:56:06 +00:00
|
|
|
if (i != difference_type(0)) {
|
2013-11-04 09:50:48 +00:00
|
|
|
sprout::iter_swap(first, first + i);
|
2014-04-05 12:56:06 +00:00
|
|
|
}
|
2013-11-04 09:50:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-10-31 09:57:41 +00:00
|
|
|
} // namespace sprout
|
|
|
|
|
2014-04-05 09:31:09 +00:00
|
|
|
#endif // #ifndef SPROUT_ALGORITHM_CXX14_SHUFFLE_HPP
|