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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
//! This module implements an iterator of forests, which provides
//! querying capabilities.
use super::*;
use graph::{error::Error as GError, labelled::SLGraph, Graph};
use graph_macro::Graph;
/// The label of a query graph.
///
/// Such a label is responsible for matching forest labels.
pub trait QueryLabel: GraphLabel {
/// The type of labels this matches against.
type Obj: GraphLabel;
/// Determine whether or not a label matches this query label.
fn matches(&self, label: Self::Obj, status: ForestLabelType) -> bool {
let _ = label;
let _ = status;
false
}
/// Perform the query for one single query label.
fn run<F>(
&self,
forest: &F,
node: usize,
packs: &mut Vec<(usize, usize)>,
) -> Result<Option<usize>, QRError>
where
F: LabelGraph<ForestLabel<<Self as QueryLabel>::Obj>>,
{
let mut dfs_stack: Vec<usize> = vec![node];
let nodes_len = forest.nodes_len();
while let Some(node) = dfs_stack.pop() {
let label = match forest.vertex_label(node) {
Ok(None) => return Err(QRError::NodeNoLabel(node)),
Err(_) => return Err(QRError::IndexOutOfBounds(node, nodes_len)),
Ok(Some(label)) => label,
};
let status = label.status();
let label = label.label();
if status == ForestLabelType::Packed {
packs.push((node, 0));
dfs_stack.extend(forest.children_of(node).unwrap().take(1));
continue;
}
if self.matches(label, status) {
return Ok(Some(node));
}
dfs_stack.extend(
forest
.children_of(node)
.unwrap()
.collect::<Vec<_>>()
.into_iter()
.rev(),
);
}
Ok(None)
}
}
/// How should the current query skip nodes
#[derive(Debug, Clone, Copy, Default, Ord, PartialOrd, Hash, Eq, PartialEq)]
pub enum SkipState {
/// Skip anything in the depth-first search
Any,
/// Skip siblings
Sibling,
/// Do not skip
#[default]
None,
}
impl std::fmt::Display for SkipState {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Any => write!(f, "skip anything"),
Self::Sibling => write!(f, "skip siblings"),
Self::None => write!(f, "no skipping"),
}
}
}
impl From<bool> for SkipState {
fn from(value: bool) -> Self {
if value {
Self::Sibling
} else {
Self::None
}
}
}
/// The type that represents queries as dependency graphs of query
/// labels.
///
/// The labels of vertices are query labels that match against forest
/// labels, whereas the labels of graphs indicate whether or not the
/// edge should allow skipping siblings before matching.
#[derive(Debug, Clone, Graph)]
pub struct Query<T: QueryLabel> {
graph: SLGraph<SkipState>,
vertex_labels: Vec<T>,
root: Option<usize>,
}
impl<T: QueryLabel> Query<T> {
/// Construct a new query from raw fields.
pub fn new(graph: SLGraph<SkipState>, vertex_labels: Vec<T>, root: Option<usize>) -> Self {
Self {
graph,
vertex_labels,
root,
}
}
/// Return the root of this query.
///
/// This returns `None` if and only if the graph is empty.
pub fn root(&self) -> Option<usize> {
self.root
}
/// Return the label of the given node.
///
/// This cannot be implemented as a method of the `LabelGraph`
/// trait as the type differs from that of the labels.
#[inline]
pub fn label_of_vertex(&self, node: usize) -> Result<T, GError> {
self.vertex_labels
.get(node)
.copied()
.ok_or(GError::IndexOutOfBounds(node, self.vertex_labels.len()))
}
}
impl<T: QueryLabel> Default for Query<T> {
fn default() -> Self {
let graph = Default::default();
let vertex_labels = Vec::new();
let root = None;
Self {
graph,
vertex_labels,
root,
}
}
}
// delegation of labelgraph
impl<T: QueryLabel> LabelGraph<SkipState> for Query<T> {
type Iter<'a> = <SLGraph<SkipState> as LabelGraph<SkipState>>::Iter<'a>
where
Self: 'a;
type TupleIter<'a> = <SLGraph<SkipState> as LabelGraph<SkipState>>::TupleIter<'a>
where
Self: 'a;
type LabelIter<'a> = <SLGraph<SkipState> as LabelGraph<SkipState>>::LabelIter<'a>
where
Self: 'a;
type EdgeLabelIter<'a> = <SLGraph<SkipState> as LabelGraph<SkipState>>::EdgeLabelIter<'a>
where
Self: 'a;
#[inline]
fn edge_label(&self, source: usize, target: usize) -> Result<Self::EdgeLabelIter<'_>, GError> {
self.graph.edge_label(source, target)
}
fn find_children_with_label(
&self,
_node_id: usize,
_label: &SkipState,
) -> Result<<Self as LabelGraph<SkipState>>::Iter<'_>, GError> {
unimplemented!("Unsupported operation")
}
fn labels_of(&self, _node_id: usize) -> Result<Self::LabelIter<'_>, GError> {
unimplemented!("Unsupported operation")
}
#[inline]
fn tuples_of(&self, node_id: usize) -> Result<Self::TupleIter<'_>, GError> {
self.graph.tuples_of(node_id)
}
#[inline]
fn has_edge_label(
&self,
node_id: usize,
label: &SkipState,
target: usize,
) -> Result<bool, GError> {
self.graph.has_edge_label(node_id, label, target)
}
}
#[derive(Debug, Clone)]
/// The type of errors that can occur when querying forests.
pub enum QRError {
/// AN index is out of bounds.
IndexOutOfBounds(usize, usize),
/// A node has no labels while it is required to have one.
NodeNoLabel(usize),
/// Encounter an invalid error in converting from an error of
/// graphs.
InvalidGraphError(GError),
}
impl std::fmt::Display for QRError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
QRError::IndexOutOfBounds(n, b) => {
write!(f, "index {n} out of bound {b}")
}
QRError::NodeNoLabel(n) => write!(f, "the node {n} has no label but should have one"),
QRError::InvalidGraphError(g) => {
write!(f, "an invalid error for forest iterators: {g}")
}
}
}
}
impl std::error::Error for QRError {}
impl From<GError> for QRError {
fn from(value: GError) -> Self {
match value {
GError::IndexOutOfBounds(n, b) => Self::IndexOutOfBounds(n, b),
_ => Self::InvalidGraphError(value),
}
}
}
/// An implementation of the forest iterator for querying purposes.
#[derive(Debug, Clone)]
pub struct QRun<T: QueryLabel> {
/// The currently visited node.
current: Option<usize>,
/// The queries dependency graph.
query: Query<T>,
/// The packed nodes that can be used to change to another branch
/// if needed.
packs: Vec<usize>,
}
impl<T: QueryLabel> Default for QRun<T> {
fn default() -> Self {
let current = Default::default();
let query = Default::default();
let packs = Default::default();
Self {
current,
query,
packs,
}
}
}
impl<T: QueryLabel> ForestIter for QRun<T> {
#[inline]
fn current(&self) -> Option<usize> {
self.current
}
type ForestLabel = <T as QueryLabel>::Obj;
type Item<'a> = Option<&'a [usize]>
where
Self: 'a;
type Error = QRError;
#[allow(unused)]
#[allow(clippy::collapsible_if)]
fn next<F>(&mut self, forest: &F, root: usize) -> Result<State, Self::Error>
where
F: LabelGraph<ForestLabel<Self::ForestLabel>>,
{
let query_root = if let Some(r) = self.query.root() {
r
} else {
return Ok(State::Finish);
};
#[derive(Debug, Clone, Copy)]
enum Item<T: Copy + Clone + std::fmt::Debug> {
Seen(T),
Unseen(T),
}
use Item::*;
impl<T: Copy + Clone + std::fmt::Debug> Item<T> {
fn is_seen(self) -> bool {
matches!(self, Self::Seen(_))
}
fn value(&self) -> T {
match self {
Seen(v) => *v,
Unseen(v) => *v,
}
}
}
let query_len = self.query.nodes_len();
let mut stack: Vec<Item<(SkipState, usize, usize)>> = Vec::with_capacity(query_len);
stack.push(Unseen((SkipState::Any, root, query_root)));
let mut parents_stack: Vec<usize> = Vec::with_capacity(query_len);
let mut match_result = false;
while let Some(item) = stack.pop() {
if item.is_seen() {
if match_result {
continue;
}
// fallback
}
let (state, top, qtop) = item.value();
let top_label = forest.vertex_label(top)?.ok_or(QRError::NodeNoLabel(top))?;
let query_label = self.query.label_of_vertex(qtop)?;
if query_label.matches(top_label.label(), top_label.status()) {
// Success branch
match_result = true;
// TODO: Deal with captures later
} else {
// Failure branch
match_result = false;
}
}
todo!()
}
#[inline]
fn clear(&mut self) {
self.current.take();
self.packs.clear();
}
}
|