use crate::vec::{Indexer, ItemVec};
use std::collections::HashMap;
use std::hash::{BuildHasher, Hash};
pub trait ItemMap<K, V>
where
K: Copy,
{
fn is_empty(&self) -> bool {
self.len() == 0
}
fn len(&self) -> usize;
fn clear(&mut self);
fn insert(&mut self, key: K, value: V) -> bool;
fn insert_or_replace(&mut self, key: K, value: V) -> bool;
fn remove(&mut self, key: K) -> bool;
fn get(&self, key: K) -> Option<&V>;
fn get_mut(&mut self, key: K) -> Option<&mut V>;
fn contains(&self, key: K) -> bool;
}
impl<'a, K, V, S> ItemMap<K, V> for &'a mut S
where
K: Copy,
S: ItemMap<K, V>,
{
fn is_empty(&self) -> bool {
(**self).is_empty()
}
fn len(&self) -> usize {
(**self).len()
}
fn clear(&mut self) {
(**self).clear()
}
fn insert(&mut self, key: K, value: V) -> bool {
(**self).insert(key, value)
}
fn insert_or_replace(&mut self, key: K, value: V) -> bool {
(**self).insert_or_replace(key, value)
}
fn remove(&mut self, key: K) -> bool {
(**self).remove(key)
}
fn get(&self, key: K) -> Option<&V> {
(**self).get(key)
}
fn get_mut(&mut self, key: K) -> Option<&mut V> {
(**self).get_mut(key)
}
fn contains(&self, key: K) -> bool {
(**self).contains(key)
}
}
impl<K, V, S> ItemMap<K, V> for HashMap<K, V, S>
where
K: Copy + Eq + Hash,
S: BuildHasher,
{
fn is_empty(&self) -> bool {
HashMap::is_empty(self)
}
fn len(&self) -> usize {
HashMap::len(self)
}
fn clear(&mut self) {
HashMap::clear(self)
}
fn insert(&mut self, key: K, value: V) -> bool {
let mut new = false;
HashMap::entry(self, key).or_insert_with(|| {
new = true;
value
});
new
}
fn insert_or_replace(&mut self, key: K, value: V) -> bool {
HashMap::insert(self, key, value).is_some()
}
fn remove(&mut self, key: K) -> bool {
HashMap::remove(self, &key).is_some()
}
fn get(&self, key: K) -> Option<&V> {
HashMap::get(self, &key)
}
fn get_mut(&mut self, key: K) -> Option<&mut V> {
HashMap::get_mut(self, &key)
}
fn contains(&self, key: K) -> bool {
HashMap::contains_key(self, &key)
}
}
pub struct ItemVecMap<'a, I, V> {
vec: &'a mut ItemVec<I, Option<V>>,
count: usize,
}
impl<'a, I, V> ItemVecMap<'a, I, V>
where
I: Indexer,
{
pub fn new_empty(vec: &'a mut ItemVec<I, Option<V>>) -> Self {
for (_, v) in vec.iter_mut() {
*v = None;
}
ItemVecMap { vec, count: 0 }
}
}
impl<'a, I, V> ItemMap<I::Idx, V> for ItemVecMap<'a, I, V>
where
I: Indexer,
I::Idx: Copy,
{
fn is_empty(&self) -> bool {
self.count == 0
}
fn len(&self) -> usize {
self.count
}
fn clear(&mut self) {
for (_, v) in self.vec.iter_mut() {
*v = None;
}
}
fn insert(&mut self, key: I::Idx, value: V) -> bool {
let x = &mut self.vec[key];
if x.is_none() {
*x = Some(value);
self.count += 1;
true
} else {
false
}
}
fn insert_or_replace(&mut self, key: I::Idx, value: V) -> bool {
if self.vec[key].replace(value).is_some() {
true
} else {
self.count += 1;
false
}
}
fn remove(&mut self, key: I::Idx) -> bool {
if self.vec[key].take().is_some() {
self.count -= 1;
true
} else {
false
}
}
fn get(&self, key: I::Idx) -> Option<&V> {
self.vec[key].as_ref()
}
fn get_mut(&mut self, key: I::Idx) -> Option<&mut V> {
self.vec[key].as_mut()
}
fn contains(&self, key: I::Idx) -> bool {
self.vec[key].is_some()
}
}