1pub fn uniq_values<K, V>(maps: &[&std::collections::HashMap<K, V>]) -> Vec<V>
31where
32 V: Clone + std::cmp::Eq + std::hash::Hash,
33{
34 let mut seen = std::collections::HashSet::new();
35 let mut result = Vec::new();
36
37 for map in maps {
38 for value in map.values() {
39 if seen.insert(value.clone()) {
40 result.push(value.clone());
41 }
42 }
43 }
44
45 result
46}
47
48#[cfg(test)]
49mod tests {
50 use super::*;
51 use std::collections::HashMap;
52
53 #[test]
54 fn test_uniq_values_single_map() {
55 let mut map = HashMap::new();
56 map.insert("a", 1);
57 map.insert("b", 2);
58
59 let result = uniq_values(&[&map]);
60 assert_eq!(result.len(), 2);
61 assert!(result.contains(&1));
62 assert!(result.contains(&2));
63 }
64
65 #[test]
66 fn test_uniq_values_multiple_maps() {
67 let mut map1 = HashMap::new();
68 map1.insert("a", 1);
69 map1.insert("b", 2);
70
71 let mut map2 = HashMap::new();
72 map2.insert("c", 2);
73 map2.insert("d", 3);
74
75 let result = uniq_values(&[&map1, &map2]);
76 assert_eq!(result.len(), 3);
77 assert!(result.contains(&1));
78 assert!(result.contains(&2));
79 assert!(result.contains(&3));
80 }
81
82 #[test]
83 fn test_uniq_values_empty_maps() {
84 let map1: HashMap<&str, i32> = HashMap::new();
85 let map2: HashMap<&str, i32> = HashMap::new();
86
87 let result = uniq_values(&[&map1, &map2]);
88 assert_eq!(result.len(), 0);
89 }
90
91 #[test]
92 fn test_uniq_values_with_integers() {
93 let mut map1 = HashMap::new();
94 map1.insert(1, "a");
95 map1.insert(2, "b");
96
97 let mut map2 = HashMap::new();
98 map2.insert(3, "b");
99 map2.insert(4, "c");
100
101 let result = uniq_values(&[&map1, &map2]);
102 assert_eq!(result.len(), 3);
103 assert!(result.contains(&"a"));
104 assert!(result.contains(&"b"));
105 assert!(result.contains(&"c"));
106 }
107
108 #[test]
109 fn test_uniq_values_with_mixed_types() {
110 let mut map1 = HashMap::new();
111 map1.insert("a", 1);
112 map1.insert("b", 2);
113
114 let mut map2 = HashMap::new();
115 map2.insert("c", 2);
116 map2.insert("d", 3);
117
118 let result = uniq_values(&[&map1, &map2]);
119 assert_eq!(result.len(), 3);
120 assert!(result.contains(&1));
121 assert!(result.contains(&2));
122 assert!(result.contains(&3));
123 }
124}