graph_api_lib/walker/steps/count.rs
1use crate::walker::builder::{EdgeWalkerBuilder, VertexWalkerBuilder};
2use crate::walker::{EdgeWalker, VertexWalker};
3use include_doc::function_body;
4
5impl<'graph, Mutability, Graph, Walker> VertexWalkerBuilder<'graph, Mutability, Graph, Walker>
6where
7 Graph: crate::graph::Graph,
8 Walker: VertexWalker<'graph, Graph = Graph>,
9{
10 /// # Count Step
11 ///
12 /// The `count` step fully traverses the graph and returns the number of elements emitted by the traversal.
13 ///
14 /// ## Visual Diagram
15 ///
16 /// Before count step (with elements in traversal):
17 /// ```text
18 /// [A]* --- edge1 ---> [B]* --- edge2 ---> [C]*
19 /// ^
20 /// |
21 /// edge3
22 /// |
23 /// [D]*
24 /// ```
25 ///
26 /// After count step (consumed all elements, returned count 4):
27 /// ```text
28 /// [A] --- edge1 ---> [B] --- edge2 ---> [C]
29 /// ^
30 /// |
31 /// edge3
32 /// |
33 /// [D]
34 /// ```
35 ///
36 /// ## Parameters
37 ///
38 /// None
39 ///
40 /// ## Return Value
41 ///
42 /// Returns an integer representing the total number of elements in the traversal.
43 ///
44 /// ## Example
45 ///
46 /// ```rust
47 #[doc = function_body!("examples/count.rs", example, [])]
48 /// ```
49 ///
50 /// For more examples, see the [count example](https://github.com/yourusername/graph-api/blob/main/graph-api-lib/examples/count.rs).
51 ///
52 /// ## Notes
53 ///
54 /// - The `count` step consumes the entire traversal
55 /// - This is a terminal operation - no further steps can be added after `count`
56 /// - For very large graphs, be aware that this will traverse the entire graph which may be expensive
57 /// - Consider using `limit` before `count` if you only need to check up to a certain number of elements
58 pub fn count(mut self) -> usize
59 where
60 'graph: 'graph,
61 {
62 let mut count = 0;
63 let graph = self.graph();
64 let mut walker = self.walker();
65 while let Some(_vertex_id) = walker.next(graph) {
66 count += 1;
67 }
68 count
69 }
70}
71
72impl<'graph, Mutability, Graph, Walker> EdgeWalkerBuilder<'graph, Mutability, Graph, Walker>
73where
74 Graph: crate::graph::Graph,
75 Walker: EdgeWalker<'graph, Graph = Graph>,
76{
77 /// Counts the total number of edges in this traversal.
78 ///
79 /// This is a terminal operation that consumes the traversal.
80 ///
81 /// See the documentation for [`VertexWalkerBuilder::count`] for more details.
82 pub fn count(mut self) -> usize
83 where
84 'graph: 'graph,
85 {
86 let mut count = 0;
87 let graph = self.graph();
88 let mut walker = self.walker();
89 while let Some(_vertex_id) = walker.next(graph) {
90 count += 1;
91 }
92 count
93 }
94}