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) #901

Draft
wants to merge 15 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions src/generic_containers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
//! **Private** generalizations of containers:
//! - `Map`: `BTreeMap` and `HashMap` (any hasher).

#![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 {
Philippe-Cholet marked this conversation as resolved.
Show resolved Hide resolved
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 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()
}
}

#[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 73 in src/generic_containers.rs

View check run for this annotation

Codecov / codecov/patch

src/generic_containers.rs#L71-L73

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

Check warning on line 76 in src/generic_containers.rs

View check run for this annotation

Codecov / codecov/patch

src/generic_containers.rs#L75-L76

Added lines #L75 - L76 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 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 93 in src/generic_containers.rs

View check run for this annotation

Codecov / codecov/patch

src/generic_containers.rs#L79-L93

Added lines #L79 - L93 were not covered by tests
}
Loading