use crate::helpers::write_to_level;
use crate::mapper::Mapper;
use crate::rbtree;
use crate::{RBMap, RBTree};
use std::fmt::{Debug, Display, Formatter, Result};
use std::iter::{ExactSizeIterator, FromIterator, FusedIterator};
impl<K: PartialOrd + Debug, V: Debug> Debug for RBMap<K, V> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
let mut levels = Vec::new();
write_to_level(&self.map.root, "".to_string(), 0, &mut levels);
let mut f_string = "".to_string();
for i in 0..levels.len() {
f_string += &levels[i];
if i != levels.len() - 1 {
f_string += "\n";
}
}
write!(f, "{}", f_string)
}
}
impl<K: PartialOrd + Debug, V: Debug> Display for RBMap<K, V> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
write!(f, "{:?}", self.ordered())
}
}
impl<K: PartialOrd, V> RBMap<K, V> {
pub fn new() -> RBMap<K, V> {
RBMap { map: RBTree::new() }
}
pub fn keyset(&self) -> RBTree<&K> {
let mut keys = RBTree::new();
for key in self.keys() {
keys.insert(key);
}
keys
}
pub fn into_keyset(self) -> RBTree<K> {
let mut kset = RBTree::new();
for (key, _) in self.into_iter() {
kset.insert(key);
}
kset
}
pub fn clear(&mut self) {
self.map = RBTree::new();
}
pub fn contains_key(&self, key: &K) -> bool {
match self.map.get(&Mapper::new(key, None)) {
None => false,
Some(v) => v.is_some(),
}
}
pub fn drain(&mut self) -> Drain<K, V> {
let mut rep = RBTree::new();
std::mem::swap(&mut self.map, &mut rep);
Drain { tree: rep }
}
pub fn get(&self, key: &K) -> Option<&V> {
self.map.get(&Mapper::new(key, None)).map(|v| v.as_ref())
}
pub fn get_pair(&self, key: &K) -> Option<(&K, &V)> {
self.map
.get(&Mapper::new(key, None))
.map(|v| (v.key(), v.as_ref()))
}
pub fn get_pair_mut(&mut self, key: &K) -> Option<(&K, &mut V)> {
self.map
.get_mut(&Mapper::new(key, None))
.map(|v| v.mut_pair())
}
pub fn get_mut(&mut self, key: &K) -> Option<&mut V> {
self.map
.get_mut(&Mapper::new(key, None))
.map(|v| v.as_mut())
}
pub fn peek(&self) -> Option<&V> {
self.map.peek().map(|v| v.as_ref())
}
pub fn peek_back(&self) -> Option<&V> {
self.map.peek_back().map(|v| v.as_ref())
}
pub fn peek_pair(&self) -> Option<(&K, &V)> {
self.map.peek().map(|v| v.pair())
}
pub fn peek_pair_back(&self) -> Option<(&K, &V)> {
self.map.peek_back().map(|v| v.pair())
}
pub fn insert(&mut self, key: K, val: V) -> Option<(K, V)> {
self.map
.replace(Mapper::new(key, Some(val)))
.map(|v| v.consume())
}
pub fn is_empty(&self) -> bool {
self.map.len() == 0
}
pub fn len(&self) -> usize {
self.map.len()
}
pub fn remove(&mut self, key: &K) -> Option<V> {
self.map
.take(&Mapper::new(key, None))
.map(|v| v.consume().1)
}
pub fn remove_entry(&mut self, key: &K) -> Option<(K, V)> {
self.map.take(&Mapper::new(key, None)).map(|v| v.consume())
}
pub fn pop(&mut self) -> Option<V> {
self.map.pop().map(|v| v.consume().1)
}
pub fn pop_back(&mut self) -> Option<V> {
self.map.pop_back().map(|v| v.consume().1)
}
pub fn pop_pair(&mut self) -> Option<(K, V)> {
self.map.pop().map(|v| v.consume())
}
pub fn pop_pair_back(&mut self) -> Option<(K, V)> {
self.map.pop_back().map(|v| v.consume())
}
pub fn retain<F: FnMut(&K, &mut V) -> bool>(&mut self, mut logic: F) {
let mut rep = RBMap::new();
for (key, mut val) in self.drain() {
if logic(&key, &mut val) {
rep.insert(key, val);
}
}
std::mem::swap(self, &mut rep);
}
pub fn iter(&self) -> Iter<K, V> {
Iter {
pos: 0,
ordered: self.ordered(),
}
}
pub fn iter_mut(&mut self) -> IterMut<K, V> {
IterMut {
iter: self.map.iter(),
}
}
pub fn values(&self) -> Values<K, V> {
Values {
pos: 0,
ordered: self.ordered(),
}
}
pub fn values_mut(&mut self) -> ValuesMut<K, V> {
ValuesMut {
iter: self.iter_mut(),
}
}
pub fn keys(&self) -> Keys<K, V> {
Keys {
pos: 0,
ordered: self.ordered(),
}
}
pub fn entry(&mut self, key: K) -> Entry<K, V> {
Entry { map: self, key }
}
fn ordered(&self) -> Vec<(&K, &V)> {
self.map.iter().map(|m| (m.key(), m.as_ref())).collect()
}
}
impl<K: PartialOrd, V: PartialOrd> RBMap<K, V> {
pub fn valueset(&self) -> RBTree<&V> {
let mut values = RBTree::new();
for value in self.values() {
values.insert(value);
}
values
}
pub fn into_sets(self) -> (RBTree<K>, RBTree<V>) {
let mut kset = RBTree::new();
let mut vset = RBTree::new();
for (key, value) in self.into_iter() {
kset.insert(key);
vset.insert(value);
}
(kset, vset)
}
pub fn into_valueset(self) -> RBTree<V> {
let mut vset = RBTree::new();
for (_, value) in self.into_iter() {
vset.insert(value);
}
vset
}
}
impl<K: PartialOrd, V> Default for RBMap<K, V> {
fn default() -> Self {
RBMap::new()
}
}
pub struct IntoIter<K: PartialOrd, V> {
tree: RBTree<Mapper<K, V>>,
}
impl<K: PartialOrd, V> Iterator for IntoIter<K, V> {
type Item = (K, V);
fn next(&mut self) -> Option<(K, V)> {
self.tree.pop().map(|v| v.consume())
}
}
impl<K: PartialOrd, V> ExactSizeIterator for IntoIter<K, V> {
fn len(&self) -> usize {
self.tree.len()
}
}
impl<K: PartialOrd, V> FusedIterator for IntoIter<K, V> {}
impl<K: PartialOrd, V> IntoIterator for RBMap<K, V> {
type Item = (K, V);
type IntoIter = IntoIter<K, V>;
fn into_iter(self) -> IntoIter<K, V> {
IntoIter { tree: self.map }
}
}
impl<K: PartialOrd, V> FromIterator<(K, V)> for RBMap<K, V> {
fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self {
let mut map = RBMap::new();
for (key, val) in iter {
map.insert(key, val);
}
map
}
}
impl<K: PartialOrd, V> Extend<(K, V)> for RBMap<K, V> {
fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I) {
for (key, val) in iter {
self.insert(key, val);
}
}
}
impl<'a, K: PartialOrd + Copy + 'a, V: Copy + 'a> Extend<(&'a K, &'a V)> for RBMap<K, V> {
fn extend<I: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: I) {
for (&key, &val) in iter {
self.insert(key, val);
}
}
}
pub struct Iter<'a, K: PartialOrd, V> {
pos: usize,
ordered: Vec<(&'a K, &'a V)>,
}
impl<'a, K: PartialOrd, V> Iterator for Iter<'a, K, V> {
type Item = (&'a K, &'a V);
fn next(&mut self) -> Option<(&'a K, &'a V)> {
match self.ordered.get(self.pos) {
Some(v) => {
self.pos += 1;
Some(*v)
}
None => None,
}
}
}
impl<'a, K: PartialOrd, V> ExactSizeIterator for Iter<'a, K, V> {
fn len(&self) -> usize {
self.ordered.len() - self.pos
}
}
impl<'a, K: PartialOrd, V> FusedIterator for Iter<'a, K, V> {}
pub struct Keys<'a, K: PartialOrd, V> {
pos: usize,
ordered: Vec<(&'a K, &'a V)>,
}
impl<'a, K: PartialOrd, V> Iterator for Keys<'a, K, V> {
type Item = &'a K;
fn next(&mut self) -> Option<&'a K> {
match self.ordered.get(self.pos) {
Some(v) => {
self.pos += 1;
Some(v.0)
}
None => None,
}
}
}
impl<'a, K: PartialOrd, V> ExactSizeIterator for Keys<'a, K, V> {
fn len(&self) -> usize {
self.ordered.len() - self.pos
}
}
impl<'a, K: PartialOrd, V> FusedIterator for Keys<'a, K, V> {}
pub struct Values<'a, K: PartialOrd, V> {
pos: usize,
ordered: Vec<(&'a K, &'a V)>,
}
impl<'a, K: PartialOrd, V> Iterator for Values<'a, K, V> {
type Item = &'a V;
fn next(&mut self) -> Option<&'a V> {
match self.ordered.get(self.pos) {
Some(v) => {
self.pos += 1;
Some(v.1)
}
None => None,
}
}
}
impl<'a, K: PartialOrd, V> ExactSizeIterator for Values<'a, K, V> {
fn len(&self) -> usize {
self.ordered.len() - self.pos
}
}
impl<'a, K: PartialOrd, V> FusedIterator for Values<'a, K, V> {}
pub struct ValuesMut<'a, K: PartialOrd, V> {
iter: IterMut<'a, K, V>,
}
impl<'a, K: PartialOrd, V> Iterator for ValuesMut<'a, K, V> {
type Item = &'a mut V;
fn next(&mut self) -> Option<&'a mut V> {
match self.iter.next() {
Some(v) => Some(v.1),
None => None,
}
}
}
impl<'a, K: PartialOrd, V> ExactSizeIterator for ValuesMut<'a, K, V> {
fn len(&self) -> usize {
self.iter.len()
}
}
impl<'a, K: PartialOrd, V> FusedIterator for ValuesMut<'a, K, V> {}
pub struct IterMut<'a, K: PartialOrd, V> {
iter: rbtree::Iter<'a, Mapper<K, V>>,
}
impl<'a, K: PartialOrd, V> Iterator for IterMut<'a, K, V> {
type Item = (&'a K, &'a mut V);
fn next(&mut self) -> Option<(&'a K, &'a mut V)> {
let next = self.iter.next();
match next {
Some(iv) => {
let v = unsafe {
let ptr = iv as *const Mapper<K, V>;
&mut *(ptr as *mut Mapper<K, V>)
};
Some(v.mut_pair())
}
None => None,
}
}
}
impl<'a, K: PartialOrd, V> ExactSizeIterator for IterMut<'a, K, V> {
fn len(&self) -> usize {
self.iter.len()
}
}
impl<'a, K: PartialOrd, V> FusedIterator for IterMut<'a, K, V> {}
pub struct Drain<K: PartialOrd, V> {
tree: RBTree<Mapper<K, V>>,
}
impl<K: PartialOrd, V> Iterator for Drain<K, V> {
type Item = (K, V);
fn next(&mut self) -> Option<(K, V)> {
self.tree.pop().map(|v| v.consume())
}
}
impl<K: PartialOrd, V> ExactSizeIterator for Drain<K, V> {
fn len(&self) -> usize {
self.tree.len()
}
}
impl<K: PartialOrd, V> FusedIterator for Drain<K, V> {}
pub struct Entry<'a, K: PartialOrd, V> {
map: &'a mut RBMap<K, V>,
key: K,
}
impl<'a, K: PartialOrd + Copy, V> Entry<'a, K, V> {
pub fn insert(self, val: V) -> (&'a K, &'a mut V) {
match self.map.remove_entry(&self.key) {
Some((k, _)) => {
self.map.insert(k, val);
}
None => {
self.map.insert(self.key, val);
}
}
self.map.get_pair_mut(&self.key).unwrap()
}
pub fn and_modify<F>(self, f: F) -> Entry<'a, K, V>
where
F: FnOnce(&mut V),
{
if let Some(v) = self.map.get_mut(&self.key).as_mut() {
f(*v);
}
self
}
pub fn or_insert(self, default: V) -> &'a mut V {
if !self.map.contains_key(&self.key) {
self.map.insert(self.key, default);
}
self.map.get_mut(&self.key).unwrap()
}
pub fn or_insert_with<F>(self, default: F) -> &'a mut V
where
F: FnOnce() -> V,
{
if !self.map.contains_key(&self.key) {
self.map.insert(self.key, default());
}
self.map.get_mut(&self.key).unwrap()
}
}
impl<'a, K: PartialOrd + Copy, V: Default> Entry<'a, K, V> {
pub fn or_default<F>(self) -> &'a mut V
where
F: FnOnce() -> V,
{
if !self.map.contains_key(&self.key) {
self.map.insert(self.key, V::default());
}
self.map.get_mut(&self.key).unwrap()
}
}