Skip to content

Commit 144496b

Browse files
committed
Create parallel iterators from slices
1 parent 18c1a30 commit 144496b

File tree

4 files changed

+99
-2
lines changed

4 files changed

+99
-2
lines changed

src/map/slice.rs

+1-1
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

+81
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use core::hash::{BuildHasher, Hash};
1717
use crate::Bucket;
1818
use crate::Entries;
1919
use crate::IndexMap;
20+
use crate::map::Slice;
2021

2122
/// Requires crate feature `"rayon"`.
2223
impl<K, V, S> IntoParallelIterator for IndexMap<K, V, S>
@@ -78,6 +79,22 @@ where
7879
}
7980
}
8081

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

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

216+
/// Parallel iterator methods and other parallel methods.
217+
///
218+
/// The following methods **require crate feature `"rayon"`**.
219+
///
220+
/// See also the `IntoParallelIterator` implementations.
221+
impl<K, V> Slice<K, V>
222+
where
223+
K: Sync,
224+
V: Sync,
225+
{
226+
/// Return a parallel iterator over the keys of the map slice.
227+
///
228+
/// While parallel iterators can process items in any order, their relative order
229+
/// in the slice is still preserved for operations like `reduce` and `collect`.
230+
pub fn par_keys(&self) -> ParKeys<'_, K, V> {
231+
ParKeys {
232+
entries: &self.entries,
233+
}
234+
}
235+
236+
/// Return a parallel iterator over the values of the map slice.
237+
///
238+
/// While parallel iterators can process items in any order, their relative order
239+
/// in the slice is still preserved for operations like `reduce` and `collect`.
240+
pub fn par_values(&self) -> ParValues<'_, K, V> {
241+
ParValues {
242+
entries: &self.entries,
243+
}
244+
}
245+
}
246+
183247
impl<K, V, S> IndexMap<K, V, S>
184248
where
185249
K: Hash + Eq + Sync,
@@ -286,6 +350,23 @@ where
286350
}
287351
}
288352

353+
/// Requires crate feature `"rayon"`.
354+
impl<K, V> Slice<K, V>
355+
where
356+
K: Send,
357+
V: Send,
358+
{
359+
/// Return a parallel iterator over mutable references to the the values of the map slice.
360+
///
361+
/// While parallel iterators can process items in any order, their relative order
362+
/// in the slice is still preserved for operations like `reduce` and `collect`.
363+
pub fn par_values_mut(&mut self) -> ParValuesMut<'_, K, V> {
364+
ParValuesMut {
365+
entries: &mut self.entries,
366+
}
367+
}
368+
}
369+
289370
impl<K, V, S> IndexMap<K, V, S>
290371
where
291372
K: Hash + Eq + Send,

src/rayon/set.rs

+16
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use core::hash::{BuildHasher, Hash};
1616

1717
use crate::Entries;
1818
use crate::IndexSet;
19+
use crate::set::Slice;
1920

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

@@ -77,6 +78,21 @@ where
7778
}
7879
}
7980

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

src/set/slice.rs

+1-1
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)