zrx_graph/graph/iter/groups.rs
1// Copyright (c) 2025-2026 Zensical and contributors
2
3// SPDX-License-Identifier: MIT
4// All contributions are certified under the DCO
5
6// Permission is hereby granted, free of charge, to any person obtaining a copy
7// of this software and associated documentation files (the "Software"), to
8// deal in the Software without restriction, including without limitation the
9// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10// sell copies of the Software, and to permit persons to whom the Software is
11// furnished to do so, subject to the following conditions:
12
13// The above copyright notice and this permission notice shall be included in
14// all copies or substantial portions of the Software.
15
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22// IN THE SOFTWARE.
23
24// ----------------------------------------------------------------------------
25
26//! Iterator over key-groups.
27
28use std::collections::btree_map::{self, BTreeMap};
29
30use crate::graph::Graph;
31
32// ----------------------------------------------------------------------------
33// Structs
34// ----------------------------------------------------------------------------
35
36/// Iterator over key-groups.
37pub struct Groups<K> {
38 /// Iterator over groups.
39 inner: btree_map::IntoIter<K, Vec<usize>>,
40}
41
42// ----------------------------------------------------------------------------
43// Implementations
44// ----------------------------------------------------------------------------
45
46impl<T> Graph<T> {
47 /// Creates an iterator over key-groups.
48 ///
49 /// # Examples
50 ///
51 /// ```
52 /// # use std::error::Error;
53 /// # fn main() -> Result<(), Box<dyn Error>> {
54 /// use zrx_graph::Graph;
55 ///
56 /// // Create graph builder and add nodes
57 /// let mut builder = Graph::builder();
58 /// let a = builder.add_node("a");
59 /// let b = builder.add_node("b");
60 /// let c = builder.add_node("c");
61 ///
62 /// // Create edges between nodes
63 /// builder.add_edge(a, b)?;
64 /// builder.add_edge(b, c)?;
65 ///
66 /// // Create graph from builder
67 /// let graph = builder.build();
68 ///
69 /// // Create iterator over key-groups
70 /// for (key, nodes) in graph.groups(|node| node.len()) {
71 /// println!("{key}: {nodes:?}");
72 /// }
73 /// # Ok(())
74 /// # }
75 /// ```
76 #[must_use]
77 pub fn groups<'a, F, K>(&'a self, f: F) -> Groups<K>
78 where
79 F: Fn(&'a T) -> K,
80 K: Ord,
81 {
82 let mut groups: BTreeMap<K, Vec<usize>> = BTreeMap::new();
83 for node in self {
84 groups.entry(f(&self[node])).or_default().push(node);
85 }
86 Groups { inner: groups.into_iter() }
87 }
88}
89
90// ----------------------------------------------------------------------------
91// Trait implementations
92// ----------------------------------------------------------------------------
93
94impl<K> Iterator for Groups<K> {
95 type Item = (K, Vec<usize>);
96
97 /// Returns the next key-group.
98 fn next(&mut self) -> Option<Self::Item> {
99 self.inner.next()
100 }
101}