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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use std::ops::{Bound, RangeBounds};
use crate::{
graph::*,
query::{GraphQuery, SubGraphs},
};
pub struct TrackedNodes<I> {
begin: Bound<I>,
end: Bound<I>,
}
impl<I> TrackedNodes<I> {
pub fn new() -> Self {
Self {
begin: Bound::Unbounded,
end: Bound::Unbounded,
}
}
pub fn range<R>(range: R) -> Self
where
I: Copy,
R: RangeBounds<I>,
{
Self {
begin: range.start_bound().cloned(),
end: range.end_bound().cloned(),
}
}
pub fn handle<'a, G>(&'a mut self, graph: &'a G) -> TrackedNodesIter<'a, I, G>
where
G: Graph<Idx = I>,
I: Idx,
{
let iter = graph.range(OwnedBounds(self.begin, self.end));
TrackedNodesIter {
owner: self,
inner: iter,
}
}
}
impl<I> Default for TrackedNodes<I> {
fn default() -> Self {
Self::new()
}
}
pub struct TrackedNodesIter<'a, I, G: ItemIter> {
owner: &'a mut TrackedNodes<I>,
inner: NodeRangeIterType<'a, G>,
}
impl<'a, I, G> Iterator for TrackedNodesIter<'a, I, G>
where
G: Graph<Idx = I>,
I: Idx,
{
type Item = NodeType<'a, G>;
fn next(&mut self) -> Option<Self::Item> {
let ret = self.inner.next();
if let Some(item) = &ret {
self.owner.begin = Bound::Excluded(item.id());
}
ret
}
}
pub struct TrackedSubGraphs<I> {
ids: Vec<I>,
cursor: Option<I>,
}
impl<I> TrackedSubGraphs<I> {
pub fn new(ids: Vec<I>) -> Self {
Self { ids, cursor: None }
}
pub fn handle<'a, G>(&'a mut self, graph: &'a G) -> TrackedSubGraphsIter<'a, I, G>
where
G: Graph<Idx = I>,
I: Idx,
{
let mut iter = self
.ids
.iter()
.map(|i| graph.index(*i))
.roots_for_graph(graph);
if let Some(cursor) = self.cursor {
iter.by_ref().take_while(|el| el.id() != cursor).count();
}
TrackedSubGraphsIter {
owner: self,
inner: iter,
}
}
pub fn add_id(&mut self, id: I) {
self.ids.push(id);
}
pub fn ids(&self) -> &Vec<I> {
&self.ids
}
}
pub struct TrackedSubGraphsIter<'a, I, G: Graph> {
owner: &'a mut TrackedSubGraphs<I>,
inner: SubGraphs<'a, G>,
}
impl<'a, I, G> Iterator for TrackedSubGraphsIter<'a, I, G>
where
G: Graph<Idx = I>,
I: Idx,
{
type Item = NodeType<'a, G>;
fn next(&mut self) -> Option<Self::Item> {
let ret = self.inner.next();
if let Some(item) = &ret {
self.owner.cursor = Some(item.id());
}
ret
}
}
pub struct OwnedBounds<I>(Bound<I>, Bound<I>);
impl<I> RangeBounds<I> for OwnedBounds<I> {
fn start_bound(&self) -> Bound<&I> {
match self.0 {
Bound::Included(ref x) => Bound::Included(x),
Bound::Excluded(ref x) => Bound::Excluded(x),
Bound::Unbounded => Bound::Unbounded,
}
}
fn end_bound(&self) -> Bound<&I> {
match self.1 {
Bound::Included(ref x) => Bound::Included(x),
Bound::Excluded(ref x) => Bound::Excluded(x),
Bound::Unbounded => Bound::Unbounded,
}
}
}