1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2024-11-12 21:09:01 +00:00
Sprout/sprout/algorithm/lexicographical_compare.hpp

178 lines
7 KiB
C++
Raw Normal View History

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-04-01 13:15:09 +00:00
#ifndef SPROUT_ALGORITHM_LEXICOGRAPHICAL_COMPARE_HPP
#define SPROUT_ALGORITHM_LEXICOGRAPHICAL_COMPARE_HPP
2013-01-11 17:39:51 +00:00
#include <iterator>
#include <type_traits>
2012-04-01 13:15:09 +00:00
#include <sprout/config.hpp>
#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>
#include <sprout/functional/less.hpp>
#include HDR_ALGORITHM_MIN_MAX_SSCRISK_CEL_OR_SPROUT
2012-04-01 13:15:09 +00:00
namespace sprout {
namespace detail {
2013-01-11 18:41:13 +00:00
template<typename InputIterator1, typename InputIterator2, typename Compare>
inline SPROUT_CONSTEXPR bool
2013-01-11 17:39:51 +00:00
lexicographical_compare_impl_check(
2013-01-11 18:41:13 +00:00
InputIterator1 last1, InputIterator2 last2, Compare comp,
sprout::pair<InputIterator1, InputIterator2> const& found
)
{
return found.second == last2 ? false
: found.first == last1 || comp(*found.first, *found.second)
;
}
2013-01-11 17:39:51 +00:00
template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename Compare>
inline SPROUT_CONSTEXPR sprout::pair<RandomAccessIterator1, RandomAccessIterator2>
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::lexicographical_compare_impl_ra_1(
sprout::next(first1, pivot), last1, sprout::next(first2, pivot), last2, comp,
(sprout::distance(first1, last1) - pivot) / 2,
sprout::detail::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 bool
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;
2013-01-11 17:39:51 +00:00
return sprout::detail::lexicographical_compare_impl_check(
last1, last2, comp,
sprout::detail::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>
2013-01-11 17:39:51 +00:00
inline SPROUT_CONSTEXPR typename std::enable_if<
sprout::is_constant_distance_iterator<RandomAccessIterator1>::value,
bool
>::type
lexicographical_compare(
RandomAccessIterator1 first1, RandomAccessIterator1 last1, RandomAccessIterator2 first2, RandomAccessIterator2 last2, Compare comp,
std::random_access_iterator_tag*
)
{
return first2 == last2 ? false
: first1 == last1 ? true
: sprout::detail::lexicographical_compare_impl_ra(
first1, last1, first2, last2, comp,
NS_SSCRISK_CEL_OR_SPROUT::min(sprout::distance(first1, last1), sprout::distance(first2, last2))
)
;
}
template<typename InputIterator1, typename InputIterator2, typename Compare>
2013-01-11 17:39:51 +00:00
inline SPROUT_CONSTEXPR sprout::pair<InputIterator1, InputIterator2>
lexicographical_compare_impl_1(
sprout::pair<InputIterator1, InputIterator2> current,
InputIterator1 last1, InputIterator2 last2, Compare comp,
typename std::iterator_traits<InputIterator1>::difference_type n
)
{
typedef sprout::pair<InputIterator1, InputIterator2> type;
return current.second == last2 || current.first == last1 ? current
: n == 1 ? comp(*current.first, *current.second) ? type(last1, current.second)
: comp(*current.second, *current.first) ? type(current.first, last2)
: type(sprout::next(current.first), sprout::next(current.second))
: sprout::detail::lexicographical_compare_impl_1(
sprout::detail::lexicographical_compare_impl_1(
current,
last1, last2, comp, n / 2
),
last1, last2, comp, n - n / 2
)
;
}
template<typename InputIterator1, typename InputIterator2, typename Compare>
inline SPROUT_CONSTEXPR sprout::pair<InputIterator1, InputIterator2>
lexicographical_compare_impl(
sprout::pair<InputIterator1, InputIterator2> current,
InputIterator1 last1, InputIterator2 last2, Compare comp,
typename std::iterator_traits<InputIterator1>::difference_type n
)
{
return current.second == last2 || current.first == last1 ? current
: sprout::detail::lexicographical_compare_impl(
sprout::detail::lexicographical_compare_impl_1(
current,
last1, last2, comp, n
),
last1, last2, comp, n * 2
)
;
}
template<typename InputIterator1, typename InputIterator2, typename Compare>
inline SPROUT_CONSTEXPR bool
lexicographical_compare(
InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp,
2013-02-23 06:21:27 +00:00
std::input_iterator_tag*
)
{
2013-01-11 17:39:51 +00:00
typedef sprout::pair<InputIterator1, InputIterator2> type;
return sprout::detail::lexicographical_compare_impl_check(
last1, last2, comp,
sprout::detail::lexicographical_compare_impl(type(first1, first2), last1, last2, comp, 1)
);
}
2013-01-10 17:55:19 +00:00
} // namespace detail
2012-04-01 13:15:09 +00:00
// 25.4.8 Lexicographical comparison
//
// recursion depth:
2013-01-11 17:39:51 +00:00
// O(log(N1+N2))
//
2012-04-01 13:15:09 +00:00
template<typename InputIterator1, typename InputIterator2, typename Compare>
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR bool
lexicographical_compare(
InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
2012-04-01 13:15:09 +00:00
Compare comp
)
{
typedef typename sprout::common_iterator_category<InputIterator1, InputIterator2>::type* category;
return sprout::detail::lexicographical_compare(first1, last1, first2, last2, comp, category());
2012-04-01 13:15:09 +00:00
}
template<typename InputIterator1, typename InputIterator2>
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR bool
lexicographical_compare(
InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2
2012-04-01 13:15:09 +00:00
)
{
2012-10-06 04:53:07 +00:00
return sprout::lexicographical_compare(
first1, last1, first2, last2,
sprout::less<>()
2012-10-06 04:53:07 +00:00
);
2012-04-01 13:15:09 +00:00
}
} // namespace sprout
#endif // #ifndef SPROUT_ALGORITHM_LEXICOGRAPHICAL_COMPARE_HPP