2013-08-08 09:54:33 +00:00
|
|
|
/*=============================================================================
|
|
|
|
Copyright (c) 2011-2013 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)
|
|
|
|
=============================================================================*/
|
2012-07-25 05:27:01 +00:00
|
|
|
#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>
|
2013-11-20 13:04:11 +00:00
|
|
|
#include <sprout/algorithm/fixed/results.hpp>
|
2012-07-25 05:27:01 +00:00
|
|
|
#include <sprout/algorithm/fixed/swap_element.hpp>
|
2013-08-11 15:09:40 +00:00
|
|
|
#include <sprout/algorithm/fixed/reverse_copy.hpp>
|
2012-07-25 05:27:01 +00:00
|
|
|
#include <sprout/algorithm/fixed/reverse.hpp>
|
2013-08-08 09:54:33 +00:00
|
|
|
#include <sprout/sub_array/sub_array.hpp>
|
2013-02-07 14:12:57 +00:00
|
|
|
#include <sprout/utility/pair/pair.hpp>
|
2012-07-25 05:27:01 +00:00
|
|
|
#include HDR_FUNCTIONAL_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
|
|
|
|
);
|
|
|
|
}
|
2013-08-07 13:13:03 +00:00
|
|
|
template<typename Result, typename Container, typename Compare, typename BidirectionalIterator>
|
2012-07-25 05:27:01 +00:00
|
|
|
inline SPROUT_CONSTEXPR Result
|
2013-08-07 13:13:03 +00:00
|
|
|
prev_permutation_impl_3(
|
|
|
|
Container const& cont, Compare comp,
|
|
|
|
BidirectionalIterator first, BidirectionalIterator last,
|
|
|
|
BidirectionalIterator i, BidirectionalIterator ii, BidirectionalIterator j
|
|
|
|
)
|
|
|
|
{
|
2012-07-25 05:27:01 +00:00
|
|
|
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)),
|
2013-01-03 08:01:50 +00:00
|
|
|
sprout::distance(first, ii)
|
2012-07-25 05:27:01 +00:00
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
2013-08-07 13:13:03 +00:00
|
|
|
template<typename Result, typename Container, typename Compare, typename BidirectionalIterator>
|
2012-07-25 05:27:01 +00:00
|
|
|
inline SPROUT_CONSTEXPR Result
|
2013-08-07 13:13:03 +00:00
|
|
|
prev_permutation_impl_2(
|
|
|
|
Container const& cont, Compare comp,
|
|
|
|
BidirectionalIterator first, BidirectionalIterator last,
|
|
|
|
BidirectionalIterator i, BidirectionalIterator ii
|
|
|
|
)
|
|
|
|
{
|
2012-07-25 05:27:01 +00:00
|
|
|
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
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
2013-08-07 13:13:03 +00:00
|
|
|
template<typename Result, typename Container, typename Compare, typename BidirectionalIterator>
|
2012-07-25 05:27:01 +00:00
|
|
|
inline SPROUT_CONSTEXPR Result
|
2013-08-07 13:13:03 +00:00
|
|
|
prev_permutation_impl_1(Container const& cont, Compare comp, BidirectionalIterator first, BidirectionalIterator last, BidirectionalIterator i) {
|
2012-07-25 05:27:01 +00:00
|
|
|
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)
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
2013-08-07 13:13:03 +00:00
|
|
|
template<typename Result, typename Container, typename Compare, typename BidirectionalIterator>
|
2012-07-25 05:27:01 +00:00
|
|
|
inline SPROUT_CONSTEXPR Result
|
2013-08-07 13:13:03 +00:00
|
|
|
prev_permutation_impl(Container const& cont, Compare comp, BidirectionalIterator first, BidirectionalIterator last) {
|
2012-07-25 05:27:01 +00:00
|
|
|
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>
|
2013-11-20 13:04:11 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::pair<typename sprout::fixed::results::algorithm<Container>::type, bool>
|
2012-07-25 05:27:01 +00:00
|
|
|
prev_permutation(Container const& cont, Compare comp) {
|
2013-11-20 13:04:11 +00:00
|
|
|
typedef sprout::pair<typename sprout::fixed::results::algorithm<Container>::type, bool> type;
|
2012-07-25 05:27:01 +00:00
|
|
|
return sprout::fixed::detail::prev_permutation_impl<type>(
|
|
|
|
cont, comp,
|
|
|
|
sprout::begin(cont), sprout::end(cont)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// prev_permutation
|
|
|
|
//
|
|
|
|
template<typename Container>
|
2013-11-20 13:04:11 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::pair<typename sprout::fixed::results::algorithm<Container>::type, bool>
|
2012-07-25 05:27:01 +00:00
|
|
|
prev_permutation(Container const& cont) {
|
2013-11-20 13:04:11 +00:00
|
|
|
typedef sprout::pair<typename sprout::fixed::results::algorithm<Container>::type, bool> type;
|
2012-07-25 05:27:01 +00:00
|
|
|
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
|