| 1 | //! This module contains the parallel iterator types for hash sets |
| 2 | //! (`HashSet<T>`). You will rarely need to interact with it directly |
| 3 | //! unless you have need to name one of the iterator types. |
| 4 | |
| 5 | use std::collections::HashSet; |
| 6 | use std::hash::{BuildHasher, Hash}; |
| 7 | use std::marker::PhantomData; |
| 8 | |
| 9 | use crate::iter::plumbing::*; |
| 10 | use crate::iter::*; |
| 11 | |
| 12 | use crate::vec; |
| 13 | |
| 14 | /// Parallel iterator over a hash set |
| 15 | #[derive (Debug)] // std doesn't Clone |
| 16 | pub struct IntoIter<T: Hash + Eq + Send> { |
| 17 | inner: vec::IntoIter<T>, |
| 18 | } |
| 19 | |
| 20 | into_par_vec! { |
| 21 | HashSet<T, S> => IntoIter<T>, |
| 22 | impl<T: Hash + Eq + Send, S: BuildHasher> |
| 23 | } |
| 24 | |
| 25 | delegate_iterator! { |
| 26 | IntoIter<T> => T, |
| 27 | impl<T: Hash + Eq + Send> |
| 28 | } |
| 29 | |
| 30 | /// Parallel iterator over an immutable reference to a hash set |
| 31 | #[derive (Debug)] |
| 32 | pub struct Iter<'a, T: Hash + Eq + Sync> { |
| 33 | inner: vec::IntoIter<&'a T>, |
| 34 | } |
| 35 | |
| 36 | impl<'a, T: Hash + Eq + Sync> Clone for Iter<'a, T> { |
| 37 | fn clone(&self) -> Self { |
| 38 | Iter { |
| 39 | inner: self.inner.clone(), |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | into_par_vec! { |
| 45 | &'a HashSet<T, S> => Iter<'a, T>, |
| 46 | impl<'a, T: Hash + Eq + Sync, S: BuildHasher> |
| 47 | } |
| 48 | |
| 49 | delegate_iterator! { |
| 50 | Iter<'a, T> => &'a T, |
| 51 | impl<'a, T: Hash + Eq + Sync + 'a> |
| 52 | } |
| 53 | |
| 54 | // `HashSet` doesn't have a mutable `Iterator` |
| 55 | |
| 56 | /// Draining parallel iterator that moves out of a hash set, |
| 57 | /// but keeps the total capacity. |
| 58 | #[derive (Debug)] |
| 59 | pub struct Drain<'a, T: Hash + Eq + Send> { |
| 60 | inner: vec::IntoIter<T>, |
| 61 | marker: PhantomData<&'a mut HashSet<T>>, |
| 62 | } |
| 63 | |
| 64 | impl<'a, T: Hash + Eq + Send, S: BuildHasher> ParallelDrainFull for &'a mut HashSet<T, S> { |
| 65 | type Iter = Drain<'a, T>; |
| 66 | type Item = T; |
| 67 | |
| 68 | fn par_drain(self) -> Self::Iter { |
| 69 | let vec: Vec<_> = self.drain().collect(); |
| 70 | Drain { |
| 71 | inner: vec.into_par_iter(), |
| 72 | marker: PhantomData, |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | delegate_iterator! { |
| 78 | Drain<'_, T> => T, |
| 79 | impl<T: Hash + Eq + Send> |
| 80 | } |
| 81 | |