mod structs;
mod balance;
mod traits;
mod iters;
mod errors; mod into_precomputed;
#[cfg(any(feature = "unchecked_mut", doc))]
pub use iters::IterMutUnchecked;
#[cfg(any(feature = "into_precomputed", doc))]
pub use into_precomputed::PrecomputedIterNode;
#[cfg(any(feature = "into_precomputed", doc))]
pub use iters::IntoIterPrecomp;
#[cfg(test)]
mod test;
use std::{
ptr::NonNull,
};
pub struct Map<KeyType:Ord, ContentType>{
head: Option<MapLink<KeyType, ContentType>>,
size: usize,
}
pub use errors::Error;
pub use iters::IntoIter;
pub use iters::Iter;
pub use iters::IterMut;
#[allow(dead_code)]
struct MapNode<KeyType:Ord, ContentType>{
key: KeyType,
content: ContentType,
father: Option<MapLink<KeyType,ContentType>>,
depth: structs::BinarySon<i32>,
son: structs::BinarySon<Option<MapLink<KeyType,ContentType>>>,
metadata: into_precomputed::FeatureField,
}
type MapLink<KeyType, ContentType> = NonNull<MapNode<KeyType, ContentType>>;
impl<KeyType:Ord, ContentType> Map<KeyType, ContentType>{
pub fn new() -> Self {
Self{head:None ,size:0}
}
pub fn add(&mut self, key:KeyType, content:ContentType) -> Result<(), Error> {
let new_node = MapNode::new_map_link(key, content);
match self.head {
None => {
self.head = Some(new_node);
self.size = 1;
Ok(())
}
Some(data) => {
if let Err(_place) = MapNode::insert_node(data, new_node) {
MapNode::free_node(new_node);
return Err(Error::KeyOcupied);
}
self.size += 1;
self.compute_balance_additive(new_node);
Ok(())
}
}
}
pub fn insert(&mut self, key:KeyType, content:ContentType) -> Option<ContentType> {
let new_node = MapNode::new_map_link(key, content);
match self.head {
None => {
self.head = Some(new_node);
self.size = 1;
None
}
Some(data) => {
if let Err(place) = MapNode::insert_node(data, new_node) {
self.replace_node(place, new_node);
return Some(MapNode::unpack_node(place)); }
self.size += 1;
self.compute_balance_additive(new_node);
None
}
}
}
pub fn empty(&mut self) {
let empty_iter = self.empty_iter();
for _elem in empty_iter {
}
}
pub fn get(&self, key:&KeyType) -> Result<&ContentType, Error> {
let pivot = match self.head {
None => {return Err(Error::NotFound);}
Some(data) => data,
};
let node = MapNode::find_node(key, pivot).ok_or(Error::NotFound)?;
let node_ref = unsafe{node.as_ref()};
Ok(&node_ref.content)
}
pub fn get_mut(&mut self, key:&KeyType) -> Result<&mut ContentType, Error> {
let pivot = match self.head {
None => {return Err(Error::NotFound);}
Some(data) => data,
};
let mut node = MapNode::find_node(key, pivot).ok_or(Error::NotFound)?;
let node_mut = unsafe{node.as_mut()};
Ok(&mut node_mut.content)
}
pub fn remove(&mut self, key:&KeyType) -> Result<ContentType, Error> {
match self.size {
0 => {
Err(Error::NotFound)
}
1 => {
let head = self.head.unwrap();
let head_ref = unsafe{head.as_ref()};
if !head_ref.key.cmp(key).is_eq() {
return Err(Error::NotFound);
}
self.size = 0;
self.head = None;
let target = unsafe{Box::from_raw(head.as_ptr())};
Ok(target.content)
}
_ => {
let target = MapNode::find_node(key, self.head.unwrap()).ok_or(Error::NotFound)?;
let balance_pivot = self.compute_subtraccion_pivot(target);
self.compute_balance_subtractive(balance_pivot);
self.size -= 1;
let target = unsafe{Box::from_raw(target.as_ptr())};
Ok(target.content)
}
}
}
pub fn delete(&mut self, key:&KeyType) -> Result<(), Error> {
match self.size {
0 => {
Err(Error::NotFound)
}
1 => {
let head = self.head.unwrap();
let head_ref = unsafe{head.as_ref()};
if !head_ref.key.cmp(key).is_eq() {
return Err(Error::NotFound);
}
self.size = 0;
self.head = None;
MapNode::free_node(head);
Ok(())
}
_ => {
let target = MapNode::find_node(key, self.head.unwrap()).ok_or(Error::NotFound)?;
let balance_pivot = self.compute_subtraccion_pivot(target);
self.compute_balance_subtractive(balance_pivot);
self.size -= 1;
MapNode::free_node(target);
Ok(())
}
}
}
#[inline(always)]
pub fn len(&self) -> usize {
self.size
}
pub fn into_iter(self) -> iters::IntoIter<KeyType, ContentType> {
iters::IntoIter::new(self)
}
pub fn iter(&self) -> iters::Iter<KeyType, ContentType> {
iters::Iter::new(self)
}
pub fn iter_mut(&mut self) -> iters::IterMut<KeyType, ContentType> {
iters::IterMut::new(self)
}
#[cfg(any(feature = "unchecked_mut", doc))]
pub fn iter_ref_mut_unchecked(&mut self) -> IterMutUnchecked<KeyType, ContentType> {
iters::IterMutUnchecked::new(self)
}
#[cfg(any(feature = "into_precomputed", doc))]
pub fn into_iter_precomputed(self) -> iters::IntoIterPrecomp<KeyType, ContentType> {
iters::IntoIterPrecomp::new(self)
}
}