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

Adds keys and values iterators #61

Merged
merged 3 commits into from
Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 16 additions & 0 deletions src/read/read_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ where
}
}

/// Iterate over all keys in the map.
jonhoo marked this conversation as resolved.
Show resolved Hide resolved
///
/// Be careful with this function! While the iteration is ongoing, any writer that tries to
/// refresh will block waiting on this reader to finish.
pub fn keys(&self) -> impl Iterator<Item = &K> {
jonhoo marked this conversation as resolved.
Show resolved Hide resolved
self.iter().map(|(k, _)| k)
}

/// Iterate over all values in the map.
///
/// Be careful with this function! While the iteration is ongoing, any writer that tries to
/// refresh will block waiting on this reader to finish.
pub fn values(&self) -> impl Iterator<Item = &V> {
jonhoo marked this conversation as resolved.
Show resolved Hide resolved
self.iter().map(|(_, v)| v).flatten()
}

/// Returns the number of non-empty keys present in the map.
pub fn len(&self) -> usize {
self.guard.data.len()
Expand Down
42 changes: 42 additions & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,48 @@ fn map_into() {
assert!(copy[&2].contains(&"c"));
}

#[test]
fn keys() {
let (r, mut w) = evmap::new();
w.insert(1, "a");
w.insert(1, "b");
w.insert(2, "c");
w.refresh();
w.insert(1, "x");

let mut keys = r
mlodato517 marked this conversation as resolved.
Show resolved Hide resolved
.read()
.iter()
.map(|r| r.keys())
.flatten()
.copied()
.collect::<Vec<_>>();
keys.sort();

assert_eq!(keys, vec![1, 2]);
}

#[test]
fn values() {
let (r, mut w) = evmap::new();
w.insert(1, "a");
w.insert(1, "b");
w.insert(2, "c");
w.refresh();
w.insert(1, "x");

let mut values = r
.read()
.iter()
.map(|r| r.values())
.flatten()
.copied()
.collect::<Vec<_>>();
values.sort();

assert_eq!(values, vec!["a", "b", "c"]);
}

#[test]
#[cfg_attr(miri, ignore)]
fn clone_churn() {
Expand Down
7 changes: 3 additions & 4 deletions tests/quick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fn insert_empty(insert: Vec<u8>, remove: Vec<u8>) -> bool {
w.refresh();
let elements = &set(&insert) - &set(&remove);
r.len() == elements.len()
&& r.map_into::<_, Vec<()>, _>(|_, _| ()).len() == elements.len()
&& r.read().iter().map(|r| r.keys()).flatten().count() == elements.len()
&& elements.iter().all(|k| r.get(k).is_some())
}

Expand Down Expand Up @@ -137,14 +137,13 @@ where
S: BuildHasher,
{
assert_eq!(a.len(), b.len());
let keys = a.map_into::<_, Vec<K>, _>(|k, _| k.clone());
for key in &keys {
for key in a.read().iter().map(|r| r.keys()).flatten() {
assert!(b.contains_key(key), "b does not contain {:?}", key);
}
for key in b.keys() {
assert!(a.get(key).is_some(), "a does not contain {:?}", key);
}
for key in &keys {
for key in a.read().iter().map(|r| r.keys()).flatten() {
let mut ev_map_values: Vec<V> = a.get(key).unwrap().iter().copied().collect();
ev_map_values.sort();
let mut map_values = b[key].clone();
Expand Down