Skip to content

Commit

Permalink
migrate read_only and set
Browse files Browse the repository at this point in the history
  • Loading branch information
xacrimon committed Feb 13, 2025
1 parent 633e094 commit 48dff88
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
13 changes: 5 additions & 8 deletions src/read_only.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::lock::RwLock;
use crate::{DashMap, HashMap};
use cfg_if::cfg_if;
use core::borrow::Borrow;
use core::fmt;
use core::hash::{BuildHasher, Hash};
use crossbeam_utils::CachePadded;
use equivalent::Equivalent;
use std::collections::hash_map::RandomState;

/// A read-only view into a `DashMap`. Allows to obtain raw references to the stored values.
Expand Down Expand Up @@ -58,26 +58,23 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: BuildHasher + Clone> ReadOnlyView<K, V, S>
/// Returns `true` if the map contains a value for the specified key.
pub fn contains_key<Q>(&'a self, key: &Q) -> bool
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
Q: Hash + Equivalent<K> + ?Sized,
{
self.get(key).is_some()
}

/// Returns a reference to the value corresponding to the key.
pub fn get<Q>(&'a self, key: &Q) -> Option<&'a V>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
Q: Hash + Equivalent<K> + ?Sized,
{
self.get_key_value(key).map(|(_k, v)| v)
}

/// Returns the key-value pair corresponding to the supplied key.
pub fn get_key_value<Q>(&'a self, key: &Q) -> Option<(&'a K, &'a V)>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
Q: Hash + Equivalent<K> + ?Sized,
{
let hash = self.map.hash_u64(&key);

Expand All @@ -87,7 +84,7 @@ impl<'a, K: 'a + Eq + Hash, V: 'a, S: BuildHasher + Clone> ReadOnlyView<K, V, S>
let shard = unsafe { &*shard.data_ptr() };

shard
.find(hash, |(k, _v)| key == k.borrow())
.find(hash, |(k, _v)| key.equivalent(k))
.map(|(k, v)| (k, v))
}

Expand Down
17 changes: 6 additions & 11 deletions src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ use crate::DashMap;
#[cfg(feature = "raw-api")]
use crate::HashMap;
use cfg_if::cfg_if;
use core::borrow::Borrow;
use core::fmt;
use core::hash::{BuildHasher, Hash};
use core::iter::FromIterator;
#[cfg(feature = "raw-api")]
use crossbeam_utils::CachePadded;
use equivalent::Equivalent;
use std::collections::hash_map::RandomState;

/// DashSet is a thin wrapper around [`DashMap`] using `()` as the value type. It uses
Expand Down Expand Up @@ -163,8 +163,7 @@ impl<'a, K: 'a + Eq + Hash, S: BuildHasher + Clone> DashSet<K, S> {
/// ```
pub fn determine_map<Q>(&self, key: &Q) -> usize
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
Q: Hash + Equivalent<K> + ?Sized,
{
self.inner.determine_map(key)
}
Expand Down Expand Up @@ -220,8 +219,7 @@ impl<'a, K: 'a + Eq + Hash, S: BuildHasher + Clone> DashSet<K, S> {
/// ```
pub fn remove<Q>(&self, key: &Q) -> Option<K>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
Q: Hash + Equivalent<K> + ?Sized,
{
self.inner.remove(key).map(|(k, _)| k)
}
Expand All @@ -247,8 +245,7 @@ impl<'a, K: 'a + Eq + Hash, S: BuildHasher + Clone> DashSet<K, S> {
/// ```
pub fn remove_if<Q>(&self, key: &Q, f: impl FnOnce(&K) -> bool) -> Option<K>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
Q: Hash + Equivalent<K> + ?Sized,
{
// TODO: Don't create another closure around f
self.inner.remove_if(key, |k, _| f(k)).map(|(k, _)| k)
Expand Down Expand Up @@ -284,8 +281,7 @@ impl<'a, K: 'a + Eq + Hash, S: BuildHasher + Clone> DashSet<K, S> {
/// ```
pub fn get<Q>(&'a self, key: &Q) -> Option<Ref<'a, K>>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
Q: Hash + Equivalent<K> + ?Sized,
{
self.inner.get(key).map(Ref::new)
}
Expand Down Expand Up @@ -380,8 +376,7 @@ impl<'a, K: 'a + Eq + Hash, S: BuildHasher + Clone> DashSet<K, S> {
/// ```
pub fn contains<Q>(&self, key: &Q) -> bool
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
Q: Hash + Equivalent<K> + ?Sized,
{
self.inner.contains_key(key)
}
Expand Down

0 comments on commit 48dff88

Please sign in to comment.