use crate::avl::{Iter, Tree};
use std::{
borrow::Borrow,
cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd},
default::Default,
fmt::{self, Debug, Formatter},
hash::{Hash, Hasher},
iter::FromIterator,
ops::{Bound, Index},
};
#[derive(Clone)]
pub struct Map<K: Ord + Clone, V: Clone>(Tree<K, V>);
impl<K, V> Hash for Map<K, V>
where
K: Hash + Ord + Clone,
V: Hash + Clone,
{
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.hash(state)
}
}
impl<K, V> Default for Map<K, V>
where
K: Ord + Clone,
V: Clone,
{
fn default() -> Map<K, V> {
Map::new()
}
}
impl<K, V> PartialEq for Map<K, V>
where
K: PartialEq + Ord + Clone,
V: PartialEq + Clone,
{
fn eq(&self, other: &Map<K, V>) -> bool {
self.0 == other.0
}
}
impl<K, V> Eq for Map<K, V>
where
K: Eq + Ord + Clone,
V: Eq + Clone,
{
}
impl<K, V> PartialOrd for Map<K, V>
where
K: Ord + Clone,
V: PartialOrd + Clone,
{
fn partial_cmp(&self, other: &Map<K, V>) -> Option<Ordering> {
self.0.partial_cmp(&other.0)
}
}
impl<K, V> Ord for Map<K, V>
where
K: Ord + Clone,
V: Ord + Clone,
{
fn cmp(&self, other: &Map<K, V>) -> Ordering {
self.0.cmp(&other.0)
}
}
impl<K, V> Debug for Map<K, V>
where
K: Debug + Ord + Clone,
V: Debug + Clone,
{
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
impl<'a, Q, K, V> Index<&'a Q> for Map<K, V>
where
Q: Ord,
K: Ord + Clone + Borrow<Q>,
V: Clone,
{
type Output = V;
fn index(&self, k: &Q) -> &V {
self.get(k).expect("element not found for key")
}
}
impl<K, V> FromIterator<(K, V)> for Map<K, V>
where
K: Ord + Clone,
V: Clone,
{
fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self {
Map::new().insert_many(iter)
}
}
impl<'a, K, V> IntoIterator for &'a Map<K, V>
where
K: 'a + Borrow<K> + Ord + Clone,
V: 'a + Clone,
{
type Item = (&'a K, &'a V);
type IntoIter = Iter<'a, K, K, V>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl<K, V> Map<K, V>
where
K: Ord + Clone,
V: Clone,
{
pub fn new() -> Self {
Map(Tree::new())
}
pub fn insert_many<E: IntoIterator<Item = (K, V)>>(&self, elts: E) -> Self {
Map(self.0.insert_many(elts))
}
pub fn update_many<Q, D, E, F>(&self, elts: E, mut f: F) -> Self
where
E: IntoIterator<Item = (Q, D)>,
Q: Ord,
K: Borrow<Q>,
F: FnMut(Q, D, Option<(&K, &V)>) -> Option<(K, V)>,
{
Map(self.0.update_many(elts, &mut f))
}
pub fn insert(&self, k: K, v: V) -> (Self, Option<V>) {
let (root, prev) = self.0.insert(k, v);
(Map(root), prev)
}
pub fn update<Q, D, F>(&self, q: Q, d: D, mut f: F) -> (Self, Option<V>)
where
Q: Ord,
K: Borrow<Q>,
F: FnMut(Q, D, Option<(&K, &V)>) -> Option<(K, V)>,
{
let (root, prev) = self.0.update(q, d, &mut f);
(Map(root), prev)
}
pub fn union<F>(&self, other: &Map<K, V>, mut f: F) -> Self
where
F: FnMut(&K, &V, &V) -> Option<V>,
{
Map(Tree::union(&self.0, &other.0, &mut f))
}
pub fn intersect<F>(&self, other: &Map<K, V>, mut f: F) -> Self
where
F: FnMut(&K, &V, &V) -> Option<V>,
{
Map(Tree::intersect(&self.0, &other.0, &mut f))
}
pub fn diff<F>(&self, other: &Map<K, V>, mut f: F) -> Self
where F: FnMut(&K, &V, &V) -> Option<V>, K: Debug, V: Debug
{
Map(Tree::diff(&self.0, &other.0, &mut f))
}
pub fn get<'a, Q: ?Sized + Ord>(&'a self, k: &Q) -> Option<&'a V>
where
K: Borrow<Q>,
{
self.0.get(k)
}
pub fn get_key<'a, Q: ?Sized + Ord>(&'a self, k: &Q) -> Option<&'a K>
where
K: Borrow<Q>,
{
self.0.get_key(k)
}
pub fn get_full<'a, Q: ?Sized + Ord>(&'a self, k: &Q) -> Option<(&'a K, &'a V)>
where
K: Borrow<Q>,
{
self.0.get_full(k)
}
pub fn remove<Q: Sized + Ord>(&self, k: &Q) -> (Self, Option<V>)
where
K: Borrow<Q>,
{
let (t, prev) = self.0.remove(k);
(Map(t), prev)
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn range<'a, Q>(&'a self, lbound: Bound<Q>, ubound: Bound<Q>) -> Iter<'a, Q, K, V>
where
Q: Ord,
K: Borrow<Q>,
{
self.0.range(lbound, ubound)
}
}
impl<K, V> Map<K, V>
where
K: Ord + Clone + Debug,
V: Clone + Debug,
{
#[allow(dead_code)]
pub fn invariant(&self) -> () {
self.0.invariant()
}
}