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
use crate::ElementId;
use crate::graph::{
EdgeReference as GraphEdgeReference, Graph, VertexReference as GraphVertexReference,
};
use crate::walker::builder::{EdgeWalkerBuilder, VertexWalkerBuilder};
use crate::walker::{EdgeWalker, VertexWalker, Walker};
use include_doc::function_body;
use std::marker::PhantomData;
// ================ REDUCE IMPLEMENTATION ================
pub struct VertexReduce<'graph, Parent, Reducer>
where
Parent: Walker<'graph>,
{
_phantom_data: PhantomData<&'graph ()>,
parent: Parent,
reducer: Reducer,
}
impl<'graph, Parent, Reducer> VertexReduce<'graph, Parent, Reducer>
where
Parent: Walker<'graph>,
{
pub(crate) fn new(parent: Parent, reducer: Reducer) -> Self {
VertexReduce {
_phantom_data: Default::default(),
parent,
reducer,
}
}
}
impl<'graph, Parent, Reducer> Walker<'graph> for VertexReduce<'graph, Parent, Reducer>
where
Parent: VertexWalker<'graph>,
Parent::Graph: 'graph,
Reducer: for<'a> Fn(
&'a <Parent::Graph as Graph>::VertexReference<'graph>,
&'a <Parent::Graph as Graph>::VertexReference<'graph>,
&Parent::Context,
) -> &'a <Parent::Graph as Graph>::VertexReference<'graph>,
{
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, Reducer> VertexWalker<'graph> for VertexReduce<'graph, Parent, Reducer>
where
Parent: VertexWalker<'graph>,
Parent::Graph: 'graph,
Reducer: for<'a> Fn(
&'a <Parent::Graph as Graph>::VertexReference<'graph>,
&'a <Parent::Graph as Graph>::VertexReference<'graph>,
&Parent::Context,
) -> &'a <Parent::Graph as Graph>::VertexReference<'graph>,
Parent::Context: Clone,
{
fn next(&mut self, graph: &'graph Self::Graph) -> Option<<Self::Graph as Graph>::VertexId> {
let mut acc_vertex = None;
loop {
if let Some(next) = self.parent.next(graph) {
let vertex_reference = graph.vertex(next).expect("vertex must exist");
if let Some(acc_vertex_ref) = &acc_vertex {
let result =
(self.reducer)(acc_vertex_ref, &vertex_reference, self.parent.ctx());
if std::ptr::eq(result, &vertex_reference) {
acc_vertex = Some(vertex_reference);
}
} else {
// For the first element, we don't apply the reducer, just set it as the accumulator
acc_vertex = Some(vertex_reference);
}
} else {
return acc_vertex.map(|acc| acc.id());
}
}
}
}
pub struct EdgeReduce<'graph, Parent, Reducer>
where
Parent: Walker<'graph>,
{
_phantom_data: PhantomData<&'graph ()>,
parent: Parent,
reducer: Reducer,
}
impl<'graph, Parent, Reducer> EdgeReduce<'graph, Parent, Reducer>
where
Parent: Walker<'graph>,
{
pub(crate) fn new(parent: Parent, reducer: Reducer) -> Self {
EdgeReduce {
_phantom_data: Default::default(),
parent,
reducer,
}
}
}
impl<'graph, Parent, Reducer> Walker<'graph> for EdgeReduce<'graph, Parent, Reducer>
where
Parent: EdgeWalker<'graph>,
Parent::Graph: 'graph,
Reducer: for<'a> Fn(
&'a <Parent::Graph as Graph>::EdgeReference<'graph>,
&'a <Parent::Graph as Graph>::EdgeReference<'graph>,
&Parent::Context,
) -> &'a <Parent::Graph as Graph>::EdgeReference<'graph>,
{
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, Reducer> EdgeWalker<'graph> for EdgeReduce<'graph, Parent, Reducer>
where
Parent: EdgeWalker<'graph>,
Parent::Graph: 'graph,
Reducer: for<'a> Fn(
&'a <Parent::Graph as Graph>::EdgeReference<'graph>,
&'a <Parent::Graph as Graph>::EdgeReference<'graph>,
&Parent::Context,
) -> &'a <Parent::Graph as Graph>::EdgeReference<'graph>,
Parent::Context: Clone,
{
fn next(&mut self, graph: &'graph Self::Graph) -> Option<<Self::Graph as Graph>::EdgeId> {
let mut acc_edge = None;
loop {
if let Some(next) = self.parent.next(graph) {
let edge_reference = graph.edge(next).expect("edge must exist");
if let Some(acc_edge_ref) = &acc_edge {
let result = (self.reducer)(acc_edge_ref, &edge_reference, self.parent.ctx());
if std::ptr::eq(result, &edge_reference) {
acc_edge = Some(edge_reference);
}
} else {
// For the first element, we don't apply the reducer, just set it as the accumulator
acc_edge = Some(edge_reference);
}
} else {
return acc_edge.map(|acc| acc.id());
}
}
}
}
// ================ BUILDER METHODS ================
impl<'graph, Mutability, Graph, Walker> VertexWalkerBuilder<'graph, Mutability, Graph, Walker>
where
Graph: crate::graph::Graph + 'graph,
Walker: VertexWalker<'graph, Graph = Graph>,
{
/// # Reduce Step
///
/// The `reduce` step combines elements in a traversal using a reducer function,
/// with the first element as the initial accumulator.
///
/// ## Visual Diagram
///
/// Before reduce step (traversal position on vertices):
/// ```text
/// [A]* --- edge1 ---> [B]* --- edge2 ---> [C]*
/// ^
/// |
/// edge3
/// |
/// [D]*
/// ```
///
/// After reduce step (a single vertex containing the combined result):
/// ```text
/// [Result]* --- ... ---> [More Traversal Steps]
/// ```
///
/// ## Parameters
/// - `reducer`: A closure that takes:
/// - The current accumulated element (left)
/// - The next element to combine (right)
/// - The parent walker's context (passed through)
/// - Returns either the left or right element to continue the reduction
///
/// ## Return Value
///
/// Returns a walker containing a single vertex representing the final reduced value.
/// If the input traversal is empty, the walker will yield nothing.
///
/// ## Example
///
/// ```rust
#[doc = function_body!("examples/reduce.rs", vertex_example, [])]
/// ```
///
/// ## Notes
///
/// - The reduce step is a non-terminal operation - it can be chained with other operations
/// - The walker will yield a single vertex - the final result of combining all input vertices
/// - If the traversal is empty, the walker will yield nothing
/// - The first element serves as the initial accumulator value
/// - Useful for finding maximum/minimum values or combining elements in a custom way
/// - Unlike `fold`, reduce doesn't require an initial value and can still participate in further traversal
/// - The reducer function must return a reference to one of the two input elements
/// - The returned element becomes the new accumulator for the next reduction step
/// - The reducer function operates on the elements only, the context remains unchanged
pub fn reduce<Reducer>(
self,
reducer: Reducer,
) -> VertexWalkerBuilder<'graph, Mutability, Graph, VertexReduce<'graph, Walker, Reducer>>
where
Reducer: for<'a> Fn(
&'a Graph::VertexReference<'graph>,
&'a Graph::VertexReference<'graph>,
&Walker::Context,
) -> &'a Graph::VertexReference<'graph>,
{
self.with_vertex_walker(|walker| walker.reduce(reducer))
}
}
impl<'graph, Mutability, Graph, Walker> EdgeWalkerBuilder<'graph, Mutability, Graph, Walker>
where
Graph: crate::graph::Graph + 'graph,
Walker: EdgeWalker<'graph, Graph = Graph>,
{
/// # Reduce Step
///
/// Combines edges in the traversal using a reducer function, with the first edge as the initial accumulator.
///
/// ## Visual Diagram
///
/// Before reduce step (traversal position on edges):
/// ```text
/// [A] --- edge1* ---> [B] --- edge2* ---> [C]
/// ^
/// |
/// edge3*
/// |
/// [D]
/// ```
///
/// After reduce step (a single edge containing the combined result):
/// ```text
/// [Source] --- [Result]* ---> [Target] --- ... ---> [More Traversal Steps]
/// ```
///
/// ## Parameters
/// - `reducer`: A closure that takes:
/// - The current accumulated edge (left)
/// - The next edge to combine (right)
/// - The parent walker's context (passed through)
/// - Returns either the left or right edge to continue the reduction
///
/// ## Return Value
///
/// Returns a walker containing a single edge representing the final reduced value.
/// If the input traversal is empty, the walker will yield nothing.
///
/// ## Example
///
/// ```rust
#[doc = function_body!("examples/reduce.rs", edge_example, [])]
/// ```
///
/// ## Notes
///
/// - The reduce step is a non-terminal operation - it can be chained with other operations
/// - The walker will yield a single edge - the final result of combining all input edges
/// - If the traversal is empty, the walker will yield nothing
/// - The first element serves as the initial accumulator value
/// - The reducer function must return a reference to one of the two input elements
/// - The returned element becomes the new accumulator for the next reduction step
/// - The reducer function operates on the elements only, the context remains unchanged
pub fn reduce<Reducer>(
self,
reducer: Reducer,
) -> EdgeWalkerBuilder<'graph, Mutability, Graph, EdgeReduce<'graph, Walker, Reducer>>
where
Reducer: for<'a> Fn(
&'a Graph::EdgeReference<'graph>,
&'a Graph::EdgeReference<'graph>,
&Walker::Context,
) -> &'a Graph::EdgeReference<'graph>,
{
self.with_edge_walker(|walker| walker.reduce(reducer))
}
}