use super::*;
use std::iter::{ExactSizeIterator, FusedIterator};
impl<K, V> DeltaHashMap<K, V> {
#[inline]
pub fn iter(&self) -> Iter<'_, K, V> {
Iter {
discard: HashSet::new(),
base: self.base.iter(),
delta: self.delta.iter(),
}
}
#[inline]
pub fn keys(&self) -> Keys<'_, K, V> {
Keys { inner: self.iter() }
}
#[inline]
pub fn values(&self) -> Values<'_, K, V> {
Values { inner: self.iter() }
}
}
impl<K, V> DeltaHashMap<K, V>
where
K: Hash + Eq,
V: Clone,
{
#[inline]
pub fn into_keys(self) -> IntoKeys<K, V> {
IntoKeys {
inner: self.into_iter(),
}
}
#[inline]
pub fn into_values(self) -> IntoValues<K, V> {
IntoValues {
inner: self.into_iter(),
}
}
}
impl<K, V> DeltaHashMap<K, V>
where
K: Clone + Hash + Eq,
V: Clone,
{
#[inline]
pub fn iter_mut(&mut self) -> IterMut<'_, K, V> {
self.cocommit();
IterMut {
delta: self.delta.iter_mut(),
}
}
#[inline]
pub fn values_mut(&mut self) -> ValuesMut<'_, K, V> {
ValuesMut {
inner: self.iter_mut(),
}
}
}
#[derive(Clone, Debug)]
pub struct Iter<'a, K: 'a, V: 'a> {
discard: HashSet<&'a K>,
base: std::collections::hash_map::Iter<'a, K, V>,
delta: std::collections::hash_map::Iter<'a, K, Option<V>>,
}
#[derive(Debug)]
pub struct IterMut<'a, K: 'a, V: 'a> {
delta: std::collections::hash_map::IterMut<'a, K, Option<V>>,
}
#[derive(Debug)]
pub struct IntoIter<K, V> {
base: std::collections::hash_map::IntoIter<K, V>,
}
#[derive(Clone, Debug)]
pub struct Keys<'a, K: 'a, V: 'a> {
inner: Iter<'a, K, V>,
}
#[derive(Clone, Debug)]
pub struct Values<'a, K: 'a, V: 'a> {
inner: Iter<'a, K, V>,
}
pub struct ValuesMut<'a, K: 'a, V: 'a> {
inner: IterMut<'a, K, V>,
}
#[derive(Debug)]
pub struct IntoKeys<K, V> {
inner: IntoIter<K, V>,
}
#[derive(Debug)]
pub struct IntoValues<K, V> {
inner: IntoIter<K, V>,
}
impl<'a, K, V> IntoIterator for &'a DeltaHashMap<K, V>
where
K: Hash + Eq,
{
type Item = (&'a K, &'a V);
type IntoIter = Iter<'a, K, V>;
#[inline]
fn into_iter(self) -> Iter<'a, K, V> {
self.iter()
}
}
impl<'a, K, V> IntoIterator for &'a mut DeltaHashMap<K, V>
where
K: Clone + Hash + Eq,
V: Clone,
{
type Item = (&'a K, &'a mut V);
type IntoIter = IterMut<'a, K, V>;
#[inline]
fn into_iter(self) -> IterMut<'a, K, V> {
self.iter_mut()
}
}
impl<K, V> IntoIterator for DeltaHashMap<K, V>
where
K: Hash + Eq,
V: Clone,
{
type Item = (K, V);
type IntoIter = IntoIter<K, V>;
#[inline]
fn into_iter(mut self) -> IntoIter<K, V> {
self.commit();
IntoIter {
base: self.base.into_iter(),
}
}
}
impl<'a, K, V> Iterator for Iter<'a, K, V>
where
K: Hash + Eq,
{
type Item = (&'a K, &'a V);
fn next(&mut self) -> Option<(&'a K, &'a V)> {
while let Some((key, state)) = self.delta.next() {
self.discard.insert(key);
match state {
Some(value) => return Some((key, value)),
None => continue,
}
}
while let Some((key, value)) = self.base.next() {
if self.discard.contains(key) {
continue;
}
return Some((key, value));
}
None
}
}
impl<K, V> FusedIterator for Iter<'_, K, V> where K: Hash + Eq {}
impl<'a, K, V> Iterator for IterMut<'a, K, V> {
type Item = (&'a K, &'a mut V);
fn next(&mut self) -> Option<(&'a K, &'a mut V)> {
loop {
match self.delta.next() {
Some((key, Some(value))) => break Some((key, value)),
Some((_key, None)) => continue,
None => break None,
}
}
}
}
impl<K, V> FusedIterator for IterMut<'_, K, V> {}
impl<K, V> Iterator for IntoIter<K, V> {
type Item = (K, V);
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.base.next()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.base.size_hint()
}
#[inline]
fn count(self) -> usize {
self.base.len()
}
#[inline]
fn fold<B, F>(self, init: B, f: F) -> B
where
Self: Sized,
F: FnMut(B, Self::Item) -> B,
{
self.base.fold(init, f)
}
}
impl<K, V> ExactSizeIterator for IntoIter<K, V> {
#[inline]
fn len(&self) -> usize {
self.base.len()
}
}
impl<K, V> FusedIterator for IntoIter<K, V> {}
impl<'a, K, V> Iterator for Keys<'a, K, V>
where
K: Hash + Eq,
{
type Item = &'a K;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.inner.next().map(|(k, _)| k)
}
}
impl<K, V> FusedIterator for Keys<'_, K, V> where K: Hash + Eq {}
impl<'a, K, V> Iterator for Values<'a, K, V>
where
K: Hash + Eq,
{
type Item = &'a V;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.inner.next().map(|(_, v)| v)
}
}
impl<K, V> FusedIterator for Values<'_, K, V> where K: Hash + Eq {}
impl<'a, K, V> Iterator for ValuesMut<'a, K, V>
where
K: Hash + Eq,
{
type Item = &'a mut V;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.inner.next().map(|(_, v)| v)
}
}
impl<K, V> FusedIterator for ValuesMut<'_, K, V> where K: Hash + Eq {}
impl<K, V> Iterator for IntoKeys<K, V> {
type Item = K;
#[inline]
fn next(&mut self) -> Option<K> {
self.inner.next().map(|(k, _)| k)
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
#[inline]
fn count(self) -> usize {
self.inner.len()
}
#[inline]
fn fold<B, F>(self, init: B, mut f: F) -> B
where
Self: Sized,
F: FnMut(B, Self::Item) -> B,
{
self.inner.fold(init, |acc, (k, _)| f(acc, k))
}
}
impl<K, V> ExactSizeIterator for IntoKeys<K, V> {
#[inline]
fn len(&self) -> usize {
self.inner.len()
}
}
impl<K, V> FusedIterator for IntoKeys<K, V> {}
impl<K, V> Iterator for IntoValues<K, V> {
type Item = V;
#[inline]
fn next(&mut self) -> Option<V> {
self.inner.next().map(|(_, v)| v)
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
#[inline]
fn count(self) -> usize {
self.inner.len()
}
#[inline]
fn fold<B, F>(self, init: B, mut f: F) -> B
where
Self: Sized,
F: FnMut(B, Self::Item) -> B,
{
self.inner.fold(init, |acc, (_, v)| f(acc, v))
}
}
impl<K, V> ExactSizeIterator for IntoValues<K, V> {
#[inline]
fn len(&self) -> usize {
self.inner.len()
}
}
impl<K, V> FusedIterator for IntoValues<K, V> {}
impl<K, V> FromIterator<(K, V)> for DeltaHashMap<K, V>
where
K: Hash + Eq,
{
fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> DeltaHashMap<K, V> {
let mut base = HashMap::new();
let cache = HashMap::new();
base.extend(iter);
DeltaHashMap { base, delta: cache }
}
}