use super::ebr::{AtomicShared, Guard, Shared, Tag};
use super::hash_table::bucket::{EntryPtr, Locker, Reader, SEQUENTIAL};
use super::hash_table::bucket_array::BucketArray;
use super::hash_table::{HashTable, LockedEntry};
use super::wait_queue::AsyncWait;
use super::Equivalent;
use std::collections::hash_map::RandomState;
use std::fmt::{self, Debug};
use std::hash::{BuildHasher, Hash};
use std::mem::replace;
use std::ops::{Deref, DerefMut, RangeInclusive};
use std::pin::Pin;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering::{Acquire, Relaxed};
pub struct HashMap<K, V, H = RandomState>
where
H: BuildHasher,
{
array: AtomicShared<BucketArray<K, V, (), SEQUENTIAL>>,
minimum_capacity: AtomicUsize,
build_hasher: H,
}
pub enum Entry<'h, K, V, H = RandomState>
where
H: BuildHasher,
{
Occupied(OccupiedEntry<'h, K, V, H>),
Vacant(VacantEntry<'h, K, V, H>),
}
pub struct OccupiedEntry<'h, K, V, H = RandomState>
where
H: BuildHasher,
{
hashmap: &'h HashMap<K, V, H>,
locked_entry: LockedEntry<'h, K, V, (), SEQUENTIAL>,
}
pub struct VacantEntry<'h, K, V, H = RandomState>
where
H: BuildHasher,
{
hashmap: &'h HashMap<K, V, H>,
key: K,
hash: u64,
locked_entry: LockedEntry<'h, K, V, (), SEQUENTIAL>,
}
pub struct Reserve<'h, K, V, H = RandomState>
where
K: Eq + Hash,
H: BuildHasher,
{
hashmap: &'h HashMap<K, V, H>,
additional: usize,
}
impl<K, V, H> HashMap<K, V, H>
where
H: BuildHasher,
{
#[cfg(not(feature = "loom"))]
#[inline]
pub const fn with_hasher(build_hasher: H) -> Self {
Self {
array: AtomicShared::null(),
minimum_capacity: AtomicUsize::new(0),
build_hasher,
}
}
#[cfg(feature = "loom")]
#[inline]
pub fn with_hasher(build_hasher: H) -> Self {
Self {
array: AtomicShared::null(),
minimum_capacity: AtomicUsize::new(0),
build_hasher,
}
}
#[inline]
pub fn with_capacity_and_hasher(capacity: usize, build_hasher: H) -> Self {
let (array, minimum_capacity) = if capacity == 0 {
(AtomicShared::null(), AtomicUsize::new(0))
} else {
let array = unsafe {
Shared::new_unchecked(BucketArray::<K, V, (), SEQUENTIAL>::new(
capacity,
AtomicShared::null(),
))
};
let minimum_capacity = array.num_entries();
(
AtomicShared::from(array),
AtomicUsize::new(minimum_capacity),
)
};
Self {
array,
minimum_capacity,
build_hasher,
}
}
}
impl<K, V, H> HashMap<K, V, H>
where
K: Eq + Hash,
H: BuildHasher,
{
#[inline]
pub fn reserve(&self, additional_capacity: usize) -> Option<Reserve<'_, K, V, H>> {
let additional = self.reserve_capacity(additional_capacity);
if additional == 0 {
None
} else {
Some(Reserve {
hashmap: self,
additional,
})
}
}
#[inline]
pub fn entry(&self, key: K) -> Entry<'_, K, V, H> {
let guard = Guard::new();
let hash = self.hash(&key);
let locked_entry = unsafe {
self.reserve_entry(&key, hash, &mut (), self.prolonged_guard_ref(&guard))
.ok()
.unwrap_unchecked()
};
if locked_entry.entry_ptr.is_valid() {
Entry::Occupied(OccupiedEntry {
hashmap: self,
locked_entry,
})
} else {
Entry::Vacant(VacantEntry {
hashmap: self,
key,
hash,
locked_entry,
})
}
}
#[inline]
pub async fn entry_async(&self, key: K) -> Entry<'_, K, V, H> {
let hash = self.hash(&key);
loop {
let mut async_wait = AsyncWait::default();
let mut async_wait_pinned = Pin::new(&mut async_wait);
{
let guard = Guard::new();
if let Ok(locked_entry) = self.reserve_entry(
&key,
hash,
&mut async_wait_pinned,
self.prolonged_guard_ref(&guard),
) {
if locked_entry.entry_ptr.is_valid() {
return Entry::Occupied(OccupiedEntry {
hashmap: self,
locked_entry,
});
}
return Entry::Vacant(VacantEntry {
hashmap: self,
key,
hash,
locked_entry,
});
}
}
async_wait_pinned.await;
}
}
#[inline]
pub fn try_entry(&self, key: K) -> Option<Entry<'_, K, V, H>> {
let guard = Guard::new();
let hash = self.hash(&key);
let locked_entry = self.try_reserve_entry(&key, hash, self.prolonged_guard_ref(&guard))?;
if locked_entry.entry_ptr.is_valid() {
Some(Entry::Occupied(OccupiedEntry {
hashmap: self,
locked_entry,
}))
} else {
Some(Entry::Vacant(VacantEntry {
hashmap: self,
key,
hash,
locked_entry,
}))
}
}
#[inline]
pub fn first_entry(&self) -> Option<OccupiedEntry<'_, K, V, H>> {
let guard = Guard::new();
let prolonged_guard = self.prolonged_guard_ref(&guard);
if let Some(locked_entry) = self.lock_first_entry(prolonged_guard) {
return Some(OccupiedEntry {
hashmap: self,
locked_entry,
});
}
None
}
#[inline]
pub async fn first_entry_async(&self) -> Option<OccupiedEntry<'_, K, V, H>> {
if let Some(locked_entry) = LockedEntry::first_entry_async(self).await {
return Some(OccupiedEntry {
hashmap: self,
locked_entry,
});
}
None
}
#[inline]
pub fn any_entry<P: FnMut(&K, &V) -> bool>(
&self,
pred: P,
) -> Option<OccupiedEntry<'_, K, V, H>> {
let guard = Guard::new();
let prolonged_guard = self.prolonged_guard_ref(&guard);
let locked_entry = self.find_entry(pred, prolonged_guard)?;
Some(OccupiedEntry {
hashmap: self,
locked_entry,
})
}
#[inline]
pub async fn any_entry_async<P: FnMut(&K, &V) -> bool>(
&self,
mut pred: P,
) -> Option<OccupiedEntry<'_, K, V, H>> {
if let Some(locked_entry) = LockedEntry::first_entry_async(self).await {
let mut entry = OccupiedEntry {
hashmap: self,
locked_entry,
};
loop {
if pred(entry.key(), entry.get()) {
return Some(entry);
}
entry = entry.next()?;
}
}
None
}
#[inline]
pub fn insert(&self, key: K, val: V) -> Result<(), (K, V)> {
let guard = Guard::new();
let hash = self.hash(&key);
if let Ok(Some((k, v))) = self.insert_entry(key, val, hash, &mut (), &guard) {
Err((k, v))
} else {
Ok(())
}
}
#[inline]
pub async fn insert_async(&self, mut key: K, mut val: V) -> Result<(), (K, V)> {
let hash = self.hash(&key);
loop {
let mut async_wait = AsyncWait::default();
let mut async_wait_pinned = Pin::new(&mut async_wait);
match self.insert_entry(key, val, hash, &mut async_wait_pinned, &Guard::new()) {
Ok(Some(returned)) => return Err(returned),
Ok(None) => return Ok(()),
Err(returned) => {
key = returned.0;
val = returned.1;
}
}
async_wait_pinned.await;
}
}
#[inline]
pub fn upsert(&self, key: K, val: V) -> Option<V> {
match self.entry(key) {
Entry::Occupied(mut o) => Some(replace(o.get_mut(), val)),
Entry::Vacant(v) => {
v.insert_entry(val);
None
}
}
}
#[inline]
pub async fn upsert_async(&self, key: K, val: V) -> Option<V> {
match self.entry_async(key).await {
Entry::Occupied(mut o) => Some(replace(o.get_mut(), val)),
Entry::Vacant(v) => {
v.insert_entry(val);
None
}
}
}
#[inline]
pub fn update<Q, U, R>(&self, key: &Q, updater: U) -> Option<R>
where
Q: Equivalent<K> + Hash + ?Sized,
U: FnOnce(&K, &mut V) -> R,
{
let guard = Guard::new();
let LockedEntry {
mut locker,
data_block_mut,
mut entry_ptr,
index: _,
} = self
.get_entry(key, self.hash(key), &mut (), &guard)
.ok()
.flatten()?;
let (k, v) = entry_ptr.get_mut(data_block_mut, &mut locker);
Some(updater(k, v))
}
#[inline]
pub async fn update_async<Q, U, R>(&self, key: &Q, updater: U) -> Option<R>
where
Q: Equivalent<K> + Hash + ?Sized,
U: FnOnce(&K, &mut V) -> R,
{
let hash = self.hash(key);
loop {
let mut async_wait = AsyncWait::default();
let mut async_wait_pinned = Pin::new(&mut async_wait);
if let Ok(result) = self.get_entry(key, hash, &mut async_wait_pinned, &Guard::new()) {
if let Some(LockedEntry {
mut locker,
data_block_mut,
mut entry_ptr,
index: _,
}) = result
{
let (k, v) = entry_ptr.get_mut(data_block_mut, &mut locker);
return Some(updater(k, v));
}
return None;
}
async_wait_pinned.await;
}
}
#[inline]
pub fn remove<Q>(&self, key: &Q) -> Option<(K, V)>
where
Q: Equivalent<K> + Hash + ?Sized,
{
self.remove_if(key, |_| true)
}
#[inline]
pub async fn remove_async<Q>(&self, key: &Q) -> Option<(K, V)>
where
Q: Equivalent<K> + Hash + ?Sized,
{
self.remove_if_async(key, |_| true).await
}
#[inline]
pub fn remove_if<Q, F: FnOnce(&mut V) -> bool>(&self, key: &Q, condition: F) -> Option<(K, V)>
where
Q: Equivalent<K> + Hash + ?Sized,
{
self.remove_entry(
key,
self.hash(key),
condition,
Option::flatten,
&mut (),
&Guard::new(),
)
.ok()
.flatten()
}
#[inline]
pub async fn remove_if_async<Q, F: FnOnce(&mut V) -> bool>(
&self,
key: &Q,
mut condition: F,
) -> Option<(K, V)>
where
Q: Equivalent<K> + Hash + ?Sized,
{
let hash = self.hash(key);
loop {
let mut async_wait = AsyncWait::default();
let mut async_wait_pinned = Pin::new(&mut async_wait);
match self.remove_entry(
key,
hash,
condition,
Option::flatten,
&mut async_wait_pinned,
&Guard::new(),
) {
Ok(r) => return r,
Err(c) => condition = c,
}
async_wait_pinned.await;
}
}
#[inline]
pub fn get<Q>(&self, key: &Q) -> Option<OccupiedEntry<'_, K, V, H>>
where
Q: Equivalent<K> + Hash + ?Sized,
{
let guard = Guard::new();
let locked_entry = self
.get_entry(
key,
self.hash(key),
&mut (),
self.prolonged_guard_ref(&guard),
)
.ok()
.flatten()?;
Some(OccupiedEntry {
hashmap: self,
locked_entry,
})
}
#[inline]
pub async fn get_async<Q>(&self, key: &Q) -> Option<OccupiedEntry<'_, K, V, H>>
where
Q: Equivalent<K> + Hash + ?Sized,
{
let hash = self.hash(key);
loop {
let mut async_wait = AsyncWait::default();
let mut async_wait_pinned = Pin::new(&mut async_wait);
if let Ok(result) = self.get_entry(
key,
hash,
&mut async_wait_pinned,
self.prolonged_guard_ref(&Guard::new()),
) {
if let Some(locked_entry) = result {
return Some(OccupiedEntry {
hashmap: self,
locked_entry,
});
}
return None;
}
async_wait_pinned.await;
}
}
#[inline]
pub fn read<Q, R, F: FnOnce(&K, &V) -> R>(&self, key: &Q, reader: F) -> Option<R>
where
Q: Equivalent<K> + Hash + ?Sized,
{
self.read_entry(key, self.hash(key), reader, &mut (), &Guard::new())
.ok()
.flatten()
}
#[inline]
pub async fn read_async<Q, R, F: FnOnce(&K, &V) -> R>(
&self,
key: &Q,
mut reader: F,
) -> Option<R>
where
Q: Equivalent<K> + Hash + ?Sized,
{
let hash = self.hash(key);
loop {
let mut async_wait = AsyncWait::default();
let mut async_wait_pinned = Pin::new(&mut async_wait);
match self.read_entry(key, hash, reader, &mut async_wait_pinned, &Guard::new()) {
Ok(result) => return result,
Err(f) => reader = f,
}
async_wait_pinned.await;
}
}
#[inline]
pub fn contains<Q>(&self, key: &Q) -> bool
where
Q: Equivalent<K> + Hash + ?Sized,
{
self.read(key, |_, _| ()).is_some()
}
#[inline]
pub async fn contains_async<Q>(&self, key: &Q) -> bool
where
Q: Equivalent<K> + Hash + ?Sized,
{
self.read_async(key, |_, _| ()).await.is_some()
}
#[inline]
pub fn scan<F: FnMut(&K, &V)>(&self, mut scanner: F) {
self.any(|k, v| {
scanner(k, v);
false
});
}
#[inline]
pub async fn scan_async<F: FnMut(&K, &V)>(&self, mut scanner: F) {
self.any_async(|k, v| {
scanner(k, v);
false
})
.await;
}
#[inline]
pub fn any<P: FnMut(&K, &V) -> bool>(&self, pred: P) -> bool {
self.contains_entry(pred)
}
#[inline]
pub async fn any_async<P: FnMut(&K, &V) -> bool>(&self, mut pred: P) -> bool {
let mut current_array_holder = self.array.get_shared(Acquire, &Guard::new());
while let Some(current_array) = current_array_holder.take() {
self.cleanse_old_array_async(¤t_array).await;
for index in 0..current_array.num_buckets() {
loop {
let mut async_wait = AsyncWait::default();
let mut async_wait_pinned = Pin::new(&mut async_wait);
{
let guard = Guard::new();
let bucket = current_array.bucket(index);
if let Ok(reader) =
Reader::try_lock_or_wait(bucket, &mut async_wait_pinned, &guard)
{
if let Some(reader) = reader {
let data_block = current_array.data_block(index);
let mut entry_ptr = EntryPtr::new(&guard);
while entry_ptr.move_to_next(*reader, &guard) {
let (k, v) = entry_ptr.get(data_block);
if pred(k, v) {
return true;
}
}
}
break;
};
}
async_wait_pinned.await;
}
}
if let Some(new_current_array) = self.array.get_shared(Acquire, &Guard::new()) {
if new_current_array.as_ptr() == current_array.as_ptr() {
break;
}
current_array_holder.replace(new_current_array);
continue;
}
break;
}
false
}
#[inline]
pub fn retain<F: FnMut(&K, &mut V) -> bool>(&self, pred: F) {
self.retain_entries(pred);
}
#[inline]
pub async fn retain_async<F: FnMut(&K, &mut V) -> bool>(&self, mut pred: F) {
let mut removed = false;
let mut current_array_holder = self.array.get_shared(Acquire, &Guard::new());
while let Some(current_array) = current_array_holder.take() {
self.cleanse_old_array_async(¤t_array).await;
for index in 0..current_array.num_buckets() {
loop {
let mut async_wait = AsyncWait::default();
let mut async_wait_pinned = Pin::new(&mut async_wait);
{
let guard = Guard::new();
let bucket = current_array.bucket_mut(index);
if let Ok(locker) =
Locker::try_lock_or_wait(bucket, &mut async_wait_pinned, &guard)
{
if let Some(mut locker) = locker {
let data_block_mut = current_array.data_block_mut(index);
let mut entry_ptr = EntryPtr::new(&guard);
while entry_ptr.move_to_next(&locker, &guard) {
let (k, v) = entry_ptr.get_mut(data_block_mut, &mut locker);
if !pred(k, v) {
locker.remove(data_block_mut, &mut entry_ptr, &guard);
removed = true;
}
}
}
break;
};
}
async_wait_pinned.await;
}
}
if let Some(new_current_array) = self.array.get_shared(Acquire, &Guard::new()) {
if new_current_array.as_ptr() == current_array.as_ptr() {
break;
}
current_array_holder.replace(new_current_array);
continue;
}
break;
}
if removed {
self.try_resize(0, &Guard::new());
}
}
#[inline]
pub fn prune<F: FnMut(&K, V) -> Option<V>>(&self, pred: F) {
self.prune_entries(pred);
}
#[inline]
pub async fn prune_async<F: FnMut(&K, V) -> Option<V>>(&self, mut pred: F) {
let mut removed = false;
let mut current_array_holder = self.array.get_shared(Acquire, &Guard::new());
while let Some(current_array) = current_array_holder.take() {
self.cleanse_old_array_async(¤t_array).await;
for index in 0..current_array.num_buckets() {
loop {
let mut async_wait = AsyncWait::default();
let mut async_wait_pinned = Pin::new(&mut async_wait);
{
let guard = Guard::new();
let bucket = current_array.bucket_mut(index);
if let Ok(locker) =
Locker::try_lock_or_wait(bucket, &mut async_wait_pinned, &guard)
{
if let Some(mut locker) = locker {
let data_block_mut = current_array.data_block_mut(index);
let mut entry_ptr = EntryPtr::new(&guard);
while entry_ptr.move_to_next(&locker, &guard) {
if locker.keep_or_consume(
data_block_mut,
&mut entry_ptr,
&mut pred,
&guard,
) {
removed = true;
}
}
}
break;
};
}
async_wait_pinned.await;
}
}
if let Some(new_current_array) = self.array.get_shared(Acquire, &Guard::new()) {
if new_current_array.as_ptr() == current_array.as_ptr() {
break;
}
current_array_holder.replace(new_current_array);
continue;
}
break;
}
if removed {
self.try_resize(0, &Guard::new());
}
}
#[inline]
pub fn clear(&self) {
self.retain(|_, _| false);
}
#[inline]
pub async fn clear_async(&self) {
self.retain_async(|_, _| false).await;
}
#[inline]
pub fn len(&self) -> usize {
self.num_entries(&Guard::new())
}
#[inline]
pub fn is_empty(&self) -> bool {
!self.has_entry(&Guard::new())
}
#[inline]
pub fn capacity(&self) -> usize {
self.num_slots(&Guard::new())
}
#[inline]
pub fn capacity_range(&self) -> RangeInclusive<usize> {
self.minimum_capacity.load(Relaxed)..=self.maximum_capacity()
}
#[inline]
pub fn bucket_index<Q>(&self, key: &Q) -> usize
where
Q: Equivalent<K> + Hash + ?Sized,
{
self.calculate_bucket_index(key)
}
async fn cleanse_old_array_async(&self, current_array: &BucketArray<K, V, (), SEQUENTIAL>) {
while current_array.has_old_array() {
let mut async_wait = AsyncWait::default();
let mut async_wait_pinned = Pin::new(&mut async_wait);
if self.incremental_rehash::<K, _, false>(
current_array,
&mut async_wait_pinned,
&Guard::new(),
) == Ok(true)
{
break;
}
async_wait_pinned.await;
}
}
}
impl<K, V> HashMap<K, V, RandomState>
where
K: Eq + Hash,
{
#[inline]
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[inline]
#[must_use]
pub fn with_capacity(capacity: usize) -> Self {
Self::with_capacity_and_hasher(capacity, RandomState::new())
}
}
impl<K, V, H> Clone for HashMap<K, V, H>
where
K: Clone + Eq + Hash,
V: Clone,
H: BuildHasher + Clone,
{
#[inline]
fn clone(&self) -> Self {
let self_clone = Self::with_capacity_and_hasher(self.capacity(), self.hasher().clone());
self.scan(|k, v| {
let _result = self_clone.insert(k.clone(), v.clone());
});
self_clone
}
}
impl<K, V, H> Debug for HashMap<K, V, H>
where
K: Debug + Eq + Hash,
V: Debug,
H: BuildHasher,
{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut d = f.debug_map();
self.scan(|k, v| {
d.entry(k, v);
});
d.finish()
}
}
impl<K, V, H> Default for HashMap<K, V, H>
where
H: BuildHasher + Default,
{
#[inline]
fn default() -> Self {
Self::with_hasher(H::default())
}
}
impl<K, V, H> Drop for HashMap<K, V, H>
where
H: BuildHasher,
{
#[inline]
fn drop(&mut self) {
self.array
.swap((None, Tag::None), Relaxed)
.0
.map(|a| unsafe {
a.drop_in_place()
});
}
}
impl<K, V, H> FromIterator<(K, V)> for HashMap<K, V, H>
where
K: Eq + Hash,
H: BuildHasher + Default,
{
#[inline]
fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self {
let into_iter = iter.into_iter();
let hashmap = Self::with_capacity_and_hasher(
Self::capacity_from_size_hint(into_iter.size_hint()),
H::default(),
);
into_iter.for_each(|e| {
hashmap.upsert(e.0, e.1);
});
hashmap
}
}
impl<K, V, H> HashTable<K, V, H, (), SEQUENTIAL> for HashMap<K, V, H>
where
K: Eq + Hash,
H: BuildHasher,
{
#[inline]
fn hasher(&self) -> &H {
&self.build_hasher
}
#[inline]
fn try_clone(_: &(K, V)) -> Option<(K, V)> {
None
}
#[inline]
fn bucket_array(&self) -> &AtomicShared<BucketArray<K, V, (), SEQUENTIAL>> {
&self.array
}
#[inline]
fn minimum_capacity(&self) -> &AtomicUsize {
&self.minimum_capacity
}
#[inline]
fn maximum_capacity(&self) -> usize {
1_usize << (usize::BITS - 1)
}
}
impl<K, V, H> PartialEq for HashMap<K, V, H>
where
K: Eq + Hash,
V: PartialEq,
H: BuildHasher,
{
#[inline]
fn eq(&self, other: &Self) -> bool {
if !self.any(|k, v| other.read(k, |_, ov| v == ov) != Some(true)) {
return !other.any(|k, v| self.read(k, |_, sv| v == sv) != Some(true));
}
false
}
}
impl<'h, K, V, H> Entry<'h, K, V, H>
where
K: Eq + Hash,
H: BuildHasher,
{
#[inline]
pub fn or_insert(self, val: V) -> OccupiedEntry<'h, K, V, H> {
self.or_insert_with(|| val)
}
#[inline]
pub fn or_insert_with<F: FnOnce() -> V>(self, constructor: F) -> OccupiedEntry<'h, K, V, H> {
self.or_insert_with_key(|_| constructor())
}
#[inline]
pub fn or_insert_with_key<F: FnOnce(&K) -> V>(
self,
constructor: F,
) -> OccupiedEntry<'h, K, V, H> {
match self {
Self::Occupied(o) => o,
Self::Vacant(v) => {
let val = constructor(v.key());
v.insert_entry(val)
}
}
}
#[inline]
pub fn key(&self) -> &K {
match self {
Self::Occupied(o) => o.key(),
Self::Vacant(v) => v.key(),
}
}
#[inline]
#[must_use]
pub fn and_modify<F>(self, f: F) -> Self
where
F: FnOnce(&mut V),
{
match self {
Self::Occupied(mut o) => {
f(o.get_mut());
Self::Occupied(o)
}
Self::Vacant(_) => self,
}
}
#[inline]
pub fn insert_entry(self, val: V) -> OccupiedEntry<'h, K, V, H> {
match self {
Self::Occupied(mut o) => {
o.insert(val);
o
}
Self::Vacant(v) => v.insert_entry(val),
}
}
}
impl<'h, K, V, H> Entry<'h, K, V, H>
where
K: Eq + Hash,
V: Default,
H: BuildHasher,
{
#[inline]
pub fn or_default(self) -> OccupiedEntry<'h, K, V, H> {
match self {
Self::Occupied(o) => o,
Self::Vacant(v) => v.insert_entry(Default::default()),
}
}
}
impl<K, V, H> Debug for Entry<'_, K, V, H>
where
K: Debug + Eq + Hash,
V: Debug,
H: BuildHasher,
{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Vacant(v) => f.debug_tuple("Entry").field(v).finish(),
Self::Occupied(o) => f.debug_tuple("Entry").field(o).finish(),
}
}
}
impl<'h, K, V, H> OccupiedEntry<'h, K, V, H>
where
K: Eq + Hash,
H: BuildHasher,
{
#[inline]
#[must_use]
pub fn key(&self) -> &K {
&self
.locked_entry
.entry_ptr
.get(self.locked_entry.data_block_mut)
.0
}
#[inline]
#[must_use]
pub fn remove_entry(mut self) -> (K, V) {
let guard = Guard::new();
let entry = self.locked_entry.locker.remove(
self.locked_entry.data_block_mut,
&mut self.locked_entry.entry_ptr,
self.hashmap.prolonged_guard_ref(&guard),
);
if self.locked_entry.locker.num_entries() <= 1 || self.locked_entry.locker.need_rebuild() {
let hashmap = self.hashmap;
if let Some(current_array) = hashmap.bucket_array().load(Acquire, &guard).as_ref() {
if !current_array.has_old_array() {
let index = self.locked_entry.index;
if current_array.initiate_sampling(index) {
drop(self);
hashmap.try_shrink_or_rebuild(current_array, index, &guard);
}
}
}
}
entry
}
#[inline]
#[must_use]
pub fn get(&self) -> &V {
&self
.locked_entry
.entry_ptr
.get(self.locked_entry.data_block_mut)
.1
}
#[inline]
pub fn get_mut(&mut self) -> &mut V {
&mut self
.locked_entry
.entry_ptr
.get_mut(
self.locked_entry.data_block_mut,
&mut self.locked_entry.locker,
)
.1
}
#[inline]
pub fn insert(&mut self, val: V) -> V {
replace(self.get_mut(), val)
}
#[inline]
#[must_use]
pub fn remove(self) -> V {
self.remove_entry().1
}
#[inline]
#[must_use]
pub fn next(self) -> Option<Self> {
let hashmap = self.hashmap;
if let Some(locked_entry) = self.locked_entry.next(hashmap) {
return Some(OccupiedEntry {
hashmap,
locked_entry,
});
}
None
}
#[inline]
pub async fn next_async(self) -> Option<OccupiedEntry<'h, K, V, H>> {
let hashmap = self.hashmap;
if let Some(locked_entry) = self.locked_entry.next_async(hashmap).await {
return Some(OccupiedEntry {
hashmap,
locked_entry,
});
}
None
}
}
impl<K, V, H> Debug for OccupiedEntry<'_, K, V, H>
where
K: Debug + Eq + Hash,
V: Debug,
H: BuildHasher,
{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("OccupiedEntry")
.field("key", self.key())
.field("value", self.get())
.finish_non_exhaustive()
}
}
impl<K, V, H> Deref for OccupiedEntry<'_, K, V, H>
where
K: Eq + Hash,
H: BuildHasher,
{
type Target = V;
#[inline]
fn deref(&self) -> &Self::Target {
self.get()
}
}
impl<K, V, H> DerefMut for OccupiedEntry<'_, K, V, H>
where
K: Eq + Hash,
H: BuildHasher,
{
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
self.get_mut()
}
}
impl<'h, K, V, H> VacantEntry<'h, K, V, H>
where
K: Eq + Hash,
H: BuildHasher,
{
#[inline]
pub fn key(&self) -> &K {
&self.key
}
#[inline]
pub fn into_key(self) -> K {
self.key
}
#[inline]
pub fn insert_entry(mut self, val: V) -> OccupiedEntry<'h, K, V, H> {
let guard = Guard::new();
let entry_ptr = self.locked_entry.locker.insert_with(
self.locked_entry.data_block_mut,
BucketArray::<K, V, (), SEQUENTIAL>::partial_hash(self.hash),
|| (self.key, val),
self.hashmap.prolonged_guard_ref(&guard),
);
OccupiedEntry {
hashmap: self.hashmap,
locked_entry: LockedEntry {
index: self.locked_entry.index,
data_block_mut: self.locked_entry.data_block_mut,
locker: self.locked_entry.locker,
entry_ptr,
},
}
}
}
impl<K, V, H> Debug for VacantEntry<'_, K, V, H>
where
K: Debug + Eq + Hash,
V: Debug,
H: BuildHasher,
{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("VacantEntry").field(self.key()).finish()
}
}
impl<K, V, H> Reserve<'_, K, V, H>
where
K: Eq + Hash,
H: BuildHasher,
{
#[inline]
#[must_use]
pub fn additional_capacity(&self) -> usize {
self.additional
}
}
impl<K, V, H> AsRef<HashMap<K, V, H>> for Reserve<'_, K, V, H>
where
K: Eq + Hash,
H: BuildHasher,
{
#[inline]
fn as_ref(&self) -> &HashMap<K, V, H> {
self.hashmap
}
}
impl<K, V, H> Debug for Reserve<'_, K, V, H>
where
K: Eq + Hash,
H: BuildHasher,
{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Reserve").field(&self.additional).finish()
}
}
impl<K, V, H> Deref for Reserve<'_, K, V, H>
where
K: Eq + Hash,
H: BuildHasher,
{
type Target = HashMap<K, V, H>;
#[inline]
fn deref(&self) -> &Self::Target {
self.hashmap
}
}
impl<K, V, H> Drop for Reserve<'_, K, V, H>
where
K: Eq + Hash,
H: BuildHasher,
{
#[inline]
fn drop(&mut self) {
let result = self
.hashmap
.minimum_capacity
.fetch_sub(self.additional, Relaxed);
self.hashmap.try_resize(0, &Guard::new());
debug_assert!(result >= self.additional);
}
}