Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impl ParallelDrainRange #197

Merged
merged 1 commit into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ bench = false
autocfg = "1"
[dependencies]
serde = { version = "1.0", optional = true, default-features = false }
rayon = { version = "1.2", optional = true }
rayon = { version = "1.4.1", optional = true }

[dependencies.hashbrown]
version = "0.11"
Expand Down
4 changes: 2 additions & 2 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ pub use self::core::{Entry, OccupiedEntry, VacantEntry};
/// ```
#[cfg(has_std)]
pub struct IndexMap<K, V, S = RandomState> {
core: IndexMapCore<K, V>,
pub(crate) core: IndexMapCore<K, V>,
hash_builder: S,
}
#[cfg(not(has_std))]
pub struct IndexMap<K, V, S> {
core: IndexMapCore<K, V>,
pub(crate) core: IndexMapCore<K, V>,
hash_builder: S,
}

Expand Down
13 changes: 13 additions & 0 deletions src/map/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,19 @@ impl<K, V> IndexMapCore<K, V> {
self.entries.drain(range)
}

#[cfg(feature = "rayon")]
pub(crate) fn par_drain<R>(&mut self, range: R) -> rayon::vec::Drain<'_, Bucket<K, V>>
where
K: Send,
V: Send,
R: RangeBounds<usize>,
{
use rayon::iter::ParallelDrainRange;
let range = simplify_range(range, self.entries.len());
self.erase_indices(range.start, range.end);
self.entries.par_drain(range)
}

pub(crate) fn split_off(&mut self, at: usize) -> Self {
assert!(at <= self.entries.len());
self.erase_indices(at, self.entries.len());
Expand Down
38 changes: 38 additions & 0 deletions src/rayon/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::vec::Vec;
use core::cmp::Ordering;
use core::fmt;
use core::hash::{BuildHasher, Hash};
use core::ops::RangeBounds;

use crate::Bucket;
use crate::Entries;
Expand Down Expand Up @@ -149,6 +150,43 @@ impl<K: Sync + Send, V: Send> IndexedParallelIterator for ParIterMut<'_, K, V> {
indexed_parallel_iterator_methods!(Bucket::ref_mut);
}

/// Requires crate feature `"rayon"`.
impl<'a, K, V, S> ParallelDrainRange<usize> for &'a mut IndexMap<K, V, S>
where
K: Send,
V: Send,
{
type Item = (K, V);
type Iter = ParDrain<'a, K, V>;

fn par_drain<R: RangeBounds<usize>>(self, range: R) -> Self::Iter {
ParDrain {
entries: self.core.par_drain(range),
}
}
}

/// A parallel draining iterator over the entries of a `IndexMap`.
///
/// This `struct` is created by the [`par_drain`] method on [`IndexMap`]
/// (provided by rayon's `ParallelDrainRange` trait). See its documentation for more.
///
/// [`par_drain`]: ../struct.IndexMap.html#method.par_drain
/// [`IndexMap`]: ../struct.IndexMap.html
pub struct ParDrain<'a, K: Send, V: Send> {
entries: rayon::vec::Drain<'a, Bucket<K, V>>,
}

impl<K: Send, V: Send> ParallelIterator for ParDrain<'_, K, V> {
type Item = (K, V);

parallel_iterator_methods!(Bucket::key_value);
}

impl<K: Send, V: Send> IndexedParallelIterator for ParDrain<'_, K, V> {
indexed_parallel_iterator_methods!(Bucket::key_value);
}

/// Parallel iterator methods and other parallel methods.
///
/// The following methods **require crate feature `"rayon"`**.
Expand Down
37 changes: 37 additions & 0 deletions src/rayon/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::vec::Vec;
use core::cmp::Ordering;
use core::fmt;
use core::hash::{BuildHasher, Hash};
use core::ops::RangeBounds;

use crate::Entries;
use crate::IndexSet;
Expand Down Expand Up @@ -111,6 +112,42 @@ impl<T: Sync> IndexedParallelIterator for ParIter<'_, T> {
indexed_parallel_iterator_methods!(Bucket::key_ref);
}

/// Requires crate feature `"rayon"`.
impl<'a, T, S> ParallelDrainRange<usize> for &'a mut IndexSet<T, S>
where
T: Send,
{
type Item = T;
type Iter = ParDrain<'a, T>;

fn par_drain<R: RangeBounds<usize>>(self, range: R) -> Self::Iter {
ParDrain {
entries: self.map.core.par_drain(range),
}
}
}

/// A parallel draining iterator over the items of a `IndexSet`.
///
/// This `struct` is created by the [`par_drain`] method on [`IndexSet`]
/// (provided by rayon's `ParallelDrainRange` trait). See its documentation for more.
///
/// [`par_drain`]: ../struct.IndexSet.html#method.par_drain
/// [`IndexSet`]: ../struct.IndexSet.html
pub struct ParDrain<'a, T: Send> {
entries: rayon::vec::Drain<'a, Bucket<T>>,
}

impl<T: Send> ParallelIterator for ParDrain<'_, T> {
type Item = T;

parallel_iterator_methods!(Bucket::key);
}

impl<T: Send> IndexedParallelIterator for ParDrain<'_, T> {
indexed_parallel_iterator_methods!(Bucket::key);
}

/// Parallel iterator methods and other parallel methods.
///
/// The following methods **require crate feature `"rayon"`**.
Expand Down
4 changes: 2 additions & 2 deletions src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ type Bucket<T> = super::Bucket<T, ()>;
/// ```
#[cfg(has_std)]
pub struct IndexSet<T, S = RandomState> {
map: IndexMap<T, (), S>,
pub(crate) map: IndexMap<T, (), S>,
}
#[cfg(not(has_std))]
pub struct IndexSet<T, S> {
map: IndexMap<T, (), S>,
pub(crate) map: IndexMap<T, (), S>,
}

impl<T, S> Clone for IndexSet<T, S>
Expand Down