Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions understory_box_tree/src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ impl<B: Backend<f64>> Tree<B> {

#[inline]
fn id_is_newer(a: NodeId, b: NodeId) -> bool {
(a.1 > b.1) || (a.1 == b.1 && a.0 > b.0)
(a.generation() > b.generation()) || (a.generation() == b.generation() && a.idx() > b.idx())
}

impl<B: Backend<f64>> Tree<B> {
Expand All @@ -533,7 +533,7 @@ impl<B: Backend<f64>> Tree<B> {
self.nodes
.get(id.idx())
.and_then(|n| n.as_ref())
.map(|n| n.generation == id.1)
.map(|n| n.generation == id.generation())
.unwrap_or(false)
}

Expand Down Expand Up @@ -681,7 +681,7 @@ impl<B: Backend<f64>> Tree<B> {

fn node_opt_mut(&mut self, id: NodeId) -> Option<&mut Node> {
let n = self.nodes.get_mut(id.idx())?.as_mut()?;
if n.generation != id.1 {
if n.generation != id.generation() {
return None;
}
Some(n)
Expand Down Expand Up @@ -1055,8 +1055,11 @@ mod tests {
assert!(tree.is_alive(b));
assert!(!tree.is_alive(a));
// Sanity: either same slot or different, but if same slot, generation must be greater.
if a.0 == b.0 {
assert!(b.1 > a.1, "generation must increase on reuse");
if a.idx() == b.idx() {
assert!(
b.generation() > a.generation(),
"generation must increase on reuse"
);
}
}

Expand Down
16 changes: 13 additions & 3 deletions understory_box_tree/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,25 @@ pub enum ClipBehavior {
/// - The generation increments on slot reuse and never decreases.
/// - `u32` is ample for practical lifetimes; behavior on generation overflow is unspecified.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub struct NodeId(pub(crate) u32, pub(crate) u32);
pub struct NodeId {
idx: u32,
generation: u32,
}

impl NodeId {
#[inline(always)]
pub(crate) const fn new(idx: u32, generation: u32) -> Self {
Self(idx, generation)
Self { idx, generation }
}

#[inline(always)]
pub(crate) const fn idx(self) -> usize {
self.0 as usize
self.idx as usize
}

#[inline(always)]
pub(crate) const fn generation(self) -> u32 {
self.generation
}
}

Expand Down