1use crate::{
2 iter::{AAIntoIter, AAIter},
3 node::{AANode, TraverseStep}
4};
5use alloc::vec::Vec;
6use core::{
7 borrow::Borrow,
8 cmp::Ordering,
9 fmt::{self, Debug},
10 iter::FromIterator,
11 mem
12};
13
14mod entry;
15mod get;
16mod kv;
17
18pub use entry::{Entry, OccupiedEntry, VacantEntry};
19use kv::KeyValue;
20
21#[derive(Clone)]
22pub struct AATreeMap<K, V> {
23 root: AANode<KeyValue<K, V>>,
24 len: usize
25}
26
27impl<K, V> Default for AATreeMap<K, V> {
28 fn default() -> Self {
29 Self::new()
30 }
31}
32
33impl<K: Debug, V: Debug> Debug for AATreeMap<K, V> {
34 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35 f.write_str("{")?;
36 for (i, (k, v)) in self.iter().enumerate() {
37 if i > 0 {
38 f.write_str(", ")?;
39 }
40 k.fmt(f)?;
41 f.write_str(": ")?;
42 v.fmt(f)?;
43 }
44 f.write_str("}")
45 }
46}
47
48impl<K: PartialEq, V: PartialEq> PartialEq for AATreeMap<K, V> {
49 fn eq(&self, other: &Self) -> bool {
50 self.len() == other.len() && self.iter().zip(other).all(|(a, b)| a == b)
51 }
52}
53
54impl<K: Eq, V: Eq> Eq for AATreeMap<K, V> {}
55
56impl<K: PartialOrd, V: PartialOrd> PartialOrd for AATreeMap<K, V> {
57 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
58 self.iter().partial_cmp(other.iter())
59 }
60}
61
62impl<K: Ord, V: Ord> Ord for AATreeMap<K, V> {
63 fn cmp(&self, other: &Self) -> Ordering {
64 self.iter().cmp(other.iter())
65 }
66}
67
68impl<K, V> AATreeMap<K, V> {
69 pub const fn new() -> Self {
79 Self {
80 root: AANode::new(),
81 len: 0
82 }
83 }
84
85 pub fn len(&self) -> usize {
97 self.len
98 }
99
100 pub fn is_empty(&self) -> bool {
112 self.len == 0
113 }
114
115 pub fn clear(&mut self) {
127 self.root = AANode::new();
128 self.len = 0;
129 }
130
131 pub fn iter(&self) -> AAIter<'_, KeyValue<K, V>, (&K, &V)> {
133 self.into_iter()
134 }
135
136 pub fn keys(&self) -> impl Iterator<Item = &K> {
138 self.iter().map(|(k, _)| k)
140 }
141
142 pub fn values(&self) -> impl Iterator<Item = &V> {
144 self.iter().map(|(_, v)| v)
146 }
147
148 pub fn into_keys(self) -> impl Iterator<Item = K> {
151 self.into_iter().map(|(k, _)| k)
153 }
154
155 pub fn into_values(self) -> impl Iterator<Item = V> {
158 self.into_iter().map(|(_, v)| v)
160 }
161
162 pub fn insert(&mut self, key: K, value: V) -> Option<V>
177 where
178 K: Ord
179 {
180 let inserted = self.root.insert_or_replace(KeyValue { key, value });
181 match inserted {
182 None => {
183 self.len += 1;
184 None
185 },
186 Some(entry) => Some(entry.value)
187 }
188 }
189
190 pub fn append(&mut self, other: &mut Self)
219 where
220 K: Ord
221 {
222 self.extend(mem::take(other));
223 }
224
225 pub fn contains_key<Q>(&self, k: &Q) -> bool
237 where
238 K: Borrow<Q> + Ord,
239 Q: Ord + ?Sized
240 {
241 self.root
242 .traverse(
243 |content| match content.key.borrow().cmp(k) {
244 Ordering::Greater => TraverseStep::Left,
245 Ordering::Less => TraverseStep::Right,
246 Ordering::Equal => TraverseStep::Value(Some(()))
247 },
248 |_, sub| sub
249 )
250 .is_some()
251 }
252
253 pub fn remove<Q>(&mut self, k: &Q) -> Option<V>
269 where
270 K: Borrow<Q> + Ord,
271 Q: Ord + ?Sized
272 {
273 self.root.remove::<Q, K>(k).map(|entry| entry.value)
274 }
275
276 pub fn remove_entry<Q>(&mut self, k: &Q) -> Option<(K, V)>
292 where
293 K: Borrow<Q> + Ord,
294 Q: Ord + ?Sized
295 {
296 self.root.remove::<Q, K>(k).map(KeyValue::into_tuple)
297 }
298}
299
300impl<K: Ord, V> FromIterator<(K, V)> for AATreeMap<K, V> {
301 fn from_iter<I>(iter: I) -> Self
302 where
303 I: IntoIterator<Item = (K, V)>
304 {
305 let mut map = Self::new();
306 for (key, value) in iter {
307 map.insert(key, value);
308 }
309 map
310 }
311}
312
313impl<K: Ord, V, const N: usize> From<[(K, V); N]> for AATreeMap<K, V> {
314 fn from(array: [(K, V); N]) -> Self {
315 array.into_iter().collect()
316 }
317}
318
319impl<K: Ord, V> From<Vec<(K, V)>> for AATreeMap<K, V> {
320 fn from(vec: Vec<(K, V)>) -> Self {
321 vec.into_iter().collect()
322 }
323}
324
325impl<K: Ord, V> Extend<(K, V)> for AATreeMap<K, V> {
326 fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I) {
327 for (key, value) in iter {
328 self.insert(key, value);
329 }
330 }
331}
332
333impl<'a, K: Ord + Copy + 'a, V: Ord + Copy + 'a> Extend<(&'a K, &'a V)>
334 for AATreeMap<K, V>
335{
336 fn extend<I: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: I) {
337 self.extend(iter.into_iter().map(|(k, v)| (*k, *v)))
338 }
339}
340
341impl<K, V> IntoIterator for AATreeMap<K, V> {
342 type Item = (K, V);
343 type IntoIter = AAIntoIter<KeyValue<K, V>, (K, V)>;
344
345 fn into_iter(self) -> Self::IntoIter {
346 AAIntoIter::new(self.root, self.len)
347 }
348}
349
350impl<'a, K, V> IntoIterator for &'a AATreeMap<K, V> {
351 type Item = (&'a K, &'a V);
352 type IntoIter = AAIter<'a, KeyValue<K, V>, (&'a K, &'a V)>;
353
354 fn into_iter(self) -> Self::IntoIter {
355 AAIter::new(&self.root, self.len)
356 }
357}