2013-08-08 09:54:33 +00:00
|
|
|
/*=============================================================================
|
2016-02-25 09:48:28 +00:00
|
|
|
Copyright (c) 2011-2016 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)
|
|
|
|
=============================================================================*/
|
2013-01-25 06:14:29 +00:00
|
|
|
#ifndef SPROUT_ITERATOR_SET_UNION_ITERATOR_HPP
|
|
|
|
#define SPROUT_ITERATOR_SET_UNION_ITERATOR_HPP
|
|
|
|
|
|
|
|
#include <iterator>
|
|
|
|
#include <utility>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <sprout/config.hpp>
|
|
|
|
#include <sprout/iterator/next.hpp>
|
|
|
|
#include <sprout/iterator/type_traits/common.hpp>
|
2013-01-26 12:30:29 +00:00
|
|
|
#include <sprout/functional/less.hpp>
|
2013-01-25 06:14:29 +00:00
|
|
|
#include <sprout/utility/swap.hpp>
|
2013-02-07 14:12:57 +00:00
|
|
|
#include <sprout/utility/pair/pair.hpp>
|
2013-01-26 12:30:29 +00:00
|
|
|
#include <sprout/algorithm/next_union.hpp>
|
2013-01-25 06:14:29 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
//
|
|
|
|
// set_union_iterator
|
|
|
|
//
|
|
|
|
template<
|
|
|
|
typename LIterator, typename RIterator,
|
|
|
|
typename Compare = sprout::less<>
|
|
|
|
>
|
|
|
|
class set_union_iterator
|
|
|
|
: public std::iterator<
|
|
|
|
typename sprout::min_iterator_category<
|
|
|
|
typename sprout::common_iterator_category<LIterator, RIterator>::type,
|
|
|
|
std::forward_iterator_tag
|
|
|
|
>::type,
|
|
|
|
typename sprout::common_iterator_value_type<LIterator, RIterator>::type,
|
|
|
|
typename sprout::common_iterator_difference_type<LIterator, RIterator>::type,
|
|
|
|
typename sprout::common_iterator_pointer<LIterator, RIterator>::type,
|
|
|
|
typename sprout::common_iterator_reference<LIterator, RIterator>::type
|
|
|
|
>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef LIterator iterator_type;
|
|
|
|
typedef RIterator iterator2_type;
|
|
|
|
typedef Compare compare_type;
|
|
|
|
typedef typename sprout::min_iterator_category<
|
|
|
|
typename sprout::common_iterator_category<LIterator, RIterator>::type,
|
|
|
|
std::forward_iterator_tag
|
|
|
|
>::type iterator_category;
|
|
|
|
typedef typename sprout::common_iterator_value_type<LIterator, RIterator>::type value_type;
|
|
|
|
typedef typename sprout::common_iterator_difference_type<LIterator, RIterator>::type difference_type;
|
|
|
|
typedef typename sprout::common_iterator_pointer<LIterator, RIterator>::type pointer;
|
|
|
|
typedef typename sprout::common_iterator_reference<LIterator, RIterator>::type reference;
|
2013-01-26 12:30:29 +00:00
|
|
|
protected:
|
|
|
|
typedef sprout::pair<iterator_type, iterator2_type> pair_type;
|
2013-01-25 06:14:29 +00:00
|
|
|
private:
|
|
|
|
static SPROUT_CONSTEXPR bool check_in_left(
|
2016-04-01 14:37:48 +00:00
|
|
|
iterator_type it1, iterator_type las1,
|
|
|
|
iterator2_type it2, iterator2_type las2,
|
2013-01-25 06:14:29 +00:00
|
|
|
Compare comp
|
|
|
|
)
|
|
|
|
{
|
2016-04-01 14:37:48 +00:00
|
|
|
return it1 != las1 ? (it2 != las2 ? comp(*it1, *it2) || !comp(*it2, *it1) : true)
|
|
|
|
: !(it2 != las2)
|
2013-01-25 06:14:29 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
protected:
|
2013-01-26 12:30:29 +00:00
|
|
|
pair_type current;
|
2016-04-01 14:37:48 +00:00
|
|
|
iterator_type las1;
|
|
|
|
iterator2_type las2;
|
2013-01-25 06:14:29 +00:00
|
|
|
Compare comp;
|
2013-01-26 12:30:29 +00:00
|
|
|
private:
|
2013-01-25 06:14:29 +00:00
|
|
|
bool in_left;
|
2013-01-26 12:30:29 +00:00
|
|
|
private:
|
|
|
|
SPROUT_CONSTEXPR set_union_iterator(set_union_iterator const& other, pair_type const& next)
|
|
|
|
: current(next)
|
2016-04-01 14:37:48 +00:00
|
|
|
, las1(other.las1), las2(other.las2)
|
2013-01-26 12:30:29 +00:00
|
|
|
, comp(other.comp)
|
2016-04-01 14:37:48 +00:00
|
|
|
, in_left(check_in_left(next.first, other.las1, next.second, other.las2, other.comp))
|
2013-01-26 12:30:29 +00:00
|
|
|
{}
|
2013-01-25 06:14:29 +00:00
|
|
|
public:
|
2013-01-26 14:51:43 +00:00
|
|
|
SPROUT_CONSTEXPR set_union_iterator()
|
2016-04-01 14:37:48 +00:00
|
|
|
: current(), las1(), las2(), comp(), in_left(true)
|
2013-01-25 06:14:29 +00:00
|
|
|
{}
|
|
|
|
set_union_iterator(set_union_iterator const&) = default;
|
|
|
|
SPROUT_CONSTEXPR set_union_iterator(
|
2016-04-01 14:37:48 +00:00
|
|
|
iterator_type it1, iterator_type las1,
|
|
|
|
iterator2_type it2, iterator2_type las2,
|
2013-01-25 06:14:29 +00:00
|
|
|
Compare comp = Compare()
|
|
|
|
)
|
2013-01-26 12:30:29 +00:00
|
|
|
: current(it1, it2)
|
2016-04-01 14:37:48 +00:00
|
|
|
, las1(las1), las2(las2)
|
2013-01-25 06:14:29 +00:00
|
|
|
, comp(comp)
|
2016-04-01 14:37:48 +00:00
|
|
|
, in_left(check_in_left(it1, las1, it2, las2, comp))
|
2013-01-25 06:14:29 +00:00
|
|
|
{}
|
|
|
|
template<typename U, typename V, typename W>
|
|
|
|
SPROUT_CONSTEXPR set_union_iterator(set_union_iterator<U, V, W> const& it)
|
2013-01-26 12:30:29 +00:00
|
|
|
: current(it.base(), it.base2())
|
2016-04-01 14:37:48 +00:00
|
|
|
, las1(it.last1()), las2(it.last2())
|
2013-01-25 06:14:29 +00:00
|
|
|
, comp(it.compare())
|
|
|
|
, in_left(it.is_in_left())
|
|
|
|
{}
|
|
|
|
template<typename U, typename V, typename W>
|
2013-10-09 14:44:25 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR set_union_iterator& operator=(set_union_iterator<U, V, W> const& it) {
|
2013-01-25 06:14:29 +00:00
|
|
|
set_union_iterator temp(it);
|
|
|
|
temp.swap(*this);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR iterator_type base() const {
|
2013-01-26 12:30:29 +00:00
|
|
|
return current.first;
|
2013-01-25 06:14:29 +00:00
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR iterator_type last1() const {
|
2016-04-01 14:37:48 +00:00
|
|
|
return las1;
|
2013-01-25 06:14:29 +00:00
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR iterator2_type base2() const {
|
2013-01-26 12:30:29 +00:00
|
|
|
return current.second;
|
2013-01-25 06:14:29 +00:00
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR iterator2_type last2() const {
|
2016-04-01 14:37:48 +00:00
|
|
|
return las2;
|
2013-01-25 06:14:29 +00:00
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR Compare compare() const {
|
|
|
|
return comp;
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR bool is_in_left() const {
|
|
|
|
return in_left;
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR reference operator*() const {
|
2013-01-26 12:30:29 +00:00
|
|
|
return is_in_left() ? *current.first : *current.second;
|
2013-01-25 06:14:29 +00:00
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR pointer operator->() const {
|
|
|
|
return &*(*this);
|
|
|
|
}
|
2013-10-09 14:44:25 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR set_union_iterator& operator++() {
|
2016-04-01 14:37:48 +00:00
|
|
|
current = sprout::next_union(current.first, las1, current.second, las2, comp);
|
|
|
|
in_left = check_in_left(current.first, las1, current.second, las2, comp);
|
2013-01-25 06:14:29 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2013-10-09 14:44:25 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR set_union_iterator operator++(int) {
|
2013-01-25 06:14:29 +00:00
|
|
|
set_union_iterator result(*this);
|
2016-04-01 14:37:48 +00:00
|
|
|
current = sprout::next_union(current.first, las1, current.second, las2, comp);
|
|
|
|
in_left = check_in_left(current.first, las1, current.second, las2, comp);
|
2013-01-25 06:14:29 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR set_union_iterator next() const {
|
2013-01-26 12:30:29 +00:00
|
|
|
return set_union_iterator(
|
|
|
|
*this,
|
2016-04-01 14:37:48 +00:00
|
|
|
sprout::next_union(current.first, las1, current.second, las2, comp)
|
2013-01-26 12:30:29 +00:00
|
|
|
);
|
2013-01-25 06:14:29 +00:00
|
|
|
}
|
2013-10-09 14:44:25 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR void swap(set_union_iterator& other)
|
2014-08-14 13:55:49 +00:00
|
|
|
SPROUT_NOEXCEPT_IF(
|
2013-01-26 12:30:29 +00:00
|
|
|
SPROUT_NOEXCEPT_EXPR(sprout::swap(current, other.current))
|
2016-04-01 14:37:48 +00:00
|
|
|
&& SPROUT_NOEXCEPT_EXPR(sprout::swap(las1, other.las1))
|
|
|
|
&& SPROUT_NOEXCEPT_EXPR(sprout::swap(las2, other.las2))
|
2013-01-25 06:14:29 +00:00
|
|
|
&& SPROUT_NOEXCEPT_EXPR(sprout::swap(comp, other.comp))
|
|
|
|
)
|
|
|
|
{
|
2013-01-26 12:30:29 +00:00
|
|
|
sprout::swap(current, other.current);
|
2016-04-01 14:37:48 +00:00
|
|
|
sprout::swap(las1, other.las1);
|
|
|
|
sprout::swap(las2, other.las2);
|
2013-01-25 06:14:29 +00:00
|
|
|
sprout::swap(comp, other.comp);
|
|
|
|
sprout::swap(in_left, other.in_left);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<
|
|
|
|
typename LIterator1, typename RIterator1, typename Compare1,
|
|
|
|
typename LIterator2, typename RIterator2, typename Compare2
|
|
|
|
>
|
|
|
|
inline SPROUT_CONSTEXPR bool operator==(
|
|
|
|
sprout::set_union_iterator<LIterator1, RIterator1, Compare1> const& lhs,
|
|
|
|
sprout::set_union_iterator<LIterator2, RIterator2, Compare2> const& rhs
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return lhs.base() == rhs.base() && lhs.base2() == rhs.base2();
|
|
|
|
}
|
|
|
|
template<
|
|
|
|
typename LIterator1, typename RIterator1, typename Compare1,
|
|
|
|
typename LIterator2, typename RIterator2, typename Compare2
|
|
|
|
>
|
|
|
|
inline SPROUT_CONSTEXPR bool operator!=(
|
|
|
|
sprout::set_union_iterator<LIterator1, RIterator1, Compare1> const& lhs,
|
|
|
|
sprout::set_union_iterator<LIterator2, RIterator2, Compare2> const& rhs
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// make_set_union_iterator
|
|
|
|
//
|
|
|
|
template<typename LIterator, typename RIterator, typename Compare>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::set_union_iterator<LIterator, RIterator, Compare>
|
2016-04-01 14:37:48 +00:00
|
|
|
make_set_union_iterator(LIterator it1, LIterator las1, RIterator it2, RIterator las2, Compare comp) {
|
|
|
|
return sprout::set_union_iterator<LIterator, RIterator, Compare>(it1, las1, it2, las2, comp);
|
2013-01-25 06:14:29 +00:00
|
|
|
}
|
|
|
|
template<typename LIterator, typename RIterator>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::set_union_iterator<LIterator, RIterator>
|
2016-04-01 14:37:48 +00:00
|
|
|
make_set_union_iterator(LIterator it1, LIterator las1, RIterator it2, RIterator las2) {
|
|
|
|
return sprout::set_union_iterator<LIterator, RIterator>(it1, las1, it2, las2);
|
2013-01-25 06:14:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// swap
|
|
|
|
//
|
|
|
|
template<typename LIterator, typename RIterator, typename Compare>
|
2013-10-09 14:44:25 +00:00
|
|
|
inline SPROUT_CXX14_CONSTEXPR void
|
2013-01-25 06:14:29 +00:00
|
|
|
swap(
|
|
|
|
sprout::set_union_iterator<LIterator, RIterator, Compare>& lhs,
|
|
|
|
sprout::set_union_iterator<LIterator, RIterator, Compare>& rhs
|
|
|
|
)
|
2014-08-14 13:55:49 +00:00
|
|
|
SPROUT_NOEXCEPT_IF_EXPR(lhs.swap(rhs))
|
2013-01-25 06:14:29 +00:00
|
|
|
{
|
|
|
|
lhs.swap(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// iterator_next
|
|
|
|
//
|
|
|
|
template<typename LIterator, typename RIterator, typename Compare>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::set_union_iterator<LIterator, RIterator, Compare>
|
|
|
|
iterator_next(sprout::set_union_iterator<LIterator, RIterator, Compare> const& it) {
|
|
|
|
return it.next();
|
|
|
|
}
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_ITERATOR_SET_UNION_ITERATOR_HPP
|