diff --git a/src/map/slice.rs b/src/map/slice.rs index 4ccfb287..741b06de 100644 --- a/src/map/slice.rs +++ b/src/map/slice.rs @@ -14,7 +14,7 @@ use core::ops::{self, Index, IndexMut}; /// and `Eq`, and it also implements `PartialOrd`, `Ord`, and `Hash`. #[repr(transparent)] pub struct Slice { - entries: [Bucket], + pub(crate) entries: [Bucket], } #[allow(unsafe_code)] diff --git a/src/rayon/map.rs b/src/rayon/map.rs index ed2da3ef..a97c9036 100644 --- a/src/rayon/map.rs +++ b/src/rayon/map.rs @@ -17,6 +17,7 @@ use core::hash::{BuildHasher, Hash}; use crate::Bucket; use crate::Entries; use crate::IndexMap; +use crate::map::Slice; /// Requires crate feature `"rayon"`. impl IntoParallelIterator for IndexMap @@ -78,6 +79,22 @@ where } } +/// Requires crate feature `"rayon"`. +impl<'a, K, V> IntoParallelIterator for &'a Slice +where + K: Sync, + V: Sync, +{ + type Item = (&'a K, &'a V); + type Iter = ParIter<'a, K, V>; + + fn into_par_iter(self) -> Self::Iter { + ParIter { + entries: &self.entries, + } + } +} + /// A parallel iterator over the entries of a `IndexMap`. /// /// This `struct` is created by the [`par_iter`] method on [`IndexMap`] @@ -128,6 +145,22 @@ where } } +/// Requires crate feature `"rayon"`. +impl<'a, K, V> IntoParallelIterator for &'a mut Slice +where + K: Sync + Send, + V: Send, +{ + type Item = (&'a K, &'a mut V); + type Iter = ParIterMut<'a, K, V>; + + fn into_par_iter(self) -> Self::Iter { + ParIterMut { + entries: &mut self.entries, + } + } +} + /// A parallel mutable iterator over the entries of a `IndexMap`. /// /// This `struct` is created by the [`par_iter_mut`] method on [`IndexMap`] @@ -180,6 +213,37 @@ where } } +/// Parallel iterator methods and other parallel methods. +/// +/// The following methods **require crate feature `"rayon"`**. +/// +/// See also the `IntoParallelIterator` implementations. +impl Slice +where + K: Sync, + V: Sync, +{ + /// Return a parallel iterator over the keys of the map slice. + /// + /// While parallel iterators can process items in any order, their relative order + /// in the slice is still preserved for operations like `reduce` and `collect`. + pub fn par_keys(&self) -> ParKeys<'_, K, V> { + ParKeys { + entries: &self.entries, + } + } + + /// Return a parallel iterator over the values of the map slice. + /// + /// While parallel iterators can process items in any order, their relative order + /// in the slice is still preserved for operations like `reduce` and `collect`. + pub fn par_values(&self) -> ParValues<'_, K, V> { + ParValues { + entries: &self.entries, + } + } +} + impl IndexMap where K: Hash + Eq + Sync, @@ -286,6 +350,23 @@ where } } +/// Requires crate feature `"rayon"`. +impl Slice +where + K: Send, + V: Send, +{ + /// Return a parallel iterator over mutable references to the the values of the map slice. + /// + /// While parallel iterators can process items in any order, their relative order + /// in the slice is still preserved for operations like `reduce` and `collect`. + pub fn par_values_mut(&mut self) -> ParValuesMut<'_, K, V> { + ParValuesMut { + entries: &mut self.entries, + } + } +} + impl IndexMap where K: Hash + Eq + Send, diff --git a/src/rayon/set.rs b/src/rayon/set.rs index f3e0da91..e1e93c53 100644 --- a/src/rayon/set.rs +++ b/src/rayon/set.rs @@ -16,6 +16,7 @@ use core::hash::{BuildHasher, Hash}; use crate::Entries; use crate::IndexSet; +use crate::set::Slice; type Bucket = crate::Bucket; @@ -77,6 +78,21 @@ where } } +/// Requires crate feature `"rayon"`. +impl<'a, T> IntoParallelIterator for &'a Slice +where + T: Sync, +{ + type Item = &'a T; + type Iter = ParIter<'a, T>; + + fn into_par_iter(self) -> Self::Iter { + ParIter { + entries: &self.entries, + } + } +} + /// A parallel iterator over the items of a `IndexSet`. /// /// This `struct` is created by the [`par_iter`] method on [`IndexSet`] diff --git a/src/set/slice.rs b/src/set/slice.rs index 8962799f..2ab190d3 100644 --- a/src/set/slice.rs +++ b/src/set/slice.rs @@ -14,7 +14,7 @@ use core::ops::{self, Index}; /// and `Eq`, and it also implements `PartialOrd`, `Ord`, and `Hash`. #[repr(transparent)] pub struct Slice { - entries: [Bucket], + pub(crate) entries: [Bucket], } #[allow(unsafe_code)]