add tristate_lexicographical_compare

This commit is contained in:
bolero-MURAKAMI 2012-12-21 23:12:54 +09:00
parent 73cdad232b
commit ddac080ec0
7 changed files with 167 additions and 20 deletions

View file

@ -4,6 +4,7 @@
#include <iterator>
#include <sprout/config.hpp>
#include <sprout/iterator/operation.hpp>
#include <sprout/iterator/type_traits/common.hpp>
#include <sprout/functional/equal_to.hpp>
#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT

View file

@ -4,6 +4,7 @@
#include <iterator>
#include <sprout/config.hpp>
#include <sprout/iterator/operation.hpp>
#include <sprout/iterator/type_traits/common.hpp>
#include <sprout/functional/equal_to.hpp>
#include <sprout/functional/bind2nd.hpp>
#include <sprout/algorithm/count_if.hpp>

View file

@ -3,6 +3,7 @@
#include <sprout/config.hpp>
#include <sprout/iterator/operation.hpp>
#include <sprout/iterator/type_traits/common.hpp>
#include <sprout/utility/pair.hpp>
#include <sprout/algorithm/min.hpp>
#include <sprout/functional/less.hpp>

View file

@ -41,6 +41,7 @@
#include <sprout/algorithm/any_of_equal.hpp>
#include <sprout/algorithm/none_of_equal.hpp>
#include <sprout/algorithm/one_of_equal.hpp>
#include <sprout/algorithm/tristate_lexicographical_compare.hpp>
#include <sprout/algorithm/is_increasing.hpp>
#include <sprout/algorithm/is_decreasing.hpp>
#include <sprout/algorithm/is_strictly_increasing.hpp>

View file

@ -0,0 +1,135 @@
#ifndef SPROUT_ALGORITHM_TRISTATE_LEXICOGRAPHICAL_COMPARE_HPP
#define SPROUT_ALGORITHM_TRISTATE_LEXICOGRAPHICAL_COMPARE_HPP
#include <sprout/config.hpp>
#include <sprout/iterator/operation.hpp>
#include <sprout/iterator/type_traits/common.hpp>
#include <sprout/utility/pair.hpp>
#include <sprout/algorithm/min.hpp>
#include <sprout/functional/less.hpp>
#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT
namespace sprout {
namespace detail {
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename Compare>
inline SPROUT_CONSTEXPR int
tristate_lexicographical_compare_impl_ra_2(
RandomAccessIterator1 last1, RandomAccessIterator2 last2, Compare comp,
sprout::pair<RandomAccessIterator1, RandomAccessIterator2> const& found
)
{
return found.second == last2 ? (found.first == last1 ? 0 : 1)
: found.first == last1 ? -1
: comp(*found.first, *found.second) ? -1
: comp(*found.second, *found.first) ? 1
: 0
;
}
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename Compare>
inline SPROUT_CONSTEXPR sprout::pair<RandomAccessIterator1, RandomAccessIterator2>
tristate_lexicographical_compare_impl_ra_1(
RandomAccessIterator1 first1, RandomAccessIterator1 last1, RandomAccessIterator2 first2, RandomAccessIterator2 last2, Compare comp,
typename std::iterator_traits<RandomAccessIterator1>::difference_type pivot, sprout::pair<RandomAccessIterator1, RandomAccessIterator2> const& found
)
{
typedef sprout::pair<RandomAccessIterator1, RandomAccessIterator2> found_type;
return found.first != first1 ? found
: pivot == 0 ? (comp(*first1, *first2) || comp(*first2, *first1) ? found_type(first1, first2) : found_type(last1, last2))
: sprout::detail::tristate_lexicographical_compare_impl_ra_1(
sprout::next(first1, pivot), last1, sprout::next(first2, pivot), last2, comp,
(NS_SSCRISK_CEL_OR_SPROUT::distance(first1, last1) - pivot) / 2,
sprout::detail::tristate_lexicographical_compare_impl_ra_1(
first1, sprout::next(first1, pivot), first2, sprout::next(first2, pivot), comp,
pivot / 2,
found_type(first1, first2)
)
)
;
}
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename Compare>
inline SPROUT_CONSTEXPR int
tristate_lexicographical_compare_impl_ra(
RandomAccessIterator1 first1, RandomAccessIterator1 last1, RandomAccessIterator2 first2, RandomAccessIterator2 last2, Compare comp,
typename std::iterator_traits<RandomAccessIterator1>::difference_type size
)
{
typedef sprout::pair<RandomAccessIterator1, RandomAccessIterator2> found_type;
return sprout::detail::tristate_lexicographical_compare_impl_ra_2(
last1, last2, comp,
sprout::detail::tristate_lexicographical_compare_impl_ra_1(
first1, sprout::next(first1, size), first2, sprout::next(first2, size), comp,
size / 2, found_type(first1, first2)
)
);
}
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename Compare>
inline SPROUT_CONSTEXPR int
tristate_lexicographical_compare(
RandomAccessIterator1 first1, RandomAccessIterator1 last1, RandomAccessIterator2 first2, RandomAccessIterator2 last2, Compare comp,
std::random_access_iterator_tag*
)
{
return first2 == last2 ? (first1 == last1 ? 0 : 1)
: first1 == last1 ? -1
: sprout::detail::tristate_lexicographical_compare_impl_ra(
first1, last1, first2, last2, comp,
sprout::min(NS_SSCRISK_CEL_OR_SPROUT::distance(first1, last1), NS_SSCRISK_CEL_OR_SPROUT::distance(first2, last2))
)
;
}
// Copyright (C) 2011 RiSK (sscrisk)
template<typename InputIterator1, typename InputIterator2, typename Compare>
inline SPROUT_CONSTEXPR int
tristate_lexicographical_compare_impl(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp) {
return first2 == last2 ? (first1 == last1 ? 0 : 1)
: first1 == last1 || comp(*first1, *first2) ? -1
: comp(*first2, *first1) ? 1
: sprout::detail::tristate_lexicographical_compare_impl(sprout::next(first1), last1, sprout::next(first2), last2, comp)
;
}
template<typename InputIterator1, typename InputIterator2, typename Compare>
inline SPROUT_CONSTEXPR int
tristate_lexicographical_compare(
InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp,
void*
)
{
return sprout::detail::tristate_lexicographical_compare_impl(first1, last1, first2, last2, comp);
}
} //namespace detail
//
// tristate_lexicographical_compare
//
// recursion depth:
// [first1, last1), [first2, last2) are RandomAccessIterator -> O(log N)
// otherwise -> O(N)
//
template<typename InputIterator1, typename InputIterator2, typename Compare>
inline SPROUT_CONSTEXPR int
tristate_lexicographical_compare(
InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
Compare comp
)
{
typedef typename sprout::common_iterator_category<InputIterator1, InputIterator2>::type* category;
return sprout::detail::tristate_lexicographical_compare(first1, last1, first2, last2, comp, category());
}
template<typename InputIterator1, typename InputIterator2>
inline SPROUT_CONSTEXPR int
tristate_lexicographical_compare(
InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2
)
{
return sprout::tristate_lexicographical_compare(
first1, last1, first2, last2,
sprout::less<>()
);
}
} // namespace sprout
#endif // #ifndef SPROUT_ALGORITHM_TRISTATE_LEXICOGRAPHICAL_COMPARE_HPP

View file

@ -4,6 +4,7 @@
#include <iterator>
#include <sprout/config.hpp>
#include <sprout/iterator/operation.hpp>
#include <sprout/iterator/type_traits/common.hpp>
#include <sprout/functional/equal_to.hpp>
#include <sprout/algorithm/mismatch.hpp>
#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT

View file

@ -7,6 +7,7 @@
#include <sprout/config.hpp>
#include <sprout/functional/bind2nd.hpp>
#include <sprout/iterator/ptr_index_iterator.hpp>
#include <sprout/algorithm/tristate_lexicographical_compare.hpp>
#include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT
#include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT
@ -31,6 +32,12 @@ namespace sprout {
return eq(c1, c2);
}
};
struct lt_ {
public:
SPROUT_CONSTEXPR bool operator()(char_type c1, char_type c2) const SPROUT_NOEXCEPT {
return lt(c1, c2);
}
};
private:
static SPROUT_CONSTEXPR char_type const* find_impl(char_type const* found, char_type const* last) {
return found == last ? nullptr
@ -48,11 +55,11 @@ namespace sprout {
return impl_type::lt(c1, c2);
}
static SPROUT_CONSTEXPR int compare(char_type const* s1, char_type const* s2, std::size_t n) {
return !n ? 0
: lt(*s1, *s2) ? -1
: lt(*s2, *s1) ? 1
: compare(s1 + 1, s2 + 1, n - 1)
;
return sprout::tristate_lexicographical_compare(
sprout::as_iterator(s1), sprout::as_iterator(s1, n),
sprout::as_iterator(s2), sprout::as_iterator(s2, n),
lt_()
);
}
static SPROUT_CONSTEXPR std::size_t length(char_type const* s) {
return !*s ? 0
@ -97,27 +104,27 @@ namespace sprout {
#if SPROUT_USE_INDEX_ITERATOR_IMPLEMENTATION
template<typename ConstIterator>
static SPROUT_CONSTEXPR int compare(char_type const* s1, ConstIterator s2, std::size_t n) {
return !n ? 0
: lt(*s1, *s2) ? -1
: lt(*s2, *s1) ? 1
: compare(s1 + 1, s2 + 1, n - 1)
;
return sprout::tristate_lexicographical_compare(
sprout::as_iterator(s1), sprout::as_iterator(s1, n),
s2, s2 + n,
lt_()
);
}
template<typename ConstIterator>
static SPROUT_CONSTEXPR int compare(ConstIterator s1, char_type const* s2, std::size_t n) {
return !n ? 0
: lt(*s1, *s2) ? -1
: lt(*s2, *s1) ? 1
: compare(s1 + 1, s2 + 1, n - 1)
;
return sprout::tristate_lexicographical_compare(
s1, s1 + n,
sprout::as_iterator(s2), sprout::as_iterator(s2, n),
lt_()
);
}
template<typename ConstIterator1, typename ConstIterator2>
static SPROUT_CONSTEXPR int compare(ConstIterator1 s1, ConstIterator2 s2, std::size_t n) {
return !n ? 0
: lt(*s1, *s2) ? -1
: lt(*s2, *s1) ? 1
: compare(s1 + 1, s2 + 1, n - 1)
;
return sprout::tristate_lexicographical_compare(
s1, s1 + n,
s2, s2 + n,
lt_()
);
}
template<typename ConstIterator>
static SPROUT_CONSTEXPR std::size_t length(ConstIterator s) {