use crate::*;
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct NodeId(u32);
impl NodeId {
#[inline]
pub(crate) fn new(index: usize) -> Self {
assert!(index < Slots::KEPT as usize, "too many frame nodes");
Self(index as u32)
}
#[inline]
pub(crate) fn index(self) -> usize {
self.0 as usize
}
}
#[derive(Clone, Copy)]
pub enum FocusRequest {
Keep,
Clear,
Focus(Id),
}
pub struct StoredNode {
pub id: Id,
pub parent: NodeId,
pub subtree_end: u32,
pub first_measure: StoredMeasureId,
pub last_paint: StoredPaintId,
pub layout: BumpId,
pub layout_kind: u16,
pub aux: AuxId,
pub place: PlaceId,
pub place_scope: NodeId,
pub item: BumpId,
pub target: NodeId,
pub interactive: bool,
}
#[derive(Clone, Copy)]
pub struct Aux<T> {
pub visual_parent: NodeId,
pub clip: Option<Corners<T>>,
pub limits: Constraints<T>,
pub padding: Sides<T>,
pub hit: Sides<T>,
pub motion: NodeMotionId,
}
pub fn visual_parent<T>(aux: &[Aux<T>], stored: &StoredNode) -> NodeId {
stored
.aux
.index()
.map_or(stored.parent, |index| aux[index].visual_parent)
}
#[derive(Clone, Copy)]
pub struct NodeMotion {
pub node: NodeId,
pub motion: Motion,
}
#[derive(Clone, Copy)]
pub struct ResolvedClip<T> {
pub parent: ResolvedClipId,
pub depth: u32,
pub area: Rect<T>,
pub radius: Corners<T>,
pub bounds: Rect<T>,
}
pub struct StoredMeasure<C: Context> {
pub run: fn(&BumpArena, BumpId, &mut C, Constraints<C::Scalar>) -> Fit<Size<C::Scalar>>,
pub bump_id: BumpId,
pub next: StoredMeasureId,
}
impl<C: Context> Clone for StoredMeasure<C> {
fn clone(&self) -> Self {
*self
}
}
impl<C: Context> Copy for StoredMeasure<C> {}
pub struct StoredPaint<C: Context> {
pub run: fn(&BumpArena, BumpId, &mut C, Rect<C::Scalar>),
pub bump_id: BumpId,
pub next: StoredPaintId,
}
impl<C: Context> Clone for StoredPaint<C> {
fn clone(&self) -> Self {
*self
}
}
impl<C: Context> Copy for StoredPaint<C> {}
pub struct StoredPlace<C: Context> {
pub bump_id: BumpId,
pub run: for<'a> fn(&mut PlaceCx<'a, C>, BumpId, Rect<C::Scalar>) -> Point<C::Scalar>,
}
impl<C: Context> Clone for StoredPlace<C> {
fn clone(&self) -> Self {
*self
}
}
impl<C: Context> Copy for StoredPlace<C> {}
#[derive(Clone, Copy)]
pub struct Index<T>(pub u32, PhantomData<fn() -> T>);
impl<T> Index<T> {
pub const NONE: Self = Self(u32::MAX, PhantomData);
pub fn new(index: usize) -> Self {
Self(u32::try_from(index).expect("too many frame values"), PhantomData)
}
pub fn index(&self) -> Option<usize> {
(self.0 != u32::MAX).then_some(self.0 as usize)
}
}
pub type StoredMeasureId = Index<StoredMeasure<()>>;
pub type StoredPaintId = Index<StoredPaint<()>>;
pub type AuxId = Index<Aux<()>>;
pub type PlaceId = Index<StoredPlace<()>>;
pub type NodeMotionId = Index<NodeMotion>;
pub type ResolvedClipId = Index<ResolvedClip<()>>;
pub struct LayoutKind<C: Context> {
pub type_id: TypeId,
pub fit: for<'a> fn(&mut LayoutCx<'a, C>, BumpId, Axis) -> Fit<C::Scalar>,
pub distribute: for<'a> fn(&mut LayoutCx<'a, C>, BumpId, Axis, C::Scalar),
pub default_item: BumpId,
}
#[derive(Default)]
pub struct Slots {
slots: Vec<Slot>,
names: Names,
build: u32,
sweep: u32,
}
#[derive(Clone, Copy)]
struct Slot {
generation: NonZeroU32,
build: u32,
node: u32,
old: u32,
}
impl Slots {
pub const NONE: u32 = u32::MAX;
const KEPT: u32 = u32::MAX - 1;
pub fn begin(&mut self) {
self.build = self.build.wrapping_add(1);
self.sweep = 0;
if self.slots.is_empty() {
self.alloc();
}
}
#[inline]
pub fn claimed(&self, id: Id) -> bool {
let slot = &self.slots[id.slot as usize];
slot.build == self.build && slot.node < Self::KEPT
}
#[inline]
pub fn set(&mut self, id: Id, node: NodeId) {
self.touch(id.slot as usize).node = node.0;
}
#[inline]
pub fn keep(&mut self, id: Id) {
if self.live(id) {
self.touch(id.slot as usize);
}
}
#[inline]
pub fn is_kept(&self, id: Id) -> bool {
self.live(id) && self.slots[id.slot as usize].build == self.build
}
pub fn alloc(&mut self) -> Id {
let build = self.build;
let mut index = self.sweep as usize;
while self
.slots
.get(index)
.is_some_and(|slot| build.wrapping_sub(slot.build) <= 1)
{
index += 1;
}
self.sweep = u32::try_from(index + 1).expect("too many ids");
let generation = self.slots.get(index).map_or(NonZeroU32::MIN, |slot| {
slot.generation.checked_add(1).unwrap_or(NonZeroU32::MIN)
});
let slot = Slot {
generation,
build,
node: Self::KEPT,
old: Self::NONE,
};
if index == self.slots.len() {
self.slots.push(slot);
} else {
self.slots[index] = slot;
}
Id {
slot: index as u32,
generation,
}
}
#[inline]
pub fn node(&self, id: Id) -> Option<NodeId> {
let slot = self.slots.get(id.slot as usize)?;
(slot.generation == id.generation && slot.build == self.build && slot.node < Self::KEPT)
.then_some(NodeId(slot.node))
}
#[inline]
pub fn old(&self, id: Id) -> Option<usize> {
let slot = self.slots.get(id.slot as usize).filter(|_| self.live(id))?;
let old = if slot.build == self.build { slot.old } else { slot.node };
(old < Self::KEPT).then_some(old as usize)
}
pub fn name(&mut self, scope: Id, key: u64) -> Id {
let mut bucket = 0;
if !self.names.table.is_empty() {
bucket = self.names.probe(scope, key);
let index = self.names.table[bucket];
if index != Names::EMPTY {
let id = self.names.entries[index as usize].id;
if self.live(id) {
return id;
}
let id = self.alloc();
self.names.entries[index as usize].id = id;
return id;
}
}
let id = self.alloc();
if self.names.entries.len() + 1 > self.names.table.len() / 2 {
let mut entries = core::mem::take(&mut self.names.entries);
entries.retain(|name| self.live(name.id));
entries.push(Name { scope, key, id });
self.names.entries = entries;
self.names.rebuild();
} else {
let index = u32::try_from(self.names.entries.len()).expect("too many names");
self.names.entries.push(Name { scope, key, id });
self.names.table[bucket] = index;
}
id
}
pub fn find(&self, scope: Id, key: u64) -> Option<Id> {
let index = (!self.names.table.is_empty()).then(|| self.names.table[self.names.probe(scope, key)])?;
let id = self.names.entries.get(index as usize)?.id;
self.live(id).then_some(id)
}
#[inline]
fn live(&self, id: Id) -> bool {
self.slots
.get(id.slot as usize)
.is_some_and(|slot| slot.generation == id.generation && self.build.wrapping_sub(slot.build) <= 1)
}
#[inline]
fn touch(&mut self, index: usize) -> &mut Slot {
let build = self.build;
let slot = &mut self.slots[index];
if slot.build != build {
slot.old = slot.node;
slot.node = Self::KEPT;
slot.build = build;
}
slot
}
}
#[derive(Clone, Copy)]
pub struct Cursor {
pub old: u32,
pub next: u32,
}
#[derive(Clone, Copy)]
pub struct Site {
pub location: &'static Location<'static>,
pub ordinal: u32,
pub scan: bool,
}
#[derive(Clone, Copy)]
pub enum Mark {
Site(&'static Location<'static>, u32),
Key(u32, u64),
Named,
}
impl Mark {
#[inline]
pub fn is_site(self, location: &Location<'_>, ordinal: u32) -> bool {
matches!(self, Self::Site(other, other_ordinal) if other_ordinal == ordinal && same_place(other, location))
}
}
#[inline]
pub fn same_place(site: &Location<'_>, other: &Location<'_>) -> bool {
#[cold]
fn same_file(site: &Location<'_>, other: &Location<'_>) -> bool {
site.file() == other.file()
}
core::ptr::eq(site, other)
|| (site.line() == other.line() && site.column() == other.column() && same_file(site, other))
}
#[derive(Default)]
struct Names {
entries: Vec<Name>,
table: Vec<u32>,
}
#[derive(Clone, Copy)]
struct Name {
scope: Id,
key: u64,
id: Id,
}
impl Names {
const EMPTY: u32 = u32::MAX;
fn rebuild(&mut self) {
self.table.clear();
if self.entries.is_empty() {
return;
}
self.table
.resize((self.entries.len() * 4).next_power_of_two(), Self::EMPTY);
for index in 0..self.entries.len() {
let Name { scope, key, .. } = self.entries[index];
let bucket = self.probe(scope, key);
self.table[bucket] = index as u32;
}
}
fn probe(&self, scope: Id, key: u64) -> usize {
let mask = self.table.len() - 1;
let mixed = (key ^ u64::from(scope.slot).rotate_left(32)).wrapping_mul(0x9e37_79b9_7f4a_7c15);
let mut bucket = (mixed >> (64 - self.table.len().trailing_zeros())) as usize;
loop {
let index = self.table[bucket];
if index == Self::EMPTY {
return bucket;
}
let name = self.entries[index as usize];
if name.scope == scope && name.key == key {
return bucket;
}
bucket = (bucket + 1) & mask;
}
}
}
#[derive(Clone, Copy)]
pub struct CallScope {
pub call: Call,
pub id: Id,
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Call {
pub owner: Id,
pub location: &'static Location<'static>,
pub repeat: u32,
}
impl Call {
pub fn new(owner: Id, location: &'static Location<'static>) -> Self {
Self {
owner,
location,
repeat: 0,
}
}
}
impl Ord for Call {
#[inline]
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
let (location, other_location) = (self.location, other.location);
self.owner
.cmp(&other.owner)
.then_with(|| location.line().cmp(&other_location.line()))
.then_with(|| location.column().cmp(&other_location.column()))
.then_with(|| self.repeat.cmp(&other.repeat))
.then_with(|| match core::ptr::eq(location, other_location) {
true => core::cmp::Ordering::Equal,
false => location.file().cmp(other_location.file()),
})
}
}
impl PartialOrd for Call {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
Some(self.cmp(other))
}
}