|
| 1 | +//! Rayon extensions for `HashTable`. |
| 2 | +
|
| 3 | +use super::raw::{RawIntoParIter, RawParDrain, RawParIter}; |
| 4 | +use crate::hash_table::HashTable; |
| 5 | +use crate::raw::{Allocator, Global}; |
| 6 | +use core::fmt; |
| 7 | +use core::marker::PhantomData; |
| 8 | +use rayon::iter::plumbing::UnindexedConsumer; |
| 9 | +use rayon::iter::{IntoParallelIterator, ParallelIterator}; |
| 10 | + |
| 11 | +/// Parallel iterator over shared references to entries in a map. |
| 12 | +/// |
| 13 | +/// This iterator is created by the [`par_iter`] method on [`HashTable`] |
| 14 | +/// (provided by the [`IntoParallelRefIterator`] trait). |
| 15 | +/// See its documentation for more. |
| 16 | +/// |
| 17 | +/// [`par_iter`]: /hashbrown/struct.HashTable.html#method.par_iter |
| 18 | +/// [`HashTable`]: /hashbrown/struct.HashTable.html |
| 19 | +/// [`IntoParallelRefIterator`]: https://docs.rs/rayon/1.0/rayon/iter/trait.IntoParallelRefIterator.html |
| 20 | +pub struct ParIter<'a, T> { |
| 21 | + inner: RawParIter<T>, |
| 22 | + marker: PhantomData<&'a T>, |
| 23 | +} |
| 24 | + |
| 25 | +impl<'a, T: Sync> ParallelIterator for ParIter<'a, T> { |
| 26 | + type Item = &'a T; |
| 27 | + |
| 28 | + #[cfg_attr(feature = "inline-more", inline)] |
| 29 | + fn drive_unindexed<C>(self, consumer: C) -> C::Result |
| 30 | + where |
| 31 | + C: UnindexedConsumer<Self::Item>, |
| 32 | + { |
| 33 | + self.inner |
| 34 | + .map(|x| unsafe { x.as_ref() }) |
| 35 | + .drive_unindexed(consumer) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +impl<T> Clone for ParIter<'_, T> { |
| 40 | + #[cfg_attr(feature = "inline-more", inline)] |
| 41 | + fn clone(&self) -> Self { |
| 42 | + Self { |
| 43 | + inner: self.inner.clone(), |
| 44 | + marker: PhantomData, |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +impl<T: fmt::Debug> fmt::Debug for ParIter<'_, T> { |
| 50 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 51 | + let iter = unsafe { self.inner.iter() }.map(|x| unsafe { x.as_ref() }); |
| 52 | + f.debug_list().entries(iter).finish() |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +/// Parallel iterator over mutable references to entries in a map. |
| 57 | +/// |
| 58 | +/// This iterator is created by the [`par_iter_mut`] method on [`HashTable`] |
| 59 | +/// (provided by the [`IntoParallelRefMutIterator`] trait). |
| 60 | +/// See its documentation for more. |
| 61 | +/// |
| 62 | +/// [`par_iter_mut`]: /hashbrown/struct.HashTable.html#method.par_iter_mut |
| 63 | +/// [`HashTable`]: /hashbrown/struct.HashTable.html |
| 64 | +/// [`IntoParallelRefMutIterator`]: https://docs.rs/rayon/1.0/rayon/iter/trait.IntoParallelRefMutIterator.html |
| 65 | +pub struct ParIterMut<'a, T> { |
| 66 | + inner: RawParIter<T>, |
| 67 | + marker: PhantomData<&'a mut T>, |
| 68 | +} |
| 69 | + |
| 70 | +impl<'a, T: Send> ParallelIterator for ParIterMut<'a, T> { |
| 71 | + type Item = &'a mut T; |
| 72 | + |
| 73 | + #[cfg_attr(feature = "inline-more", inline)] |
| 74 | + fn drive_unindexed<C>(self, consumer: C) -> C::Result |
| 75 | + where |
| 76 | + C: UnindexedConsumer<Self::Item>, |
| 77 | + { |
| 78 | + self.inner |
| 79 | + .map(|x| unsafe { x.as_mut() }) |
| 80 | + .drive_unindexed(consumer) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +impl<T: fmt::Debug> fmt::Debug for ParIterMut<'_, T> { |
| 85 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 86 | + ParIter { |
| 87 | + inner: self.inner.clone(), |
| 88 | + marker: PhantomData, |
| 89 | + } |
| 90 | + .fmt(f) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +/// Parallel iterator over entries of a consumed map. |
| 95 | +/// |
| 96 | +/// This iterator is created by the [`into_par_iter`] method on [`HashTable`] |
| 97 | +/// (provided by the [`IntoParallelIterator`] trait). |
| 98 | +/// See its documentation for more. |
| 99 | +/// |
| 100 | +/// [`into_par_iter`]: /hashbrown/struct.HashTable.html#method.into_par_iter |
| 101 | +/// [`HashTable`]: /hashbrown/struct.HashTable.html |
| 102 | +/// [`IntoParallelIterator`]: https://docs.rs/rayon/1.0/rayon/iter/trait.IntoParallelIterator.html |
| 103 | +pub struct IntoParIter<T, A: Allocator = Global> { |
| 104 | + inner: RawIntoParIter<T, A>, |
| 105 | +} |
| 106 | + |
| 107 | +impl<T: Send, A: Allocator + Send> ParallelIterator for IntoParIter<T, A> { |
| 108 | + type Item = T; |
| 109 | + |
| 110 | + #[cfg_attr(feature = "inline-more", inline)] |
| 111 | + fn drive_unindexed<C>(self, consumer: C) -> C::Result |
| 112 | + where |
| 113 | + C: UnindexedConsumer<Self::Item>, |
| 114 | + { |
| 115 | + self.inner.drive_unindexed(consumer) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +impl<T: fmt::Debug, A: Allocator> fmt::Debug for IntoParIter<T, A> { |
| 120 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 121 | + ParIter { |
| 122 | + inner: unsafe { self.inner.par_iter() }, |
| 123 | + marker: PhantomData, |
| 124 | + } |
| 125 | + .fmt(f) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +/// Parallel draining iterator over entries of a map. |
| 130 | +/// |
| 131 | +/// This iterator is created by the [`par_drain`] method on [`HashTable`]. |
| 132 | +/// See its documentation for more. |
| 133 | +/// |
| 134 | +/// [`par_drain`]: /hashbrown/struct.HashTable.html#method.par_drain |
| 135 | +/// [`HashTable`]: /hashbrown/struct.HashTable.html |
| 136 | +pub struct ParDrain<'a, T, A: Allocator = Global> { |
| 137 | + inner: RawParDrain<'a, T, A>, |
| 138 | +} |
| 139 | + |
| 140 | +impl<T: Send, A: Allocator + Sync> ParallelIterator for ParDrain<'_, T, A> { |
| 141 | + type Item = T; |
| 142 | + |
| 143 | + #[cfg_attr(feature = "inline-more", inline)] |
| 144 | + fn drive_unindexed<C>(self, consumer: C) -> C::Result |
| 145 | + where |
| 146 | + C: UnindexedConsumer<Self::Item>, |
| 147 | + { |
| 148 | + self.inner.drive_unindexed(consumer) |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +impl<T: fmt::Debug, A: Allocator> fmt::Debug for ParDrain<'_, T, A> { |
| 153 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 154 | + ParIter { |
| 155 | + inner: unsafe { self.inner.par_iter() }, |
| 156 | + marker: PhantomData, |
| 157 | + } |
| 158 | + .fmt(f) |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +impl<T: Send, A: Allocator> HashTable<T, A> { |
| 163 | + /// Consumes (potentially in parallel) all values in an arbitrary order, |
| 164 | + /// while preserving the map's allocated memory for reuse. |
| 165 | + #[cfg_attr(feature = "inline-more", inline)] |
| 166 | + pub fn par_drain(&mut self) -> ParDrain<'_, T, A> { |
| 167 | + ParDrain { |
| 168 | + inner: self.raw.par_drain(), |
| 169 | + } |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +impl<T: Send, A: Allocator + Send> IntoParallelIterator for HashTable<T, A> { |
| 174 | + type Item = T; |
| 175 | + type Iter = IntoParIter<T, A>; |
| 176 | + |
| 177 | + #[cfg_attr(feature = "inline-more", inline)] |
| 178 | + fn into_par_iter(self) -> Self::Iter { |
| 179 | + IntoParIter { |
| 180 | + inner: self.raw.into_par_iter(), |
| 181 | + } |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +impl<'a, T: Sync, A: Allocator> IntoParallelIterator for &'a HashTable<T, A> { |
| 186 | + type Item = &'a T; |
| 187 | + type Iter = ParIter<'a, T>; |
| 188 | + |
| 189 | + #[cfg_attr(feature = "inline-more", inline)] |
| 190 | + fn into_par_iter(self) -> Self::Iter { |
| 191 | + ParIter { |
| 192 | + inner: unsafe { self.raw.par_iter() }, |
| 193 | + marker: PhantomData, |
| 194 | + } |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +impl<'a, T: Send, A: Allocator> IntoParallelIterator for &'a mut HashTable<T, A> { |
| 199 | + type Item = &'a mut T; |
| 200 | + type Iter = ParIterMut<'a, T>; |
| 201 | + |
| 202 | + #[cfg_attr(feature = "inline-more", inline)] |
| 203 | + fn into_par_iter(self) -> Self::Iter { |
| 204 | + ParIterMut { |
| 205 | + inner: unsafe { self.raw.par_iter() }, |
| 206 | + marker: PhantomData, |
| 207 | + } |
| 208 | + } |
| 209 | +} |
| 210 | + |
| 211 | +#[cfg(test)] |
| 212 | +mod test_par_table { |
| 213 | + use alloc::vec::Vec; |
| 214 | + use core::sync::atomic::{AtomicUsize, Ordering}; |
| 215 | + |
| 216 | + use rayon::prelude::*; |
| 217 | + |
| 218 | + use crate::{ |
| 219 | + hash_map::{make_hash, DefaultHashBuilder}, |
| 220 | + hash_table::HashTable, |
| 221 | + }; |
| 222 | + |
| 223 | + #[test] |
| 224 | + fn test_iterate() { |
| 225 | + let hasher = DefaultHashBuilder::default(); |
| 226 | + let mut a = HashTable::new(); |
| 227 | + for i in 0..32 { |
| 228 | + a.insert_unique(make_hash(&hasher, &i), i, |x| make_hash(&hasher, x)); |
| 229 | + } |
| 230 | + let observed = AtomicUsize::new(0); |
| 231 | + a.par_iter().for_each(|k| { |
| 232 | + observed.fetch_or(1 << *k, Ordering::Relaxed); |
| 233 | + }); |
| 234 | + assert_eq!(observed.into_inner(), 0xFFFF_FFFF); |
| 235 | + } |
| 236 | + |
| 237 | + #[test] |
| 238 | + fn test_move_iter() { |
| 239 | + let hasher = DefaultHashBuilder::default(); |
| 240 | + let hs = { |
| 241 | + let mut hs = HashTable::new(); |
| 242 | + |
| 243 | + hs.insert_unique(make_hash(&hasher, &'a'), 'a', |x| make_hash(&hasher, x)); |
| 244 | + hs.insert_unique(make_hash(&hasher, &'b'), 'b', |x| make_hash(&hasher, x)); |
| 245 | + |
| 246 | + hs |
| 247 | + }; |
| 248 | + |
| 249 | + let v = hs.into_par_iter().collect::<Vec<char>>(); |
| 250 | + assert!(v == ['a', 'b'] || v == ['b', 'a']); |
| 251 | + } |
| 252 | +} |
0 commit comments