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

More generic maps in GroupingMap (v2) #906

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
113 changes: 113 additions & 0 deletions src/generic_containers.rs
@@ -0,0 +1,113 @@
//! **Private** generalizations of containers:
//! - `Map`: `BTreeMap`, `HashMap` (any hasher) and _unordered_ `Vec`.

#![cfg(feature = "use_alloc")]

use alloc::collections::BTreeMap;
use alloc::vec::Vec;
#[cfg(feature = "use_std")]
use core::hash::{BuildHasher, Hash};
#[cfg(feature = "use_std")]
use std::collections::HashMap;

pub trait Map {
type Key;
type Value;
fn insert(&mut self, key: Self::Key, value: Self::Value) -> Option<Self::Value>;
fn remove(&mut self, key: &Self::Key) -> Option<Self::Value>;
fn aggregate<T, F>(&mut self, key: Self::Key, t: T, mut operation: F)
where
F: FnMut(Option<Self::Value>, &Self::Key, T) -> Option<Self::Value>,
{
let opt_value = self.remove(&key);
if let Some(value) = operation(opt_value, &key, t) {
self.insert(key, value);
}
}
fn entry_or_default(&mut self, key: Self::Key) -> &mut Self::Value
where
Self::Value: Default;
}

impl<K, V> Map for BTreeMap<K, V>
where
K: Ord,
{
type Key = K;
type Value = V;
fn insert(&mut self, key: K, value: V) -> Option<V> {
self.insert(key, value)
}
fn remove(&mut self, key: &K) -> Option<V> {
self.remove(key)
}
fn entry_or_default(&mut self, key: K) -> &mut V
where
V: Default,
{
self.entry(key).or_default()
}

Check warning on line 49 in src/generic_containers.rs

View check run for this annotation

Codecov / codecov/patch

src/generic_containers.rs#L38-L49

Added lines #L38 - L49 were not covered by tests
}

#[cfg(feature = "use_std")]
impl<K, V, S> Map for HashMap<K, V, S>
where
K: Eq + Hash,
S: BuildHasher,
{
type Key = K;
type Value = V;
fn insert(&mut self, key: K, value: V) -> Option<V> {
self.insert(key, value)
}
fn remove(&mut self, key: &K) -> Option<V> {
self.remove(key)
}
fn entry_or_default(&mut self, key: K) -> &mut V
where
V: Default,
{
self.entry(key).or_default()
}
}

impl<K, V> Map for Vec<(K, V)>
where
K: Eq,
{
type Key = K;
type Value = V;
fn insert(&mut self, key: K, value: V) -> Option<V> {
match self.iter_mut().find(|(k, _)| k == &key) {
Some((_, v)) => Some(core::mem::replace(v, value)),

Check warning on line 82 in src/generic_containers.rs

View check run for this annotation

Codecov / codecov/patch

src/generic_containers.rs#L80-L82

Added lines #L80 - L82 were not covered by tests
None => {
self.push((key, value));
None

Check warning on line 85 in src/generic_containers.rs

View check run for this annotation

Codecov / codecov/patch

src/generic_containers.rs#L84-L85

Added lines #L84 - L85 were not covered by tests
}
}
}
fn remove(&mut self, key: &K) -> Option<V> {
let index = self.iter().position(|(k, _)| k == key)?;
Some(self.swap_remove(index).1)
}
fn aggregate<T, F>(&mut self, key: K, t: T, mut operation: F)
where
F: FnMut(Option<V>, &K, T) -> Option<V>,
{
let opt_value = Map::remove(self, &key);
if let Some(value) = operation(opt_value, &key, t) {
// The key was removed so a single push is enough to insert it back.
self.push((key, value));
}
}
fn entry_or_default(&mut self, key: K) -> &mut V
where
V: Default,
{
let index = self.iter().position(|(k, _)| k == &key).unwrap_or_else(|| {
self.push((key, V::default()));
self.len() - 1
});
&mut self[index].1
}

Check warning on line 112 in src/generic_containers.rs

View check run for this annotation

Codecov / codecov/patch

src/generic_containers.rs#L88-L112

Added lines #L88 - L112 were not covered by tests
}