extern crate alloc;
use super::table::{Entry, Table, Walk, Word};
use super::{HopscotchMap, NEIGHBORHOOD_SIZE};
use alloc::boxed::Box;
use core::hash::{BuildHasher, Hash};
use core::marker::PhantomData;
use core::sync::atomic::Ordering;
use kovan::pin;
impl<K: Eq + 'static, V: 'static, S> HopscotchMap<K, V, S> {
pub fn iter(&self) -> HopscotchIter<'_, K, V, S> {
let guard = pin();
let table = self.table.load(Ordering::Acquire, &guard).as_raw();
let current = unsafe { &*table };
let (slots, mask) = (current.buckets.len(), current.home_mask());
HopscotchIter {
table,
slots,
mask,
bucket_idx: 0,
recent: [core::ptr::null(); NEIGHBORHOOD_SIZE],
recent_hash: [0; NEIGHBORHOOD_SIZE],
same_key: <K as PartialEq>::eq,
guard,
_map: PhantomData,
}
}
pub fn keys(&self) -> HopscotchKeys<'_, K, V, S> {
HopscotchKeys { iter: self.iter() }
}
pub fn values(&self) -> HopscotchValues<'_, K, V, S> {
HopscotchValues { iter: self.iter() }
}
}
pub struct HopscotchIter<'a, K: 'static, V: 'static, S> {
table: *const Table<K, V>,
slots: usize,
mask: usize,
bucket_idx: usize,
recent: [*const Entry<K, V>; NEIGHBORHOOD_SIZE],
recent_hash: [u64; NEIGHBORHOOD_SIZE],
same_key: fn(&K, &K) -> bool,
guard: kovan::Guard,
_map: PhantomData<&'a HopscotchMap<K, V, S>>,
}
impl<K, V, S> HopscotchIter<'_, K, V, S> {
fn met_before(&self, home: usize, idx: usize, entry: &Entry<K, V>) -> bool {
let lowest = home.max(idx.saturating_sub(NEIGHBORHOOD_SIZE - 1));
(lowest..idx).any(|seen_idx| {
let slot = seen_idx % NEIGHBORHOOD_SIZE;
if self.recent_hash[slot] != entry.hash {
return false;
}
let seen = self.recent[slot];
unsafe { seen.as_ref() }.is_some_and(|seen| {
core::ptr::eq(seen, entry) || (self.same_key)(&seen.key, &entry.key)
})
})
}
}
impl<'a, K, V, S> Iterator for HopscotchIter<'a, K, V, S>
where
K: Clone,
V: Clone,
{
type Item = (K, V);
fn next(&mut self) -> Option<Self::Item> {
let table = unsafe { &*self.table };
while self.bucket_idx < self.slots {
let idx = self.bucket_idx;
self.bucket_idx += 1;
let entry_ptr: *const Entry<K, V> = table
.get_bucket(idx)
.load(Ordering::Acquire, &self.guard)
.ptr();
let Some(entry) = (unsafe { entry_ptr.as_ref() }) else {
continue;
};
self.recent[idx % NEIGHBORHOOD_SIZE] = entry_ptr;
self.recent_hash[idx % NEIGHBORHOOD_SIZE] = entry.hash;
if !self.met_before((entry.hash as usize) & self.mask, idx, entry) {
return Some((entry.key.clone(), entry.value.clone()));
}
}
None
}
}
pub struct HopscotchKeys<'a, K: 'static, V: 'static, S> {
iter: HopscotchIter<'a, K, V, S>,
}
impl<'a, K, V, S> Iterator for HopscotchKeys<'a, K, V, S>
where
K: Clone,
V: Clone,
{
type Item = K;
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map(|(k, _)| k)
}
}
pub struct HopscotchValues<'a, K: 'static, V: 'static, S> {
iter: HopscotchIter<'a, K, V, S>,
}
impl<'a, K, V, S> Iterator for HopscotchValues<'a, K, V, S>
where
K: Clone,
V: Clone,
{
type Item = V;
#[inline]
fn next(&mut self) -> Option<V> {
self.iter.next().map(|(_, v)| v)
}
}
pub struct HopscotchIntoIter<K: 'static, V: 'static> {
table: *mut Table<K, V>,
walk: Walk<K, V>,
guard: kovan::Guard,
}
impl<K, V> Iterator for HopscotchIntoIter<K, V> {
type Item = (K, V);
fn next(&mut self) -> Option<(K, V)> {
let table = unsafe { &*self.table };
while let Some((idx, entry)) = self.walk.next(table, &self.guard) {
if !entry.is_null() {
table.get_bucket(idx).store(Word::free(), Ordering::Relaxed);
let k = unsafe { core::ptr::read(&(*entry).key) };
let v = unsafe { core::ptr::read(&(*entry).value) };
unsafe {
alloc::alloc::dealloc(
entry as *mut u8,
core::alloc::Layout::new::<Entry<K, V>>(),
);
}
return Some((k, v));
}
}
None
}
}
impl<K, V> Drop for HopscotchIntoIter<K, V> {
fn drop(&mut self) {
while self.next().is_some() {}
unsafe { drop(Box::from_raw(self.table)) };
}
}
impl<K, V, S> IntoIterator for HopscotchMap<K, V, S>
where
K: 'static,
V: 'static,
{
type Item = (K, V);
type IntoIter = HopscotchIntoIter<K, V>;
fn into_iter(self) -> HopscotchIntoIter<K, V> {
let mut me = core::mem::ManuallyDrop::new(self);
let guard = pin();
let table = me.table.load(Ordering::Relaxed, &guard).as_raw();
unsafe { core::ptr::drop_in_place(&mut me.hasher) };
let walk = Walk::new(unsafe { &*table }, &guard);
HopscotchIntoIter { table, walk, guard }
}
}
impl<K, V, S> core::iter::FromIterator<(K, V)> for HopscotchMap<K, V, S>
where
K: Hash + Eq + Clone + Send + 'static,
V: Clone + Send + 'static,
S: BuildHasher + Default,
{
fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self {
let map = Self::with_hasher(S::default());
for (k, v) in iter {
map.insert(k, v);
}
map
}
}
impl<'a, K, V, S> IntoIterator for &'a HopscotchMap<K, V, S>
where
K: Eq + Clone + 'static,
V: Clone + 'static,
{
type Item = (K, V);
type IntoIter = HopscotchIter<'a, K, V, S>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}