Sprout/sprout/cstring/strcmp.hpp

100 lines
3.6 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
2015-01-10 10:13:57 +00:00
Copyright (c) 2011-2015 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_CSTRING_STRCMP_HPP
#define SPROUT_CSTRING_STRCMP_HPP
2013-01-12 16:16:48 +00:00
#include <iterator>
#include <type_traits>
2012-04-01 13:15:09 +00:00
#include <sprout/config.hpp>
2013-01-12 16:16:48 +00:00
#include <sprout/iterator/operation.hpp>
2013-02-07 14:12:57 +00:00
#include <sprout/utility/pair/pair.hpp>
2013-02-07 15:49:47 +00:00
#include <sprout/tuple/tuple/tuple.hpp>
#include <sprout/tuple/tuple/get.hpp>
2013-01-12 16:16:48 +00:00
#include <sprout/type_traits/is_char_type.hpp>
2012-04-01 13:15:09 +00:00
namespace sprout {
2013-01-12 16:16:48 +00:00
namespace detail {
template<typename InputIterator1, typename InputIterator2>
inline SPROUT_CONSTEXPR int
strcmp_impl_check(sprout::tuples::tuple<InputIterator1, InputIterator2, int> const& found) {
return !*sprout::tuples::get<1>(found) ? (!*sprout::tuples::get<0>(found) ? 0 : 1)
: !*sprout::tuples::get<0>(found) ? -1
: sprout::tuples::get<2>(found)
;
}
template<typename InputIterator1, typename InputIterator2>
inline SPROUT_CONSTEXPR sprout::tuples::tuple<InputIterator1, InputIterator2, int>
strcmp_impl_1(
sprout::tuples::tuple<InputIterator1, InputIterator2, int> const& current,
typename std::iterator_traits<InputIterator1>::difference_type n
)
{
typedef sprout::tuples::tuple<InputIterator1, InputIterator2, int> type;
return sprout::tuples::get<2>(current) || !*sprout::tuples::get<1>(current) || !*sprout::tuples::get<0>(current) ? current
: n == 1 ? (*sprout::tuples::get<0>(current)) < (*sprout::tuples::get<1>(current)) ? type(sprout::tuples::get<0>(current), sprout::tuples::get<1>(current), -1)
: (*sprout::tuples::get<1>(current)) < (*sprout::tuples::get<0>(current)) ? type(sprout::tuples::get<0>(current), sprout::tuples::get<1>(current), 1)
: type(sprout::next(sprout::tuples::get<0>(current)), sprout::next(sprout::tuples::get<1>(current)), 0)
: sprout::detail::strcmp_impl_1(
sprout::detail::strcmp_impl_1(
current,
n / 2
),
n - n / 2
)
;
}
template<typename InputIterator1, typename InputIterator2>
inline SPROUT_CONSTEXPR sprout::tuples::tuple<InputIterator1, InputIterator2, int>
strcmp_impl(
sprout::tuples::tuple<InputIterator1, InputIterator2, int> const& current,
typename std::iterator_traits<InputIterator1>::difference_type n
)
{
return sprout::tuples::get<2>(current) || !*sprout::tuples::get<1>(current) || !*sprout::tuples::get<0>(current) ? current
: sprout::detail::strcmp_impl(
sprout::detail::strcmp_impl_1(
current,
n
),
n * 2
)
;
}
template<typename InputIterator1, typename InputIterator2>
inline SPROUT_CONSTEXPR int
strcmp(InputIterator1 first1, InputIterator2 first2)
{
typedef sprout::tuples::tuple<InputIterator1, InputIterator2, int> type;
return sprout::detail::strcmp_impl_check(
sprout::detail::strcmp_impl(type(first1, first2, 0), 1)
);
}
} // namespace detail
2012-04-01 13:15:09 +00:00
2013-03-22 05:24:19 +00:00
// 7.21.4.2 strcmp <20>֐<EFBFBD>
2013-01-12 16:16:48 +00:00
//
// recursion depth:
// O(log(N1+N2))
//
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR int
strcmp(char const* s1, char const* s2) {
2013-01-12 16:16:48 +00:00
return sprout::detail::strcmp(s1, s2);
}
template<typename Elem>
inline SPROUT_CONSTEXPR typename std::enable_if<
sprout::is_char_type<Elem>::value,
int
>::type
strcmp(Elem* s1, Elem* s2) {
return sprout::detail::strcmp(s1, s2);
2012-04-01 13:15:09 +00:00
}
} // namespace sprout
#endif // #ifndef SPROUT_CSTRING_STRCMP_HPP