@@ -3,7 +3,7 @@ use core::{fmt, iter::FusedIterator, marker::PhantomData};
3
3
use crate :: {
4
4
raw:: {
5
5
Allocator , Bucket , Global , InsertSlot , RawDrain , RawExtractIf , RawIntoIter , RawIter ,
6
- RawTable ,
6
+ RawIterHash , RawTable ,
7
7
} ,
8
8
TryReserveError ,
9
9
} ;
@@ -741,6 +741,45 @@ where
741
741
}
742
742
}
743
743
744
+ /// An iterator visiting all elements which may match a hash.
745
+ /// The iterator element type is `&'a T`.
746
+ ///
747
+ /// This iterator may return elements from the table that have a hash value
748
+ /// different than the one provided. You should always validate the returned
749
+ /// values before using them.
750
+ ///
751
+ /// # Examples
752
+ ///
753
+ /// ```
754
+ /// # #[cfg(feature = "nightly")]
755
+ /// # fn test() {
756
+ /// use hashbrown::{HashTable, DefaultHashBuilder};
757
+ /// use std::hash::BuildHasher;
758
+ ///
759
+ /// let mut table = HashTable::new();
760
+ /// let hasher = DefaultHashBuilder::default();
761
+ /// let hasher = |val: &_| hasher.hash_one(val);
762
+ /// table.insert_unique(hasher(&"a"), "a", hasher);
763
+ /// table.insert_unique(hasher(&"a"), "b", hasher);
764
+ /// table.insert_unique(hasher(&"b"), "c", hasher);
765
+ ///
766
+ /// // Will print "a" and "b" (and possibly "c") in an arbitrary order.
767
+ /// for x in table.iter_hash(hasher(&"a")) {
768
+ /// println!("{}", x);
769
+ /// }
770
+ /// # }
771
+ /// # fn main() {
772
+ /// # #[cfg(feature = "nightly")]
773
+ /// # test()
774
+ /// # }
775
+ /// ```
776
+ pub fn iter_hash ( & self , hash : u64 ) -> IterHash < ' _ , T > {
777
+ IterHash {
778
+ inner : unsafe { self . raw . iter_hash ( hash) } ,
779
+ _marker : PhantomData ,
780
+ }
781
+ }
782
+
744
783
/// Retains only the elements specified by the predicate.
745
784
///
746
785
/// In other words, remove all elements `e` such that `f(&e)` returns `false`.
@@ -1932,6 +1971,31 @@ impl<T> ExactSizeIterator for IterMut<'_, T> {
1932
1971
1933
1972
impl < T > FusedIterator for IterMut < ' _ , T > { }
1934
1973
1974
+ /// An iterator over the entries of a `HashTable` that could match a given hash.
1975
+ /// The iterator element type is `&'a T`.
1976
+ ///
1977
+ /// This `struct` is created by the [`iter_hash`] method on [`HashTable`]. See its
1978
+ /// documentation for more.
1979
+ ///
1980
+ /// [`iter_hash`]: struct.HashTable.html#method.iter_hash
1981
+ /// [`HashTable`]: struct.HashTable.html
1982
+ pub struct IterHash < ' a , T > {
1983
+ inner : RawIterHash < T > ,
1984
+ _marker : PhantomData < & ' a T > ,
1985
+ }
1986
+
1987
+ impl < ' a , T > Iterator for IterHash < ' a , T > {
1988
+ type Item = & ' a T ;
1989
+
1990
+ fn next ( & mut self ) -> Option < Self :: Item > {
1991
+ // Avoid `Option::map` because it bloats LLVM IR.
1992
+ match self . inner . next ( ) {
1993
+ Some ( bucket) => Some ( unsafe { bucket. as_ref ( ) } ) ,
1994
+ None => None ,
1995
+ }
1996
+ }
1997
+ }
1998
+
1935
1999
/// An owning iterator over the entries of a `HashTable` in arbitrary order.
1936
2000
/// The iterator element type is `T`.
1937
2001
///
0 commit comments