Skip to content
Merged
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
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ rayon = { version = "1", optional = true }
arbitrary = { version = "1.0", optional = true }
bincode = {version = "2.0.1", optional = true, default-features=false, features = ["alloc", "std"]}
wide = "0.7"
equivalent = "1.0.2"

[dev-dependencies]
proptest = "1.0"
Expand Down
1 change: 1 addition & 0 deletions benches/hashmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ where
}

fn remove(&mut self, k: &K) -> Option<V> {
#[expect(clippy::if_same_then_else)]
if self.remove_mut(k) {
None // rpds doesn't return the removed value
} else {
Expand Down
8 changes: 5 additions & 3 deletions benches/ordmap.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use criterion::{criterion_group, criterion_main, Bencher, Criterion};
use equivalent::Comparable;
use imbl::ordmap::OrdMap;
use std::borrow::Borrow;
use std::collections::BTreeMap;
Expand Down Expand Up @@ -85,8 +86,7 @@ where

fn get<Q>(&self, k: &Q) -> Option<&V>
where
K: Borrow<Q>,
Q: Ord + ?Sized,
Q: Comparable<K> + ?Sized,
{
self.get(k)
}
Expand All @@ -96,7 +96,7 @@ where
}

fn range<'a>(&'a self, range: std::ops::RangeFrom<&'a K>) -> Self::RangeIter<'a> {
self.range(range)
self.range::<_, K>(range)
}

fn is_empty(&self) -> bool {
Expand Down Expand Up @@ -146,6 +146,7 @@ where
}

fn remove(&mut self, k: &K) -> Option<V> {
#[expect(clippy::if_same_then_else)]
if self.remove_mut(k) {
None // rpds doesn't return the removed value
} else {
Expand Down Expand Up @@ -585,6 +586,7 @@ where
});
}

#[expect(clippy::single_element_loop)]
for size in &[1000] {
group.bench_function(format!("remove_min_{}", size), |b| {
bench_remove_min::<M, K, V>(b, *size)
Expand Down
75 changes: 32 additions & 43 deletions src/hash/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use std::mem;
use std::ops::{Add, Index, IndexMut};

use archery::{SharedPointer, SharedPointerKind};
use equivalent::Equivalent;

use crate::nodes::hamt::{
hash_key, Drain as NodeDrain, HashBits, HashValue, Iter as NodeIter, IterMut as NodeIterMut,
Expand Down Expand Up @@ -520,10 +521,9 @@ where
/// );
/// ```
#[must_use]
pub fn get<BK>(&self, key: &BK) -> Option<&V>
pub fn get<Q>(&self, key: &Q) -> Option<&V>
where
BK: Hash + Eq + ?Sized,
K: Borrow<BK>,
Q: Hash + Equivalent<K> + ?Sized,
{
if let Some(root) = &self.root {
root.get(hash_key(&self.hasher, key), 0, key)
Expand All @@ -549,10 +549,9 @@ where
/// );
/// ```
#[must_use]
pub fn get_key_value<BK>(&self, key: &BK) -> Option<(&K, &V)>
pub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)>
where
BK: Hash + Eq + ?Sized,
K: Borrow<BK>,
Q: Hash + Equivalent<K> + ?Sized,
{
if let Some(root) = &self.root {
root.get(hash_key(&self.hasher, key), 0, key)
Expand Down Expand Up @@ -581,10 +580,9 @@ where
/// ```
#[inline]
#[must_use]
pub fn contains_key<BK>(&self, k: &BK) -> bool
pub fn contains_key<Q>(&self, k: &Q) -> bool
where
BK: Hash + Eq + ?Sized,
K: Borrow<BK>,
Q: Hash + Equivalent<K> + ?Sized,
{
self.get(k).is_some()
}
Expand Down Expand Up @@ -722,10 +720,9 @@ where
/// );
/// ```
#[must_use]
pub fn get_mut<BK>(&mut self, key: &BK) -> Option<&mut V>
pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
where
BK: Hash + Eq + ?Sized,
K: Borrow<BK>,
Q: Hash + Equivalent<K> + ?Sized,
{
self.get_key_value_mut(key).map(|(_, v)| v)
}
Expand All @@ -746,14 +743,11 @@ where
/// );
/// ```
#[must_use]
pub fn get_key_value_mut<BK>(&mut self, key: &BK) -> Option<(&K, &mut V)>
pub fn get_key_value_mut<Q>(&mut self, key: &Q) -> Option<(&K, &mut V)>
where
BK: Hash + Eq + ?Sized,
K: Borrow<BK>,
Q: Hash + Equivalent<K> + ?Sized,
{
let Some(root) = self.root.as_mut() else {
return None;
};
let root = self.root.as_mut()?;
match SharedPointer::make_mut(root).get_mut(hash_key(&self.hasher, key), 0, key) {
None => None,
Some((key, value)) => Some((key, value)),
Expand Down Expand Up @@ -811,10 +805,9 @@ where
/// assert_eq!(None, map.remove(&789));
/// assert!(map.is_empty());
/// ```
pub fn remove<BK>(&mut self, k: &BK) -> Option<V>
pub fn remove<Q>(&mut self, k: &Q) -> Option<V>
where
BK: Hash + Eq + ?Sized,
K: Borrow<BK>,
Q: Hash + Equivalent<K> + ?Sized,
{
self.remove_with_key(k).map(|(_, v)| v)
}
Expand All @@ -835,10 +828,9 @@ where
/// assert_eq!(None, map.remove_with_key(&789));
/// assert!(map.is_empty());
/// ```
pub fn remove_with_key<BK>(&mut self, k: &BK) -> Option<(K, V)>
pub fn remove_with_key<Q>(&mut self, k: &Q) -> Option<(K, V)>
where
BK: Hash + Eq + ?Sized,
K: Borrow<BK>,
Q: Hash + Equivalent<K> + ?Sized,
{
let Some(root) = &mut self.root else {
return None;
Expand Down Expand Up @@ -1001,10 +993,9 @@ where
///
/// Time: O(log n)
#[must_use]
pub fn without<BK>(&self, k: &BK) -> Self
pub fn without<Q>(&self, k: &Q) -> Self
where
BK: Hash + Eq + ?Sized,
K: Borrow<BK>,
Q: Hash + Equivalent<K> + ?Sized,
{
match self.extract_with_key(k) {
None => self.clone(),
Expand Down Expand Up @@ -1052,10 +1043,9 @@ where
///
/// Time: O(log n)
#[must_use]
pub fn extract<BK>(&self, k: &BK) -> Option<(V, Self)>
pub fn extract<Q>(&self, k: &Q) -> Option<(V, Self)>
where
BK: Hash + Eq + ?Sized,
K: Borrow<BK>,
Q: Hash + Equivalent<K> + ?Sized,
{
self.extract_with_key(k).map(|(_, v, m)| (v, m))
}
Expand All @@ -1065,10 +1055,9 @@ where
///
/// Time: O(log n)
#[must_use]
pub fn extract_with_key<BK>(&self, k: &BK) -> Option<(K, V, Self)>
pub fn extract_with_key<Q>(&self, k: &Q) -> Option<(K, V, Self)>
where
BK: Hash + Eq + ?Sized,
K: Borrow<BK>,
Q: Hash + Equivalent<K> + ?Sized,
{
let mut out = self.clone();
out.remove_with_key(k).map(|(k, v)| (k, v, out))
Expand Down Expand Up @@ -1852,32 +1841,32 @@ where
}
}

impl<BK, K, V, S, P> Index<&BK> for GenericHashMap<K, V, S, P>
impl<Q, K, V, S, P> Index<&Q> for GenericHashMap<K, V, S, P>
where
BK: Hash + Eq + ?Sized,
K: Hash + Eq + Borrow<BK>,
Q: Hash + Equivalent<K> + ?Sized,
K: Hash + Eq,
S: BuildHasher + Clone,
P: SharedPointerKind,
{
type Output = V;

fn index(&self, key: &BK) -> &Self::Output {
fn index(&self, key: &Q) -> &Self::Output {
match self.get(key) {
None => panic!("HashMap::index: invalid key"),
Some(value) => value,
}
}
}

impl<BK, K, V, S, P> IndexMut<&BK> for GenericHashMap<K, V, S, P>
impl<Q, K, V, S, P> IndexMut<&Q> for GenericHashMap<K, V, S, P>
where
BK: Hash + Eq + ?Sized,
K: Hash + Eq + Clone + Borrow<BK>,
Q: Hash + Equivalent<K> + ?Sized,
K: Hash + Eq + Clone,
V: Clone,
S: BuildHasher + Clone,
P: SharedPointerKind,
{
fn index_mut(&mut self, key: &BK) -> &mut Self::Output {
fn index_mut(&mut self, key: &Q) -> &mut Self::Output {
match self.get_mut(key) {
None => panic!("HashMap::index_mut: invalid key"),
Some(value) => value,
Expand Down Expand Up @@ -2112,9 +2101,9 @@ impl<K, V, S, P: SharedPointerKind> AsRef<GenericHashMap<K, V, S, P>>
impl<K, V, OK, OV, SA, SB, P1, P2> From<&GenericHashMap<&K, &V, SA, P1>>
for GenericHashMap<OK, OV, SB, P2>
where
K: Hash + Eq + ToOwned<Owned = OK> + ?Sized,
K: Hash + Equivalent<OK> + ToOwned<Owned = OK> + ?Sized,
V: ToOwned<Owned = OV> + ?Sized,
OK: Hash + Eq + Clone + Borrow<K>,
OK: Hash + Eq + Clone,
OV: Borrow<V> + Clone,
SA: BuildHasher + Clone,
SB: BuildHasher + Default + Clone,
Expand Down
28 changes: 13 additions & 15 deletions src/hash/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use std::iter::{FromIterator, FusedIterator, Sum};
use std::ops::{Add, Deref, Mul};

use archery::{SharedPointer, SharedPointerKind};
use equivalent::Equivalent;

use crate::nodes::hamt::{hash_key, Drain as NodeDrain, HashValue, Iter as NodeIter, Node};
use crate::ordset::GenericOrdSet;
Expand Down Expand Up @@ -321,13 +322,12 @@ where
///
/// Time: O(log n)
#[must_use]
pub fn contains<BA>(&self, a: &BA) -> bool
pub fn contains<Q>(&self, value: &Q) -> bool
where
BA: Hash + Eq + ?Sized,
A: Borrow<BA>,
Q: Hash + Equivalent<A> + ?Sized,
{
if let Some(root) = &self.root {
root.get(hash_key(&self.hasher, a), 0, a).is_some()
root.get(hash_key(&self.hasher, value), 0, value).is_some()
} else {
false
}
Expand Down Expand Up @@ -385,13 +385,12 @@ where
/// Remove a value from a set if it exists.
///
/// Time: O(log n)
pub fn remove<BA>(&mut self, a: &BA) -> Option<A>
pub fn remove<Q>(&mut self, value: &Q) -> Option<A>
where
BA: Hash + Eq + ?Sized,
A: Borrow<BA>,
Q: Hash + Equivalent<A> + ?Sized,
{
let root = SharedPointer::make_mut(self.root.get_or_insert_with(Default::default));
let result = root.remove(hash_key(&self.hasher, a), 0, a);
let result = root.remove(hash_key(&self.hasher, value), 0, value);
if result.is_some() {
self.size -= 1;
}
Expand Down Expand Up @@ -427,13 +426,12 @@ where
///
/// Time: O(log n)
#[must_use]
pub fn without<BA>(&self, a: &BA) -> Self
pub fn without<Q>(&self, value: &Q) -> Self
where
BA: Hash + Eq + ?Sized,
A: Borrow<BA>,
Q: Hash + Equivalent<A> + ?Sized,
{
let mut out = self.clone();
out.remove(a);
out.remove(value);
out
}

Expand Down Expand Up @@ -466,7 +464,7 @@ where
let old_root = root.clone();
let root = SharedPointer::make_mut(root);
for (value, hash) in NodeIter::new(Some(&old_root), self.size) {
if !f(value) && root.remove(hash, 0, value).is_some() {
if !f(value) && root.remove(hash, 0, &**value).is_some() {
self.size -= 1;
}
}
Expand Down Expand Up @@ -889,8 +887,8 @@ where

impl<A, OA, SA, SB, P1, P2> From<&GenericHashSet<&A, SA, P1>> for GenericHashSet<OA, SB, P2>
where
A: ToOwned<Owned = OA> + Hash + Eq + ?Sized,
OA: Borrow<A> + Hash + Eq + Clone,
A: ToOwned<Owned = OA> + Hash + Equivalent<A> + ?Sized,
OA: Hash + Eq + Clone,
SA: BuildHasher,
SB: BuildHasher + Default + Clone,
P1: SharedPointerKind,
Expand Down
Loading