-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathancestor_iterator.hpp
136 lines (114 loc) · 4.06 KB
/
ancestor_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
132
133
134
135
136
// 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_ANCESTOR_ITERATOR
#define MEDIASEQUENCER_PLUGIN_UTIL_XPATH_ANCESTOR_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 all ancestors of all nodes in that
// other range.
template <typename ParentIterator>
class ancestor_iterator : public boost::iterator_facade<
ancestor_iterator<ParentIterator>,
typename ParentIterator::value_type,
boost::forward_traversal_tag> {
private:
typedef boost::iterator_facade<
ancestor_iterator<ParentIterator>,
typename ParentIterator::value_type,
boost::forward_traversal_tag> super;
public:
ancestor_iterator() {}
ancestor_iterator(ParentIterator begin, ParentIterator end) {
if (begin != end) {
d.reset(new data(std::move(begin), std::move(end)));
increment();
}
}
ancestor_iterator(ancestor_iterator<ParentIterator> const& other) {
if (other.d) {
d.reset(new data(*(other.d)));
}
}
ancestor_iterator(ancestor_iterator<ParentIterator>&& other) {
d = std::move(other.d);
}
ancestor_iterator& operator=(ancestor_iterator<ParentIterator> const& other) {
if (other.d) {
d.reset(new data(*(other.d)));
} else {
d.reset();
}
return *this;
}
ancestor_iterator& operator=(ancestor_iterator<ParentIterator>&& other) {
d = std::move(other.d);
return *this;
}
void increment() {
if (d->c->is_root() || d->c->is_null()) {
++(d->parent_it);
if (d->parent_it != d->parent_end) {
d->c.reset(new typename super::value_type (*(d->parent_it)));
increment();
} else {
d.reset();
return;
}
} else {
d->c->parent();
}
}
bool equal(ancestor_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:
// the data is put in a unique_ptr, so that it is nullable
// in order to preserve memory
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)))
{
}
// An iterator in the input range. The iterator points to
// a decendant of the node this iterator points to.
ParentIterator parent_it;
// Represents the end of the input range
ParentIterator parent_end;
// points to the context we are at in this iterator
std::shared_ptr<typename super::value_type> c;
};
std::unique_ptr<data> d;
};
template <typename Range>
boost::iterator_range<ancestor_iterator<typename Range::iterator> >
make_ancestor(Range const& r) {
return boost::make_iterator_range
(ancestor_iterator<typename Range::iterator>(r.begin(), r.end()),
ancestor_iterator<typename Range::iterator>());
}
}}}}
#endif // MEDIASEQUENCER_PLUGIN_UTIL_XPATH_ANCESTOR_ITERATOR