From 2e984eacb7648d63fcd61c22619dbe80f224191c Mon Sep 17 00:00:00 2001 From: Mitsuru Kariya Date: Mon, 2 Sep 2013 09:39:02 +0900 Subject: [PATCH] intermediate fix tristate_lexicographical_compare(w/delimiter version) reduce complexity remove unnecessary code etc. remove testcode --- sprout/algorithm/lexicographical_compare.hpp | 100 ++------- .../tristate_lexicographical_compare.hpp | 204 +++++------------- .../algorithm/lexicographical_compare.hpp | 73 +++++++ 3 files changed, 151 insertions(+), 226 deletions(-) create mode 100644 sprout/detail/algorithm/lexicographical_compare.hpp diff --git a/sprout/algorithm/lexicographical_compare.hpp b/sprout/algorithm/lexicographical_compare.hpp index e5f1e709..814ad1a9 100644 --- a/sprout/algorithm/lexicographical_compare.hpp +++ b/sprout/algorithm/lexicographical_compare.hpp @@ -11,7 +11,8 @@ #include #include #include -#include +#include +#include #include #include #include @@ -20,55 +21,16 @@ namespace sprout { namespace detail { - template + template inline SPROUT_CONSTEXPR bool lexicographical_compare_impl_check( - InputIterator1 last1, InputIterator2 last2, Compare comp, - sprout::pair const& found + Difference1 size1, Difference2 size2, + int found ) { - return found.second == last2 ? false - : found.first == last1 || comp(*found.first, *found.second) - ; + return found < 0 || found == 2 && size1 < size2; } - template - inline SPROUT_CONSTEXPR sprout::pair - 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::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 - inline SPROUT_CONSTEXPR bool - 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::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 inline SPROUT_CONSTEXPR typename std::enable_if< sprout::is_constant_distance_iterator::value, @@ -81,53 +43,29 @@ namespace sprout { { 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)) + : sprout::detail::lexicographical_compare_impl_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 + ) ) ; } template - inline SPROUT_CONSTEXPR sprout::pair - lexicographical_compare_impl_1( - sprout::pair current, + inline SPROUT_CONSTEXPR bool + lexicographical_compare_impl_check( InputIterator1 last1, InputIterator2 last2, Compare comp, - typename std::iterator_traits::difference_type n + sprout::pair const& found ) { - typedef sprout::pair 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 - inline SPROUT_CONSTEXPR sprout::pair - lexicographical_compare_impl( - sprout::pair current, - InputIterator1 last1, InputIterator2 last2, Compare comp, - typename std::iterator_traits::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 - ) + return found.second == last2 ? false + : found.first == last1 || comp(*found.first, *found.second) ; } + template inline SPROUT_CONSTEXPR bool lexicographical_compare( diff --git a/sprout/algorithm/tristate_lexicographical_compare.hpp b/sprout/algorithm/tristate_lexicographical_compare.hpp index ab0b9cf1..8eda54de 100644 --- a/sprout/algorithm/tristate_lexicographical_compare.hpp +++ b/sprout/algorithm/tristate_lexicographical_compare.hpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -20,58 +21,18 @@ namespace sprout { namespace detail { - template + template inline SPROUT_CONSTEXPR int - tristate_lexicographical_compare_impl_check( - InputIterator1 last1, InputIterator2 last2, Compare comp, - sprout::pair const& found + tristate_lexicographical_compare_impl_ra_check( + Difference1 size1, Difference2 size2, int 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 + return found != 2 ? found + : size1 < size2 ? -1 + : size1 > size2 ? 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, - (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_check( - 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 typename std::enable_if< sprout::is_constant_distance_iterator::value, @@ -84,53 +45,32 @@ namespace sprout { { return first2 == last2 ? (first1 == last1 ? 0 : 1) : first1 == last1 ? -1 - : sprout::detail::tristate_lexicographical_compare_impl_ra( - first1, last1, first2, last2, comp, - NS_SSCRISK_CEL_OR_SPROUT::min(sprout::distance(first1, last1), sprout::distance(first2, last2)) + : 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 + ) ) ; } template - inline SPROUT_CONSTEXPR sprout::pair - tristate_lexicographical_compare_impl_1( - sprout::pair const& current, - InputIterator1 last1, InputIterator2 last2, Compare comp, - typename std::iterator_traits::difference_type n - ) + inline SPROUT_CONSTEXPR int + tristate_lexicographical_compare_impl_check( + InputIterator1 last1, InputIterator2 last2, Compare comp, + sprout::pair const& found + ) { - typedef sprout::pair 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::tristate_lexicographical_compare_impl_1( - sprout::detail::tristate_lexicographical_compare_impl_1( - current, - last1, last2, comp, n / 2 - ), - last1, last2, comp, n - n / 2 - ) - ; - } - template - inline SPROUT_CONSTEXPR sprout::pair - tristate_lexicographical_compare_impl( - sprout::pair const& current, - InputIterator1 last1, InputIterator2 last2, Compare comp, - typename std::iterator_traits::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, last2, comp, n - ), - last1, last2, comp, n * 2 - ) - ; + 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 int tristate_lexicographical_compare( @@ -141,7 +81,7 @@ namespace sprout { typedef sprout::pair type; return sprout::detail::tristate_lexicographical_compare_impl_check( last1, last2, comp, - sprout::detail::tristate_lexicographical_compare_impl(type(first1, first2), last1, last2, comp, 1) + sprout::detail::lexicographical_compare_impl(type(first1, first2), last1, last2, comp, 1) ); } } // namespace detail @@ -180,65 +120,33 @@ namespace sprout { namespace detail { template inline SPROUT_CONSTEXPR int - tristate_lexicographical_compare_2_impl_ra_2( - RandomAccessIterator1 last1, T1 const& delim1, RandomAccessIterator2 last2, T2 const& delim2, Compare comp, - sprout::pair const& found - ) - { - return found.second == last2 || (!comp(*found.second, delim2) && !comp(delim2, *found.second)) - ? (found.first == last1 || (!comp(*found.first, delim1) && !comp(delim1, *found.first)) ? 0 : 1) - : found.first == last1 || (!comp(*found.first, delim1) && !comp(delim1, *found.first)) ? -1 - : comp(*found.first, *found.second) ? -1 - : comp(*found.second, *found.first) ? 1 - : 0 - ; - } - template - inline SPROUT_CONSTEXPR sprout::pair - tristate_lexicographical_compare_2_impl_ra_1( - RandomAccessIterator1 first1, RandomAccessIterator1 last1, T1 const& delim1, - RandomAccessIterator2 first2, RandomAccessIterator2 last2, T2 const& delim2, + tristate_lexicographical_compare_2_impl_ra( + RandomAccessIterator1 first1, T1 const& delim1, + RandomAccessIterator2 first2, T2 const& delim2, Compare comp, - typename std::iterator_traits::difference_type pivot, sprout::pair const& found + typename std::iterator_traits::difference_type size, int found ) { - typedef sprout::pair found_type; - return found.first != first1 ? found - : pivot == 0 ? ( - comp(*first1, *first2) || comp(*first2, *first1) || (!comp(*first1, delim1) && !comp(delim1, *first1)) || (!comp(*first2, delim2) && !comp(delim2, *first2)) - ? found_type(first1, first2) : found_type(last1, last2) - ) - : sprout::detail::tristate_lexicographical_compare_2_impl_ra_1( - sprout::next(first1, pivot), last1, delim1, sprout::next(first2, pivot), last2, delim2, comp, - (sprout::distance(first1, last1) - pivot) / 2, - sprout::detail::tristate_lexicographical_compare_2_impl_ra_1( - first1, sprout::next(first1, pivot), delim1, first2, sprout::next(first2, pivot), delim2, comp, - pivot / 2, - found_type(first1, first2) + return found != 2 ? found + : size == 1 + ? !comp(*first2, delim2) && !comp(delim2, *first2) + ? !comp(*first1, delim1) && !comp(delim1, *first1) ? 0 : 1 + : !comp(*first1, delim1) && !comp(delim1, *first1) || comp(*first1, *first2) + ? -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 ) ) ; } template inline SPROUT_CONSTEXPR int - tristate_lexicographical_compare_2_impl_ra( - RandomAccessIterator1 first1, RandomAccessIterator1 last1, T1 const& delim1, - RandomAccessIterator2 first2, RandomAccessIterator2 last2, T2 const& delim2, - Compare comp, - typename std::iterator_traits::difference_type size - ) - { - typedef sprout::pair found_type; - return sprout::detail::tristate_lexicographical_compare_2_impl_ra_2( - last1, delim1, last2, delim2, comp, - sprout::detail::tristate_lexicographical_compare_2_impl_ra_1( - first1, sprout::next(first1, size), delim1, first2, sprout::next(first2, size), delim2, comp, - size / 2, found_type(first1, first2) - ) - ); - } - template - inline SPROUT_CONSTEXPR int tristate_lexicographical_compare_2( RandomAccessIterator1 first1, RandomAccessIterator1 last1, T1 const& delim1, RandomAccessIterator2 first2, RandomAccessIterator2 last2, T2 const& delim2, @@ -246,12 +154,16 @@ namespace sprout { std::random_access_iterator_tag* ) { - return first2 == last2 || (!comp(*first2, delim2) && !comp(delim2, *first2)) ? (first1 == last1 || (!comp(*first1, delim1) && !comp(delim1, *first1)) ? 0 : 1) - : first1 == last1 || (!comp(*first1, delim1) && !comp(delim1, *first1)) ? -1 + return sprout::detail::tristate_lexicographical_compare_impl_ra_check( + sprout::distance(first1, last1), sprout::distance(first2, last2), + first2 == last2 || first1 == last1 + ? 2 : sprout::detail::tristate_lexicographical_compare_2_impl_ra( - first1, last1, delim1, first2, last2, delim2, comp, - NS_SSCRISK_CEL_OR_SPROUT::min(sprout::distance(first1, last1), sprout::distance(first2, last2)) + first1, delim1, first2, delim2, comp, + NS_SSCRISK_CEL_OR_SPROUT::min(sprout::distance(first1, last1), sprout::distance(first2, last2)), + 2 ) + ) ; } @@ -265,9 +177,11 @@ namespace sprout { { typedef sprout::pair type; return current.second == last2 || current.first == last1 ? current - : !comp(*current.second, delim2) && !comp(delim2, *current.second) ? type(!comp(*current.first, delim1) && !comp(delim1, *current.first) ? last1 : current.first, last2) - : !comp(*current.first, delim1) && !comp(delim1, *current.first) ? type(last1, current.second) - : n == 1 ? comp(*current.first, *current.second) ? type(last1, current.second) + : n == 1 + ? !comp(*current.second, delim2) && !comp(delim2, *current.second) + ? type(!comp(*current.first, delim1) && !comp(delim1, *current.first) ? last1 : current.first, last2) + : !comp(*current.first, delim1) && !comp(delim1, *current.first) || 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::tristate_lexicographical_compare_impl_1( @@ -299,7 +213,7 @@ namespace sprout { } template inline SPROUT_CONSTEXPR int - tristate_lexicographical_compare( + tristate_lexicographical_compare_2( InputIterator1 first1, InputIterator1 last1, T1 const& delim1, InputIterator2 first2, InputIterator2 last2, T2 const& delim2, Compare comp, diff --git a/sprout/detail/algorithm/lexicographical_compare.hpp b/sprout/detail/algorithm/lexicographical_compare.hpp new file mode 100644 index 00000000..c9eb38db --- /dev/null +++ b/sprout/detail/algorithm/lexicographical_compare.hpp @@ -0,0 +1,73 @@ +#ifndef SPROUT_DETAIL_ALGORITHM_LEXICOGRAPHICAL_COMPARE_HPP +#define SPROUT_DETAIL_ALGORITHM_LEXICOGRAPHICAL_COMPARE_HPP + +#include +#include +#include + +namespace sprout { + namespace detail { + template + inline SPROUT_CONSTEXPR int + lexicographical_compare_impl_ra( + RandomAccessIterator1 first1, RandomAccessIterator2 first2, Compare comp, + typename std::iterator_traits::difference_type size, + int found + ) + { + return found != 2 ? found + : size == 1 ? comp(*first1, *first2) ? -1 : comp(*first2, *first1) ? 1 : 2 + : sprout::detail::lexicographical_compare_impl_ra( + sprout::next(first1, size / 2), sprout::next(first2, size / 2), comp, size - size / 2, + sprout::detail::lexicographical_compare_impl_ra( + first1, first2, comp, size / 2, + 2 + ) + ) + ; + } + + template + inline SPROUT_CONSTEXPR sprout::pair + lexicographical_compare_impl_1( + sprout::pair const& current, + InputIterator1 last1, InputIterator2 last2, Compare comp, + typename std::iterator_traits::difference_type n + ) + { + typedef sprout::pair 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 + inline SPROUT_CONSTEXPR sprout::pair + lexicographical_compare_impl( + sprout::pair const& current, + InputIterator1 last1, InputIterator2 last2, Compare comp, + typename std::iterator_traits::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 + ) + ; + } + } // namespace detail +} // namespace sprout + +#endif /* #ifndef SPROUT_DETAIL_ALGORITHM_LEXICOGRAPHICAL_COMPARE_HPP */