From 57d4e78d54dbb37f891f98942105224ddee6d976 Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Sat, 5 Apr 2014 21:56:06 +0900 Subject: [PATCH] add C++14 constexpr bogo_sort, bozo_sort --- sprout/algorithm/cxx14.hpp | 7 ++--- sprout/algorithm/cxx14/bogo_sort.hpp | 35 ++++++++++++++++++++++++ sprout/algorithm/cxx14/bozo_sort.hpp | 35 ++++++++++++++++++++++++ sprout/algorithm/cxx14/random_swap.hpp | 38 ++++++++++++++++++++++++++ sprout/algorithm/cxx14/shuffle.hpp | 5 ++-- 5 files changed, 114 insertions(+), 6 deletions(-) create mode 100644 sprout/algorithm/cxx14/bogo_sort.hpp create mode 100644 sprout/algorithm/cxx14/bozo_sort.hpp create mode 100644 sprout/algorithm/cxx14/random_swap.hpp diff --git a/sprout/algorithm/cxx14.hpp b/sprout/algorithm/cxx14.hpp index 710e471d..9953d372 100644 --- a/sprout/algorithm/cxx14.hpp +++ b/sprout/algorithm/cxx14.hpp @@ -62,9 +62,8 @@ #include #include #include -//#include -//#include -//#include -//#include +#include +#include +#include #endif // #ifndef SPROUT_ALGORITHM_CXX14_HPP diff --git a/sprout/algorithm/cxx14/bogo_sort.hpp b/sprout/algorithm/cxx14/bogo_sort.hpp new file mode 100644 index 00000000..87af4282 --- /dev/null +++ b/sprout/algorithm/cxx14/bogo_sort.hpp @@ -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 +#include +#include + +namespace sprout { + // + // bogo_sort + // + template + 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 + 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 diff --git a/sprout/algorithm/cxx14/bozo_sort.hpp b/sprout/algorithm/cxx14/bozo_sort.hpp new file mode 100644 index 00000000..af72626f --- /dev/null +++ b/sprout/algorithm/cxx14/bozo_sort.hpp @@ -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 +#include +#include + +namespace sprout { + // + // bozo_sort + // + template + 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 + 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 diff --git a/sprout/algorithm/cxx14/random_swap.hpp b/sprout/algorithm/cxx14/random_swap.hpp new file mode 100644 index 00000000..892b94f1 --- /dev/null +++ b/sprout/algorithm/cxx14/random_swap.hpp @@ -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 +#include +#include +#include +#include + +namespace sprout { + // + // random_swap + // + template + inline SPROUT_CXX14_CONSTEXPR void + random_swap(RandomAccessIterator first, RandomAccessIterator last, UniformRandomNumberGenerator&& g) { + typedef typename std::iterator_traits::difference_type difference_type; + typedef SPROUT_WORKAROUND_DETAIL_UNIFORM_INT_DISTRIBUTION 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 diff --git a/sprout/algorithm/cxx14/shuffle.hpp b/sprout/algorithm/cxx14/shuffle.hpp index 15ac44b4..889c5912 100644 --- a/sprout/algorithm/cxx14/shuffle.hpp +++ b/sprout/algorithm/cxx14/shuffle.hpp @@ -8,9 +8,9 @@ #ifndef SPROUT_ALGORITHM_CXX14_SHUFFLE_HPP #define SPROUT_ALGORITHM_CXX14_SHUFFLE_HPP +#include #include #include -#include #include #include @@ -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); + } } } }