//! Parallel iterator types for [`IndexMap`] with [`rayon`][::rayon]. //! //! You will rarely need to interact with this module directly unless you need to name one of the //! iterator types.
usesuper::collect; use rayon::iter::plumbing::{Consumer, ProducerCallback, UnindexedConsumer}; use rayon::prelude::*;
usecrate::vec::Vec; use alloc::boxed::Box; use core::cmp::Ordering; use core::fmt; use core::hash::{BuildHasher, Hash}; use core::ops::RangeBounds;
/// A parallel owning iterator over the entries of an [`IndexMap`]. /// /// This `struct` is created by the [`IndexMap::into_par_iter`] method /// (provided by rayon's [`IntoParallelIterator`] trait). See its documentation for more. pubstruct IntoParIter<K, V> {
entries: Vec<Bucket<K, V>>,
}
impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for IntoParIter<K, V> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let iter = self.entries.iter().map(Bucket::refs);
f.debug_list().entries(iter).finish()
}
}
impl<K: Send, V: Send> ParallelIterator for IntoParIter<K, V> { type Item = (K, V);
/// A parallel iterator over the entries of an [`IndexMap`]. /// /// This `struct` is created by the [`IndexMap::par_iter`] method /// (provided by rayon's [`IntoParallelRefIterator`] trait). See its documentation for more. /// /// [`IndexMap::par_iter`]: ../struct.IndexMap.html#method.par_iter pubstruct ParIter<'a, K, V> {
entries: &'a [Bucket<K, V>],
}
/// A parallel mutable iterator over the entries of an [`IndexMap`]. /// /// This `struct` is created by the [`IndexMap::par_iter_mut`] method /// (provided by rayon's [`IntoParallelRefMutIterator`] trait). See its documentation for more. /// /// [`IndexMap::par_iter_mut`]: ../struct.IndexMap.html#method.par_iter_mut pubstruct ParIterMut<'a, K, V> {
entries: &'a mut [Bucket<K, V>],
}
impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for ParIterMut<'_, K, V> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let iter = self.entries.iter().map(Bucket::refs);
f.debug_list().entries(iter).finish()
}
}
/// A parallel draining iterator over the entries of an [`IndexMap`]. /// /// This `struct` is created by the [`IndexMap::par_drain`] method /// (provided by rayon's [`ParallelDrainRange`] trait). See its documentation for more. /// /// [`IndexMap::par_drain`]: ../struct.IndexMap.html#method.par_drain pubstruct 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 and other parallel methods. /// /// The following methods **require crate feature `"rayon"`**. /// /// See also the `IntoParallelIterator` implementations. impl<K, V, S> IndexMap<K, V, S> where
K: Sync,
V: Sync,
{ /// Return a parallel iterator over the keys of the map. /// /// While parallel iterators can process items in any order, their relative order /// in the map is still preserved for operations like `reduce` and `collect`. pubfn par_keys(&self) -> ParKeys<'_, K, V> {
ParKeys {
entries: self.as_entries(),
}
}
/// Return a parallel iterator over the values of the map. /// /// While parallel iterators can process items in any order, their relative order /// in the map is still preserved for operations like `reduce` and `collect`. pubfn par_values(&self) -> ParValues<'_, K, V> {
ParValues {
entries: self.as_entries(),
}
}
}
/// 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`. pubfn 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`. pubfn par_values(&self) -> ParValues<'_, K, V> {
ParValues {
entries: &self.entries,
}
}
}
impl<K, V, S> IndexMap<K, V, S> where
K: Hash + Eq + Sync,
V: Sync,
S: BuildHasher,
{ /// Returns `true` if `self` contains all of the same key-value pairs as `other`, /// regardless of each map's indexed order, determined in parallel. pubfn par_eq<V2, S2>(&self, other: &IndexMap<K, V2, S2>) -> bool where
V: PartialEq<V2>,
V2: Sync,
S2: BuildHasher + Sync,
{ self.len() == other.len()
&& self
.par_iter()
.all(move |(key, value)| other.get(key).map_or(false, |v| *value == *v))
}
}
/// A parallel iterator over the keys of an [`IndexMap`]. /// /// This `struct` is created by the [`IndexMap::par_keys`] method. /// See its documentation for more. pubstruct ParKeys<'a, K, V> {
entries: &'a [Bucket<K, V>],
}
/// A parallel iterator over the values of an [`IndexMap`]. /// /// This `struct` is created by the [`IndexMap::par_values`] method. /// See its documentation for more. pubstruct ParValues<'a, K, V> {
entries: &'a [Bucket<K, V>],
}
impl<K, V, S> IndexMap<K, V, S> where
K: Send,
V: Send,
{ /// Return a parallel iterator over mutable references to the values of the map /// /// While parallel iterators can process items in any order, their relative order /// in the map is still preserved for operations like `reduce` and `collect`. pubfn par_values_mut(&mutself) -> ParValuesMut<'_, K, V> {
ParValuesMut {
entries: self.as_entries_mut(),
}
}
}
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`. pubfn par_values_mut(&mutself) -> ParValuesMut<'_, K, V> {
ParValuesMut {
entries: &mutself.entries,
}
}
}
impl<K, V, S> IndexMap<K, V, S> where
K: Send,
V: Send,
{ /// Sort the map’s key-value pairs in parallel, by the default ordering of the keys. pubfn par_sort_keys(&mutself) where
K: Ord,
{ self.with_entries(|entries| {
entries.par_sort_by(|a, b| K::cmp(&a.key, &b.key));
});
}
/// Sort the map’s key-value pairs in place and in parallel, using the comparison /// function `cmp`. /// /// The comparison function receives two key and value pairs to compare (you /// can sort by keys or values or their combination as needed). pubfn par_sort_by<F>(&mutself, cmp: F) where
F: Fn(&K, &V, &K, &V) -> Ordering + Sync,
{ self.with_entries(|entries| {
entries.par_sort_by(move |a, b| cmp(&a.key, &a.value, &b.key, &b.value));
});
}
/// Sort the key-value pairs of the map in parallel and return a by-value parallel /// iterator of the key-value pairs with the result. pubfn par_sorted_by<F>(self, cmp: F) -> IntoParIter<K, V> where
F: Fn(&K, &V, &K, &V) -> Ordering + Sync,
{ letmut entries = self.into_entries();
entries.par_sort_by(move |a, b| cmp(&a.key, &a.value, &b.key, &b.value));
IntoParIter { entries }
}
/// Sort the map's key-value pairs in parallel, by the default ordering of the keys. pubfn par_sort_unstable_keys(&mutself) where
K: Ord,
{ self.with_entries(|entries| {
entries.par_sort_unstable_by(|a, b| K::cmp(&a.key, &b.key));
});
}
/// Sort the map's key-value pairs in place and in parallel, using the comparison /// function `cmp`. /// /// The comparison function receives two key and value pairs to compare (you /// can sort by keys or values or their combination as needed). pubfn par_sort_unstable_by<F>(&mutself, cmp: F) where
F: Fn(&K, &V, &K, &V) -> Ordering + Sync,
{ self.with_entries(|entries| {
entries.par_sort_unstable_by(move |a, b| cmp(&a.key, &a.value, &b.key, &b.value));
});
}
/// Sort the key-value pairs of the map in parallel and return a by-value parallel /// iterator of the key-value pairs with the result. pubfn par_sorted_unstable_by<F>(self, cmp: F) -> IntoParIter<K, V> where
F: Fn(&K, &V, &K, &V) -> Ordering + Sync,
{ letmut entries = self.into_entries();
entries.par_sort_unstable_by(move |a, b| cmp(&a.key, &a.value, &b.key, &b.value));
IntoParIter { entries }
}
/// Sort the map’s key-value pairs in place and in parallel, using a sort-key extraction /// function. pubfn par_sort_by_cached_key<T, F>(&mutself, sort_key: F) where
T: Ord + Send,
F: Fn(&K, &V) -> T + Sync,
{ self.with_entries(move |entries| {
entries.par_sort_by_cached_key(move |a| sort_key(&a.key, &a.value));
});
}
}
/// A parallel mutable iterator over the values of an [`IndexMap`]. /// /// This `struct` is created by the [`IndexMap::par_values_mut`] method. /// See its documentation for more. pubstruct ParValuesMut<'a, K, V> {
entries: &'a mut [Bucket<K, V>],
}
impl<K, V: fmt::Debug> fmt::Debug for ParValuesMut<'_, K, V> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let iter = self.entries.iter().map(Bucket::value_ref);
f.debug_list().entries(iter).finish()
}
}
impl<'a, K: Send, V: Send> ParallelIterator for ParValuesMut<'a, K, V> { type Item = &'a mut V;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.