iddqd/bi_hash_map/
iter.rs

1use super::{RefMut, tables::BiHashMapTables};
2use crate::{
3    BiHashItem, DefaultHashBuilder,
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 [`BiHashMap`] by shared reference.
13/// Created by [`BiHashMap::iter`].
14///
15/// Similar to [`HashMap`], the iteration order is arbitrary and not guaranteed
16/// to be stable.
17///
18/// [`BiHashMap`]: crate::BiHashMap
19/// [`BiHashMap::iter`]: crate::BiHashMap::iter
20/// [`HashMap`]: std::collections::HashMap
21#[derive(Clone, Debug, Default)]
22pub struct Iter<'a, T: BiHashItem> {
23    inner: hash_map::Values<'a, usize, T>,
24}
25
26impl<'a, T: BiHashItem> 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: BiHashItem> 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: BiHashItem> 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: BiHashItem> FusedIterator for Iter<'_, T> {}
50
51/// An iterator over the elements of a [`BiHashMap`] by mutable reference.
52/// Created by [`BiHashMap::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/// [`BiHashMap`]: crate::BiHashMap
60/// [`BiHashMap::iter_mut`]: crate::BiHashMap::iter_mut
61/// [`HashMap`]: std::collections::HashMap
62#[derive(Debug)]
63pub struct IterMut<
64    'a,
65    T: BiHashItem,
66    S = DefaultHashBuilder,
67    A: Allocator = Global,
68> {
69    tables: &'a BiHashMapTables<S, A>,
70    inner: hash_map::ValuesMut<'a, usize, T>,
71}
72
73impl<'a, T: BiHashItem, S: Clone + BuildHasher, A: Allocator>
74    IterMut<'a, T, S, A>
75{
76    pub(super) fn new(
77        tables: &'a BiHashMapTables<S, A>,
78        items: &'a mut ItemSet<T, A>,
79    ) -> Self {
80        Self { tables, inner: items.values_mut() }
81    }
82}
83
84impl<'a, T: BiHashItem, 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_hashes::<T>(&next.key1(), &next.key2());
93        Some(RefMut::new(hashes, next))
94    }
95}
96
97impl<T: BiHashItem, 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: BiHashItem, S: Clone + BuildHasher, A: Allocator> FusedIterator
108    for IterMut<'_, T, S, A>
109{
110}
111
112/// An iterator over the elements of a [`BiHashMap`] by ownership. Created by
113/// [`BiHashMap::into_iter`].
114///
115/// Similar to [`HashMap`], the iteration order is arbitrary and not guaranteed
116/// to be stable.
117///
118/// [`BiHashMap`]: crate::BiHashMap
119/// [`BiHashMap::into_iter`]: crate::BiHashMap::into_iter
120/// [`HashMap`]: std::collections::HashMap
121#[derive(Debug)]
122pub struct IntoIter<T: BiHashItem, A: Allocator = Global> {
123    inner: hash_map::IntoValues<usize, T, AllocWrapper<A>>,
124}
125
126impl<T: BiHashItem, 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: BiHashItem, 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: BiHashItem, 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: BiHashItem, A: Allocator> FusedIterator for IntoIter<T, A> {}