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
use crate::graph::Graph;
use crate::search::vertex::VertexSearch;
use crate::walker::builder::{StartWalkerBuilder, VertexWalkerBuilder};
use crate::walker::steps::Empty;
use crate::walker::{VertexWalker, Walker};
use crate::{ElementId, VertexReference};
use include_doc::function_body;
use std::marker::PhantomData;
// ================ VERTICES IMPLEMENTATION ================
pub struct Vertices<'search, 'graph, Parent>
where
Parent: VertexWalker<'graph>,
<Parent as Walker<'graph>>::Graph: 'graph,
{
_phantom_data: PhantomData<&'graph ()>,
parent: Parent,
current_iter: Option<<Parent::Graph as Graph>::VertexIter<'search, 'graph>>,
vertex_search: VertexSearch<'search, Parent::Graph>,
}
impl<'search, 'graph, Parent> Vertices<'search, 'graph, Parent>
where
Parent: VertexWalker<'graph>,
<Parent as Walker<'graph>>::Graph: 'graph,
{
pub fn new(parent: Parent, vertex_search: VertexSearch<'search, Parent::Graph>) -> Self {
Self {
_phantom_data: Default::default(),
parent,
current_iter: None,
vertex_search,
}
}
}
impl<'graph, Parent> Walker<'graph> for Vertices<'_, 'graph, Parent>
where
Parent: VertexWalker<'graph>,
<Parent as Walker<'graph>>::Graph: '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) -> &Parent::Context {
self.parent.ctx()
}
fn ctx_mut(&mut self) -> &mut Self::Context {
self.parent.ctx_mut()
}
}
impl<'graph, Parent> VertexWalker<'graph> for Vertices<'_, 'graph, Parent>
where
Parent: VertexWalker<'graph>,
<Parent as Walker<'graph>>::Graph: 'graph,
{
fn next(&mut self, graph: &'graph Self::Graph) -> Option<<Self::Graph as Graph>::VertexId> {
if self.current_iter.is_none() {
self.current_iter = Some(graph.vertices(&self.vertex_search));
}
self.current_iter
.as_mut()
.expect("iterator must be populated")
.next()
.map(|next| next.id())
}
}
impl<'graph, Mutability, Graph, Walker> VertexWalkerBuilder<'graph, Mutability, Graph, Walker>
where
Graph: crate::graph::Graph,
Walker: VertexWalker<'graph, Graph = Graph>,
{
/// # Vertices Step
///
/// The `vertices` step is the primary entry point for graph traversals, allowing you to select a set of
/// vertices to start traversing from. It accepts a `VertexSearch` parameter that specifies which
/// vertices to include in the traversal.
///
/// ## Visual Diagram
///
/// Before vertices step (empty traversal):
/// ```text
/// [A] --- edge1 ---> [B] --- edge2 ---> [C]
/// ^
/// |
/// edge3
/// |
/// [D]
/// ```
///
/// After vertices step (with VertexSearch::scan()):
/// ```text
/// [A]* --- edge1 ---> [B]* --- edge2 ---> [C]*
/// ^
/// |
/// edge3
/// |
/// [D]*
/// ```
///
/// After vertices step (with VertexSearch::scan().with_label(Vertex::person_label())):
/// ```text
/// [Person A]* --- edge1 ---> [Project B] --- edge2 ---> [Person C]*
/// ^
/// |
/// edge3
/// |
/// [Person D]*
/// ```
///
/// ## Parameters
///
/// - `vertex_search`: A `VertexSearch` object that defines the criteria for selecting vertices
///
/// ## Return Value
///
/// Returns a traversal containing all vertices that match the search criteria.
///
/// ## Example
///
/// ```rust
#[doc = function_body!("examples/vertices.rs", example, [])]
/// ```
///
/// For more examples, see the [vertices example](https://github.com/yourusername/graph-api/blob/main/graph-api-lib/examples/vertices.rs).
///
/// ## Notes
///
/// - The `vertices` step is typically the first step in a traversal
/// - Use `VertexIndex` methods for faster access when you have appropriate indexes defined
/// - For more complex criteria, you can chain the `filter` step after this one
/// - When working with large graphs, consider using indexed properties for better performance
/// - This step supports all vertex search mechanisms, including label-based, index-based, and full scans
/// - The traversal order is not guaranteed unless you specifically use an range index
pub fn vertices<'a, T: Into<VertexSearch<'a, Graph>>>(
self,
vertex_search: T,
) -> VertexWalkerBuilder<'graph, Mutability, Graph, Vertices<'a, 'graph, Walker>> {
self.with_vertex_walker(|walker| walker.vertices(vertex_search.into()))
}
}
impl<'graph, Graph, Mutability, Context> StartWalkerBuilder<'graph, Mutability, Graph, Context>
where
Graph: crate::graph::Graph,
Context: Clone + 'static,
{
pub fn vertices<'search>(
self,
vertex_search: VertexSearch<'search, Graph>,
) -> VertexWalkerBuilder<
'graph,
Mutability,
Graph,
Vertices<'search, 'graph, Empty<Graph, Context>>,
> {
crate::walker::builder::new(self.graph, self.empty.vertices(vertex_search))
}
}