2012-06-23 23:22:12 +00:00
|
|
|
#ifndef SPROUT_DETAIL_ALGORITHM_SET_OVERLAP_COUNT_HPP
|
|
|
|
#define SPROUT_DETAIL_ALGORITHM_SET_OVERLAP_COUNT_HPP
|
2012-06-23 04:22:50 +00:00
|
|
|
|
|
|
|
#include <iterator>
|
|
|
|
#include <sprout/config.hpp>
|
|
|
|
#include <sprout/iterator/operation.hpp>
|
2013-02-26 07:14:04 +00:00
|
|
|
#include <sprout/iterator/type_traits/category.hpp>
|
2013-01-03 08:01:50 +00:00
|
|
|
#include <sprout/iterator/type_traits/common.hpp>
|
2013-02-07 14:12:57 +00:00
|
|
|
#include <sprout/tuple/tuple/tuple.hpp>
|
|
|
|
#include <sprout/tuple/tuple/get.hpp>
|
2012-12-16 17:14:07 +00:00
|
|
|
#include <sprout/functional/less.hpp>
|
2012-06-23 04:22:50 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
namespace detail {
|
2013-01-03 08:01:50 +00:00
|
|
|
template<typename InputIterator1, typename InputIterator2, typename Compare>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::tuples::tuple<InputIterator1, InputIterator2, typename std::iterator_traits<InputIterator1>::difference_type>
|
|
|
|
set_overlap_count_impl_1(
|
|
|
|
sprout::tuples::tuple<InputIterator1, InputIterator2, typename std::iterator_traits<InputIterator1>::difference_type> const& current,
|
|
|
|
InputIterator1 last1, InputIterator2 last2, Compare comp, typename std::iterator_traits<InputIterator1>::difference_type n
|
|
|
|
)
|
|
|
|
{
|
|
|
|
typedef sprout::tuples::tuple<InputIterator1, InputIterator2, typename std::iterator_traits<InputIterator1>::difference_type> type;
|
|
|
|
return sprout::tuples::get<0>(current) == last1 || sprout::tuples::get<1>(current) == last2 ? current
|
|
|
|
: n == 1 ? comp(*sprout::tuples::get<0>(current), *sprout::tuples::get<1>(current)) ? type(
|
|
|
|
sprout::next(sprout::tuples::get<0>(current)), sprout::tuples::get<1>(current),
|
|
|
|
sprout::tuples::get<2>(current)
|
|
|
|
)
|
|
|
|
: comp(*sprout::tuples::get<1>(current), *sprout::tuples::get<0>(current)) ? type(
|
|
|
|
sprout::tuples::get<0>(current), sprout::next(sprout::tuples::get<1>(current)),
|
|
|
|
sprout::tuples::get<2>(current)
|
|
|
|
)
|
|
|
|
: type(
|
|
|
|
sprout::next(sprout::tuples::get<0>(current)), sprout::next(sprout::tuples::get<1>(current)),
|
|
|
|
sprout::tuples::get<2>(current) + 1
|
|
|
|
)
|
|
|
|
: sprout::detail::set_overlap_count_impl_1(
|
|
|
|
sprout::detail::set_overlap_count_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::tuples::tuple<InputIterator1, InputIterator2, typename std::iterator_traits<InputIterator1>::difference_type>
|
|
|
|
set_overlap_count_impl(
|
|
|
|
sprout::tuples::tuple<InputIterator1, InputIterator2, typename std::iterator_traits<InputIterator1>::difference_type> const& current,
|
|
|
|
InputIterator1 last1, InputIterator2 last2, Compare comp, typename std::iterator_traits<InputIterator1>::difference_type n
|
|
|
|
)
|
|
|
|
{
|
|
|
|
typedef sprout::tuples::tuple<InputIterator1, InputIterator2, typename std::iterator_traits<InputIterator1>::difference_type> type;
|
|
|
|
return sprout::tuples::get<0>(current) == last1 || sprout::tuples::get<1>(current) == last2 ? current
|
|
|
|
: sprout::detail::set_overlap_count_impl(
|
|
|
|
sprout::detail::set_overlap_count_impl_1(
|
|
|
|
current,
|
|
|
|
last1, last2, comp, n
|
|
|
|
),
|
|
|
|
last1, last2, comp, n * 2
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2012-06-23 04:22:50 +00:00
|
|
|
//
|
|
|
|
// set_overlap_count
|
|
|
|
//
|
2013-01-03 08:01:50 +00:00
|
|
|
// recursion depth:
|
|
|
|
// O(log N)
|
|
|
|
//
|
2012-06-23 04:22:50 +00:00
|
|
|
template<typename InputIterator1, typename InputIterator2, typename Compare>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR typename std::iterator_traits<InputIterator1>::difference_type
|
|
|
|
set_overlap_count(
|
|
|
|
InputIterator1 first1, InputIterator1 last1,
|
|
|
|
InputIterator2 first2, InputIterator2 last2,
|
2012-06-23 04:22:50 +00:00
|
|
|
Compare comp
|
|
|
|
)
|
|
|
|
{
|
2013-01-03 08:01:50 +00:00
|
|
|
typedef sprout::tuples::tuple<InputIterator1, InputIterator2, typename std::iterator_traits<InputIterator1>::difference_type> type;
|
|
|
|
return sprout::tuples::get<2>(
|
|
|
|
sprout::detail::set_overlap_count_impl(type(first1, first2, 0), last1, last2, comp, 1)
|
|
|
|
);
|
2012-06-23 04:22:50 +00:00
|
|
|
}
|
2012-12-16 17:14:07 +00:00
|
|
|
|
2012-06-23 04:22:50 +00:00
|
|
|
template<typename InputIterator1, typename InputIterator2>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR typename std::iterator_traits<InputIterator1>::difference_type
|
|
|
|
set_overlap_count(
|
|
|
|
InputIterator1 first1, InputIterator1 last1,
|
|
|
|
InputIterator2 first2, InputIterator2 last2
|
2012-06-23 04:22:50 +00:00
|
|
|
)
|
|
|
|
{
|
2012-12-16 17:14:07 +00:00
|
|
|
return sprout::detail::set_overlap_count(first1, last1, first2, last2, sprout::less<>());
|
2012-06-23 04:22:50 +00:00
|
|
|
}
|
|
|
|
} // namespace detail
|
|
|
|
} // namespace sprout
|
|
|
|
|
2012-06-23 23:22:12 +00:00
|
|
|
#endif // #ifndef SPROUT_DETAIL_ALGORITHM_SET_OVERLAP_COUNT_HPP
|