add C++14 constexpr bogo_sort, bozo_sort

This commit is contained in:
bolero-MURAKAMI 2014-04-05 21:56:06 +09:00
parent c54c980b79
commit 57d4e78d54
5 changed files with 114 additions and 6 deletions

View file

@ -62,9 +62,8 @@
#include <sprout/algorithm/cxx14/copy_while.hpp>
#include <sprout/algorithm/cxx14/copy_until.hpp>
#include <sprout/algorithm/cxx14/clamp_range.hpp>
//#include <sprout/algorithm/cxx14/bogo_sort.hpp>
//#include <sprout/algorithm/cxx14/bogo_sort_result.hpp>
//#include <sprout/algorithm/cxx14/bozo_sort.hpp>
//#include <sprout/algorithm/cxx14/bozo_sort_result.hpp>
#include <sprout/algorithm/fixed/random_swap.hpp>
#include <sprout/algorithm/cxx14/bogo_sort.hpp>
#include <sprout/algorithm/cxx14/bozo_sort.hpp>
#endif // #ifndef SPROUT_ALGORITHM_CXX14_HPP

View file

@ -0,0 +1,35 @@
/*=============================================================================
Copyright (c) 2011-2014 Bolero MURAKAMI
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)
=============================================================================*/
#ifndef SPROUT_ALGORITHM_CXX14_BOGO_SORT_HPP
#define SPROUT_ALGORITHM_CXX14_BOGO_SORT_HPP
#include <sprout/config.hpp>
#include <sprout/algorithm/is_sorted.hpp>
#include <sprout/algorithm/cxx14/shuffle.hpp>
namespace sprout {
//
// bogo_sort
//
template<typename RandomAccessIterator, typename UniformRandomNumberGenerator>
inline SPROUT_CXX14_CONSTEXPR void
bogo_sort(RandomAccessIterator first, RandomAccessIterator last, UniformRandomNumberGenerator&& g) {
while (!sprout::is_sorted(first, last)) {
sprout::shuffle(first, last, g);
}
}
template<typename RandomAccessIterator, typename UniformRandomNumberGenerator, typename Compare>
inline SPROUT_CXX14_CONSTEXPR void
bogo_sort(RandomAccessIterator first, RandomAccessIterator last, UniformRandomNumberGenerator&& g, Compare comp) {
while (!sprout::is_sorted(first, last, comp)) {
sprout::shuffle(first, last, g);
}
}
} // namespace sprout
#endif // #ifndef SPROUT_ALGORITHM_CXX14_BOGO_SORT_HPP

View file

@ -0,0 +1,35 @@
/*=============================================================================
Copyright (c) 2011-2014 Bolero MURAKAMI
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)
=============================================================================*/
#ifndef SPROUT_ALGORITHM_CXX14_BOZO_SORT_HPP
#define SPROUT_ALGORITHM_CXX14_BOZO_SORT_HPP
#include <sprout/config.hpp>
#include <sprout/algorithm/is_sorted.hpp>
#include <sprout/algorithm/cxx14/random_swap.hpp>
namespace sprout {
//
// bozo_sort
//
template<typename RandomAccessIterator, typename UniformRandomNumberGenerator>
inline SPROUT_CXX14_CONSTEXPR void
bozo_sort(RandomAccessIterator first, RandomAccessIterator last, UniformRandomNumberGenerator&& g) {
while (!sprout::is_sorted(first, last)) {
sprout::random_swap(first, last, g);
}
}
template<typename RandomAccessIterator, typename UniformRandomNumberGenerator, typename Compare>
inline SPROUT_CXX14_CONSTEXPR void
bozo_sort(RandomAccessIterator first, RandomAccessIterator last, UniformRandomNumberGenerator&& g, Compare comp) {
while (!sprout::is_sorted(first, last, comp)) {
sprout::random_swap(first, last, g);
}
}
} // namespace sprout
#endif // #ifndef SPROUT_ALGORITHM_CXX14_BOZO_SORT_HPP

View file

@ -0,0 +1,38 @@
/*=============================================================================
Copyright (c) 2011-2014 Bolero MURAKAMI
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)
=============================================================================*/
#ifndef SPROUT_ALGORITHM_CXX14_RANDOM_SWAP_HPP
#define SPROUT_ALGORITHM_CXX14_RANDOM_SWAP_HPP
#include <cstddef>
#include <iterator>
#include <sprout/config.hpp>
#include <sprout/algorithm/cxx14/iter_swap.hpp>
#include <sprout/workaround/detail/uniform_int_distribution.hpp>
namespace sprout {
//
// random_swap
//
template<typename RandomAccessIterator, typename UniformRandomNumberGenerator>
inline SPROUT_CXX14_CONSTEXPR void
random_swap(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;
difference_type d = last - first;
if (d > 1) {
distribution_type dist(0, d);
difference_type i = dist(g);
difference_type j = dist(g);
if (i != j) {
sprout::iter_swap(first + i, first + j);
}
}
}
} // namespace sprout
#endif // #ifndef SPROUT_ALGORITHM_CXX14_RANDOM_SWAP_HPP

View file

@ -8,9 +8,9 @@
#ifndef SPROUT_ALGORITHM_CXX14_SHUFFLE_HPP
#define SPROUT_ALGORITHM_CXX14_SHUFFLE_HPP
#include <cstddef>
#include <iterator>
#include <sprout/config.hpp>
#include <sprout/iterator/distance.hpp>
#include <sprout/algorithm/cxx14/iter_swap.hpp>
#include <sprout/workaround/detail/uniform_int_distribution.hpp>
@ -29,8 +29,9 @@ namespace sprout {
distribution_type dist;
for (--last, --d; first < last; ++first, --d) {
difference_type i = dist(g, param_type(0, d));
if (i != difference_type(0))
if (i != difference_type(0)) {
sprout::iter_swap(first, first + i);
}
}
}
}