mirror of https://github.com/xacrimon/dashmap
feat: add ReadOnlyView::par_iter() and ReadOnlyView::into_par_iter() (#223)
This commit is contained in:
parent
9deeb37ccb
commit
d1f58eaf48
|
@ -16,6 +16,7 @@ mod util;
|
|||
#[cfg(feature = "rayon")]
|
||||
pub mod rayon {
|
||||
pub mod map;
|
||||
pub mod read_only;
|
||||
pub mod set;
|
||||
}
|
||||
|
||||
|
|
|
@ -80,7 +80,7 @@ where
|
|||
}
|
||||
|
||||
pub struct OwningIter<K, V, S = RandomState> {
|
||||
shards: Box<[RwLock<HashMap<K, V, S>>]>,
|
||||
pub(super) shards: Box<[RwLock<HashMap<K, V, S>>]>,
|
||||
}
|
||||
|
||||
impl<K, V, S> ParallelIterator for OwningIter<K, V, S>
|
||||
|
@ -125,7 +125,7 @@ where
|
|||
}
|
||||
|
||||
pub struct Iter<'a, K, V, S = RandomState> {
|
||||
shards: &'a [RwLock<HashMap<K, V, S>>],
|
||||
pub(super) shards: &'a [RwLock<HashMap<K, V, S>>],
|
||||
}
|
||||
|
||||
impl<'a, K, V, S> ParallelIterator for Iter<'a, K, V, S>
|
||||
|
|
|
@ -0,0 +1,96 @@
|
|||
use crate::mapref::multiple::RefMulti;
|
||||
use crate::rayon::map::Iter;
|
||||
use crate::ReadOnlyView;
|
||||
use core::hash::{BuildHasher, Hash};
|
||||
use rayon::iter::IntoParallelIterator;
|
||||
|
||||
impl<K, V, S> IntoParallelIterator for ReadOnlyView<K, V, S>
|
||||
where
|
||||
K: Send + Eq + Hash,
|
||||
V: Send,
|
||||
S: Send + Clone + BuildHasher,
|
||||
{
|
||||
type Iter = super::map::OwningIter<K, V, S>;
|
||||
type Item = (K, V);
|
||||
|
||||
fn into_par_iter(self) -> Self::Iter {
|
||||
super::map::OwningIter {
|
||||
shards: self.map.shards,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// This impl also enables `IntoParallelRefIterator::par_iter`
|
||||
impl<'a, K, V, S> IntoParallelIterator for &'a ReadOnlyView<K, V, S>
|
||||
where
|
||||
K: Send + Sync + Eq + Hash,
|
||||
V: Send + Sync,
|
||||
S: Send + Sync + Clone + BuildHasher,
|
||||
{
|
||||
type Iter = Iter<'a, K, V, S>;
|
||||
type Item = RefMulti<'a, K, V, S>;
|
||||
|
||||
fn into_par_iter(self) -> Self::Iter {
|
||||
Iter {
|
||||
shards: &self.map.shards,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::DashMap;
|
||||
use rayon::iter::{IntoParallelIterator, IntoParallelRefIterator, ParallelIterator};
|
||||
|
||||
fn construct_sample_map() -> DashMap<i32, String> {
|
||||
let map = DashMap::new();
|
||||
|
||||
map.insert(1, "one".to_string());
|
||||
|
||||
map.insert(10, "ten".to_string());
|
||||
|
||||
map.insert(27, "twenty seven".to_string());
|
||||
|
||||
map.insert(45, "forty five".to_string());
|
||||
|
||||
map
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_par_iter() {
|
||||
let map = construct_sample_map();
|
||||
|
||||
let view = map.clone().into_read_only();
|
||||
|
||||
view.par_iter().for_each(|entry| {
|
||||
let key = *entry.key();
|
||||
|
||||
assert!(view.contains_key(&key));
|
||||
|
||||
let map_entry = map.get(&key).unwrap();
|
||||
|
||||
assert_eq!(view.get(&key).unwrap(), map_entry.value());
|
||||
|
||||
let key_value: (&i32, &String) = view.get_key_value(&key).unwrap();
|
||||
|
||||
assert_eq!(key_value.0, map_entry.key());
|
||||
|
||||
assert_eq!(key_value.1, map_entry.value());
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_into_par_iter() {
|
||||
let map = construct_sample_map();
|
||||
|
||||
let view = map.clone().into_read_only();
|
||||
|
||||
view.into_par_iter().for_each(|(key, value)| {
|
||||
let map_entry = map.get(&key).unwrap();
|
||||
|
||||
assert_eq!(&key, map_entry.key());
|
||||
|
||||
assert_eq!(&value, map_entry.value());
|
||||
});
|
||||
}
|
||||
}
|
|
@ -7,7 +7,7 @@ use std::collections::hash_map::RandomState;
|
|||
|
||||
/// A read-only view into a `DashMap`. Allows to obtain raw references to the stored values.
|
||||
pub struct ReadOnlyView<K, V, S = RandomState> {
|
||||
map: DashMap<K, V, S>,
|
||||
pub(crate) map: DashMap<K, V, S>,
|
||||
}
|
||||
|
||||
impl<K: Eq + Hash + Clone, V: Clone, S: Clone> Clone for ReadOnlyView<K, V, S> {
|
||||
|
|
Loading…
Reference in New Issue