iddqd/id_hash_map/
iter.rs1use super::{RefMut, tables::IdHashMapTables};
2use crate::{
3 DefaultHashBuilder, IdHashItem,
4 support::{
5 alloc::{Allocator, Global},
6 item_set::{self, ItemSet},
7 },
8};
9use core::{hash::BuildHasher, iter::FusedIterator};
10
11#[derive(Clone, Debug, Default)]
21pub struct Iter<'a, T: IdHashItem> {
22 inner: item_set::Values<'a, T>,
23}
24
25impl<'a, T: IdHashItem> Iter<'a, T> {
26 pub(crate) fn new<A: Allocator>(items: &'a ItemSet<T, A>) -> Self {
27 Self { inner: items.values() }
28 }
29}
30
31impl<'a, T: IdHashItem> Iterator for Iter<'a, T> {
32 type Item = &'a T;
33
34 #[inline]
35 fn next(&mut self) -> Option<Self::Item> {
36 self.inner.next()
37 }
38}
39
40impl<T: IdHashItem> ExactSizeIterator for Iter<'_, T> {
41 #[inline]
42 fn len(&self) -> usize {
43 self.inner.len()
44 }
45}
46
47impl<T: IdHashItem> FusedIterator for Iter<'_, T> {}
48
49#[derive(Debug)]
61pub struct IterMut<
62 'a,
63 T: IdHashItem,
64 S = DefaultHashBuilder,
65 A: Allocator = Global,
66> {
67 tables: &'a IdHashMapTables<S, A>,
68 inner: item_set::ValuesMut<'a, T>,
69}
70
71impl<'a, T: IdHashItem, S: BuildHasher, A: Allocator> IterMut<'a, T, S, A> {
72 pub(super) fn new(
73 tables: &'a IdHashMapTables<S, A>,
74 items: &'a mut ItemSet<T, A>,
75 ) -> Self {
76 Self { tables, inner: items.values_mut() }
77 }
78}
79
80impl<'a, T: IdHashItem, S: Clone + BuildHasher, A: Allocator> Iterator
81 for IterMut<'a, T, S, A>
82{
83 type Item = RefMut<'a, T, S>;
84
85 #[inline]
86 fn next(&mut self) -> Option<Self::Item> {
87 let next = self.inner.next()?;
88 let hashes = self.tables.make_hash(next);
89 Some(RefMut::new(self.tables.state.clone(), hashes, next))
90 }
91}
92
93impl<T: IdHashItem, S: Clone + BuildHasher, A: Allocator> ExactSizeIterator
94 for IterMut<'_, T, S, A>
95{
96 #[inline]
97 fn len(&self) -> usize {
98 self.inner.len()
99 }
100}
101
102impl<T: IdHashItem, S: Clone + BuildHasher, A: Allocator> FusedIterator
103 for IterMut<'_, T, S, A>
104{
105}
106
107#[derive(Debug)]
117pub struct IntoIter<T: IdHashItem, A: Allocator = Global> {
118 inner: item_set::IntoValues<T, A>,
119}
120
121impl<T: IdHashItem, A: Allocator> IntoIter<T, A> {
122 pub(crate) fn new(items: ItemSet<T, A>) -> Self {
123 Self { inner: items.into_values() }
124 }
125}
126
127impl<T: IdHashItem, A: Allocator> Iterator for IntoIter<T, A> {
128 type Item = T;
129
130 #[inline]
131 fn next(&mut self) -> Option<Self::Item> {
132 self.inner.next()
133 }
134}
135
136impl<T: IdHashItem, A: Allocator> ExactSizeIterator for IntoIter<T, A> {
137 #[inline]
138 fn len(&self) -> usize {
139 self.inner.len()
140 }
141}
142
143impl<T: IdHashItem, A: Allocator> FusedIterator for IntoIter<T, A> {}