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
use crate::ElementId;
use crate::graph::Graph;
use crate::walker::builder::{EdgeWalkerBuilder, VertexWalkerBuilder};
use crate::walker::{EdgeWalker, VertexWalker, Walker};
use include_doc::function_body;
use std::marker::PhantomData;
// ================ FILTER IMPLEMENTATION ================
pub struct VertexFilter<'graph, Parent, Predicate> {
_phantom_data: PhantomData<&'graph ()>,
parent: Parent,
predicate: Predicate,
}
impl<Parent, Predicate> VertexFilter<'_, Parent, Predicate> {
pub(crate) fn new(parent: Parent, predicate: Predicate) -> Self {
VertexFilter {
_phantom_data: Default::default(),
parent,
predicate,
}
}
}
impl<'graph, Parent, Predicate> Walker<'graph> for VertexFilter<'graph, Parent, Predicate>
where
Parent: VertexWalker<'graph>,
Predicate: Fn(&<Parent::Graph as Graph>::VertexReference<'_>, &Parent::Context) -> bool,
{
type Graph = Parent::Graph;
type Context = Parent::Context;
fn next_element(&mut self, graph: &'graph Self::Graph) -> Option<ElementId<Self::Graph>> {
self.next(graph).map(ElementId::Vertex)
}
fn ctx(&self) -> &Self::Context {
self.parent.ctx()
}
fn ctx_mut(&mut self) -> &mut Self::Context {
self.parent.ctx_mut()
}
}
impl<'graph, Parent, Predicate> VertexWalker<'graph> for VertexFilter<'graph, Parent, Predicate>
where
Parent: VertexWalker<'graph>,
Predicate: Fn(&<Parent::Graph as Graph>::VertexReference<'_>, &Parent::Context) -> bool,
{
fn next(&mut self, graph: &'graph Self::Graph) -> Option<<Self::Graph as Graph>::VertexId> {
while let Some(next) = self.parent.next(graph) {
if let Some(vertex) = graph.vertex(next) {
if (self.predicate)(&vertex, self.parent.ctx()) {
return Some(next);
}
}
}
None
}
}
pub struct EdgeFilter<'graph, Parent, Predicate> {
_phantom_data: PhantomData<&'graph ()>,
parent: Parent,
predicate: Predicate,
}
impl<Parent, Predicate> EdgeFilter<'_, Parent, Predicate> {
pub(crate) fn new(parent: Parent, predicate: Predicate) -> Self {
EdgeFilter {
_phantom_data: Default::default(),
parent,
predicate,
}
}
}
impl<'graph, Parent, Predicate> Walker<'graph> for EdgeFilter<'graph, Parent, Predicate>
where
Parent: EdgeWalker<'graph>,
Predicate: Fn(&<Parent::Graph as Graph>::EdgeReference<'_>, &Parent::Context) -> bool,
{
type Graph = Parent::Graph;
type Context = Parent::Context;
fn next_element(&mut self, graph: &'graph Self::Graph) -> Option<ElementId<Self::Graph>> {
self.next(graph).map(ElementId::Edge)
}
fn ctx(&self) -> &Self::Context {
self.parent.ctx()
}
fn ctx_mut(&mut self) -> &mut Self::Context {
self.parent.ctx_mut()
}
}
impl<'graph, Parent, Predicate> EdgeWalker<'graph> for EdgeFilter<'graph, Parent, Predicate>
where
Parent: EdgeWalker<'graph>,
Predicate: Fn(&<Parent::Graph as Graph>::EdgeReference<'_>, &Parent::Context) -> bool,
{
fn next(&mut self, graph: &'graph Self::Graph) -> Option<<Self::Graph as Graph>::EdgeId> {
while let Some(next) = self.parent.next(graph) {
let edge = graph.edge(next).expect("edge must exist");
if (self.predicate)(&edge, self.parent.ctx()) {
return Some(next);
}
}
None
}
}
impl<'graph, Mutability, Graph, Walker> VertexWalkerBuilder<'graph, Mutability, Graph, Walker>
where
Graph: crate::graph::Graph,
Walker: VertexWalker<'graph, Graph = Graph>,
{
/// # Filter Step
///
/// The `filter` step allows you to keep only vertices that match a predicate function.
/// Vertices that don't match the predicate are excluded from further traversal.
///
/// ## Visual Diagram
///
/// Before filter step (all vertices in traversal):
/// ```text
/// [Person A]* --- knows ---> [Person B]* --- created ---> [Project]*
/// ^
/// |
/// owns
/// |
/// [Company C]*
/// ```
///
/// After filter(is_project) step (only Project vertices remain in traversal):
/// ```text
/// [Person A] --- knows ---> [Person B] --- created ---> [Project]*
/// ^
/// |
/// owns
/// |
/// [Company C]
/// ```
///
/// ## Parameters
///
/// - `predicate`: A function that takes a reference to a vertex and its context, and returns a boolean.
/// Only vertices for which this function returns `true` will be included in the traversal.
///
/// ## Return Value
///
/// A new walker containing only the vertices that matched the predicate.
///
/// ## Example
///
/// ```rust
#[doc = function_body!("examples/filter.rs", vertex_example, [])]
/// ```
///
/// ## Notes
///
/// - The filter step does not modify the graph, only the traversal
/// - For complex filtering logic, consider breaking into multiple filter steps for better readability
/// - Use type projections or pattern matching when filtering to access type-specific methods and properties
/// - The filter is applied lazily during traversal, not when the step is added to the walker
pub fn filter<Predicate>(
self,
predicate: Predicate,
) -> VertexWalkerBuilder<'graph, Mutability, Graph, VertexFilter<'graph, Walker, Predicate>>
where
Predicate: Fn(&Graph::VertexReference<'_>, &Walker::Context) -> bool,
{
self.with_vertex_walker(|walker| walker.filter(predicate))
}
}
impl<'graph, Mutability, Graph, Walker> EdgeWalkerBuilder<'graph, Mutability, Graph, Walker>
where
Graph: crate::graph::Graph,
Walker: EdgeWalker<'graph, Graph = Graph>,
<Walker as crate::walker::Walker<'graph>>::Context: Clone + 'static,
{
/// # Filter Step
///
/// The `filter` step allows you to keep only edges that match a predicate function.
/// Edges that don't match the predicate are excluded from further traversal.
///
/// ## Visual Diagram
///
/// Before filter step (all edges in traversal):
/// ```text
/// [Person A] --- knows(2018)* ---> [Person B] --- created(2022)* ---> [Project]
/// ^
/// |
/// owns(2020)*
/// |
/// [Company C]
/// ```
///
/// After filter(year >= 2020) step (only recent edges remain in traversal):
/// ```text
/// [Person A] --- knows(2018) ---> [Person B] --- created(2022)* ---> [Project]
/// ^
/// |
/// owns(2020)*
/// |
/// [Company C]
/// ```
///
/// ## Parameters
///
/// - `predicate`: A function that takes a reference to an edge and its context, and returns a boolean.
/// Only edges for which this function returns `true` will be included in the traversal.
///
/// ## Return Value
///
/// A new walker containing only the edges that matched the predicate.
///
/// ## Example
///
/// ```rust
#[doc = function_body!("examples/filter.rs", edge_example, [])]
/// ```
///
/// ## Notes
///
/// - The filter step does not modify the graph, only the traversal
/// - For complex filtering logic, consider breaking into multiple filter steps for better readability
/// - Use pattern matching to work with different edge types
/// - The filter is applied lazily during traversal, not when the step is added to the walker
pub fn filter<Predicate>(
self,
predicate: Predicate,
) -> EdgeWalkerBuilder<'graph, Mutability, Graph, EdgeFilter<'graph, Walker, Predicate>>
where
Predicate: Fn(&Graph::EdgeReference<'_>, &Walker::Context) -> bool,
{
self.with_edge_walker(|walker| walker.filter(predicate))
}
}