Skip to content

Commit

Permalink
Create parallel iterators from slices
Browse files Browse the repository at this point in the history
  • Loading branch information
cuviper committed Feb 28, 2021
1 parent 354dc17 commit ea9437c
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/map/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<K, V> {
entries: [Bucket<K, V>],
pub(crate) entries: [Bucket<K, V>],
}

#[allow(unsafe_code)]
Expand Down
81 changes: 81 additions & 0 deletions src/rayon/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<K, V, S> IntoParallelIterator for IndexMap<K, V, S>
Expand Down Expand Up @@ -78,6 +79,22 @@ where
}
}

/// Requires crate feature `"rayon"`.
impl<'a, K, V> IntoParallelIterator for &'a Slice<K, V>
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`]
Expand Down Expand Up @@ -128,6 +145,22 @@ where
}
}

/// Requires crate feature `"rayon"`.
impl<'a, K, V> IntoParallelIterator for &'a mut Slice<K, V>
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`]
Expand Down Expand Up @@ -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<K, V> Slice<K, V>
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<K, V, S> IndexMap<K, V, S>
where
K: Hash + Eq + Sync,
Expand Down Expand Up @@ -286,6 +350,23 @@ where
}
}

/// Requires crate feature `"rayon"`.
impl<K, V> Slice<K, V>
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<K, V, S> IndexMap<K, V, S>
where
K: Hash + Eq + Send,
Expand Down
16 changes: 16 additions & 0 deletions src/rayon/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use core::hash::{BuildHasher, Hash};

use crate::Entries;
use crate::IndexSet;
use crate::set::Slice;

type Bucket<T> = crate::Bucket<T, ()>;

Expand Down Expand Up @@ -77,6 +78,21 @@ where
}
}

/// Requires crate feature `"rayon"`.
impl<'a, T> IntoParallelIterator for &'a Slice<T>
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`]
Expand Down
2 changes: 1 addition & 1 deletion src/set/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use core::ops::{self, Index};
/// and `Eq`, and it also implements `PartialOrd`, `Ord`, and `Hash`.
#[repr(transparent)]
pub struct Slice<T> {
entries: [Bucket<T>],
pub(crate) entries: [Bucket<T>],
}

#[allow(unsafe_code)]
Expand Down

0 comments on commit ea9437c

Please sign in to comment.