use std::num::NonZero;
use crate::{
base::num::{Generation, bounded},
depot::queue::links,
};
mod raw;
pub mod scoped;
mod sealed;
pub use sealed::entries::{Occupied, Vacant};
pub use sealed::table::Table;
type Id = bounded::U64<0, { u64::MAX >> 2 }>;
#[derive(Clone, Copy, Debug)]
pub struct Plan {
records: u32,
buckets: NonZero<u32>,
}
impl Plan {
pub const fn counts<const N: usize, const LIMIT: u32>(
counts: [bounded::raw::U32<0, LIMIT>; N],
) -> Self {
const { assert!((N as u128) * (LIMIT as u128) <= 1 << 29, "capacity") };
let mut records = 0_u32;
let mut index = 0;
while index < N {
records = records.wrapping_add(counts[index].get());
index += 1;
}
let shift = records.wrapping_mul(2).wrapping_sub(1).leading_zeros();
let buckets = 1_u32.wrapping_shl(shift.wrapping_neg());
Self {
records,
buckets: NonZero::<u32>::MIN.saturating_add(buckets - 1),
}
}
}
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Key<const MAX: u32>(NonZero<u64>);
impl<const MAX: u32> Key<MAX> {
pub const fn from_raw(raw: NonZero<u64>) -> Option<Self> {
if raw.get() as u32 != 0 && (raw.get() >> 32) as u32 <= MAX {
Some(Self(raw))
} else {
None
}
}
pub const fn raw(self) -> NonZero<u64> {
self.0
}
pub const fn index(self) -> u32 {
(self.0.get() as u32) - 1
}
pub const fn generation(self) -> u32 {
(self.0.get() >> 32) as u32
}
fn new(index: links::Index, epoch: Generation<MAX>) -> Self {
let slot = NonZero::<u64>::MIN.saturating_add(u64::from(index.raw()));
Self(slot | (u64::from(epoch.get()) << 32))
}
}
const _: () = {
assert!(size_of::<Key<{ u32::MAX }>>() == 8);
assert!(size_of::<Option<Key<{ u32::MAX }>>>() == 8);
};