luaur_common/records/
dense_hash_map.rs1use core::marker::PhantomData;
10
11use crate::records::const_iterator::ConstIterator;
12use crate::records::dense_hash_table::{
13 DenseEq, DenseEqDefault, DenseHashTable, DenseHasher, ItemInterfaceMap,
14};
15use crate::records::iterator::MutIterator;
16use crate::type_aliases::dense_hash_default::DenseHashDefault;
17
18type MapImpl<K, V, H, E> = DenseHashTable<K, (K, V), ItemInterfaceMap<K, V>, H, E>;
19
20pub struct DenseHashMap<K, V, H = DenseHashDefault<K>, E = DenseEqDefault<K>> {
21 pub(crate) impl_: MapImpl<K, V, H, E>,
22}
23
24impl<K: Clone, V: Clone, H: Clone, E: Clone> Clone for DenseHashMap<K, V, H, E> {
25 fn clone(&self) -> Self {
26 DenseHashMap {
27 impl_: self.impl_.clone(),
28 }
29 }
30}
31
32impl<K: core::fmt::Debug, V: core::fmt::Debug, H, E> core::fmt::Debug for DenseHashMap<K, V, H, E> {
33 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
34 f.debug_struct("DenseHashMap")
35 .field("impl_", &self.impl_)
36 .finish()
37 }
38}
39
40impl<K, V, H, E> DenseHashMap<K, V, H, E>
41where
42 K: Clone,
43 V: crate::records::dense_hash_table::DenseDefault,
44 H: DenseHasher<K> + Default,
45 E: DenseEq<K> + Default + Clone,
46{
47 pub fn new(empty_key: K) -> Self {
49 DenseHashMap {
50 impl_: DenseHashTable::new(empty_key, 0),
51 }
52 }
53
54 pub fn get_or_insert(&mut self, key: K) -> &mut V {
57 self.impl_.rehash_if_full(&key);
58 let idx = self.impl_.insert_unsafe(key);
59 &mut self.impl_.data[idx].1
60 }
61
62 pub fn find(&self, key: &K) -> Option<&V> {
64 self.impl_.find(key).map(|idx| &self.impl_.data[idx].1)
65 }
66
67 pub fn get(&self, key: &K) -> Option<&V> {
69 self.find(key)
70 }
71
72 pub fn find_mut(&mut self, key: &K) -> Option<&mut V> {
74 match self.impl_.find(key) {
75 Some(idx) => Some(&mut self.impl_.data[idx].1),
76 None => None,
77 }
78 }
79
80 pub fn get_mut(&mut self, key: &K) -> Option<&mut V> {
82 self.find_mut(key)
83 }
84
85 pub fn contains_key(&self, key: &K) -> bool {
88 self.contains(key)
89 }
90
91 pub fn contains(&self, key: &K) -> bool {
92 self.impl_.find(key).is_some()
93 }
94
95 pub fn try_insert(&mut self, key: K, value: V) -> (&mut V, bool) {
99 self.impl_.rehash_if_full(&key);
100
101 let before = self.impl_.size();
102 let idx = self.impl_.insert_unsafe(key);
103
104 let fresh = self.impl_.size() > before;
106
107 if fresh {
108 self.impl_.data[idx].1 = value;
109 }
110
111 (&mut self.impl_.data[idx].1, fresh)
112 }
113
114 pub fn size(&self) -> usize {
116 self.impl_.size()
117 }
118
119 pub fn empty(&self) -> bool {
121 self.impl_.size() == 0
122 }
123
124 pub fn is_empty(&self) -> bool {
126 self.empty()
127 }
128
129 pub fn clear(&mut self) {
131 self.impl_.clear();
132 }
133
134 pub fn iter(&self) -> impl Iterator<Item = (&K, &V)> {
136 ConstIterator {
137 table: &self.impl_,
138 index: self.impl_.first_occupied(),
139 }
140 .map(|item| (&item.0, &item.1))
141 }
142
143 pub fn iter_mut(&mut self) -> impl Iterator<Item = (&K, &mut V)> {
145 let empty_key = self.impl_.empty_key.clone();
146 let eq = self.impl_.eq.clone();
147 MutIterator::<K, (K, V), ItemInterfaceMap<K, V>, E> {
148 inner: self.impl_.data.iter_mut(),
149 empty_key,
150 eq,
151 _iface: PhantomData,
152 }
153 .map(|item| (&item.0, &mut item.1))
154 }
155}