#ifndef SPROUT_ALGORITHM_TRISTATE_LEXICOGRAPHICAL_COMPARE_HPP #define SPROUT_ALGORITHM_TRISTATE_LEXICOGRAPHICAL_COMPARE_HPP #include #include #include #include #include #include #include HDR_ITERATOR_SSCRISK_CEL_OR_SPROUT namespace sprout { namespace detail { template inline SPROUT_CONSTEXPR int tristate_lexicographical_compare_impl_ra_2( RandomAccessIterator1 last1, RandomAccessIterator2 last2, Compare comp, sprout::pair 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 inline SPROUT_CONSTEXPR sprout::pair tristate_lexicographical_compare_impl_ra_1( RandomAccessIterator1 first1, RandomAccessIterator1 last1, RandomAccessIterator2 first2, RandomAccessIterator2 last2, Compare comp, typename std::iterator_traits::difference_type pivot, sprout::pair const& found ) { typedef sprout::pair 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 inline SPROUT_CONSTEXPR int tristate_lexicographical_compare_impl_ra( RandomAccessIterator1 first1, RandomAccessIterator1 last1, RandomAccessIterator2 first2, RandomAccessIterator2 last2, Compare comp, typename std::iterator_traits::difference_type size ) { typedef sprout::pair 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 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 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 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 inline SPROUT_CONSTEXPR int tristate_lexicographical_compare( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp ) { typedef typename sprout::common_iterator_category::type* category; return sprout::detail::tristate_lexicographical_compare(first1, last1, first2, last2, comp, category()); } template 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