mirror of
https://github.com/bolero-MURAKAMI/Sprout.git
synced 2024-12-23 21:25:49 +00:00
sprout/algorithm/bogo_sort.hpp 追加
sprout/algorithm/bogo_sort_result.hpp 追加 sprout/algorithm/shuffle_result.hpp 追加
This commit is contained in:
parent
e660c7261f
commit
01e458886a
14 changed files with 706 additions and 3 deletions
8
sprout/algorithm/bogo_sort.hpp
Normal file
8
sprout/algorithm/bogo_sort.hpp
Normal file
|
@ -0,0 +1,8 @@
|
|||
#ifndef SPROUT_ALGORITHM_BOGO_SORT_HPP
|
||||
#define SPROUT_ALGORITHM_BOGO_SORT_HPP
|
||||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/algorithm/fixed/bogo_sort.hpp>
|
||||
#include <sprout/algorithm/fit/bogo_sort.hpp>
|
||||
|
||||
#endif // #ifndef SPROUT_ALGORITHM_BOGO_SORT_HPP
|
8
sprout/algorithm/bogo_sort_result.hpp
Normal file
8
sprout/algorithm/bogo_sort_result.hpp
Normal file
|
@ -0,0 +1,8 @@
|
|||
#ifndef SPROUT_ALGORITHM_BOGO_SORT_RESULT_HPP
|
||||
#define SPROUT_ALGORITHM_BOGO_SORT_RESULT_HPP
|
||||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/algorithm/fixed/bogo_sort_result.hpp>
|
||||
#include <sprout/algorithm/fit/bogo_sort_result.hpp>
|
||||
|
||||
#endif // #ifndef SPROUT_ALGORITHM_BOGO_SORT_RESULT_HPP
|
|
@ -26,6 +26,7 @@
|
|||
#include <sprout/algorithm/fit/rotate.hpp>
|
||||
#include <sprout/algorithm/fit/rotate_copy.hpp>
|
||||
#include <sprout/algorithm/fit/shuffle.hpp>
|
||||
#include <sprout/algorithm/fit/shuffle_result.hpp>
|
||||
#include <sprout/algorithm/fit/partition.hpp>
|
||||
#include <sprout/algorithm/fit/partition_copy.hpp>
|
||||
#include <sprout/algorithm/fit/stable_partition.hpp>
|
||||
|
@ -47,5 +48,7 @@
|
|||
#include <sprout/algorithm/fit/sort_heap.hpp>
|
||||
#include <sprout/algorithm/fit/swap_element.hpp>
|
||||
#include <sprout/algorithm/fit/swap_element_copy.hpp>
|
||||
#include <sprout/algorithm/fit/bogo_sort.hpp>
|
||||
#include <sprout/algorithm/fit/bogo_sort_result.hpp>
|
||||
|
||||
#endif // #ifndef SPROUT_ALGORITHM_FIT_HPP
|
||||
|
|
80
sprout/algorithm/fit/bogo_sort.hpp
Normal file
80
sprout/algorithm/fit/bogo_sort.hpp
Normal file
|
@ -0,0 +1,80 @@
|
|||
#ifndef SPROUT_ALGORITHM_FIT_BOGO_SORT_HPP
|
||||
#define SPROUT_ALGORITHM_FIT_BOGO_SORT_HPP
|
||||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/fixed_container/traits.hpp>
|
||||
#include <sprout/fixed_container/functions.hpp>
|
||||
#include <sprout/algorithm/fixed/bogo_sort.hpp>
|
||||
#include <sprout/algorithm/fit/result_of.hpp>
|
||||
#include <sprout/sub_array.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace fit {
|
||||
namespace detail {
|
||||
template<typename Container, typename UniformRandomNumberGenerator, typename Compare>
|
||||
SPROUT_CONSTEXPR inline typename sprout::fit::result_of::algorithm<Container>::type bogo_sort_impl(
|
||||
Container const& cont,
|
||||
UniformRandomNumberGenerator&& g,
|
||||
Compare comp,
|
||||
typename sprout::fixed_container_traits<Container>::difference_type offset
|
||||
)
|
||||
{
|
||||
return sprout::sub_copy(
|
||||
sprout::get_fixed(sprout::fixed::bogo_sort(cont, sprout::forward<UniformRandomNumberGenerator>(g), comp)),
|
||||
offset,
|
||||
offset + sprout::size(cont)
|
||||
);
|
||||
}
|
||||
} // namespace detail
|
||||
//
|
||||
// bogo_sort
|
||||
//
|
||||
template<typename Container, typename UniformRandomNumberGenerator, typename Compare>
|
||||
SPROUT_CONSTEXPR inline typename sprout::fit::result_of::algorithm<Container>::type bogo_sort(
|
||||
Container const& cont,
|
||||
UniformRandomNumberGenerator&& g,
|
||||
Compare comp
|
||||
)
|
||||
{
|
||||
return sprout::fit::detail::bogo_sort_impl(
|
||||
cont,
|
||||
sprout::forward<UniformRandomNumberGenerator>(g),
|
||||
comp,
|
||||
sprout::fixed_begin_offset(cont)
|
||||
);
|
||||
}
|
||||
|
||||
namespace detail {
|
||||
template<typename Container, typename UniformRandomNumberGenerator>
|
||||
SPROUT_CONSTEXPR inline typename sprout::fit::result_of::algorithm<Container>::type bogo_sort_impl(
|
||||
Container const& cont,
|
||||
UniformRandomNumberGenerator&& g,
|
||||
typename sprout::fixed_container_traits<Container>::difference_type offset
|
||||
)
|
||||
{
|
||||
return sprout::sub_copy(
|
||||
sprout::get_fixed(sprout::fixed::bogo_sort(cont, sprout::forward<UniformRandomNumberGenerator>(g))),
|
||||
offset,
|
||||
offset + sprout::size(cont)
|
||||
);
|
||||
}
|
||||
} // namespace detail
|
||||
//
|
||||
// bogo_sort
|
||||
//
|
||||
template<typename Container, typename UniformRandomNumberGenerator>
|
||||
SPROUT_CONSTEXPR inline typename sprout::fit::result_of::algorithm<Container>::type bogo_sort(
|
||||
Container const& cont,
|
||||
UniformRandomNumberGenerator&& g
|
||||
)
|
||||
{
|
||||
return sprout::fit::detail::bogo_sort_impl(
|
||||
cont,
|
||||
sprout::forward<UniformRandomNumberGenerator>(g),
|
||||
sprout::fixed_begin_offset(cont)
|
||||
);
|
||||
}
|
||||
} // namespace fit
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_ALGORITHM_FIT_BOGO_SORT_HPP
|
114
sprout/algorithm/fit/bogo_sort_result.hpp
Normal file
114
sprout/algorithm/fit/bogo_sort_result.hpp
Normal file
|
@ -0,0 +1,114 @@
|
|||
#ifndef SPROUT_ALGORITHM_FIT_BOGO_SORT_RESULT_HPP
|
||||
#define SPROUT_ALGORITHM_FIT_BOGO_SORT_RESULT_HPP
|
||||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/tuple/tuple.hpp>
|
||||
#include <sprout/fixed_container/traits.hpp>
|
||||
#include <sprout/fixed_container/functions.hpp>
|
||||
#include <sprout/utility/forward.hpp>
|
||||
#include <sprout/algorithm/fixed/bogo_sort_result.hpp>
|
||||
#include <sprout/algorithm/fit/result_of.hpp>
|
||||
#include <sprout/sub_array.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace fit {
|
||||
namespace detail {
|
||||
template<typename Container, typename UniformRandomNumberGenerator, typename Sorted>
|
||||
SPROUT_CONSTEXPR inline sprout::tuples::tuple<
|
||||
typename sprout::fit::result_of::algorithm<Container>::type,
|
||||
typename std::decay<UniformRandomNumberGenerator>::type
|
||||
> bogo_sort_result_impl_1(
|
||||
Sorted const& sorted,
|
||||
typename sprout::fixed_container_traits<Container>::difference_type offset
|
||||
)
|
||||
{
|
||||
typedef sprout::tuples::tuple<
|
||||
typename sprout::fit::result_of::algorithm<Container>::type,
|
||||
typename std::decay<UniformRandomNumberGenerator>::type
|
||||
> result_type;
|
||||
return result_type(
|
||||
sprout::sub_copy(
|
||||
sprout::get_fixed(sprout::tuples::get<0>(sorted)),
|
||||
offset,
|
||||
offset + sprout::size(sprout::tuples::get<0>(sorted))
|
||||
),
|
||||
sprout::tuples::get<1>(sorted)
|
||||
);
|
||||
}
|
||||
template<typename Container, typename UniformRandomNumberGenerator, typename Compare>
|
||||
SPROUT_CONSTEXPR inline sprout::tuples::tuple<
|
||||
typename sprout::fit::result_of::algorithm<Container>::type,
|
||||
typename std::decay<UniformRandomNumberGenerator>::type
|
||||
> bogo_sort_result_impl(
|
||||
Container const& cont,
|
||||
UniformRandomNumberGenerator&& g,
|
||||
Compare comp,
|
||||
typename sprout::fixed_container_traits<Container>::difference_type offset
|
||||
)
|
||||
{
|
||||
return sprout::fit::detail::bogo_sort_result_impl_1<Container, UniformRandomNumberGenerator>(
|
||||
sprout::fixed::bogo_sort_result(cont, sprout::forward<UniformRandomNumberGenerator>(g), comp),
|
||||
offset
|
||||
);
|
||||
}
|
||||
} // namespace detail
|
||||
//
|
||||
// bogo_sort_result
|
||||
//
|
||||
template<typename Container, typename UniformRandomNumberGenerator, typename Compare>
|
||||
SPROUT_CONSTEXPR inline sprout::tuples::tuple<
|
||||
typename sprout::fit::result_of::algorithm<Container>::type,
|
||||
typename std::decay<UniformRandomNumberGenerator>::type
|
||||
> bogo_sort_result(
|
||||
Container const& cont,
|
||||
UniformRandomNumberGenerator&& g,
|
||||
Compare comp
|
||||
)
|
||||
{
|
||||
return sprout::fit::detail::bogo_sort_result_impl(
|
||||
cont,
|
||||
sprout::forward<UniformRandomNumberGenerator>(g),
|
||||
comp,
|
||||
sprout::fixed_begin_offset(cont)
|
||||
);
|
||||
}
|
||||
|
||||
namespace detail {
|
||||
template<typename Container, typename UniformRandomNumberGenerator>
|
||||
SPROUT_CONSTEXPR inline sprout::tuples::tuple<
|
||||
typename sprout::fit::result_of::algorithm<Container>::type,
|
||||
typename std::decay<UniformRandomNumberGenerator>::type
|
||||
> bogo_sort_result_impl(
|
||||
Container const& cont,
|
||||
UniformRandomNumberGenerator&& g,
|
||||
typename sprout::fixed_container_traits<Container>::difference_type offset
|
||||
)
|
||||
{
|
||||
return sprout::fit::detail::bogo_sort_result_impl_1<Container, UniformRandomNumberGenerator>(
|
||||
sprout::fixed::bogo_sort_result(cont, sprout::forward<UniformRandomNumberGenerator>(g)),
|
||||
offset
|
||||
);
|
||||
}
|
||||
} // namespace detail
|
||||
//
|
||||
// bogo_sort_result
|
||||
//
|
||||
template<typename Container, typename UniformRandomNumberGenerator>
|
||||
SPROUT_CONSTEXPR inline sprout::tuples::tuple<
|
||||
typename sprout::fit::result_of::algorithm<Container>::type,
|
||||
typename std::decay<UniformRandomNumberGenerator>::type
|
||||
> bogo_sort_result(
|
||||
Container const& cont,
|
||||
UniformRandomNumberGenerator&& g
|
||||
)
|
||||
{
|
||||
return sprout::fit::detail::bogo_sort_result_impl(
|
||||
cont,
|
||||
sprout::forward<UniformRandomNumberGenerator>(g),
|
||||
sprout::fixed_begin_offset(cont)
|
||||
);
|
||||
}
|
||||
} // namespace fit
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_ALGORITHM_FIT_BOGO_SORT_RESULT_HPP
|
|
@ -35,7 +35,11 @@ namespace sprout {
|
|||
UniformRandomNumberGenerator&& g
|
||||
)
|
||||
{
|
||||
return sprout::fit::detail::shuffle_impl(cont, sprout::forward<UniformRandomNumberGenerator>(g), sprout::fixed_begin_offset(cont));
|
||||
return sprout::fit::detail::shuffle_impl(
|
||||
cont,
|
||||
sprout::forward<UniformRandomNumberGenerator>(g),
|
||||
sprout::fixed_begin_offset(cont)
|
||||
);
|
||||
}
|
||||
} // namespace fit
|
||||
} // namespace sprout
|
||||
|
|
75
sprout/algorithm/fit/shuffle_result.hpp
Normal file
75
sprout/algorithm/fit/shuffle_result.hpp
Normal file
|
@ -0,0 +1,75 @@
|
|||
#ifndef SPROUT_ALGORITHM_FIT_SHUFFLE_RESULT_HPP
|
||||
#define SPROUT_ALGORITHM_FIT_SHUFFLE_RESULT_HPP
|
||||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/tuple/tuple.hpp>
|
||||
#include <sprout/fixed_container/traits.hpp>
|
||||
#include <sprout/fixed_container/functions.hpp>
|
||||
#include <sprout/utility/forward.hpp>
|
||||
#include <sprout/algorithm/fixed/shuffle_result.hpp>
|
||||
#include <sprout/algorithm/fit/result_of.hpp>
|
||||
#include <sprout/sub_array.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace fit {
|
||||
namespace detail {
|
||||
template<typename Container, typename UniformRandomNumberGenerator, typename Shuffled>
|
||||
SPROUT_CONSTEXPR inline sprout::tuples::tuple<
|
||||
typename sprout::fit::result_of::algorithm<Container>::type,
|
||||
typename std::decay<UniformRandomNumberGenerator>::type
|
||||
> shuffle_result_impl_1(
|
||||
Shuffled const& shuffled,
|
||||
typename sprout::fixed_container_traits<Container>::difference_type offset
|
||||
)
|
||||
{
|
||||
typedef sprout::tuples::tuple<
|
||||
typename sprout::fit::result_of::algorithm<Container>::type,
|
||||
typename std::decay<UniformRandomNumberGenerator>::type
|
||||
> result_type;
|
||||
return result_type(
|
||||
sprout::sub_copy(
|
||||
sprout::get_fixed(sprout::tuples::get<0>(shuffled)),
|
||||
offset,
|
||||
offset + sprout::size(sprout::tuples::get<0>(shuffled))
|
||||
),
|
||||
sprout::tuples::get<1>(shuffled)
|
||||
);
|
||||
}
|
||||
template<typename Container, typename UniformRandomNumberGenerator>
|
||||
SPROUT_CONSTEXPR inline sprout::tuples::tuple<
|
||||
typename sprout::fit::result_of::algorithm<Container>::type,
|
||||
typename std::decay<UniformRandomNumberGenerator>::type
|
||||
> shuffle_result_impl(
|
||||
Container const& cont,
|
||||
UniformRandomNumberGenerator&& g,
|
||||
typename sprout::fixed_container_traits<Container>::difference_type offset
|
||||
)
|
||||
{
|
||||
return sprout::fit::detail::shuffle_result_impl_1<Container, UniformRandomNumberGenerator>(
|
||||
sprout::fixed::shuffle_result(cont, sprout::forward<UniformRandomNumberGenerator>(g)),
|
||||
offset
|
||||
);
|
||||
}
|
||||
} // namespace detail
|
||||
//
|
||||
// shuffle_result
|
||||
//
|
||||
template<typename Container, typename UniformRandomNumberGenerator>
|
||||
SPROUT_CONSTEXPR inline sprout::tuples::tuple<
|
||||
typename sprout::fit::result_of::algorithm<Container>::type,
|
||||
typename std::decay<UniformRandomNumberGenerator>::type
|
||||
> shuffle_result(
|
||||
Container const& cont,
|
||||
UniformRandomNumberGenerator&& g
|
||||
)
|
||||
{
|
||||
return sprout::fit::detail::shuffle_result_impl(
|
||||
cont,
|
||||
sprout::forward<UniformRandomNumberGenerator>(g),
|
||||
sprout::fixed_begin_offset(cont)
|
||||
);
|
||||
}
|
||||
} // namespace fit
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_ALGORITHM_FIT_SHUFFLE_RESULT_HPP
|
|
@ -26,6 +26,7 @@
|
|||
#include <sprout/algorithm/fixed/rotate.hpp>
|
||||
#include <sprout/algorithm/fixed/rotate_copy.hpp>
|
||||
#include <sprout/algorithm/fixed/shuffle.hpp>
|
||||
#include <sprout/algorithm/fixed/shuffle_result.hpp>
|
||||
#include <sprout/algorithm/fixed/partition.hpp>
|
||||
#include <sprout/algorithm/fixed/partition_copy.hpp>
|
||||
#include <sprout/algorithm/fixed/stable_partition.hpp>
|
||||
|
@ -47,5 +48,7 @@
|
|||
#include <sprout/algorithm/fixed/sort_heap.hpp>
|
||||
#include <sprout/algorithm/fixed/swap_element.hpp>
|
||||
#include <sprout/algorithm/fixed/swap_element_copy.hpp>
|
||||
#include <sprout/algorithm/fixed/bogo_sort.hpp>
|
||||
#include <sprout/algorithm/fixed/bogo_sort_result.hpp>
|
||||
|
||||
#endif // #ifndef SPROUT_ALGORITHM_FIXED_HPP
|
||||
|
|
98
sprout/algorithm/fixed/bogo_sort.hpp
Normal file
98
sprout/algorithm/fixed/bogo_sort.hpp
Normal file
|
@ -0,0 +1,98 @@
|
|||
#ifndef SPROUT_ALGORITHM_FIXED_BOGO_SORT_HPP
|
||||
#define SPROUT_ALGORITHM_FIXED_BOGO_SORT_HPP
|
||||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/tuple/tuple.hpp>
|
||||
#include <sprout/fixed_container/traits.hpp>
|
||||
#include <sprout/fixed_container/functions.hpp>
|
||||
#include <sprout/iterator/operation.hpp>
|
||||
#include <sprout/utility/forward.hpp>
|
||||
#include <sprout/algorithm/fixed/result_of.hpp>
|
||||
#include <sprout/algorithm/fixed/shuffle.hpp>
|
||||
#include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT_DETAIL
|
||||
#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT_DETAIL
|
||||
|
||||
namespace sprout {
|
||||
namespace fixed {
|
||||
namespace detail {
|
||||
template<typename Container, typename Shuffled, typename Compare>
|
||||
SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm<Container>::type bogo_sort_impl_1(
|
||||
Shuffled const& shuffled,
|
||||
Compare comp
|
||||
)
|
||||
{
|
||||
return NS_SSCRISK_CEL_OR_SPROUT_DETAIL::is_sorted(
|
||||
sprout::begin(sprout::tuples::get<0>(shuffled)),
|
||||
sprout::end(sprout::tuples::get<0>(shuffled)),
|
||||
comp
|
||||
)
|
||||
? sprout::tuples::get<0>(shuffled)
|
||||
: sprout::fixed::detail::bogo_sort_impl_1<Container>(
|
||||
sprout::fixed::shuffle_result(
|
||||
sprout::tuples::get<0>(shuffled),
|
||||
sprout::tuples::get<1>(shuffled)
|
||||
),
|
||||
comp
|
||||
)
|
||||
;
|
||||
}
|
||||
template<typename Container, typename UniformRandomNumberGenerator, typename Compare>
|
||||
SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm<Container>::type bogo_sort_impl(
|
||||
Container const& cont,
|
||||
UniformRandomNumberGenerator&& g,
|
||||
Compare comp
|
||||
)
|
||||
{
|
||||
return NS_SSCRISK_CEL_OR_SPROUT_DETAIL::is_sorted(
|
||||
sprout::begin(cont),
|
||||
sprout::end(cont),
|
||||
comp
|
||||
)
|
||||
? sprout::clone(cont)
|
||||
: sprout::fixed::detail::bogo_sort_impl_1<Container>(
|
||||
sprout::fixed::shuffle_result(
|
||||
cont,
|
||||
sprout::forward<UniformRandomNumberGenerator>(g)
|
||||
),
|
||||
comp
|
||||
)
|
||||
;
|
||||
}
|
||||
} // namespace detail
|
||||
//
|
||||
// bogo_sort
|
||||
//
|
||||
template<typename Container, typename UniformRandomNumberGenerator, typename Compare>
|
||||
SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm<Container>::type bogo_sort(
|
||||
Container const& cont,
|
||||
UniformRandomNumberGenerator&& g,
|
||||
Compare comp
|
||||
)
|
||||
{
|
||||
return sprout::fixed::detail::bogo_sort_impl(
|
||||
cont,
|
||||
sprout::forward<UniformRandomNumberGenerator>(g),
|
||||
comp
|
||||
);
|
||||
}
|
||||
//
|
||||
// bogo_sort
|
||||
//
|
||||
template<typename Container, typename UniformRandomNumberGenerator>
|
||||
SPROUT_CONSTEXPR inline typename sprout::fixed::result_of::algorithm<Container>::type bogo_sort(
|
||||
Container const& cont,
|
||||
UniformRandomNumberGenerator&& g
|
||||
)
|
||||
{
|
||||
return sprout::fixed::detail::bogo_sort_impl(
|
||||
cont,
|
||||
sprout::forward<UniformRandomNumberGenerator>(g),
|
||||
NS_SSCRISK_CEL_OR_SPROUT_DETAIL::less<typename sprout::fixed_container_traits<Container>::value_type>()
|
||||
);
|
||||
}
|
||||
} // namespace fixed
|
||||
|
||||
using sprout::fixed::bogo_sort;
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_ALGORITHM_FIXED_BOGO_SORT_HPP
|
118
sprout/algorithm/fixed/bogo_sort_result.hpp
Normal file
118
sprout/algorithm/fixed/bogo_sort_result.hpp
Normal file
|
@ -0,0 +1,118 @@
|
|||
#ifndef SPROUT_ALGORITHM_FIXED_BOGO_SORT_RESULT_HPP
|
||||
#define SPROUT_ALGORITHM_FIXED_BOGO_SORT_RESULT_HPP
|
||||
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/tuple/tuple.hpp>
|
||||
#include <sprout/fixed_container/traits.hpp>
|
||||
#include <sprout/fixed_container/functions.hpp>
|
||||
#include <sprout/iterator/operation.hpp>
|
||||
#include <sprout/utility/forward.hpp>
|
||||
#include <sprout/algorithm/fixed/result_of.hpp>
|
||||
#include <sprout/algorithm/fixed/shuffle.hpp>
|
||||
#include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT_DETAIL
|
||||
#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT_DETAIL
|
||||
|
||||
namespace sprout {
|
||||
namespace fixed {
|
||||
namespace detail {
|
||||
template<typename Container, typename UniformRandomNumberGenerator, typename Shuffled, typename Compare>
|
||||
SPROUT_CONSTEXPR inline sprout::tuples::tuple<
|
||||
typename sprout::fixed::result_of::algorithm<Container>::type,
|
||||
typename std::decay<UniformRandomNumberGenerator>::type
|
||||
> bogo_sort_result_impl_1(
|
||||
Shuffled const& shuffled,
|
||||
Compare comp
|
||||
)
|
||||
{
|
||||
return NS_SSCRISK_CEL_OR_SPROUT_DETAIL::is_sorted(
|
||||
sprout::begin(sprout::tuples::get<0>(shuffled)),
|
||||
sprout::end(sprout::tuples::get<0>(shuffled)),
|
||||
comp
|
||||
)
|
||||
? shuffled
|
||||
: sprout::fixed::detail::bogo_sort_result_impl_1<Container, UniformRandomNumberGenerator>(
|
||||
sprout::fixed::shuffle_result(
|
||||
sprout::tuples::get<0>(shuffled),
|
||||
sprout::tuples::get<1>(shuffled)
|
||||
),
|
||||
comp
|
||||
)
|
||||
;
|
||||
}
|
||||
template<typename Container, typename UniformRandomNumberGenerator, typename Compare>
|
||||
SPROUT_CONSTEXPR inline sprout::tuples::tuple<
|
||||
typename sprout::fixed::result_of::algorithm<Container>::type,
|
||||
typename std::decay<UniformRandomNumberGenerator>::type
|
||||
> bogo_sort_result_impl(
|
||||
Container const& cont,
|
||||
UniformRandomNumberGenerator&& g,
|
||||
Compare comp
|
||||
)
|
||||
{
|
||||
typedef sprout::tuples::tuple<
|
||||
typename sprout::fixed::result_of::algorithm<Container>::type,
|
||||
typename std::decay<UniformRandomNumberGenerator>::type
|
||||
> result_type;
|
||||
return NS_SSCRISK_CEL_OR_SPROUT_DETAIL::is_sorted(
|
||||
sprout::begin(cont),
|
||||
sprout::end(cont),
|
||||
comp
|
||||
)
|
||||
? result_type(
|
||||
sprout::clone(cont),
|
||||
sprout::forward<UniformRandomNumberGenerator>(g)
|
||||
)
|
||||
: sprout::fixed::detail::bogo_sort_result_impl_1<Container, UniformRandomNumberGenerator>(
|
||||
sprout::fixed::shuffle_result(
|
||||
cont,
|
||||
sprout::forward<UniformRandomNumberGenerator>(g)
|
||||
),
|
||||
comp
|
||||
)
|
||||
;
|
||||
}
|
||||
} // namespace detail
|
||||
//
|
||||
// bogo_sort_result
|
||||
//
|
||||
template<typename Container, typename UniformRandomNumberGenerator, typename Compare>
|
||||
SPROUT_CONSTEXPR inline sprout::tuples::tuple<
|
||||
typename sprout::fixed::result_of::algorithm<Container>::type,
|
||||
typename std::decay<UniformRandomNumberGenerator>::type
|
||||
> bogo_sort_result(
|
||||
Container const& cont,
|
||||
UniformRandomNumberGenerator&& g,
|
||||
Compare comp
|
||||
)
|
||||
{
|
||||
return sprout::fixed::detail::bogo_sort_result_impl(
|
||||
cont,
|
||||
sprout::forward<UniformRandomNumberGenerator>(g),
|
||||
comp
|
||||
);
|
||||
}
|
||||
//
|
||||
// bogo_sort_result
|
||||
//
|
||||
template<typename Container, typename UniformRandomNumberGenerator>
|
||||
SPROUT_CONSTEXPR inline sprout::tuples::tuple<
|
||||
typename sprout::fixed::result_of::algorithm<Container>::type,
|
||||
typename std::decay<UniformRandomNumberGenerator>::type
|
||||
> bogo_sort_result(
|
||||
Container const& cont,
|
||||
UniformRandomNumberGenerator&& g
|
||||
)
|
||||
{
|
||||
return sprout::fixed::detail::bogo_sort_result_impl(
|
||||
cont,
|
||||
sprout::forward<UniformRandomNumberGenerator>(g),
|
||||
NS_SSCRISK_CEL_OR_SPROUT_DETAIL::less<typename sprout::fixed_container_traits<Container>::value_type>()
|
||||
);
|
||||
}
|
||||
} // namespace fixed
|
||||
|
||||
using sprout::fixed::bogo_sort_result;
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_ALGORITHM_FIXED_BOGO_SORT_RESULT_HPP
|
|
@ -27,14 +27,14 @@ namespace sprout {
|
|||
std::ptrdiff_t i
|
||||
)
|
||||
{
|
||||
return i < n
|
||||
return i < n - 1
|
||||
? sprout::fixed::detail::make_shuffle_indexes_1(
|
||||
n,
|
||||
rnd(),
|
||||
sprout::fixed::swap_element(arr, arr.begin() + i, arr.begin() + rnd.result()),
|
||||
i + 1
|
||||
)
|
||||
: arr
|
||||
: sprout::fixed::swap_element(arr, arr.begin() + i, arr.begin() + rnd.result())
|
||||
;
|
||||
}
|
||||
template<std::size_t N, typename UniformRandomNumberGenerator>
|
||||
|
|
154
sprout/algorithm/fixed/shuffle_result.hpp
Normal file
154
sprout/algorithm/fixed/shuffle_result.hpp
Normal file
|
@ -0,0 +1,154 @@
|
|||
#ifndef SPROUT_ALGORITHM_FIXED_SHUFFLE_RESULT_HPP
|
||||
#define SPROUT_ALGORITHM_FIXED_SHUFFLE_RESULT_HPP
|
||||
|
||||
#include <cstddef>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/index_tuple.hpp>
|
||||
#include <sprout/array.hpp>
|
||||
#include <sprout/pit.hpp>
|
||||
#include <sprout/tuple/tuple.hpp>
|
||||
#include <sprout/fixed_container/traits.hpp>
|
||||
#include <sprout/fixed_container/functions.hpp>
|
||||
#include <sprout/iterator/operation.hpp>
|
||||
#include <sprout/utility/forward.hpp>
|
||||
#include <sprout/algorithm/fixed/result_of.hpp>
|
||||
#include <sprout/algorithm/fixed/swap_element.hpp>
|
||||
#include <sprout/numeric/fixed/iota.hpp>
|
||||
#include <sprout/random/uniform_int_distribution.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace fixed {
|
||||
namespace detail {
|
||||
template<typename UniformRandomNumberGenerator, std::size_t N, typename Random>
|
||||
SPROUT_CONSTEXPR inline sprout::tuples::tuple<
|
||||
sprout::array<std::ptrdiff_t, N>,
|
||||
typename std::decay<UniformRandomNumberGenerator>::type
|
||||
> make_shuffle_result_indexes_1(
|
||||
std::ptrdiff_t n,
|
||||
Random const& rnd,
|
||||
sprout::array<std::ptrdiff_t, N> const& arr,
|
||||
std::ptrdiff_t i
|
||||
)
|
||||
{
|
||||
typedef sprout::tuples::tuple<
|
||||
sprout::array<std::ptrdiff_t, N>,
|
||||
typename std::decay<UniformRandomNumberGenerator>::type
|
||||
> result_type;
|
||||
return i < n - 1
|
||||
? sprout::fixed::detail::make_shuffle_result_indexes_1<UniformRandomNumberGenerator>(
|
||||
n,
|
||||
rnd(),
|
||||
sprout::fixed::swap_element(arr, arr.begin() + i, arr.begin() + rnd.result()),
|
||||
i + 1
|
||||
)
|
||||
: result_type(
|
||||
sprout::fixed::swap_element(arr, arr.begin() + i, arr.begin() + rnd.result()),
|
||||
rnd.engine()
|
||||
)
|
||||
;
|
||||
}
|
||||
template<std::size_t N, typename UniformRandomNumberGenerator>
|
||||
SPROUT_CONSTEXPR inline sprout::tuples::tuple<
|
||||
sprout::array<std::ptrdiff_t, N>,
|
||||
typename std::decay<UniformRandomNumberGenerator>::type
|
||||
> make_shuffle_result_indexes(
|
||||
std::ptrdiff_t n,
|
||||
UniformRandomNumberGenerator&& g
|
||||
)
|
||||
{
|
||||
typedef sprout::tuples::tuple<
|
||||
sprout::array<std::ptrdiff_t, N>,
|
||||
typename std::decay<UniformRandomNumberGenerator>::type
|
||||
> result_type;
|
||||
return n > 0
|
||||
? sprout::fixed::detail::make_shuffle_result_indexes_1<UniformRandomNumberGenerator>(
|
||||
n,
|
||||
sprout::random::uniform_int_distribution<std::ptrdiff_t>(0, n - 1)(sprout::forward<UniformRandomNumberGenerator>(g)),
|
||||
sprout::fixed::iota(sprout::pit<sprout::array<std::ptrdiff_t, N> >(), 0),
|
||||
0
|
||||
)
|
||||
: result_type(
|
||||
sprout::array<std::ptrdiff_t, N>{{}},
|
||||
sprout::forward<UniformRandomNumberGenerator>(g)
|
||||
)
|
||||
;
|
||||
}
|
||||
template<typename UniformRandomNumberGenerator, typename Container, typename Shuffled, std::ptrdiff_t... Indexes>
|
||||
SPROUT_CONSTEXPR inline sprout::tuples::tuple<
|
||||
typename sprout::fixed::result_of::algorithm<Container>::type,
|
||||
typename std::decay<UniformRandomNumberGenerator>::type
|
||||
> shuffle_result_impl_1(
|
||||
Container const& cont,
|
||||
sprout::index_tuple<Indexes...>,
|
||||
Shuffled const& shuffled,
|
||||
typename sprout::fixed_container_traits<Container>::difference_type offset,
|
||||
typename sprout::fixed_container_traits<Container>::size_type size
|
||||
)
|
||||
{
|
||||
typedef sprout::tuples::tuple<
|
||||
typename sprout::fixed::result_of::algorithm<Container>::type,
|
||||
typename std::decay<UniformRandomNumberGenerator>::type
|
||||
> result_type;
|
||||
return result_type(
|
||||
sprout::remake_clone<Container>(
|
||||
cont,
|
||||
sprout::size(cont),
|
||||
(Indexes >= offset && Indexes < offset + size
|
||||
? *sprout::next(sprout::begin(cont), sprout::tuples::get<0>(shuffled)[Indexes - offset])
|
||||
: *sprout::next(sprout::fixed_begin(cont), Indexes)
|
||||
)...
|
||||
),
|
||||
sprout::tuples::get<1>(shuffled)
|
||||
);
|
||||
}
|
||||
template<typename Container, typename UniformRandomNumberGenerator, std::ptrdiff_t... Indexes>
|
||||
SPROUT_CONSTEXPR inline sprout::tuples::tuple<
|
||||
typename sprout::fixed::result_of::algorithm<Container>::type,
|
||||
typename std::decay<UniformRandomNumberGenerator>::type
|
||||
> shuffle_result_impl(
|
||||
Container const& cont,
|
||||
sprout::index_tuple<Indexes...> indexes,
|
||||
UniformRandomNumberGenerator&& g,
|
||||
typename sprout::fixed_container_traits<Container>::difference_type offset,
|
||||
typename sprout::fixed_container_traits<Container>::size_type size
|
||||
)
|
||||
{
|
||||
return sprout::fixed::detail::shuffle_result_impl_1<UniformRandomNumberGenerator>(
|
||||
cont,
|
||||
indexes,
|
||||
sprout::fixed::detail::make_shuffle_result_indexes<sprout::fixed_container_traits<Container>::fixed_size>(
|
||||
size,
|
||||
sprout::forward<UniformRandomNumberGenerator>(g)
|
||||
),
|
||||
offset,
|
||||
size
|
||||
);
|
||||
}
|
||||
} // namespace detail
|
||||
//
|
||||
// shuffle_result
|
||||
//
|
||||
template<typename Container, typename UniformRandomNumberGenerator>
|
||||
SPROUT_CONSTEXPR inline sprout::tuples::tuple<
|
||||
typename sprout::fixed::result_of::algorithm<Container>::type,
|
||||
typename std::decay<UniformRandomNumberGenerator>::type
|
||||
> shuffle_result(
|
||||
Container const& cont,
|
||||
UniformRandomNumberGenerator&& g
|
||||
)
|
||||
{
|
||||
return sprout::fixed::detail::shuffle_result_impl(
|
||||
cont,
|
||||
typename sprout::index_range<0, sprout::fixed_container_traits<Container>::fixed_size>::type(),
|
||||
sprout::forward<UniformRandomNumberGenerator>(g),
|
||||
sprout::fixed_begin_offset(cont),
|
||||
sprout::size(cont)
|
||||
);
|
||||
}
|
||||
} // namespace fixed
|
||||
|
||||
using sprout::fixed::shuffle_result;
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_ALGORITHM_FIXED_SHUFFLE_RESULT_HPP
|
8
sprout/algorithm/shuffle_result.hpp
Normal file
8
sprout/algorithm/shuffle_result.hpp
Normal file
|
@ -0,0 +1,8 @@
|
|||
#ifndef SPROUT_ALGORITHM_SHUFFLE_RESULT_HPP
|
||||
#define SPROUT_ALGORITHM_SHUFFLE_RESULT_HPP
|
||||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/algorithm/fixed/shuffle_result.hpp>
|
||||
#include <sprout/algorithm/fit/shuffle_result.hpp>
|
||||
|
||||
#endif // #ifndef SPROUT_ALGORITHM_SHUFFLE_RESULT_HPP
|
|
@ -141,6 +141,36 @@ namespace sprout {
|
|||
: sprout::detail::find(sprout::next(first), last, value)
|
||||
;
|
||||
}
|
||||
|
||||
//
|
||||
// is_sorted_until
|
||||
//
|
||||
template<typename InputIterator>
|
||||
SPROUT_CONSTEXPR InputIterator is_sorted_until(InputIterator first, InputIterator last) {
|
||||
return first == last || sprout::next(first) == last ? last
|
||||
: *sprout::next(first) < *first ? sprout::next(first)
|
||||
: sprout::detail::is_sorted_until(sprout::next(first), last)
|
||||
;
|
||||
}
|
||||
template<typename InputIterator, typename Compare>
|
||||
SPROUT_CONSTEXPR InputIterator is_sorted_until(InputIterator first, InputIterator last, Compare comp) {
|
||||
return first == last || sprout::next(first) == last ? last
|
||||
: comp(*sprout::next(first), *first) != false ? sprout::next(first)
|
||||
: sprout::detail::is_sorted_until(sprout::next(first), last)
|
||||
;
|
||||
}
|
||||
|
||||
//
|
||||
// is_sorted
|
||||
//
|
||||
template<typename InputIterator>
|
||||
SPROUT_CONSTEXPR bool is_sorted(InputIterator first, InputIterator last) {
|
||||
return sprout::detail::is_sorted_until(first, last) == last;
|
||||
}
|
||||
template<typename InputIterator, typename Compare>
|
||||
SPROUT_CONSTEXPR bool is_sorted(InputIterator first, InputIterator last, Compare comp) {
|
||||
return sprout::detail::is_sorted_until(first, last, comp) == last;
|
||||
}
|
||||
} // namespace detail
|
||||
} // namespace sprout
|
||||
|
||||
|
|
Loading…
Reference in a new issue