use compare::{Compare, Natural};
use std::cmp::Ordering;
use std::cmp::Ordering::*;
#[cfg(feature = "range")] use std::collections::Bound;
use std::fmt::{self, Debug};
use std::hash::{self, Hash};
use std::iter;
use std::ops;
use super::node::{self, Extreme, Max, Min, MarkedNode, MutMarkedNode, Node};
use super::node::build::{Get, GetMut, PathBuilder};
pub use super::node::{OccupiedEntry, VacantEntry};
#[derive(Clone)]
pub struct Map<K, V, C = Natural<K>> where C: Compare<K> {
root: node::Link<K, V>,
len: usize,
cmp: C,
}
impl<K, V> Map<K, V> where K: Ord {
pub fn new() -> Self { Map::with_cmp(Natural::default()) }
}
impl<K, V, C> Map<K, V, C> where C: Compare<K> {
pub fn with_cmp(cmp: C) -> Self {
Map { root: None, len: 0, cmp: cmp }
}
pub fn is_empty(&self) -> bool { self.root.is_none() }
pub fn len(&self) -> usize { self.len }
pub fn cmp(&self) -> &C { &self.cmp }
pub fn clear(&mut self) {
self.root = None;
self.len = 0;
}
pub fn insert(&mut self, key: K, value: V) -> Option<V> {
let old_value = node::insert(&mut self.root, &self.cmp, key, value);
if old_value.is_none() { self.len += 1; }
old_value
}
pub fn remove<Q: ?Sized>(&mut self, key: &Q) -> Option<(K, V)>
where C: Compare<Q, K> {
node::find(&mut self.root, PathBuilder::default(), &self.cmp, key).remove(&mut self.len)
}
pub fn entry(&mut self, key: K) -> Entry<K, V> {
node::find(&mut self.root, PathBuilder::default(), &self.cmp, &key)
.into_entry(&mut self.len, key)
}
pub fn contains_key<Q: ?Sized>(&self, key: &Q) -> bool where C: Compare<Q, K> {
self.get(key).is_some()
}
pub fn get<Q: ?Sized>(&self, key: &Q) -> Option<&V> where C: Compare<Q, K> {
node::find(&self.root, Get::default(), &self.cmp, key).map(|e| e.1)
}
pub fn get_mut<Q: ?Sized>(&mut self, key: &Q) -> Option<&mut V>
where C: Compare<Q, K> {
node::find(&mut self.root, GetMut::default(), &self.cmp, key).map(|e| e.1)
}
pub fn max(&self) -> Option<(&K, &V)> {
Max::extreme(&self.root, Get::default())
}
pub fn max_mut(&mut self) -> Option<(&K, &mut V)> {
Max::extreme(&mut self.root, GetMut::default())
}
pub fn remove_max(&mut self) -> Option<(K, V)> {
Max::extreme(&mut self.root, PathBuilder::default()).remove(&mut self.len)
}
pub fn max_entry(&mut self) -> Option<OccupiedEntry<K, V>> {
Max::extreme(&mut self.root, PathBuilder::default()).into_occupied_entry(&mut self.len)
}
pub fn min(&self) -> Option<(&K, &V)> {
Min::extreme(&self.root, Get::default())
}
pub fn min_mut(&mut self) -> Option<(&K, &mut V)> {
Min::extreme(&mut self.root, GetMut::default())
}
pub fn remove_min(&mut self) -> Option<(K, V)> {
Min::extreme(&mut self.root, PathBuilder::default()).remove(&mut self.len)
}
pub fn min_entry(&mut self) -> Option<OccupiedEntry<K, V>> {
Min::extreme(&mut self.root, PathBuilder::default()).into_occupied_entry(&mut self.len)
}
pub fn pred<Q: ?Sized>(&self, key: &Q, inclusive: bool) -> Option<(&K, &V)>
where C: Compare<Q, K> {
Min::closest(&self.root, Get::default(), &self.cmp, key, inclusive)
}
pub fn pred_mut<Q: ?Sized>(&mut self, key: &Q, inclusive: bool) -> Option<(&K, &mut V)>
where C: Compare<Q, K> {
Min::closest(&mut self.root, GetMut::default(), &self.cmp, key, inclusive)
}
pub fn remove_pred<Q: ?Sized>(&mut self, key: &Q, inclusive: bool) -> Option<(K, V)>
where C: Compare<Q, K> {
Min::closest(&mut self.root, PathBuilder::default(), &self.cmp, key, inclusive)
.remove(&mut self.len)
}
pub fn pred_entry<Q: ?Sized>(&mut self, key: &Q, inclusive: bool)
-> Option<OccupiedEntry<K, V>> where C: Compare<Q, K> {
Min::closest(&mut self.root, PathBuilder::default(), &self.cmp, key, inclusive)
.into_occupied_entry(&mut self.len)
}
pub fn succ<Q: ?Sized>(&self, key: &Q, inclusive: bool) -> Option<(&K, &V)>
where C: Compare<Q, K> {
Max::closest(&self.root, Get::default(), &self.cmp, key, inclusive)
}
pub fn succ_mut<Q: ?Sized>(&mut self, key: &Q, inclusive: bool) -> Option<(&K, &mut V)>
where C: Compare<Q, K> {
Max::closest(&mut self.root, GetMut::default(), &self.cmp, key, inclusive)
}
pub fn remove_succ<Q: ?Sized>(&mut self, key: &Q, inclusive: bool) -> Option<(K, V)>
where C: Compare<Q, K> {
Max::closest(&mut self.root, PathBuilder::default(), &self.cmp, key, inclusive)
.remove(&mut self.len)
}
pub fn succ_entry<Q: ?Sized>(&mut self, key: &Q, inclusive: bool)
-> Option<OccupiedEntry<K, V>> where C: Compare<Q, K> {
Max::closest(&mut self.root, PathBuilder::default(), &self.cmp, key, inclusive)
.into_occupied_entry(&mut self.len)
}
pub fn iter(&self) -> Iter<K, V> {
Iter(node::Iter::new(self.root.as_ref().map(MarkedNode::new), self.len))
}
pub fn iter_mut(&mut self) -> IterMut<K, V> {
IterMut(node::Iter::new(self.root.as_mut().map(MutMarkedNode::new), self.len))
}
#[cfg(test)]
pub fn root(&self) -> &node::Link<K, V> { &self.root }
}
#[cfg(feature = "range")]
impl<K, V, C> Map<K, V, C> where C: Compare<K> {
pub fn into_range<Min: ?Sized, Max: ?Sized>(mut self, min: Bound<&Min>, max: Bound<&Max>)
-> IntoRange<K, V> where C: Compare<Min, K> + Compare<Max, K> {
IntoRange(node::Range::new(self.root.take(), self.len, &self.cmp, min, max))
}
pub fn range<Min: ?Sized, Max: ?Sized>(&self, min: Bound<&Min>, max: Bound<&Max>)
-> Range<K, V> where C: Compare<Min, K> + Compare<Max, K> {
Range(node::Range::new(self.root.as_ref().map(MarkedNode::new), self.len, &self.cmp, min,
max))
}
pub fn range_mut<Min: ?Sized, Max: ?Sized>(&mut self, min: Bound<&Min>, max: Bound<&Max>)
-> RangeMut<K, V> where C: Compare<Min, K> + Compare<Max, K> {
RangeMut(node::Range::new(self.root.as_mut().map(MutMarkedNode::new), self.len, &self.cmp,
min, max))
}
}
impl<K, V, C> Debug for Map<K, V, C> where K: Debug, V: Debug, C: Compare<K> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_map().entries(self).finish()
}
}
impl<K, V, C> Default for Map<K, V, C> where C: Compare<K> + Default {
fn default() -> Self { Map::with_cmp(C::default()) }
}
impl<K, V, C> Extend<(K, V)> for Map<K, V, C> where C: Compare<K> {
fn extend<I: IntoIterator<Item=(K, V)>>(&mut self, it: I) {
for (k, v) in it { self.insert(k, v); }
}
}
impl<K, V, C> iter::FromIterator<(K, V)> for Map<K, V, C>
where C: Compare<K> + Default {
fn from_iter<I: IntoIterator<Item=(K, V)>>(it: I) -> Self {
let mut map = Map::default();
map.extend(it);
map
}
}
impl<K, V, C> Hash for Map<K, V, C> where K: Hash, V: Hash, C: Compare<K> {
fn hash<H: hash::Hasher>(&self, h: &mut H) {
for e in self.iter() { e.hash(h); }
}
}
impl<'a, K, V, C, Q: ?Sized> ops::Index<&'a Q> for Map<K, V, C>
where C: Compare<K> + Compare<Q, K> {
type Output = V;
fn index(&self, key: &Q) -> &V { self.get(key).expect("key not found") }
}
impl<'a, K, V, C> IntoIterator for &'a Map<K, V, C> where C: Compare<K> {
type Item = (&'a K, &'a V);
type IntoIter = Iter<'a, K, V>;
fn into_iter(self) -> Iter<'a, K, V> { self.iter() }
}
impl<'a, K, V, C> IntoIterator for &'a mut Map<K, V, C> where C: Compare<K> {
type Item = (&'a K, &'a mut V);
type IntoIter = IterMut<'a, K, V>;
fn into_iter(self) -> IterMut<'a, K, V> { self.iter_mut() }
}
impl<K, V, C> IntoIterator for Map<K, V, C> where C: Compare<K> {
type Item = (K, V);
type IntoIter = IntoIter<K, V>;
fn into_iter(self) -> IntoIter<K, V> { IntoIter(node::Iter::new(self.root, self.len)) }
}
impl<K, V, C> PartialEq for Map<K, V, C> where V: PartialEq, C: Compare<K> {
fn eq(&self, other: &Self) -> bool {
self.len() == other.len() && self.iter().zip(other.iter()).all(|(l, r)| {
self.cmp.compares_eq(&l.0, &r.0) && l.1 == r.1
})
}
}
impl<K, V, C> Eq for Map<K, V, C> where V: Eq, C: Compare<K> {}
impl<K, V, C> PartialOrd for Map<K, V, C> where V: PartialOrd, C: Compare<K> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
let mut l = self.iter();
let mut r = other.iter();
loop {
match (l.next(), r.next()) {
(None, None) => return Some(Equal),
(None, Some(_)) => return Some(Less),
(Some(_), None) => return Some(Greater),
(Some(l), Some(r)) => match self.cmp.compare(&l.0, &r.0) {
Equal => match l.1.partial_cmp(&r.1) {
Some(Equal) => {}
non_eq => return non_eq,
},
non_eq => return Some(non_eq),
},
}
}
}
}
impl<K, V, C> Ord for Map<K, V, C> where V: Ord, C: Compare<K> {
fn cmp(&self, other: &Self) -> Ordering {
let mut l = self.iter();
let mut r = other.iter();
loop {
match (l.next(), r.next()) {
(None, None) => return Equal,
(None, Some(_)) => return Less,
(Some(_), None) => return Greater,
(Some(l), Some(r)) => match self.cmp.compare(&l.0, &r.0) {
Equal => match l.1.cmp(&r.1) {
Equal => {}
non_eq => return non_eq,
},
non_eq => return non_eq,
},
}
}
}
}
#[derive(Clone)]
pub struct IntoIter<K, V>(node::Iter<Box<Node<K, V>>>);
impl<K, V> Iterator for IntoIter<K, V> {
type Item = (K, V);
fn next(&mut self) -> Option<Self::Item> { self.0.next() }
fn size_hint(&self) -> (usize, Option<usize>) { self.0.size_hint() }
fn count(self) -> usize { self.len() }
fn last(mut self) -> Option<Self::Item> { self.next_back() }
}
impl<K, V> DoubleEndedIterator for IntoIter<K, V> {
fn next_back(&mut self) -> Option<Self::Item> { self.0.next_back() }
}
impl<K, V> ExactSizeIterator for IntoIter<K, V> {
fn len(&self) -> usize { self.0.len() }
}
pub struct Iter<'a, K: 'a, V: 'a>(node::Iter<MarkedNode<'a, K, V>>);
impl<'a, K, V> Clone for Iter<'a, K, V> {
fn clone(&self) -> Self { Iter(self.0.clone()) }
}
impl<'a, K, V> Iterator for Iter<'a, K, V> {
type Item = (&'a K, &'a V);
fn next(&mut self) -> Option<Self::Item> { self.0.next() }
fn size_hint(&self) -> (usize, Option<usize>) { self.0.size_hint() }
fn count(self) -> usize { self.len() }
fn last(mut self) -> Option<Self::Item> { self.next_back() }
}
impl<'a, K, V> DoubleEndedIterator for Iter<'a, K, V> {
fn next_back(&mut self) -> Option<Self::Item> { self.0.next_back() }
}
impl<'a, K, V> ExactSizeIterator for Iter<'a, K, V> {
fn len(&self) -> usize { self.0.len() }
}
pub struct IterMut<'a, K: 'a, V: 'a>(node::Iter<MutMarkedNode<'a, K, V>>);
impl<'a, K, V> Iterator for IterMut<'a, K, V> {
type Item = (&'a K, &'a mut V);
fn next(&mut self) -> Option<Self::Item> { self.0.next() }
fn size_hint(&self) -> (usize, Option<usize>) { self.0.size_hint() }
fn count(self) -> usize { self.len() }
fn last(mut self) -> Option<Self::Item> { self.next_back() }
}
impl<'a, K, V> DoubleEndedIterator for IterMut<'a, K, V> {
fn next_back(&mut self) -> Option<Self::Item> { self.0.next_back() }
}
impl<'a, K, V> ExactSizeIterator for IterMut<'a, K, V> {
fn len(&self) -> usize { self.0.len() }
}
#[cfg(feature = "range")]
#[derive(Clone)]
pub struct IntoRange<K, V>(node::Range<Box<Node<K, V>>>);
#[cfg(feature = "range")]
impl<K, V> Iterator for IntoRange<K, V> {
type Item = (K, V);
fn next(&mut self) -> Option<Self::Item> { self.0.next() }
fn size_hint(&self) -> (usize, Option<usize>) { self.0.size_hint() }
fn last(mut self) -> Option<Self::Item> { self.next_back() }
}
#[cfg(feature = "range")]
impl<K, V> DoubleEndedIterator for IntoRange<K, V> {
fn next_back(&mut self) -> Option<Self::Item> { self.0.next_back() }
}
#[cfg(feature = "range")]
pub struct Range<'a, K: 'a, V: 'a>(node::Range<MarkedNode<'a, K, V>>);
#[cfg(feature = "range")]
impl<'a, K, V> Clone for Range<'a, K, V> {
fn clone(&self) -> Self { Range(self.0.clone()) }
}
#[cfg(feature = "range")]
impl<'a, K, V> Iterator for Range<'a, K, V> {
type Item = (&'a K, &'a V);
fn next(&mut self) -> Option<Self::Item> { self.0.next() }
fn size_hint(&self) -> (usize, Option<usize>) { self.0.size_hint() }
fn last(mut self) -> Option<Self::Item> { self.next_back() }
}
#[cfg(feature = "range")]
impl<'a, K, V> DoubleEndedIterator for Range<'a, K, V> {
fn next_back(&mut self) -> Option<Self::Item> { self.0.next_back() }
}
#[cfg(feature = "range")]
pub struct RangeMut<'a, K: 'a, V: 'a>(node::Range<MutMarkedNode<'a, K, V>>);
#[cfg(feature = "range")]
impl<'a, K, V> Iterator for RangeMut<'a, K, V> {
type Item = (&'a K, &'a mut V);
fn next(&mut self) -> Option<Self::Item> { self.0.next() }
fn size_hint(&self) -> (usize, Option<usize>) { self.0.size_hint() }
fn last(mut self) -> Option<Self::Item> { self.next_back() }
}
#[cfg(feature = "range")]
impl<'a, K, V> DoubleEndedIterator for RangeMut<'a, K, V> {
fn next_back(&mut self) -> Option<Self::Item> { self.0.next_back() }
}
pub enum Entry<'a, K: 'a, V: 'a> {
Occupied(OccupiedEntry<'a, K, V>),
Vacant(VacantEntry<'a, K, V>),
}
impl<'a, K, V> Entry<'a, K, V> {
pub fn or_insert(self, default: V) -> &'a mut V {
match self {
Entry::Occupied(e) => e.into_mut(),
Entry::Vacant(e) => e.insert(default),
}
}
pub fn or_insert_with<F>(self, default: F) -> &'a mut V where F: FnOnce() -> V {
match self {
Entry::Occupied(e) => e.into_mut(),
Entry::Vacant(e) => e.insert(default()),
}
}
}