emap/
map.rs

1// Copyright (c) 2023 Yegor Bugayenko
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included
11// in all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21use crate::Map;
22use std::ptr;
23
24impl<V: Clone> Map<V> {
25    /// Is it empty?
26    #[inline]
27    #[must_use]
28    pub fn is_empty(&self) -> bool {
29        self.len() == 0
30    }
31
32    /// Return the total number of items inside.
33    ///
34    /// # Panics
35    ///
36    /// It may panic in debug mode, if the [`Map`] is not initialized.
37    #[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    /// Does the map contain this key?
52    ///
53    /// # Panics
54    ///
55    /// It may panic if you attempt to refer to they key that is outside
56    /// of the boundary of this map. It will not return `None`, it will panic.
57    /// However, in "release" mode it will not panic, but will lead to
58    /// undefined behavior.
59    #[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    /// Remove by key.
68    ///
69    /// # Panics
70    ///
71    /// It may panic if you attempt to refer to they key that is outside
72    /// of the boundary of this map. It will not return `None`, it will panic.
73    /// However, in "release" mode it will not panic, but will lead to
74    /// undefined behavior.
75    #[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    /// Push to the rightmost position and return the key.
84    #[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    /// Insert a single pair into the map.
92    ///
93    /// # Panics
94    ///
95    /// It may panic if you attempt to refer to they key that is outside
96    /// of the boundary of this map. It will not return `None`, it will panic.
97    /// However, in "release" mode it will not panic, but will lead to
98    /// undefined behavior.
99    #[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    /// Get a reference to a single value.
111    ///
112    /// # Panics
113    ///
114    /// It may panic if you attempt to refer to they key that is outside
115    /// of the boundary of this map. It will not return `None`, it will panic.
116    /// However, in "release" mode it will not panic, but will lead to
117    /// undefined behavior.
118    #[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    /// Get a mutable reference to a single value.
126    ///
127    /// # Panics
128    ///
129    /// It may panic if you attempt to refer to they key that is outside
130    /// of the boundary of this map. It will not return `None`, it will panic.
131    /// However, in "release" mode it will not panic, but will lead to
132    /// undefined behavior.
133    #[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    /// Remove all items from it, but keep the space intact for future use.
141    #[inline]
142    pub fn clear(&mut self) {
143        self.max = 0;
144    }
145
146    /// Retains only the elements specified by the predicate.
147    ///
148    /// # Panics
149    ///
150    /// It may panic in debug mode, if the [`Map`] is not initialized.
151    #[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    /// Check the boundary condition.
167    #[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}