grafeo_common/
collections.rs1use crate::utils::hash::FxBuildHasher;
30
31pub type GrafeoMap<K, V> = hashbrown::HashMap<K, V, FxBuildHasher>;
36
37pub type GrafeoSet<T> = hashbrown::HashSet<T, FxBuildHasher>;
39
40pub type GrafeoConcurrentMap<K, V> = dashmap::DashMap<K, V, FxBuildHasher>;
45
46pub type GrafeoConcurrentSet<T> = dashmap::DashSet<T, FxBuildHasher>;
48
49pub type GrafeoIndexMap<K, V> = indexmap::IndexMap<K, V, FxBuildHasher>;
53
54pub type GrafeoIndexSet<T> = indexmap::IndexSet<T, FxBuildHasher>;
56
57#[inline]
59#[must_use]
60pub fn grafeo_map<K, V>() -> GrafeoMap<K, V> {
61 GrafeoMap::with_hasher(FxBuildHasher::default())
62}
63
64#[inline]
66#[must_use]
67pub fn grafeo_map_with_capacity<K, V>(capacity: usize) -> GrafeoMap<K, V> {
68 GrafeoMap::with_capacity_and_hasher(capacity, FxBuildHasher::default())
69}
70
71#[inline]
73#[must_use]
74pub fn grafeo_set<T>() -> GrafeoSet<T> {
75 GrafeoSet::with_hasher(FxBuildHasher::default())
76}
77
78#[inline]
80#[must_use]
81pub fn grafeo_set_with_capacity<T>(capacity: usize) -> GrafeoSet<T> {
82 GrafeoSet::with_capacity_and_hasher(capacity, FxBuildHasher::default())
83}
84
85#[inline]
87#[must_use]
88pub fn grafeo_concurrent_map<K, V>() -> GrafeoConcurrentMap<K, V>
89where
90 K: Eq + std::hash::Hash,
91{
92 GrafeoConcurrentMap::with_hasher(FxBuildHasher::default())
93}
94
95#[inline]
97#[must_use]
98pub fn grafeo_concurrent_map_with_capacity<K, V>(capacity: usize) -> GrafeoConcurrentMap<K, V>
99where
100 K: Eq + std::hash::Hash,
101{
102 GrafeoConcurrentMap::with_capacity_and_hasher(capacity, FxBuildHasher::default())
103}
104
105#[inline]
107#[must_use]
108pub fn grafeo_index_map<K, V>() -> GrafeoIndexMap<K, V> {
109 GrafeoIndexMap::with_hasher(FxBuildHasher::default())
110}
111
112#[inline]
114#[must_use]
115pub fn grafeo_index_map_with_capacity<K, V>(capacity: usize) -> GrafeoIndexMap<K, V> {
116 GrafeoIndexMap::with_capacity_and_hasher(capacity, FxBuildHasher::default())
117}
118
119#[cfg(test)]
120mod tests {
121 use super::*;
122
123 #[test]
124 fn test_grafeo_map() {
125 let mut map = grafeo_map::<String, i32>();
126 map.insert("key".to_string(), 42);
127 assert_eq!(map.get("key"), Some(&42));
128 }
129
130 #[test]
131 fn test_grafeo_set() {
132 let mut set = grafeo_set::<i32>();
133 set.insert(1);
134 set.insert(2);
135 assert!(set.contains(&1));
136 assert!(!set.contains(&3));
137 }
138
139 #[test]
140 fn test_grafeo_concurrent_map() {
141 let map = grafeo_concurrent_map::<String, i32>();
142 map.insert("key".to_string(), 42);
143 assert_eq!(*map.get("key").unwrap(), 42);
144 }
145
146 #[test]
147 fn test_grafeo_index_map_preserves_order() {
148 let mut map = grafeo_index_map::<&str, i32>();
149 map.insert("c", 3);
150 map.insert("a", 1);
151 map.insert("b", 2);
152
153 let keys: Vec<_> = map.keys().copied().collect();
154 assert_eq!(keys, vec!["c", "a", "b"]);
155 }
156}