allocated_btree/compressed/
wrapper.rs1use core::borrow::Borrow;
8use core::fmt::Debug;
9use core::mem::ManuallyDrop;
10use core::ops::Add;
11use core::ops::Mul;
12
13use allocator_api2::alloc::{Allocator, Global};
14use generic_array::ArrayLength;
15use typenum::{Prod, Sum, U1, U2, U6};
16
17use allocated::{AllocResult, AllocResultExt, DropIn};
18
19use super::{AllocatedBTreeMap, Entry, Iter, Keys, OccupiedEntry, Values, ValuesMut};
20
21pub struct CompressedBTreeMap<
44 K: core::cmp::PartialOrd + core::fmt::Debug,
45 V,
46 B: ArrayLength = U6,
47 A: Allocator = Global,
48> where
49 U2: Mul<B>,
50 Prod<U2, B>: ArrayLength,
51 U1: Add<Prod<U2, B>>,
52 Sum<U1, Prod<U2, B>>: ArrayLength,
53{
54 alloc: A,
55 raw: ManuallyDrop<AllocatedBTreeMap<K, V, B>>,
56}
57
58impl<K: core::cmp::PartialOrd + Debug, V> CompressedBTreeMap<K, V> {
59 #[inline]
65 pub fn new() -> Self {
66 let raw = AllocatedBTreeMap::new_in(&Global)
67 .handle_alloc_error()
68 .into_inner();
69
70 Self { alloc: Global, raw }
71 }
72}
73
74impl<K: core::cmp::PartialOrd + Debug, V> Default for CompressedBTreeMap<K, V> {
75 fn default() -> Self {
76 Self::new()
77 }
78}
79
80impl<K: core::cmp::PartialOrd + Debug, V, B: ArrayLength, A: Allocator> Drop
81 for CompressedBTreeMap<K, V, B, A>
82where
83 U2: Mul<B>,
84 Prod<U2, B>: ArrayLength,
85 U1: Add<Prod<U2, B>>,
86 Sum<U1, Prod<U2, B>>: ArrayLength,
87{
88 fn drop(&mut self) {
89 unsafe {
91 self.raw.drop_in(&self.alloc);
92 }
93 }
94}
95
96impl<K: core::cmp::PartialOrd + Debug, V, B: ArrayLength, A: Allocator>
97 CompressedBTreeMap<K, V, B, A>
98where
99 U2: Mul<B>,
100 Prod<U2, B>: ArrayLength,
101 U1: Add<Prod<U2, B>>,
102 Sum<U1, Prod<U2, B>>: ArrayLength,
103{
104 pub fn new_in(alloc: A) -> AllocResult<Self> {
110 let raw = AllocatedBTreeMap::new_in(&alloc)?.into_inner();
111 Ok(Self { alloc, raw })
112 }
113
114 #[inline]
116 pub fn len(&self) -> usize {
117 self.raw.len()
118 }
119
120 #[inline]
122 pub fn is_empty(&self) -> bool {
123 self.raw.is_empty()
124 }
125
126 pub fn contains_key<Q>(&self, key: &Q) -> bool
128 where
129 K: Borrow<Q>,
130 Q: core::cmp::PartialOrd + Debug,
131 {
132 self.raw.contains_key(key)
133 }
134
135 pub fn get<Q>(&self, key: &Q) -> Option<&V>
137 where
138 K: Borrow<Q>,
139 Q: core::cmp::PartialOrd + Debug,
140 {
141 self.raw.get(key)
142 }
143
144 pub fn get_key_value<'s, Q>(&'s self, key: &'s Q) -> Option<(&'s K, &'s V)>
146 where
147 K: Borrow<Q>,
148 Q: core::cmp::PartialOrd + Debug,
149 {
150 self.raw.get_key_value(key)
151 }
152
153 pub fn get_mut<'s, Q>(&'s mut self, key: &'s Q) -> Option<&'s mut V>
155 where
156 K: Borrow<Q>,
157 Q: core::cmp::PartialOrd + Debug,
158 {
159 self.raw.get_mut(key)
160 }
161
162 pub fn insert(&mut self, key: K, value: V) -> AllocResult<Option<V>> {
172 unsafe { self.raw.insert_in(&self.alloc, key, value) }
174 }
175
176 pub fn clear(&mut self) -> AllocResult<()> {
182 unsafe { self.raw.clear_in(&self.alloc) }
184 }
185
186 pub fn entry(&mut self, key: K) -> Entry<'_, '_, A, K, V, B> {
188 unsafe { self.raw.entry_in(&self.alloc, key) }
190 }
191
192 pub fn first_entry(&mut self) -> Option<OccupiedEntry<'_, '_, A, K, V, B>> {
194 unsafe { self.raw.first_entry_in(&self.alloc) }
196 }
197
198 pub fn first_key_value(&self) -> Option<(&K, &V)> {
200 self.raw.first_key_value()
201 }
202
203 pub fn iter(&self) -> Iter<'_, K, V, B> {
205 self.raw.iter()
206 }
207
208 pub fn keys(&self) -> Keys<'_, K, V, B> {
210 self.raw.keys()
211 }
212
213 pub fn values(&self) -> Values<'_, K, V, B> {
215 self.raw.values()
216 }
217
218 pub fn values_mut(&mut self) -> ValuesMut<'_, K, V, B> {
220 self.raw.values_mut()
221 }
222}
223
224impl<'s, K: core::cmp::PartialOrd + Debug, V, B: ArrayLength, A: Allocator> IntoIterator
225 for &'s CompressedBTreeMap<K, V, B, A>
226where
227 U2: Mul<B>,
228 Prod<U2, B>: ArrayLength,
229 U1: Add<Prod<U2, B>>,
230 Sum<U1, Prod<U2, B>>: ArrayLength,
231{
232 type IntoIter = Iter<'s, K, V, B>;
233 type Item = (&'s K, &'s V);
234
235 fn into_iter(self) -> Self::IntoIter {
236 self.iter()
237 }
238}