2013-08-08 09:54:33 +00:00
|
|
|
/*=============================================================================
|
2014-01-08 07:48:12 +00:00
|
|
|
Copyright (c) 2011-2014 Bolero MURAKAMI
|
2013-08-08 09:54:33 +00:00
|
|
|
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-12-21 14:12:54 +00:00
|
|
|
#ifndef SPROUT_ALGORITHM_TRISTATE_LEXICOGRAPHICAL_COMPARE_HPP
|
|
|
|
#define SPROUT_ALGORITHM_TRISTATE_LEXICOGRAPHICAL_COMPARE_HPP
|
|
|
|
|
2013-01-11 18:41:13 +00:00
|
|
|
#include <iterator>
|
|
|
|
#include <type_traits>
|
2012-12-21 14:12:54 +00:00
|
|
|
#include <sprout/config.hpp>
|
2013-09-02 00:39:02 +00:00
|
|
|
#include <sprout/detail/algorithm/lexicographical_compare.hpp>
|
2012-12-21 14:12:54 +00:00
|
|
|
#include <sprout/iterator/operation.hpp>
|
2013-02-26 07:14:04 +00:00
|
|
|
#include <sprout/iterator/type_traits/category.hpp>
|
2012-12-21 14:12:54 +00:00
|
|
|
#include <sprout/iterator/type_traits/common.hpp>
|
2013-02-07 14:12:57 +00:00
|
|
|
#include <sprout/utility/pair/pair.hpp>
|
2012-12-21 14:12:54 +00:00
|
|
|
#include <sprout/functional/less.hpp>
|
2013-05-16 06:13:49 +00:00
|
|
|
#include HDR_ALGORITHM_MIN_MAX_SSCRISK_CEL_OR_SPROUT
|
2012-12-21 14:12:54 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
namespace detail {
|
2013-09-02 00:39:02 +00:00
|
|
|
template<typename Difference1, typename Difference2>
|
2012-12-21 14:12:54 +00:00
|
|
|
inline SPROUT_CONSTEXPR int
|
2013-09-02 00:39:02 +00:00
|
|
|
tristate_lexicographical_compare_impl_ra_check(
|
|
|
|
Difference1 size1, Difference2 size2, int found
|
2012-12-21 14:12:54 +00:00
|
|
|
)
|
|
|
|
{
|
2013-09-02 00:39:02 +00:00
|
|
|
return found != 2 ? found
|
|
|
|
: size1 < size2 ? -1
|
|
|
|
: size1 > size2 ? 1 : 0
|
2012-12-21 14:12:54 +00:00
|
|
|
;
|
|
|
|
}
|
2013-01-11 18:41:13 +00:00
|
|
|
|
2012-12-21 14:12:54 +00:00
|
|
|
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename Compare>
|
2013-01-11 18:41:13 +00:00
|
|
|
inline SPROUT_CONSTEXPR typename std::enable_if<
|
|
|
|
sprout::is_constant_distance_iterator<RandomAccessIterator1>::value,
|
|
|
|
int
|
|
|
|
>::type
|
2012-12-21 14:12:54 +00:00
|
|
|
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
|
2013-09-02 00:39:02 +00:00
|
|
|
: sprout::detail::tristate_lexicographical_compare_impl_ra_check(
|
|
|
|
sprout::distance(first1, last1), sprout::distance(first2, last2),
|
|
|
|
sprout::detail::lexicographical_compare_impl_ra(
|
|
|
|
first1, first2, comp,
|
|
|
|
NS_SSCRISK_CEL_OR_SPROUT::min(sprout::distance(first1, last1), sprout::distance(first2, last2)),
|
|
|
|
2
|
|
|
|
)
|
2012-12-21 14:12:54 +00:00
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename InputIterator1, typename InputIterator2, typename Compare>
|
2013-09-02 00:39:02 +00:00
|
|
|
inline SPROUT_CONSTEXPR int
|
|
|
|
tristate_lexicographical_compare_impl_check(
|
|
|
|
InputIterator1 last1, InputIterator2 last2, Compare comp,
|
|
|
|
sprout::pair<InputIterator1, InputIterator2> const& found
|
|
|
|
)
|
2013-01-11 18:41:13 +00:00
|
|
|
{
|
2013-09-02 00:39:02 +00:00
|
|
|
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
|
|
|
|
;
|
2012-12-21 14:12:54 +00:00
|
|
|
}
|
2013-09-02 00:39:02 +00:00
|
|
|
|
2012-12-21 14:12:54 +00:00
|
|
|
template<typename InputIterator1, typename InputIterator2, typename Compare>
|
|
|
|
inline SPROUT_CONSTEXPR int
|
|
|
|
tristate_lexicographical_compare(
|
|
|
|
InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp,
|
2013-02-23 06:21:27 +00:00
|
|
|
std::input_iterator_tag*
|
2012-12-21 14:12:54 +00:00
|
|
|
)
|
|
|
|
{
|
2013-01-11 18:41:13 +00:00
|
|
|
typedef sprout::pair<InputIterator1, InputIterator2> type;
|
|
|
|
return sprout::detail::tristate_lexicographical_compare_impl_check(
|
|
|
|
last1, last2, comp,
|
2013-09-02 00:39:02 +00:00
|
|
|
sprout::detail::lexicographical_compare_impl(type(first1, first2), last1, last2, comp, 1)
|
2013-01-11 18:41:13 +00:00
|
|
|
);
|
2012-12-21 14:12:54 +00:00
|
|
|
}
|
2013-01-10 17:55:19 +00:00
|
|
|
} // namespace detail
|
2012-12-21 14:12:54 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// tristate_lexicographical_compare
|
|
|
|
//
|
|
|
|
// recursion depth:
|
2013-01-11 18:41:13 +00:00
|
|
|
// O(log(N1+N2))
|
2012-12-21 14:12:54 +00:00
|
|
|
//
|
|
|
|
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<>()
|
|
|
|
);
|
|
|
|
}
|
2012-12-21 17:53:21 +00:00
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
template<typename RandomAccessIterator1, typename T1, typename RandomAccessIterator2, typename T2, typename Compare>
|
|
|
|
inline SPROUT_CONSTEXPR int
|
2013-09-02 00:39:02 +00:00
|
|
|
tristate_lexicographical_compare_2_impl_ra(
|
|
|
|
RandomAccessIterator1 first1, T1 const& delim1,
|
|
|
|
RandomAccessIterator2 first2, T2 const& delim2,
|
2012-12-21 17:53:21 +00:00
|
|
|
Compare comp,
|
2013-09-02 00:39:02 +00:00
|
|
|
typename std::iterator_traits<RandomAccessIterator1>::difference_type size, int found
|
2012-12-21 17:53:21 +00:00
|
|
|
)
|
|
|
|
{
|
2013-09-02 00:39:02 +00:00
|
|
|
return found != 2 ? found
|
|
|
|
: size == 1
|
|
|
|
? !comp(*first2, delim2) && !comp(delim2, *first2)
|
|
|
|
? !comp(*first1, delim1) && !comp(delim1, *first1) ? 0 : 1
|
2014-04-14 06:52:20 +00:00
|
|
|
: (!comp(*first1, delim1) && !comp(delim1, *first1)) || comp(*first1, *first2)
|
2013-09-02 00:39:02 +00:00
|
|
|
? -1
|
|
|
|
: comp(*first2, *first1) ? 1 : 2
|
|
|
|
: sprout::detail::tristate_lexicographical_compare_2_impl_ra(
|
|
|
|
sprout::next(first1, size / 2), delim1, sprout::next(first2, size / 2), delim2, comp,
|
|
|
|
size - size / 2,
|
|
|
|
sprout::detail::tristate_lexicographical_compare_2_impl_ra(
|
|
|
|
first1, delim1, first2, delim2, comp,
|
|
|
|
size / 2,
|
|
|
|
2
|
2012-12-21 17:53:21 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename RandomAccessIterator1, typename T1, typename RandomAccessIterator2, typename T2, typename Compare>
|
|
|
|
inline SPROUT_CONSTEXPR int
|
|
|
|
tristate_lexicographical_compare_2(
|
|
|
|
RandomAccessIterator1 first1, RandomAccessIterator1 last1, T1 const& delim1,
|
|
|
|
RandomAccessIterator2 first2, RandomAccessIterator2 last2, T2 const& delim2,
|
|
|
|
Compare comp,
|
|
|
|
std::random_access_iterator_tag*
|
|
|
|
)
|
|
|
|
{
|
2013-09-02 00:39:02 +00:00
|
|
|
return sprout::detail::tristate_lexicographical_compare_impl_ra_check(
|
|
|
|
sprout::distance(first1, last1), sprout::distance(first2, last2),
|
|
|
|
first2 == last2 || first1 == last1
|
|
|
|
? 2
|
2012-12-21 17:53:21 +00:00
|
|
|
: sprout::detail::tristate_lexicographical_compare_2_impl_ra(
|
2013-09-02 00:39:02 +00:00
|
|
|
first1, delim1, first2, delim2, comp,
|
|
|
|
NS_SSCRISK_CEL_OR_SPROUT::min(sprout::distance(first1, last1), sprout::distance(first2, last2)),
|
|
|
|
2
|
2012-12-21 17:53:21 +00:00
|
|
|
)
|
2013-09-02 00:39:02 +00:00
|
|
|
)
|
2012-12-21 17:53:21 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename InputIterator1, typename T1, typename InputIterator2, typename T2, typename Compare>
|
2013-01-11 18:41:13 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::pair<InputIterator1, InputIterator2>
|
|
|
|
tristate_lexicographical_compare_impl_1(
|
2013-01-12 16:16:48 +00:00
|
|
|
sprout::pair<InputIterator1, InputIterator2> const& current,
|
2013-01-11 18:41:13 +00:00
|
|
|
InputIterator1 last1, T1 const& delim1, InputIterator2 last2, T2 const& delim2, Compare comp,
|
|
|
|
typename std::iterator_traits<InputIterator1>::difference_type n
|
2012-12-21 17:53:21 +00:00
|
|
|
)
|
|
|
|
{
|
2013-01-11 18:41:13 +00:00
|
|
|
typedef sprout::pair<InputIterator1, InputIterator2> type;
|
|
|
|
return current.second == last2 || current.first == last1 ? current
|
2013-09-02 00:39:02 +00:00
|
|
|
: n == 1
|
|
|
|
? !comp(*current.second, delim2) && !comp(delim2, *current.second)
|
|
|
|
? type(!comp(*current.first, delim1) && !comp(delim1, *current.first) ? last1 : current.first, last2)
|
2014-04-14 06:52:20 +00:00
|
|
|
: (!comp(*current.first, delim1) && !comp(delim1, *current.first)) || comp(*current.first, *current.second)
|
2013-09-02 00:39:02 +00:00
|
|
|
? type(last1, current.second)
|
2013-01-11 18:41:13 +00:00
|
|
|
: comp(*current.second, *current.first) ? type(current.first, last2)
|
|
|
|
: type(sprout::next(current.first), sprout::next(current.second))
|
|
|
|
: sprout::detail::tristate_lexicographical_compare_impl_1(
|
|
|
|
sprout::detail::tristate_lexicographical_compare_impl_1(
|
|
|
|
current,
|
|
|
|
last1, delim1, last2, delim2, comp, n / 2
|
|
|
|
),
|
|
|
|
last1, delim1, last2, delim2, comp, n - n / 2
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename InputIterator1, typename T1, typename InputIterator2, typename T2, typename Compare>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::pair<InputIterator1, InputIterator2>
|
|
|
|
tristate_lexicographical_compare_impl(
|
2013-01-12 16:16:48 +00:00
|
|
|
sprout::pair<InputIterator1, InputIterator2> const& current,
|
2013-01-11 18:41:13 +00:00
|
|
|
InputIterator1 last1, T1 const& delim1, InputIterator2 last2, T2 const& delim2, Compare comp,
|
|
|
|
typename std::iterator_traits<InputIterator1>::difference_type n
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return current.second == last2 || current.first == last1 ? current
|
|
|
|
: sprout::detail::tristate_lexicographical_compare_impl(
|
|
|
|
sprout::detail::tristate_lexicographical_compare_impl_1(
|
|
|
|
current,
|
|
|
|
last1, delim1, last2, delim2, comp, n
|
|
|
|
),
|
|
|
|
last1, delim1, last2, delim2, comp, n * 2
|
|
|
|
)
|
2012-12-21 17:53:21 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
template<typename InputIterator1, typename T1, typename InputIterator2, typename T2, typename Compare>
|
|
|
|
inline SPROUT_CONSTEXPR int
|
2013-09-02 00:39:02 +00:00
|
|
|
tristate_lexicographical_compare_2(
|
2012-12-21 17:53:21 +00:00
|
|
|
InputIterator1 first1, InputIterator1 last1, T1 const& delim1,
|
|
|
|
InputIterator2 first2, InputIterator2 last2, T2 const& delim2,
|
|
|
|
Compare comp,
|
2013-02-23 06:21:27 +00:00
|
|
|
std::input_iterator_tag*
|
2012-12-21 17:53:21 +00:00
|
|
|
)
|
|
|
|
{
|
2013-01-11 18:41:13 +00:00
|
|
|
typedef sprout::pair<InputIterator1, InputIterator2> type;
|
|
|
|
return sprout::detail::tristate_lexicographical_compare_impl_check(
|
|
|
|
last1, last2, comp,
|
|
|
|
sprout::detail::tristate_lexicographical_compare_impl(type(first1, first2), last1, delim1, last2, delim2, comp, 1)
|
|
|
|
);
|
2012-12-21 17:53:21 +00:00
|
|
|
}
|
2013-01-10 17:55:19 +00:00
|
|
|
} // namespace detail
|
2012-12-21 17:53:21 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// tristate_lexicographical_compare
|
|
|
|
//
|
|
|
|
// recursion depth:
|
2013-01-11 18:41:13 +00:00
|
|
|
// O(log(N1+N2))
|
2012-12-21 17:53:21 +00:00
|
|
|
//
|
|
|
|
template<typename InputIterator1, typename T1, typename InputIterator2, typename T2, typename Compare>
|
|
|
|
inline SPROUT_CONSTEXPR int
|
|
|
|
tristate_lexicographical_compare(
|
|
|
|
InputIterator1 first1, InputIterator1 last1, T1 const& delim1,
|
|
|
|
InputIterator2 first2, InputIterator2 last2, T2 const& delim2,
|
|
|
|
Compare comp
|
|
|
|
)
|
|
|
|
{
|
|
|
|
typedef typename sprout::common_iterator_category<InputIterator1, InputIterator2>::type* category;
|
|
|
|
return sprout::detail::tristate_lexicographical_compare_2(first1, last1, delim1, first2, last2, delim2, comp, category());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename InputIterator1, typename T1, typename InputIterator2, typename T2>
|
|
|
|
inline SPROUT_CONSTEXPR int
|
|
|
|
tristate_lexicographical_compare(
|
|
|
|
InputIterator1 first1, InputIterator1 last1, T1 const& delim1,
|
|
|
|
InputIterator2 first2, InputIterator2 last2, T2 const& delim2
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return sprout::tristate_lexicographical_compare(
|
|
|
|
first1, last1, delim1, first2, last2, delim2,
|
|
|
|
sprout::less<>()
|
|
|
|
);
|
|
|
|
}
|
2012-12-21 14:12:54 +00:00
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_ALGORITHM_TRISTATE_LEXICOGRAPHICAL_COMPARE_HPP
|