use std::convert::Infallible;
use crate::depot::map;
pub(in crate::depot::map) unsafe trait Directory {
fn buckets(&self) -> usize;
unsafe fn hash(&self, index: usize) -> Option<u64>;
unsafe fn candidate(&self, index: usize, hash: u64) -> Candidate {
match unsafe { self.hash(index) } {
None => Candidate::Vacant,
Some(actual) => {
if actual == hash {
Candidate::Occupied
} else {
Candidate::Skip
}
}
}
}
}
pub(in crate::depot::map) enum Candidate {
Occupied,
Vacant,
Skip,
}
pub(in crate::depot::map) unsafe trait Shift: Directory {
unsafe fn shift(&mut self, hole: &map::raw::Vacant, next: &map::raw::Occupied, hash: u64);
}
unsafe impl<P> Directory for map::raw::Table<P> {
fn buckets(&self) -> usize {
self.buckets
}
unsafe fn hash(&self, index: usize) -> Option<u64> {
if unsafe { *self.controls.as_ptr().add(index) } == 1 << 7 {
None
} else {
Some(unsafe { (*self.hashes.as_ptr().add(index)).assume_init() })
}
}
unsafe fn candidate(&self, index: usize, hash: u64) -> Candidate {
let control = unsafe { *self.controls.as_ptr().add(index) };
if control == 1 << 7 {
Candidate::Vacant
} else if control == (hash >> 57) as u8
&& unsafe { (*self.hashes.as_ptr().add(index)).assume_init() } == hash
{
Candidate::Occupied
} else {
Candidate::Skip
}
}
}
pub(crate) unsafe fn spare<Q: map::raw::Query>(
owner: Q,
hash: u64,
matches: impl Fn(&Q::Value) -> bool,
) -> Result<map::Vacancy<Q, u64>, Q::Occupied> {
let table = owner.table();
let entry = unsafe {
super::scan_spare(table, hash, table.buckets - 1, |bucket| {
Ok::<_, Infallible>(matches(Q::value(map::Found {
owner: &owner,
bucket,
})))
})
}
.unwrap_or_else(|never| match never {});
match entry {
map::Entry::Occupied(bucket) => {
drop(matches);
Err(Q::occupied(map::Found { owner, bucket }))
}
map::Entry::Vacant(bucket) => Ok(map::Vacancy {
owner,
bucket,
phase: hash,
}),
}
}
fn query<Q: map::raw::Query, E>(
owner: &Q,
hash: u64,
matches: impl Fn(&Q::Value) -> Result<bool, E>,
) -> Result<Option<super::Bucket>, E> {
let matches = |bucket| matches(Q::value(map::Found { owner, bucket }));
if Q::NONEMPTY && Q::SPARE {
let table = owner.table();
return unsafe { super::scan_spare(table, hash, table.buckets - 1, matches) }.map(Some);
}
if Q::SPARE {
super::scan::<_, _, true>(owner.table(), hash, matches)
} else {
super::scan::<_, _, false>(owner.table(), hash, matches)
}
}
pub fn find<Q: map::raw::Query>(
owner: Q,
hash: u64,
matches: impl Fn(&Q::Value) -> bool,
) -> Option<Q::Occupied> {
try_find(owner, hash, move |value| {
Ok::<_, Infallible>(matches(value))
})
.unwrap_or_else(|never| match never {})
}
pub(crate) fn try_find<Q: map::raw::Query, E>(
owner: Q,
hash: u64,
matches: impl Fn(&Q::Value) -> Result<bool, E>,
) -> Result<Option<Q::Occupied>, E> {
let Some(map::Entry::Occupied(bucket)) = query(&owner, hash, &matches)? else {
return Ok(None);
};
Ok(Some(Q::occupied(map::Found { owner, bucket })))
}
pub(crate) fn entry<Q: map::raw::Insertion>(
owner: Q,
hash: u64,
matches: impl Fn(&Q::Value) -> bool,
) -> map::Entry<Q::Occupied, Option<Q::Vacant>> {
try_entry(owner, hash, move |value| {
Ok::<_, Infallible>(matches(value))
})
.unwrap_or_else(|never| match never {})
}
pub(crate) fn try_entry<Q: map::raw::Insertion, E>(
owner: Q,
hash: u64,
matches: impl Fn(&Q::Value) -> Result<bool, E>,
) -> Result<map::Entry<Q::Occupied, Option<Q::Vacant>>, E> {
let entry = query(&owner, hash, &matches)?;
drop(matches);
Ok(match entry {
Some(map::Entry::Occupied(bucket)) => {
map::Entry::Occupied(Q::occupied(map::Found { owner, bucket }))
}
Some(map::Entry::Vacant(bucket)) => map::Entry::Vacant(bucket.admit(owner, hash)),
None => map::Entry::Vacant(None),
})
}
pub fn reserve<Q: map::raw::Insertion>(
owner: Q,
hash: u64,
matches: impl Fn(&Q::Value) -> bool,
) -> map::Entry<(), Option<Q::Vacant>> {
let entry = query(&owner, hash, |value| Ok::<_, Infallible>(matches(value)))
.unwrap_or_else(|never| match never {});
match entry {
Some(map::Entry::Occupied(_)) => map::Entry::Occupied(()),
Some(map::Entry::Vacant(bucket)) => map::Entry::Vacant(bucket.admit(owner, hash)),
None => map::Entry::Vacant(None),
}
}
impl map::raw::Vacant {
fn admit<Q: map::raw::Insertion>(self, owner: Q, hash: u64) -> Option<Q::Vacant> {
Q::vacant(
map::Vacancy {
owner,
bucket: self,
phase: (),
},
hash,
)
}
}
const _: () = {
assert!(size_of::<map::raw::Occupied>() == size_of::<u32>());
assert!(size_of::<Option<map::raw::Occupied>>() == size_of::<map::raw::Occupied>());
assert!(size_of::<map::raw::Vacant>() == size_of::<u32>());
assert!(size_of::<Option<super::Bucket>>() == size_of::<u64>());
};