use std::{marker, num::NonZero};
use crate::depot::capacity;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(transparent)]
pub struct Rows<C: crate::depot::raw::Count, const N: usize>(C);
impl<C: crate::depot::raw::Count, const N: usize> Rows<C, N> {
pub const fn new(count: C) -> Self {
const {
let _ = <Self as crate::depot::raw::Count>::MAX;
}
Self(count)
}
pub const fn root(self) -> C {
self.0
}
}
unsafe impl<C: crate::depot::raw::Count, const N: usize> crate::depot::raw::Count for Rows<C, N> {
const MAX: u32 = {
assert!(N != 0 && N <= u32::MAX as usize);
assert!(C::MAX as u64 * N as u64 <= u32::MAX as u64);
(C::MAX as u64 * N as u64) as u32
};
const MIN: u32 = {
let _ = Self::MAX;
(C::MIN as u64 * N as u64) as u32
};
fn count(self) -> capacity::Count {
capacity::Count::new((self.0.count().raw() as u64 * N as u64) as u32)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Buckets<C: crate::depot::raw::Count, const MULTIPLIER: usize> {
count: NonZero<u32>,
root: marker::PhantomData<C>,
}
impl<C: crate::depot::raw::Count, const MULTIPLIER: usize> Buckets<C, MULTIPLIER> {
pub fn new(root: C) -> Self {
const {
assert!(C::MIN != 0);
let _ = <Self as crate::depot::raw::Count>::MAX;
}
let count = (root.count().raw() as u64 * MULTIPLIER as u64).next_power_of_two() as u32;
Self {
count: NonZero::<u32>::MIN.saturating_add(count - 1),
root: marker::PhantomData,
}
}
}
unsafe impl<C: crate::depot::raw::Count, const MULTIPLIER: usize> crate::depot::raw::Count
for Buckets<C, MULTIPLIER>
{
const MAX: u32 = {
assert!(MULTIPLIER != 0 && MULTIPLIER <= u32::MAX as usize);
let count = (C::MAX as u64 * MULTIPLIER as u64).next_power_of_two();
assert!(count <= u32::MAX as u64);
count as u32
};
const MIN: u32 = 1;
fn count(self) -> capacity::Count {
capacity::Count::new(self.count.get())
}
}