-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparent_iterator.hpp
131 lines (111 loc) · 3.81 KB
/
parent_iterator.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright Morten Bendiksen 2014 - 2016.
// 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)
#ifndef MEDIASEQUENCER_PLUGIN_UTIL_XPATH_PARENT_ITERATOR
#define MEDIASEQUENCER_PLUGIN_UTIL_XPATH_PARENT_ITERATOR
#include <boost/iterator/iterator_facade.hpp>
#include <boost/range/iterator_range.hpp>
#include <boost/range/adaptor/filtered.hpp>
#include <memory>
namespace mediasequencer { namespace plugin { namespace util { namespace xpath {
// An iterator over a range of context nodes. It is given another range
// of nodes and iterates through the parents of all nodes in that
// other range.
template <typename ParentIterator>
class parent_iterator : public boost::iterator_facade<
parent_iterator<ParentIterator>,
typename ParentIterator::value_type,
boost::forward_traversal_tag> {
private:
typedef boost::iterator_facade<
parent_iterator<ParentIterator>,
typename ParentIterator::value_type,
boost::forward_traversal_tag> super;
public:
parent_iterator() {}
parent_iterator(ParentIterator begin, ParentIterator end) {
if (begin != end) {
d.reset(new data(std::move(begin), std::move(end)));
d->c.reset(new typename super::value_type(*(d->parent_it)));
if (d->c->is_root()) {
increment();
} else {
d->c->parent();
}
}
};
parent_iterator(parent_iterator<ParentIterator> const& other) {
if (other.d) {
d.reset(new data(*(other.d)));
}
}
parent_iterator(parent_iterator<ParentIterator>&& other)
: d(std::move(other.d)) {
}
parent_iterator& operator=(parent_iterator<ParentIterator> const& other) {
if (other.d) {
d.reset(new data(*(other.d)));
} else {
d.reset();
}
return *this;
}
parent_iterator& operator=(parent_iterator<ParentIterator>&& other) {
d = std::move(other.d);
return *this;
}
void increment() {
++(d->parent_it);
if (d->parent_it != d->parent_end) {
d->c.reset(new typename super::value_type(*(d->parent_it)));
if (d->c->is_root()) {
increment();
} else {
d->c->parent();
}
} else {
d.reset();
}
}
bool equal(parent_iterator const& other) const {
if (!d || !other.d) {
return d.get() == other.d.get();
}
assert(d->c && other.d->c);
return
*(d->c) == *(other.d->c) &&
d->parent_it == other.d->parent_it &&
d->parent_end == other.d->parent_end;
}
typename super::reference dereference() const {
return *(d->c);
}
private:
struct data {
data(ParentIterator parent_it,
ParentIterator parent_end)
: parent_it(parent_it),
parent_end(parent_end),
c(std::make_shared<typename super::value_type>(*parent_it)) {
}
data(data const& other)
: parent_it(other.parent_it),
parent_end(other.parent_end),
c(std::make_shared<typename super::value_type>(*(other.c))) {
}
ParentIterator parent_it;
ParentIterator parent_end;
std::shared_ptr<typename super::value_type> c;
};
std::unique_ptr<data> d;
};
template <typename Range>
boost::iterator_range<parent_iterator<typename Range::iterator> >
make_parent(Range const& r) {
return boost::make_iterator_range
(parent_iterator<typename Range::iterator>(r.begin(), r.end()),
parent_iterator<typename Range::iterator>());
}
}}}}
#endif //MEDIASEQUENCER_PLUGIN_UTIL_XPATH_PARENT_ITERATOR