mirror of
https://github.com/bolero-MURAKAMI/Sprout.git
synced 2025-07-02 14:04:20 +00:00
add next_permutatin, prev_premutation
This commit is contained in:
parent
e3511d71b0
commit
a64938fad0
12 changed files with 668 additions and 1 deletions
|
@ -46,6 +46,8 @@
|
|||
#include <sprout/algorithm/fit/make_heap.hpp>
|
||||
#include <sprout/algorithm/fit/make_partial_heap.hpp>
|
||||
#include <sprout/algorithm/fit/sort_heap.hpp>
|
||||
#include <sprout/algorithm/fit/next_permutation.hpp>
|
||||
#include <sprout/algorithm/fit/prev_permutation.hpp>
|
||||
#include <sprout/algorithm/fit/swap_element.hpp>
|
||||
#include <sprout/algorithm/fit/swap_element_copy.hpp>
|
||||
#include <sprout/algorithm/fit/bogo_sort.hpp>
|
||||
|
|
82
sprout/algorithm/fit/next_permutation.hpp
Normal file
82
sprout/algorithm/fit/next_permutation.hpp
Normal file
|
@ -0,0 +1,82 @@
|
|||
#ifndef SPROUT_ALGORITHM_FIT_NEXT_PERMUTATION_HPP
|
||||
#define SPROUT_ALGORITHM_FIT_NEXT_PERMUTATION_HPP
|
||||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/container/traits.hpp>
|
||||
#include <sprout/container/functions.hpp>
|
||||
#include <sprout/algorithm/fixed/next_permutation.hpp>
|
||||
#include <sprout/algorithm/fit/result_of.hpp>
|
||||
#include <sprout/sub_array.hpp>
|
||||
#include <sprout/utility/pair.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace fit {
|
||||
namespace detail {
|
||||
template<typename Container, typename Permutation>
|
||||
inline SPROUT_CONSTEXPR sprout::pair<typename sprout::fit::result_of::algorithm<Container>::type, bool>
|
||||
next_permutation_impl_1(
|
||||
Container const& cont,
|
||||
Permutation const& perm,
|
||||
typename sprout::container_traits<Container>::difference_type offset
|
||||
)
|
||||
{
|
||||
return sprout::pair<typename sprout::fit::result_of::algorithm<Container>::type, bool>(
|
||||
sprout::sub_copy(
|
||||
sprout::get_internal(perm.first),
|
||||
offset,
|
||||
offset + sprout::size(cont)
|
||||
),
|
||||
perm.second
|
||||
);
|
||||
}
|
||||
|
||||
template<typename Container, typename Compare>
|
||||
inline SPROUT_CONSTEXPR sprout::pair<typename sprout::fit::result_of::algorithm<Container>::type, bool>
|
||||
next_permutation_impl(
|
||||
Container const& cont,
|
||||
Compare comp,
|
||||
typename sprout::container_traits<Container>::difference_type offset
|
||||
)
|
||||
{
|
||||
return next_permutation_impl_1(cont, sprout::fixed::next_permutation(cont, comp), offset);
|
||||
}
|
||||
} // namespace detail
|
||||
//
|
||||
// next_permutation
|
||||
//
|
||||
template<typename Container, typename Compare>
|
||||
inline SPROUT_CONSTEXPR sprout::pair<typename sprout::fit::result_of::algorithm<Container>::type, bool>
|
||||
next_permutation(
|
||||
Container const& cont,
|
||||
Compare comp
|
||||
)
|
||||
{
|
||||
return sprout::fit::detail::next_permutation_impl(cont, comp, sprout::internal_begin_offset(cont));
|
||||
}
|
||||
|
||||
namespace detail {
|
||||
template<typename Container>
|
||||
inline SPROUT_CONSTEXPR sprout::pair<typename sprout::fit::result_of::algorithm<Container>::type, bool>
|
||||
next_permutation_impl(
|
||||
Container const& cont,
|
||||
typename sprout::container_traits<Container>::difference_type offset
|
||||
)
|
||||
{
|
||||
return next_permutation_impl_1(cont, sprout::fixed::next_permutation(cont), offset);
|
||||
}
|
||||
} // namespace detail
|
||||
//
|
||||
// next_permutation
|
||||
//
|
||||
template<typename Container>
|
||||
inline SPROUT_CONSTEXPR sprout::pair<typename sprout::fit::result_of::algorithm<Container>::type, bool>
|
||||
next_permutation(
|
||||
Container const& cont
|
||||
)
|
||||
{
|
||||
return sprout::fit::detail::next_permutation_impl(cont, sprout::internal_begin_offset(cont));
|
||||
}
|
||||
} // namespace fit
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_ALGORITHM_FIT_NEXT_PERMUTATION_HPP
|
82
sprout/algorithm/fit/prev_permutation.hpp
Normal file
82
sprout/algorithm/fit/prev_permutation.hpp
Normal file
|
@ -0,0 +1,82 @@
|
|||
#ifndef SPROUT_ALGORITHM_FIT_PREV_PERMUTATION_HPP
|
||||
#define SPROUT_ALGORITHM_FIT_PREV_PERMUTATION_HPP
|
||||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/container/traits.hpp>
|
||||
#include <sprout/container/functions.hpp>
|
||||
#include <sprout/algorithm/fixed/prev_permutation.hpp>
|
||||
#include <sprout/algorithm/fit/result_of.hpp>
|
||||
#include <sprout/sub_array.hpp>
|
||||
#include <sprout/utility/pair.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace fit {
|
||||
namespace detail {
|
||||
template<typename Container, typename Permutation>
|
||||
inline SPROUT_CONSTEXPR sprout::pair<typename sprout::fit::result_of::algorithm<Container>::type, bool>
|
||||
prev_permutation_impl_1(
|
||||
Container const& cont,
|
||||
Permutation const& perm,
|
||||
typename sprout::container_traits<Container>::difference_type offset
|
||||
)
|
||||
{
|
||||
return sprout::pair<typename sprout::fit::result_of::algorithm<Container>::type, bool>(
|
||||
sprout::sub_copy(
|
||||
sprout::get_internal(perm.first),
|
||||
offset,
|
||||
offset + sprout::size(cont)
|
||||
),
|
||||
perm.second
|
||||
);
|
||||
}
|
||||
|
||||
template<typename Container, typename Compare>
|
||||
inline SPROUT_CONSTEXPR sprout::pair<typename sprout::fit::result_of::algorithm<Container>::type, bool>
|
||||
prev_permutation_impl(
|
||||
Container const& cont,
|
||||
Compare comp,
|
||||
typename sprout::container_traits<Container>::difference_type offset
|
||||
)
|
||||
{
|
||||
return prev_permutation_impl_1(cont, sprout::fixed::prev_permutation(cont, comp), offset);
|
||||
}
|
||||
} // namespace detail
|
||||
//
|
||||
// prev_permutation
|
||||
//
|
||||
template<typename Container, typename Compare>
|
||||
inline SPROUT_CONSTEXPR sprout::pair<typename sprout::fit::result_of::algorithm<Container>::type, bool>
|
||||
prev_permutation(
|
||||
Container const& cont,
|
||||
Compare comp
|
||||
)
|
||||
{
|
||||
return sprout::fit::detail::prev_permutation_impl(cont, comp, sprout::internal_begin_offset(cont));
|
||||
}
|
||||
|
||||
namespace detail {
|
||||
template<typename Container>
|
||||
inline SPROUT_CONSTEXPR sprout::pair<typename sprout::fit::result_of::algorithm<Container>::type, bool>
|
||||
prev_permutation_impl(
|
||||
Container const& cont,
|
||||
typename sprout::container_traits<Container>::difference_type offset
|
||||
)
|
||||
{
|
||||
return prev_permutation_impl_1(cont, sprout::fixed::prev_permutation(cont), offset);
|
||||
}
|
||||
} // namespace detail
|
||||
//
|
||||
// prev_permutation
|
||||
//
|
||||
template<typename Container>
|
||||
inline SPROUT_CONSTEXPR sprout::pair<typename sprout::fit::result_of::algorithm<Container>::type, bool>
|
||||
prev_permutation(
|
||||
Container const& cont
|
||||
)
|
||||
{
|
||||
return sprout::fit::detail::prev_permutation_impl(cont, sprout::internal_begin_offset(cont));
|
||||
}
|
||||
} // namespace fit
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_ALGORITHM_FIT_PREV_PERMUTATION_HPP
|
|
@ -46,6 +46,8 @@
|
|||
#include <sprout/algorithm/fixed/make_heap.hpp>
|
||||
#include <sprout/algorithm/fixed/make_partial_heap.hpp>
|
||||
#include <sprout/algorithm/fixed/sort_heap.hpp>
|
||||
#include <sprout/algorithm/fixed/next_permutation.hpp>
|
||||
#include <sprout/algorithm/fixed/prev_permutation.hpp>
|
||||
#include <sprout/algorithm/fixed/swap_element.hpp>
|
||||
#include <sprout/algorithm/fixed/swap_element_copy.hpp>
|
||||
#include <sprout/algorithm/fixed/bogo_sort.hpp>
|
||||
|
|
103
sprout/algorithm/fixed/next_permutation.hpp
Normal file
103
sprout/algorithm/fixed/next_permutation.hpp
Normal file
|
@ -0,0 +1,103 @@
|
|||
#ifndef SPROUT_ALGORITHM_FIXED_NEXT_PERMUTATION_HPP
|
||||
#define SPROUT_ALGORITHM_FIXED_NEXT_PERMUTATION_HPP
|
||||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/container/traits.hpp>
|
||||
#include <sprout/container/functions.hpp>
|
||||
#include <sprout/iterator/operation.hpp>
|
||||
#include <sprout/algorithm/fixed/result_of.hpp>
|
||||
#include <sprout/algorithm/fixed/swap_element.hpp>
|
||||
#include <sprout/algorithm/fixed/reverse.hpp>
|
||||
#include <sprout/sub_array.hpp>
|
||||
#include <sprout/utility/pair.hpp>
|
||||
#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT
|
||||
#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT
|
||||
|
||||
namespace sprout {
|
||||
namespace fixed {
|
||||
namespace detail {
|
||||
template<typename Result, typename Container, typename Difference>
|
||||
inline SPROUT_CONSTEXPR Result
|
||||
next_permutation_impl_4(Container const& cont, Difference d) {
|
||||
return Result(
|
||||
sprout::get_internal(sprout::fixed::reverse(sprout::sub_array<Container const&>(cont, d, sprout::size(cont)))),
|
||||
true
|
||||
);
|
||||
}
|
||||
template<typename Result, typename Container, typename Compare, typename Iterator>
|
||||
inline SPROUT_CONSTEXPR Result
|
||||
next_permutation_impl_3(Container const& cont, Compare comp, Iterator first, Iterator last, Iterator i, Iterator ii, Iterator j) {
|
||||
return !comp(*i, *sprout::prev(j)) ? sprout::fixed::detail::next_permutation_impl_3<Result>(
|
||||
cont, comp, first, last,
|
||||
i, ii, sprout::prev(j)
|
||||
)
|
||||
: sprout::fixed::detail::next_permutation_impl_4<Result>(
|
||||
sprout::fixed::swap_element(cont, i, sprout::prev(j)),
|
||||
NS_SSCRISK_CEL_OR_SPROUT::distance(first, ii)
|
||||
)
|
||||
;
|
||||
}
|
||||
template<typename Result, typename Container, typename Compare, typename Iterator>
|
||||
inline SPROUT_CONSTEXPR Result
|
||||
next_permutation_impl_2(Container const& cont, Compare comp, Iterator first, Iterator last, Iterator i, Iterator ii) {
|
||||
return comp(*i, *ii) ? sprout::fixed::detail::next_permutation_impl_3<Result>(
|
||||
cont, comp, first, last,
|
||||
i, ii, last
|
||||
)
|
||||
: i == first ? Result(sprout::fixed::reverse_copy(first, last, cont), false)
|
||||
: sprout::fixed::detail::next_permutation_impl_2<Result>(
|
||||
cont, comp, first, last,
|
||||
sprout::prev(i), i
|
||||
)
|
||||
;
|
||||
}
|
||||
template<typename Result, typename Container, typename Compare, typename Iterator>
|
||||
inline SPROUT_CONSTEXPR Result
|
||||
next_permutation_impl_1(Container const& cont, Compare comp, Iterator first, Iterator last, Iterator i) {
|
||||
return i == last ? Result(sprout::deep_copy(cont), false)
|
||||
: sprout::fixed::detail::next_permutation_impl_2<Result>(
|
||||
cont, comp, first, last,
|
||||
sprout::prev(last, 2), sprout::prev(last)
|
||||
)
|
||||
;
|
||||
}
|
||||
template<typename Result, typename Container, typename Compare, typename Iterator>
|
||||
inline SPROUT_CONSTEXPR Result
|
||||
next_permutation_impl(Container const& cont, Compare comp, Iterator first, Iterator last) {
|
||||
return first == last ? Result(sprout::deep_copy(cont), false)
|
||||
: sprout::fixed::detail::next_permutation_impl_1<Result>(
|
||||
cont, comp, first, last,
|
||||
sprout::next(first)
|
||||
);
|
||||
}
|
||||
} // namespace detail
|
||||
//
|
||||
// next_permutation
|
||||
//
|
||||
template<typename Container, typename Compare>
|
||||
inline SPROUT_CONSTEXPR sprout::pair<typename sprout::fixed::result_of::algorithm<Container>::type, bool>
|
||||
next_permutation(Container const& cont, Compare comp) {
|
||||
typedef sprout::pair<typename sprout::fixed::result_of::algorithm<Container>::type, bool> type;
|
||||
return sprout::fixed::detail::next_permutation_impl<type>(
|
||||
cont, comp,
|
||||
sprout::begin(cont), sprout::end(cont)
|
||||
);
|
||||
}
|
||||
//
|
||||
// next_permutation
|
||||
//
|
||||
template<typename Container>
|
||||
inline SPROUT_CONSTEXPR sprout::pair<typename sprout::fixed::result_of::algorithm<Container>::type, bool>
|
||||
next_permutation(Container const& cont) {
|
||||
typedef sprout::pair<typename sprout::fixed::result_of::algorithm<Container>::type, bool> type;
|
||||
return sprout::fixed::detail::next_permutation_impl<type>(
|
||||
cont, NS_SSCRISK_CEL_OR_SPROUT::less<typename sprout::container_traits<Container>::value_type>(),
|
||||
sprout::begin(cont), sprout::end(cont)
|
||||
);
|
||||
}
|
||||
} // namespace fixed
|
||||
|
||||
using sprout::fixed::next_permutation;
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_ALGORITHM_FIXED_NEXT_PERMUTATION_HPP
|
103
sprout/algorithm/fixed/prev_permutation.hpp
Normal file
103
sprout/algorithm/fixed/prev_permutation.hpp
Normal file
|
@ -0,0 +1,103 @@
|
|||
#ifndef SPROUT_ALGORITHM_FIXED_PREV_PERMUTATION_HPP
|
||||
#define SPROUT_ALGORITHM_FIXED_PREV_PERMUTATION_HPP
|
||||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/container/traits.hpp>
|
||||
#include <sprout/container/functions.hpp>
|
||||
#include <sprout/iterator/operation.hpp>
|
||||
#include <sprout/algorithm/fixed/result_of.hpp>
|
||||
#include <sprout/algorithm/fixed/swap_element.hpp>
|
||||
#include <sprout/algorithm/fixed/reverse.hpp>
|
||||
#include <sprout/sub_array.hpp>
|
||||
#include <sprout/utility/pair.hpp>
|
||||
#include HDR_FUNCTIONAL_SSCRISK_CEL_OR_SPROUT
|
||||
#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT
|
||||
|
||||
namespace sprout {
|
||||
namespace fixed {
|
||||
namespace detail {
|
||||
template<typename Result, typename Container, typename Difference>
|
||||
inline SPROUT_CONSTEXPR Result
|
||||
prev_permutation_impl_4(Container const& cont, Difference d) {
|
||||
return Result(
|
||||
sprout::get_internal(sprout::fixed::reverse(sprout::sub_array<Container const&>(cont, d, sprout::size(cont)))),
|
||||
true
|
||||
);
|
||||
}
|
||||
template<typename Result, typename Container, typename Compare, typename Iterator>
|
||||
inline SPROUT_CONSTEXPR Result
|
||||
prev_permutation_impl_3(Container const& cont, Compare comp, Iterator first, Iterator last, Iterator i, Iterator ii, Iterator j) {
|
||||
return !comp(*sprout::prev(j), *i) ? sprout::fixed::detail::prev_permutation_impl_3<Result>(
|
||||
cont, comp, first, last,
|
||||
i, ii, sprout::prev(j)
|
||||
)
|
||||
: sprout::fixed::detail::prev_permutation_impl_4<Result>(
|
||||
sprout::fixed::swap_element(cont, i, sprout::prev(j)),
|
||||
NS_SSCRISK_CEL_OR_SPROUT::distance(first, ii)
|
||||
)
|
||||
;
|
||||
}
|
||||
template<typename Result, typename Container, typename Compare, typename Iterator>
|
||||
inline SPROUT_CONSTEXPR Result
|
||||
prev_permutation_impl_2(Container const& cont, Compare comp, Iterator first, Iterator last, Iterator i, Iterator ii) {
|
||||
return comp(*ii, *i) ? sprout::fixed::detail::prev_permutation_impl_3<Result>(
|
||||
cont, comp, first, last,
|
||||
i, ii, last
|
||||
)
|
||||
: i == first ? Result(sprout::fixed::reverse_copy(first, last, cont), false)
|
||||
: sprout::fixed::detail::prev_permutation_impl_2<Result>(
|
||||
cont, comp, first, last,
|
||||
sprout::prev(i), i
|
||||
)
|
||||
;
|
||||
}
|
||||
template<typename Result, typename Container, typename Compare, typename Iterator>
|
||||
inline SPROUT_CONSTEXPR Result
|
||||
prev_permutation_impl_1(Container const& cont, Compare comp, Iterator first, Iterator last, Iterator i) {
|
||||
return i == last ? Result(sprout::deep_copy(cont), false)
|
||||
: sprout::fixed::detail::prev_permutation_impl_2<Result>(
|
||||
cont, comp, first, last,
|
||||
sprout::prev(last, 2), sprout::prev(last)
|
||||
)
|
||||
;
|
||||
}
|
||||
template<typename Result, typename Container, typename Compare, typename Iterator>
|
||||
inline SPROUT_CONSTEXPR Result
|
||||
prev_permutation_impl(Container const& cont, Compare comp, Iterator first, Iterator last) {
|
||||
return first == last ? Result(sprout::deep_copy(cont), false)
|
||||
: sprout::fixed::detail::prev_permutation_impl_1<Result>(
|
||||
cont, comp, first, last,
|
||||
sprout::next(first)
|
||||
);
|
||||
}
|
||||
} // namespace detail
|
||||
//
|
||||
// prev_permutation
|
||||
//
|
||||
template<typename Container, typename Compare>
|
||||
inline SPROUT_CONSTEXPR sprout::pair<typename sprout::fixed::result_of::algorithm<Container>::type, bool>
|
||||
prev_permutation(Container const& cont, Compare comp) {
|
||||
typedef sprout::pair<typename sprout::fixed::result_of::algorithm<Container>::type, bool> type;
|
||||
return sprout::fixed::detail::prev_permutation_impl<type>(
|
||||
cont, comp,
|
||||
sprout::begin(cont), sprout::end(cont)
|
||||
);
|
||||
}
|
||||
//
|
||||
// prev_permutation
|
||||
//
|
||||
template<typename Container>
|
||||
inline SPROUT_CONSTEXPR sprout::pair<typename sprout::fixed::result_of::algorithm<Container>::type, bool>
|
||||
prev_permutation(Container const& cont) {
|
||||
typedef sprout::pair<typename sprout::fixed::result_of::algorithm<Container>::type, bool> type;
|
||||
return sprout::fixed::detail::prev_permutation_impl<type>(
|
||||
cont, NS_SSCRISK_CEL_OR_SPROUT::less<typename sprout::container_traits<Container>::value_type>(),
|
||||
sprout::begin(cont), sprout::end(cont)
|
||||
);
|
||||
}
|
||||
} // namespace fixed
|
||||
|
||||
using sprout::fixed::prev_permutation;
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_ALGORITHM_FIXED_PREV_PERMUTATION_HPP
|
8
sprout/algorithm/next_permutation.hpp
Normal file
8
sprout/algorithm/next_permutation.hpp
Normal file
|
@ -0,0 +1,8 @@
|
|||
#ifndef SPROUT_ALGORITHM_NEXT_PERMUTATION_HPP
|
||||
#define SPROUT_ALGORITHM_NEXT_PERMUTATION_HPP
|
||||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/algorithm/fixed/next_permutation.hpp>
|
||||
#include <sprout/algorithm/fit/next_permutation.hpp>
|
||||
|
||||
#endif // #ifndef SPROUT_ALGORITHM_NEXT_PERMUTATION_HPP
|
8
sprout/algorithm/prev_permutation.hpp
Normal file
8
sprout/algorithm/prev_permutation.hpp
Normal file
|
@ -0,0 +1,8 @@
|
|||
#ifndef SPROUT_ALGORITHM_PREV_PERMUTATION_HPP
|
||||
#define SPROUT_ALGORITHM_PREV_PERMUTATION_HPP
|
||||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/algorithm/fixed/prev_permutation.hpp>
|
||||
#include <sprout/algorithm/fit/prev_permutation.hpp>
|
||||
|
||||
#endif // #ifndef SPROUT_ALGORITHM_PREV_PERMUTATION_HPP
|
Loading…
Add table
Add a link
Reference in a new issue