iddqd/id_hash_map/
iter.rs1use 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#[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
48impl<T: IdHashItem> FusedIterator for Iter<'_, T> {}
50
51#[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
106impl<T: IdHashItem, S: Clone + BuildHasher, A: Allocator> FusedIterator
108    for IterMut<'_, T, S, A>
109{
110}
111
112#[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
148impl<T: IdHashItem, A: Allocator> FusedIterator for IntoIter<T, A> {}