use crate::{
iter::{AAIntoIter, AAIter},
node::{AANode, TraverseStep}
};
use alloc::vec::Vec;
use core::{
borrow::Borrow,
cmp::Ordering,
fmt::{self, Debug},
iter::FromIterator,
mem
};
mod entry;
mod get;
mod kv;
pub use entry::{Entry, OccupiedEntry, VacantEntry};
use kv::KeyValue;
#[derive(Clone)]
pub struct AATreeMap<K, V> {
root: AANode<KeyValue<K, V>>,
len: usize
}
impl<K, V> Default for AATreeMap<K, V> {
fn default() -> Self {
Self::new()
}
}
impl<K: Debug, V: Debug> Debug for AATreeMap<K, V> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("{")?;
for (i, (k, v)) in self.iter().enumerate() {
if i > 0 {
f.write_str(", ")?;
}
k.fmt(f)?;
f.write_str(": ")?;
v.fmt(f)?;
}
f.write_str("}")
}
}
impl<K: PartialEq, V: PartialEq> PartialEq for AATreeMap<K, V> {
fn eq(&self, other: &Self) -> bool {
self.len() == other.len() && self.iter().zip(other).all(|(a, b)| a == b)
}
}
impl<K: Eq, V: Eq> Eq for AATreeMap<K, V> {}
impl<K: PartialOrd, V: PartialOrd> PartialOrd for AATreeMap<K, V> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.iter().partial_cmp(other.iter())
}
}
impl<K: Ord, V: Ord> Ord for AATreeMap<K, V> {
fn cmp(&self, other: &Self) -> Ordering {
self.iter().cmp(other.iter())
}
}
impl<K, V> AATreeMap<K, V> {
pub const fn new() -> Self {
Self {
root: AANode::new(),
len: 0
}
}
pub fn len(&self) -> usize {
self.len
}
pub fn is_empty(&self) -> bool {
self.len == 0
}
pub fn clear(&mut self) {
self.root = AANode::new();
self.len = 0;
}
pub fn iter(&self) -> AAIter<'_, KeyValue<K, V>, (&K, &V)> {
self.into_iter()
}
pub fn keys(&self) -> impl Iterator<Item = &K> {
self.iter().map(|(k, _)| k)
}
pub fn values(&self) -> impl Iterator<Item = &V> {
self.iter().map(|(_, v)| v)
}
pub fn into_keys(self) -> impl Iterator<Item = K> {
self.into_iter().map(|(k, _)| k)
}
pub fn into_values(self) -> impl Iterator<Item = V> {
self.into_iter().map(|(_, v)| v)
}
pub fn insert(&mut self, key: K, value: V) -> Option<V>
where
K: Ord
{
let inserted = self.root.insert_or_replace(KeyValue { key, value });
match inserted {
None => {
self.len += 1;
None
},
Some(entry) => Some(entry.value)
}
}
pub fn append(&mut self, other: &mut Self)
where
K: Ord
{
self.extend(mem::take(other));
}
pub fn contains_key<Q>(&self, k: &Q) -> bool
where
K: Borrow<Q> + Ord,
Q: Ord + ?Sized
{
self.root
.traverse(
|content| match content.key.borrow().cmp(k) {
Ordering::Greater => TraverseStep::Left,
Ordering::Less => TraverseStep::Right,
Ordering::Equal => TraverseStep::Value(Some(()))
},
|_, sub| sub
)
.is_some()
}
pub fn remove<Q>(&mut self, k: &Q) -> Option<V>
where
K: Borrow<Q> + Ord,
Q: Ord + ?Sized
{
self.root.remove::<Q, K>(k).map(|entry| {
self.len -= 1;
entry.value
})
}
pub fn remove_entry<Q>(&mut self, k: &Q) -> Option<(K, V)>
where
K: Borrow<Q> + Ord,
Q: Ord + ?Sized
{
self.root.remove::<Q, K>(k).map(|kv| {
self.len -= 1;
kv.into_tuple()
})
}
}
impl<K: Ord, V> FromIterator<(K, V)> for AATreeMap<K, V> {
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = (K, V)>
{
let mut map = Self::new();
for (key, value) in iter {
map.insert(key, value);
}
map
}
}
impl<K: Ord, V, const N: usize> From<[(K, V); N]> for AATreeMap<K, V> {
fn from(array: [(K, V); N]) -> Self {
array.into_iter().collect()
}
}
impl<K: Ord, V> From<Vec<(K, V)>> for AATreeMap<K, V> {
fn from(vec: Vec<(K, V)>) -> Self {
vec.into_iter().collect()
}
}
impl<K: Ord, V> Extend<(K, V)> for AATreeMap<K, V> {
fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I) {
for (key, value) in iter {
self.insert(key, value);
}
}
}
impl<'a, K: Ord + Copy + 'a, V: Ord + Copy + 'a> Extend<(&'a K, &'a V)>
for AATreeMap<K, V>
{
fn extend<I: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: I) {
self.extend(iter.into_iter().map(|(k, v)| (*k, *v)))
}
}
impl<K, V> IntoIterator for AATreeMap<K, V> {
type Item = (K, V);
type IntoIter = AAIntoIter<KeyValue<K, V>, (K, V)>;
fn into_iter(self) -> Self::IntoIter {
AAIntoIter::new(self.root, self.len)
}
}
impl<'a, K, V> IntoIterator for &'a AATreeMap<K, V> {
type Item = (&'a K, &'a V);
type IntoIter = AAIter<'a, KeyValue<K, V>, (&'a K, &'a V)>;
fn into_iter(self) -> Self::IntoIter {
AAIter::new(&self.root, self.len)
}
}