micromap_rawl/set/functions.rs
1use crate::{Set, SetDrain};
2use core::borrow::Borrow;
3
4impl<T: PartialEq, const N: usize> Set<T, N> {
5 /// Get its total capacity.
6 #[inline]
7 #[must_use]
8 pub const fn capacity(&self) -> usize {
9 self.map.capacity()
10 }
11
12 /// Is it empty?
13 #[inline]
14 #[must_use]
15 pub const fn is_empty(&self) -> bool {
16 self.map.is_empty()
17 }
18
19 /// Return the total number of pairs inside.
20 #[inline]
21 #[must_use]
22 pub const fn len(&self) -> usize {
23 self.map.len()
24 }
25
26 /// Clears the set, returning all elements as an iterator. Keeps the allocated memory for reuse.
27 ///
28 /// If the returned iterator is dropped before being fully consumed, it drops the remaining elements.
29 /// The returned iterator keeps a mutable borrow on the set to optimize its implementation.
30 pub fn drain(&mut self) -> SetDrain<'_, T> {
31 SetDrain {
32 iter: self.map.drain(),
33 }
34 }
35
36 /// Does the set contain this key?
37 #[inline]
38 #[must_use]
39 pub fn contains_key<Q: PartialEq + ?Sized>(
40 &self,
41 k: &Q,
42 ) -> bool
43 where
44 T: Borrow<Q>,
45 {
46 self.map.contains_key(k)
47 }
48
49 /// Removes a value from the set. Returns whether the value was present in the set.
50 #[inline]
51 pub fn remove<Q: PartialEq + ?Sized>(
52 &mut self,
53 k: &Q,
54 ) -> bool
55 where
56 T: Borrow<Q>,
57 {
58 self.map.remove(k).is_some()
59 }
60
61 /// Adds a value to the set.
62 ///
63 /// Returns whether the value was newly inserted. That is:
64 ///
65 /// If the set did not previously contain this value, true is returned.
66 /// If the set already contained this value, false is returned, and the set is not modified:
67 /// original value is not replaced, and the value passed as argument is dropped.
68 ///
69 /// # Panics
70 ///
71 /// It may panic if there are too many pairs in the set already. Pay attention,
72 /// it panics only in the "debug" mode. In the "release" mode, you are going to get
73 /// undefined behavior. This is done for the sake of performance, in order to
74 /// avoid a repetitive check for the boundary condition on every `insert()`.
75 #[inline]
76 pub fn insert(
77 &mut self,
78 k: T,
79 ) -> bool {
80 self.map.insert(k, ()).is_none()
81 }
82
83 /// Get a reference to a single value.
84 #[inline]
85 #[must_use]
86 pub fn get<Q: PartialEq + ?Sized>(
87 &self,
88 k: &Q,
89 ) -> Option<&T>
90 where
91 T: Borrow<Q>,
92 {
93 self.map.get_key_value(k).map(|p| p.0)
94 }
95
96 /// Remove all pairs from it, but keep the space intact for future use.
97 #[inline]
98 pub fn clear(&mut self) {
99 self.map.clear();
100 }
101
102 /// Retains only the elements specified by the predicate.
103 #[inline]
104 pub fn retain<F: Fn(&T) -> bool>(
105 &mut self,
106 f: F,
107 ) {
108 self.map.retain(|k, ()| f(k));
109 }
110
111 /// Removes a key from the set, returning the stored key and value if the
112 /// key was previously in the set.
113 #[inline]
114 pub fn take<Q: PartialEq + ?Sized>(
115 &mut self,
116 k: &Q,
117 ) -> Option<T>
118 where
119 T: Borrow<Q>,
120 {
121 self.map.remove_entry(k).map(|p| p.0)
122 }
123}