Skip to content

Commit e2c6a2f

Browse files
committed
Create parallel iterators from slices
1 parent 4fba592 commit e2c6a2f

File tree

4 files changed

+99
-2
lines changed

4 files changed

+99
-2
lines changed

src/map/slice.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use core::ops::{self, Index, IndexMut};
1414
/// and `Eq`, and it also implements `PartialOrd`, `Ord`, and `Hash`.
1515
#[repr(transparent)]
1616
pub struct Slice<K, V> {
17-
entries: [Bucket<K, V>],
17+
pub(crate) entries: [Bucket<K, V>],
1818
}
1919

2020
#[allow(unsafe_code)]

src/rayon/map.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use core::fmt;
1515
use core::hash::{BuildHasher, Hash};
1616
use core::ops::RangeBounds;
1717

18+
use crate::map::Slice;
1819
use crate::Bucket;
1920
use crate::Entries;
2021
use crate::IndexMap;
@@ -79,6 +80,22 @@ where
7980
}
8081
}
8182

83+
/// Requires crate feature `"rayon"`.
84+
impl<'a, K, V> IntoParallelIterator for &'a Slice<K, V>
85+
where
86+
K: Sync,
87+
V: Sync,
88+
{
89+
type Item = (&'a K, &'a V);
90+
type Iter = ParIter<'a, K, V>;
91+
92+
fn into_par_iter(self) -> Self::Iter {
93+
ParIter {
94+
entries: &self.entries,
95+
}
96+
}
97+
}
98+
8299
/// A parallel iterator over the entries of a `IndexMap`.
83100
///
84101
/// This `struct` is created by the [`par_iter`] method on [`IndexMap`]
@@ -129,6 +146,22 @@ where
129146
}
130147
}
131148

149+
/// Requires crate feature `"rayon"`.
150+
impl<'a, K, V> IntoParallelIterator for &'a mut Slice<K, V>
151+
where
152+
K: Sync + Send,
153+
V: Send,
154+
{
155+
type Item = (&'a K, &'a mut V);
156+
type Iter = ParIterMut<'a, K, V>;
157+
158+
fn into_par_iter(self) -> Self::Iter {
159+
ParIterMut {
160+
entries: &mut self.entries,
161+
}
162+
}
163+
}
164+
132165
/// A parallel mutable iterator over the entries of a `IndexMap`.
133166
///
134167
/// This `struct` is created by the [`par_iter_mut`] method on [`IndexMap`]
@@ -225,6 +258,37 @@ where
225258
}
226259
}
227260

261+
/// Parallel iterator methods and other parallel methods.
262+
///
263+
/// The following methods **require crate feature `"rayon"`**.
264+
///
265+
/// See also the `IntoParallelIterator` implementations.
266+
impl<K, V> Slice<K, V>
267+
where
268+
K: Sync,
269+
V: Sync,
270+
{
271+
/// Return a parallel iterator over the keys of the map slice.
272+
///
273+
/// While parallel iterators can process items in any order, their relative order
274+
/// in the slice is still preserved for operations like `reduce` and `collect`.
275+
pub fn par_keys(&self) -> ParKeys<'_, K, V> {
276+
ParKeys {
277+
entries: &self.entries,
278+
}
279+
}
280+
281+
/// Return a parallel iterator over the values of the map slice.
282+
///
283+
/// While parallel iterators can process items in any order, their relative order
284+
/// in the slice is still preserved for operations like `reduce` and `collect`.
285+
pub fn par_values(&self) -> ParValues<'_, K, V> {
286+
ParValues {
287+
entries: &self.entries,
288+
}
289+
}
290+
}
291+
228292
impl<K, V, S> IndexMap<K, V, S>
229293
where
230294
K: Hash + Eq + Sync,
@@ -331,6 +395,23 @@ where
331395
}
332396
}
333397

398+
/// Requires crate feature `"rayon"`.
399+
impl<K, V> Slice<K, V>
400+
where
401+
K: Send,
402+
V: Send,
403+
{
404+
/// Return a parallel iterator over mutable references to the the values of the map slice.
405+
///
406+
/// While parallel iterators can process items in any order, their relative order
407+
/// in the slice is still preserved for operations like `reduce` and `collect`.
408+
pub fn par_values_mut(&mut self) -> ParValuesMut<'_, K, V> {
409+
ParValuesMut {
410+
entries: &mut self.entries,
411+
}
412+
}
413+
}
414+
334415
impl<K, V, S> IndexMap<K, V, S>
335416
where
336417
K: Hash + Eq + Send,

src/rayon/set.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use core::fmt;
1515
use core::hash::{BuildHasher, Hash};
1616
use core::ops::RangeBounds;
1717

18+
use crate::set::Slice;
1819
use crate::Entries;
1920
use crate::IndexSet;
2021

@@ -78,6 +79,21 @@ where
7879
}
7980
}
8081

82+
/// Requires crate feature `"rayon"`.
83+
impl<'a, T> IntoParallelIterator for &'a Slice<T>
84+
where
85+
T: Sync,
86+
{
87+
type Item = &'a T;
88+
type Iter = ParIter<'a, T>;
89+
90+
fn into_par_iter(self) -> Self::Iter {
91+
ParIter {
92+
entries: &self.entries,
93+
}
94+
}
95+
}
96+
8197
/// A parallel iterator over the items of a `IndexSet`.
8298
///
8399
/// This `struct` is created by the [`par_iter`] method on [`IndexSet`]

src/set/slice.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use core::ops::{self, Index};
1414
/// and `Eq`, and it also implements `PartialOrd`, `Ord`, and `Hash`.
1515
#[repr(transparent)]
1616
pub struct Slice<T> {
17-
entries: [Bucket<T>],
17+
pub(crate) entries: [Bucket<T>],
1818
}
1919

2020
#[allow(unsafe_code)]

0 commit comments

Comments
 (0)