Sprout/sprout/algorithm/includes.hpp

152 lines
5.2 KiB
C++
Raw Permalink Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
Copyright (c) 2011-2019 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_INCLUDES_HPP
#define SPROUT_ALGORITHM_INCLUDES_HPP
2012-12-15 14:48:52 +00:00
#include <iterator>
2013-01-11 17:39:51 +00:00
#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-15 14:48:52 +00:00
#include <sprout/functional/less.hpp>
2013-02-07 14:12:57 +00:00
#include <sprout/utility/pair/pair.hpp>
2012-04-01 13:15:09 +00:00
namespace sprout {
2012-12-15 14:48:52 +00:00
namespace detail {
2013-01-11 17:39:51 +00:00
template<typename RandomAccessIterator1, typename InputIterator2, typename Compare>
inline SPROUT_CONSTEXPR sprout::pair<InputIterator2, bool>
2012-12-15 14:48:52 +00:00
includes_impl_ra(
RandomAccessIterator1 first1,
sprout::pair<InputIterator2, bool> current,
InputIterator2 last2, Compare comp,
typename std::iterator_traits<RandomAccessIterator1>::difference_type len
2012-12-15 14:48:52 +00:00
)
{
typedef sprout::pair<InputIterator2, bool> type;
return current.second || current.first == last2 ? current
: len == 1 ? comp(*current.first, *first1)
? type(current.first, true)
: type(comp(*first1, *current.first) ? current.first : sprout::next(current.first), false)
2012-12-15 14:48:52 +00:00
: sprout::detail::includes_impl_ra(
sprout::next(first1, len / 2),
2012-12-15 14:48:52 +00:00
sprout::detail::includes_impl_ra(
first1,
current,
2012-12-15 14:48:52 +00:00
last2, comp,
len / 2
2012-12-15 14:48:52 +00:00
),
last2, comp,
len - len / 2
2012-12-15 14:48:52 +00:00
)
;
}
2013-01-11 17:39:51 +00:00
template<typename RandomAccessIterator1, typename InputIterator2, typename Compare>
inline SPROUT_CONSTEXPR typename std::enable_if<
sprout::is_constant_distance_iterator<RandomAccessIterator1>::value,
bool
>::type
2012-12-15 14:48:52 +00:00
includes(
2013-01-11 17:39:51 +00:00
RandomAccessIterator1 first1, RandomAccessIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp,
2012-12-15 14:48:52 +00:00
std::random_access_iterator_tag*
)
{
return first1 == last1 ? first2 == last2
: sprout::detail::includes_impl_ra(
first1,
sprout::pair<InputIterator2, bool>(first2, false),
last2, comp,
sprout::distance(first1, last1)
).first
2012-12-15 14:48:52 +00:00
== last2
;
}
template<typename InputIterator1, typename InputIterator2, typename Compare>
2013-01-11 17:39:51 +00:00
inline SPROUT_CONSTEXPR sprout::pair<InputIterator1, InputIterator2>
includes_impl_1(
sprout::pair<InputIterator1, InputIterator2> const& 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.second, *current.first)
? type(last1, current.second)
: type(
sprout::next(current.first),
comp(*current.first, *current.second) ? current.second : sprout::next(current.second)
)
2013-01-11 17:39:51 +00:00
: sprout::detail::includes_impl_1(
sprout::detail::includes_impl_1(
current,
last1, last2, comp, n / 2
),
last1, last2, comp, n / 2
2013-01-11 17:39:51 +00:00
)
;
}
template<typename InputIterator1, typename InputIterator2, typename Compare>
inline SPROUT_CONSTEXPR sprout::pair<InputIterator1, InputIterator2>
includes_impl(
sprout::pair<InputIterator1, InputIterator2> const& 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::includes_impl(
sprout::detail::includes_impl_1(
current,
last1, last2, comp, n
),
last1, last2, comp, n * 2
)
2012-12-15 14:48:52 +00:00
;
}
template<typename InputIterator1, typename InputIterator2, typename Compare>
inline SPROUT_CONSTEXPR bool
includes(
2013-01-11 17:39:51 +00:00
InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
Compare comp,
2013-02-23 06:21:27 +00:00
std::input_iterator_tag*
2012-12-15 14:48:52 +00:00
)
{
2013-01-11 17:39:51 +00:00
typedef sprout::pair<InputIterator1, InputIterator2> type;
return first2 == last2 ? true
: sprout::detail::includes_impl(type(first1, first2), last1, last2, comp, 1).second == last2
;
2012-12-15 14:48:52 +00:00
}
2013-01-10 17:55:19 +00:00
} // namespace detail
2012-04-01 13:15:09 +00:00
// 25.4.5.1 includes
2012-12-15 14:48:52 +00:00
//
// recursion depth:
2013-01-11 17:39:51 +00:00
// O(log(N1+N2))
2012-12-15 14:48:52 +00:00
//
template<typename InputIterator1, typename InputIterator2, typename Compare>
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR bool
2012-12-15 14:48:52 +00:00
includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp) {
typedef typename std::iterator_traits<InputIterator1>::iterator_category* category;
return sprout::detail::includes(first1, last1, first2, last2, comp, category());
2012-04-01 13:15:09 +00:00
}
2012-12-15 14:48:52 +00:00
template<typename InputIterator1, typename InputIterator2>
2012-10-06 04:53:07 +00:00
inline SPROUT_CONSTEXPR bool
2012-12-15 14:48:52 +00:00
includes(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2) {
return sprout::includes(
first1, last1, first2, last2,
sprout::less<>()
);
2012-04-01 13:15:09 +00:00
}
} // namespace sprout
#endif // #ifndef SPROUT_ALGORITHM_INCLUDES_HPP