use std::marker;
use crate::{key::Key, storage::Storage};
pub struct Map<K, V>
where
K: Key<K, V>,
{
storage: K::Storage,
}
impl<K, V> Map<K, V>
where
K: Key<K, V>,
{
#[inline]
pub fn new() -> Map<K, V> {
Map {
storage: K::Storage::default(),
}
}
pub fn keys<'a>(&'a self) -> Keys<'a, K, V> {
Keys { inner: self.iter() }
}
pub fn values<'a>(&'a self) -> Values<'a, K, V> {
Values { inner: self.iter() }
}
pub fn values_mut<'a>(&'a mut self) -> ValuesMut<'a, K, V> {
ValuesMut {
inner: self.iter_mut(),
}
}
pub fn iter<'a>(&'a self) -> Iter<'a, K, V> {
Iter {
iter: self.storage.iter(),
marker: marker::PhantomData,
}
}
pub fn iter_mut<'a>(&'a mut self) -> IterMut<'a, K, V> {
IterMut {
iter: self.storage.iter_mut(),
marker: std::marker::PhantomData,
}
}
#[inline]
pub fn get(&self, key: K) -> Option<&V> {
self.storage.get(key)
}
#[inline]
pub fn get_mut(&mut self, key: K) -> Option<&mut V> {
self.storage.get_mut(key)
}
#[inline]
pub fn insert(&mut self, key: K, value: V) -> Option<V> {
self.storage.insert(key, value)
}
#[inline]
pub fn remove(&mut self, key: K) -> Option<V> {
self.storage.remove(key)
}
#[inline]
pub fn clear(&mut self) {
self.storage.clear()
}
pub fn is_empty(&self) -> bool {
self.iter().next().is_none()
}
pub fn len(&self) -> usize {
self.iter().count()
}
}
impl<K, V> Clone for Map<K, V>
where
K: Key<K, V>,
K::Storage: Clone,
{
fn clone(&self) -> Map<K, V> {
Map {
storage: self.storage.clone(),
}
}
}
impl<K, V> Default for Map<K, V>
where
K: Key<K, V>,
{
fn default() -> Self {
Self::new()
}
}
impl<K, V> std::fmt::Debug for Map<K, V>
where
K: Key<K, V> + std::fmt::Debug,
V: std::fmt::Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let mut debug_map = f.debug_map();
for (k, v) in self.iter() {
debug_map.entry(&k, v);
}
debug_map.finish()
}
}
impl<K, V> PartialEq for Map<K, V>
where
K: Key<K, V>,
K::Storage: PartialEq,
{
fn eq(&self, other: &Self) -> bool {
self.storage == other.storage
}
}
impl<K, V> Eq for Map<K, V>
where
K: Key<K, V>,
K::Storage: Eq,
{
}
pub struct Iter<'a, K, V: 'a>
where
K: Key<K, V>,
{
iter: <K::Storage as Storage<K, V>>::Iter,
marker: marker::PhantomData<&'a ()>,
}
impl<'a, K, V: 'a> Clone for Iter<'a, K, V>
where
K: Key<K, V>,
{
fn clone(&self) -> Iter<'a, K, V> {
Iter {
iter: self.iter.clone(),
marker: marker::PhantomData,
}
}
}
impl<'a, K: 'a, V: 'a> Iterator for Iter<'a, K, V>
where
K: Key<K, V>,
{
type Item = (K, &'a V);
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map(|(k, v)| (k, unsafe { &*v }))
}
}
pub struct IterMut<'a, K, V: 'a>
where
K: Key<K, V>,
{
iter: <K::Storage as Storage<K, V>>::IterMut,
marker: marker::PhantomData<&'a ()>,
}
impl<'a, K, V: 'a> Iterator for IterMut<'a, K, V>
where
K: Key<K, V>,
{
type Item = (K, &'a mut V);
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map(|(k, v)| (k, unsafe { &mut *v }))
}
}
#[derive(Clone)]
pub struct Keys<'a, K, V: 'a>
where
K: Key<K, V>,
{
inner: Iter<'a, K, V>,
}
impl<'a, K: 'a, V: 'a> Iterator for Keys<'a, K, V>
where
K: Key<K, V>,
{
type Item = K;
fn next(&mut self) -> Option<Self::Item> {
self.inner.next().map(|(k, _)| k)
}
}
#[derive(Clone)]
pub struct Values<'a, K, V: 'a>
where
K: Key<K, V>,
{
inner: Iter<'a, K, V>,
}
impl<'a, K: 'a, V: 'a> Iterator for Values<'a, K, V>
where
K: Key<K, V>,
{
type Item = &'a V;
fn next(&mut self) -> Option<Self::Item> {
self.inner.next().map(|(_, v)| v)
}
}
pub struct ValuesMut<'a, K, V: 'a>
where
K: Key<K, V>,
{
inner: IterMut<'a, K, V>,
}
impl<'a, K: 'a, V: 'a> Iterator for ValuesMut<'a, K, V>
where
K: Key<K, V>,
{
type Item = &'a mut V;
fn next(&mut self) -> Option<Self::Item> {
self.inner.next().map(|(_, v)| v)
}
}