iddqd/id_hash_map/
iter.rs

1use super::{RefMut, tables::IdHashMapTables};
2use crate::{
3    DefaultHashBuilder, IdHashItem,
4    support::{
5        alloc::{AllocWrapper, Allocator, Global},
6        item_set::ItemSet,
7    },
8};
9use core::{hash::BuildHasher, iter::FusedIterator};
10use hashbrown::hash_map;
11
12/// An iterator over the elements of a [`IdHashMap`] by shared reference.
13/// Created by [`IdHashMap::iter`].
14///
15/// Similar to [`HashMap`], the iteration order is arbitrary and not guaranteed
16/// to be stable.
17///
18/// [`IdHashMap`]: crate::IdHashMap
19/// [`IdHashMap::iter`]: crate::IdHashMap::iter
20/// [`HashMap`]: std::collections::HashMap
21#[derive(Clone, Debug, Default)]
22pub struct Iter<'a, T: IdHashItem> {
23    inner: hash_map::Values<'a, usize, T>,
24}
25
26impl<'a, T: IdHashItem> Iter<'a, T> {
27    pub(crate) fn new<A: Allocator>(items: &'a ItemSet<T, A>) -> Self {
28        Self { inner: items.values() }
29    }
30}
31
32impl<'a, T: IdHashItem> Iterator for Iter<'a, T> {
33    type Item = &'a T;
34
35    #[inline]
36    fn next(&mut self) -> Option<Self::Item> {
37        self.inner.next()
38    }
39}
40
41impl<T: IdHashItem> ExactSizeIterator for Iter<'_, T> {
42    #[inline]
43    fn len(&self) -> usize {
44        self.inner.len()
45    }
46}
47
48// hash_map::Iter is a FusedIterator, so Iter is as well.
49impl<T: IdHashItem> FusedIterator for Iter<'_, T> {}
50
51/// An iterator over the elements of a [`IdHashMap`] by mutable reference.
52/// Created by [`IdHashMap::iter_mut`].
53///
54/// This iterator returns [`RefMut`] instances.
55///
56/// Similar to [`HashMap`], the iteration order is arbitrary and not guaranteed
57/// to be stable.
58///
59/// [`IdHashMap`]: crate::IdHashMap
60/// [`IdHashMap::iter_mut`]: crate::IdHashMap::iter_mut
61/// [`HashMap`]: std::collections::HashMap
62#[derive(Debug)]
63pub struct IterMut<
64    'a,
65    T: IdHashItem,
66    S = DefaultHashBuilder,
67    A: Allocator = Global,
68> {
69    tables: &'a IdHashMapTables<S, A>,
70    inner: hash_map::ValuesMut<'a, usize, T>,
71}
72
73impl<'a, T: IdHashItem, S: Clone + BuildHasher, A: Allocator>
74    IterMut<'a, T, S, A>
75{
76    pub(super) fn new(
77        tables: &'a IdHashMapTables<S, A>,
78        items: &'a mut ItemSet<T, A>,
79    ) -> Self {
80        Self { tables, inner: items.values_mut() }
81    }
82}
83
84impl<'a, T: IdHashItem, S: Clone + BuildHasher, A: Allocator> Iterator
85    for IterMut<'a, T, S, A>
86{
87    type Item = RefMut<'a, T, S>;
88
89    #[inline]
90    fn next(&mut self) -> Option<Self::Item> {
91        let next = self.inner.next()?;
92        let hashes = self.tables.make_hash(next);
93        Some(RefMut::new(hashes, next))
94    }
95}
96
97impl<T: IdHashItem, S: Clone + BuildHasher, A: Allocator> ExactSizeIterator
98    for IterMut<'_, T, S, A>
99{
100    #[inline]
101    fn len(&self) -> usize {
102        self.inner.len()
103    }
104}
105
106// hash_map::IterMut is a FusedIterator, so IterMut is as well.
107impl<T: IdHashItem, S: Clone + BuildHasher, A: Allocator> FusedIterator
108    for IterMut<'_, T, S, A>
109{
110}
111
112/// An iterator over the elements of a [`IdHashMap`] by ownership. Created by
113/// [`IdHashMap::into_iter`].
114///
115/// Similar to [`HashMap`], the iteration order is arbitrary and not guaranteed
116/// to be stable.
117///
118/// [`IdHashMap`]: crate::IdHashMap
119/// [`IdHashMap::into_iter`]: crate::IdHashMap::into_iter
120/// [`HashMap`]: std::collections::HashMap
121#[derive(Debug)]
122pub struct IntoIter<T: IdHashItem, A: Allocator = Global> {
123    inner: hash_map::IntoValues<usize, T, AllocWrapper<A>>,
124}
125
126impl<T: IdHashItem, A: Allocator> IntoIter<T, A> {
127    pub(crate) fn new(items: ItemSet<T, A>) -> Self {
128        Self { inner: items.into_values() }
129    }
130}
131
132impl<T: IdHashItem, A: Allocator> Iterator for IntoIter<T, A> {
133    type Item = T;
134
135    #[inline]
136    fn next(&mut self) -> Option<Self::Item> {
137        self.inner.next()
138    }
139}
140
141impl<T: IdHashItem, A: Allocator> ExactSizeIterator for IntoIter<T, A> {
142    #[inline]
143    fn len(&self) -> usize {
144        self.inner.len()
145    }
146}
147
148// hash_map::IterMut is a FusedIterator, so IterMut is as well.
149impl<T: IdHashItem, A: Allocator> FusedIterator for IntoIter<T, A> {}