1use crate::Map;
22use std::ptr;
23
24impl<V: Clone> Map<V> {
25 #[inline]
27 #[must_use]
28 pub fn is_empty(&self) -> bool {
29 self.len() == 0
30 }
31
32 #[inline]
38 #[must_use]
39 pub fn len(&self) -> usize {
40 #[cfg(debug_assertions)]
41 assert!(self.initialized, "Can't do len() on non-initialized Map");
42 let mut busy = 0;
43 for i in 0..self.max {
44 if self.get(i).is_some() {
45 busy += 1;
46 }
47 }
48 busy
49 }
50
51 #[inline]
60 #[must_use]
61 #[allow(clippy::missing_const_for_fn)]
62 pub fn contains_key(&self, k: usize) -> bool {
63 self.assert_boundaries(k);
64 matches!(unsafe { &*self.head.add(k) }, Some(_))
65 }
66
67 #[inline]
76 pub fn remove(&mut self, k: usize) {
77 self.assert_boundaries(k);
78 unsafe {
79 ptr::write(self.head.add(k), None);
80 }
81 }
82
83 #[inline]
85 pub fn push(&mut self, v: V) -> usize {
86 let k = self.next_key();
87 self.insert(k, v);
88 k
89 }
90
91 #[inline]
100 pub fn insert(&mut self, k: usize, v: V) {
101 self.assert_boundaries(k);
102 unsafe {
103 ptr::write(self.head.add(k), Some(v));
104 }
105 if self.max <= k {
106 self.max = k + 1;
107 }
108 }
109
110 #[inline]
119 #[must_use]
120 pub fn get(&self, k: usize) -> Option<&V> {
121 self.assert_boundaries(k);
122 unsafe { &*self.head.add(k) }.as_ref()
123 }
124
125 #[inline]
134 #[must_use]
135 pub fn get_mut(&mut self, k: usize) -> Option<&mut V> {
136 self.assert_boundaries(k);
137 unsafe { &mut *(self.head.add(k)) }.as_mut()
138 }
139
140 #[inline]
142 pub fn clear(&mut self) {
143 self.max = 0;
144 }
145
146 #[inline]
152 pub fn retain<F: Fn(&usize, &V) -> bool>(&mut self, f: F) {
153 #[cfg(debug_assertions)]
154 assert!(self.initialized, "Can't do retain() on non-initialized Map");
155 for i in 0..self.max {
156 if let Some(p) = self.get_mut(i) {
157 if !f(&i, p) {
158 unsafe {
159 ptr::write(self.head.add(i), None);
160 }
161 }
162 }
163 }
164 }
165
166 #[inline]
168 #[allow(unused_variables)]
169 fn assert_boundaries(&self, k: usize) {
170 #[cfg(debug_assertions)]
171 assert!(
172 k < self.capacity(),
173 "The key {k} is over the boundary {}",
174 self.capacity()
175 );
176 }
177}
178
179#[test]
180fn insert_and_check_length() {
181 let mut m: Map<&str> = Map::with_capacity_none(16);
182 m.insert(0, "zero");
183 assert_eq!(1, m.len());
184 m.insert(1, "first");
185 assert_eq!(2, m.len());
186 m.insert(1, "first");
187 assert_eq!(2, m.len());
188}
189
190#[test]
191fn empty_length() {
192 let m: Map<u32> = Map::with_capacity_none(16);
193 assert_eq!(0, m.len());
194}
195
196#[test]
197fn is_empty_check() {
198 let mut m: Map<u32> = Map::with_capacity_none(16);
199 assert!(m.is_empty());
200 m.insert(0, 42);
201 assert!(!m.is_empty());
202}
203
204#[test]
205fn insert_and_gets() {
206 let mut m: Map<&str> = Map::with_capacity_none(16);
207 m.insert(0, "zero");
208 m.insert(1, "one");
209 assert_eq!("one", *m.get(1).unwrap());
210}
211
212#[test]
213fn insert_and_gets_mut() {
214 let mut m: Map<[i32; 3]> = Map::with_capacity_none(16);
215 m.insert(0, [1, 2, 3]);
216 let a = m.get_mut(0).unwrap();
217 a[0] = 500;
218 assert_eq!(500, m.get(0).unwrap()[0]);
219}
220
221#[test]
222fn checks_key() {
223 let mut m: Map<&str> = Map::with_capacity_none(16);
224 m.insert(0, "one");
225 assert!(m.contains_key(0));
226 m.insert(8, "");
227 m.remove(8);
228 assert!(!m.contains_key(8));
229}
230
231#[test]
232fn gets_missing_key() {
233 let mut m: Map<&str> = Map::with_capacity_none(16);
234 m.insert(0, "one");
235 m.insert(1, "one");
236 m.remove(1);
237 assert!(m.get(1).is_none());
238}
239
240#[test]
241fn mut_gets_missing_key() {
242 let mut m: Map<&str> = Map::with_capacity_none(16);
243 m.insert(0, "one");
244 m.insert(1, "one");
245 m.remove(1);
246 assert!(m.get_mut(1).is_none());
247}
248
249#[test]
250fn removes_simple_pair() {
251 let mut m: Map<&str> = Map::with_capacity_none(16);
252 m.insert(0, "one");
253 m.remove(0);
254 m.remove(1);
255 assert!(m.get(0).is_none());
256}
257
258#[cfg(test)]
259#[derive(Clone, Copy)]
260struct Foo {
261 v: [u32; 3],
262}
263
264#[test]
265fn insert_struct() {
266 let mut m: Map<Foo> = Map::with_capacity_none(16);
267 let foo = Foo { v: [1, 2, 100] };
268 m.insert(0, foo);
269 assert_eq!(100, m.into_iter().next().unwrap().1.v[2]);
270}
271
272#[test]
273fn large_map_in_heap() {
274 let m: Box<Map<[u64; 10]>> = Box::new(Map::with_capacity_none(16));
275 assert_eq!(0, m.len());
276}
277
278#[test]
279fn clears_it_up() {
280 let mut m: Map<&str> = Map::with_capacity_none(16);
281 m.insert(7, "one");
282 m.clear();
283 assert_eq!(0, m.len());
284}
285
286#[test]
287fn pushes_into() {
288 let mut m: Map<&str> = Map::with_capacity_none(16);
289 assert_eq!(0, m.push("one"));
290 assert_eq!(1, m.push("two"));
291}
292
293#[test]
294#[should_panic]
295#[cfg(debug_assertions)]
296fn insert_out_of_boundary() {
297 let mut m: Map<&str> = Map::with_capacity(1);
298 m.insert(5, "one");
299}
300
301#[test]
302#[should_panic]
303#[cfg(debug_assertions)]
304fn get_out_of_boundary() {
305 let m: Map<&str> = Map::with_capacity(1);
306 m.get(5).unwrap();
307}
308
309#[test]
310#[should_panic]
311#[cfg(debug_assertions)]
312fn remove_out_of_boundary() {
313 let mut m: Map<&str> = Map::with_capacity(1);
314 m.remove(5);
315}