1use crate::graph::Graph;
2use crate::search::vertex::VertexSearch;
3use crate::walker::builder::{ImmutableMarker, VertexWalkerBuilder};
4use crate::walker::steps::{
5 Detour, EdgeContext, EdgeControlFlow, EdgeFilter, EdgeReduce, EdgeTake, Edges, End, Endpoints,
6 VertexContext, VertexControlFlow, VertexFilter, VertexIter, VertexReduce, VertexTake, Vertices,
7 Waypoint,
8};
9use crate::{EdgeSearch, ElementId};
10
11pub mod builder;
12mod iter;
13pub mod steps;
14
15pub trait Walker<'graph>
27where
28 Self: Sized,
29{
30 type Graph: Graph;
32
33 type Context: Clone + 'static;
36
37 fn next_element(&mut self, graph: &'graph Self::Graph) -> Option<ElementId<Self::Graph>>;
39
40 fn ctx(&self) -> &Self::Context;
42
43 fn ctx_mut(&mut self) -> &mut Self::Context;
45}
46
47pub trait VertexWalker<'graph>: Walker<'graph> {
59 fn vertices_by_id<Iter>(self, vertex_ids: Iter) -> VertexIter<'graph, Self, Iter::IntoIter>
60 where
61 Iter: IntoIterator<Item = <Self::Graph as Graph>::VertexId>,
62 {
63 VertexIter::new(self, vertex_ids.into_iter())
64 }
65
66 fn vertices<'search>(
67 self,
68 vertex_search: VertexSearch<'search, Self::Graph>,
69 ) -> Vertices<'search, 'graph, Self> {
70 Vertices::new(self, vertex_search)
71 }
72
73 fn filter<Predicate>(self, predicate: Predicate) -> VertexFilter<'graph, Self, Predicate>
74 where
75 Predicate: Fn(&<Self::Graph as Graph>::VertexReference<'_>, &Self::Context) -> bool,
76 {
77 VertexFilter::new(self, predicate)
78 }
79
80 fn control_flow<Predicate>(
81 self,
82 predicate: Predicate,
83 ) -> VertexControlFlow<'graph, Self, Predicate>
84 where
85 Self: 'graph,
86 for<'a> Predicate: Fn(
87 &'a <Self::Graph as Graph>::VertexReference<'graph>,
88 &mut Self::Context,
89 ) -> std::ops::ControlFlow<
90 Option<&'a <Self::Graph as Graph>::VertexReference<'graph>>,
91 Option<&'a <Self::Graph as Graph>::VertexReference<'graph>>,
92 >,
93 {
94 VertexControlFlow::new(self, predicate)
95 }
96
97 fn take(self, n: usize) -> VertexTake<'graph, Self> {
98 VertexTake::new(self, n)
99 }
100
101 fn detour<Path, Terminal, WalkerBuilder>(
102 self,
103 path: Path,
104 ) -> Detour<'graph, Self, Path, Terminal>
105 where
106 Path: Fn(
107 VertexWalkerBuilder<
108 'graph,
109 ImmutableMarker,
110 Self::Graph,
111 Waypoint<'graph, Self::Graph, Self::Context>,
112 >,
113 ) -> WalkerBuilder,
114 WalkerBuilder: Into<builder::WalkerBuilder<'graph, ImmutableMarker, Self::Graph, Terminal>>,
115 Terminal: Walker<'graph, Graph = Self::Graph>,
116 <Self as Walker<'graph>>::Graph: 'graph,
117 {
118 Detour::new(self, path)
119 }
120
121 fn context<Callback, Context>(
122 self,
123 predicate: Callback,
124 ) -> VertexContext<'graph, Self, Callback, Context>
125 where
126 Callback: Fn(&<Self::Graph as Graph>::VertexReference<'_>, &Self::Context) -> Context,
127 {
128 VertexContext::new(self, predicate)
129 }
130
131 fn edges(self, search: EdgeSearch<Self::Graph>) -> Edges<'_, 'graph, Self> {
132 Edges::new(self, search)
133 }
134
135 fn reduce<Reducer>(self, reducer: Reducer) -> VertexReduce<'graph, Self, Reducer>
136 where
137 Reducer: for<'a> Fn(
138 &'a <Self::Graph as Graph>::VertexReference<'graph>,
139 &'a <Self::Graph as Graph>::VertexReference<'graph>,
140 &Self::Context,
141 ) -> &'a <Self::Graph as Graph>::VertexReference<'graph>,
142 <Self as Walker<'graph>>::Graph: 'graph,
143 {
144 VertexReduce::new(self, reducer)
145 }
146
147 fn next(&mut self, graph: &'graph Self::Graph) -> Option<<Self::Graph as Graph>::VertexId>;
158}
159
160pub trait EdgeWalker<'graph>: Walker<'graph> {
170 fn context<Callback, Context>(
171 self,
172 predicate: Callback,
173 ) -> EdgeContext<'graph, Self, Callback, Context>
174 where
175 Callback: Fn(&<Self::Graph as Graph>::EdgeReference<'_>, &Self::Context) -> Context,
176 {
177 EdgeContext::new(self, predicate)
178 }
179
180 fn filter<Predicate>(self, predicate: Predicate) -> EdgeFilter<'graph, Self, Predicate>
181 where
182 Predicate: Fn(&<Self::Graph as Graph>::EdgeReference<'_>, &Self::Context) -> bool,
183 {
184 EdgeFilter::new(self, predicate)
185 }
186
187 fn control_flow<Predicate>(
188 self,
189 predicate: Predicate,
190 ) -> EdgeControlFlow<'graph, Self, Predicate>
191 where
192 Self: 'graph,
193 for<'a> Predicate: Fn(
194 &'a <Self::Graph as Graph>::EdgeReference<'graph>,
195 &mut Self::Context,
196 ) -> std::ops::ControlFlow<
197 Option<&'a <Self::Graph as Graph>::EdgeReference<'graph>>,
198 Option<&'a <Self::Graph as Graph>::EdgeReference<'graph>>,
199 >,
200 {
201 EdgeControlFlow::new(self, predicate)
202 }
203
204 fn head(self) -> Endpoints<'graph, Self> {
205 Endpoints::new(self, End::Head)
206 }
207
208 fn tail(self) -> Endpoints<'graph, Self> {
209 Endpoints::new(self, End::Tail)
210 }
211
212 fn take(self, n: usize) -> EdgeTake<'graph, Self> {
213 EdgeTake::new(self, n)
214 }
215
216 fn reduce<Reducer>(self, reducer: Reducer) -> EdgeReduce<'graph, Self, Reducer>
217 where
218 Reducer: for<'a> Fn(
219 &'a <Self::Graph as Graph>::EdgeReference<'graph>,
220 &'a <Self::Graph as Graph>::EdgeReference<'graph>,
221 &Self::Context,
222 ) -> &'a <Self::Graph as Graph>::EdgeReference<'graph>,
223 <Self as Walker<'graph>>::Graph: 'graph,
224 {
225 EdgeReduce::new(self, reducer)
226 }
227
228 fn next(&mut self, graph: &'graph Self::Graph) -> Option<<Self::Graph as Graph>::EdgeId>;
239}