use alloc::vec;
use alloc::vec::Vec;
use crate::Rect;
use crate::dynamic::DynamicIndex;
use crate::dynamic::node::Child;
fn entry_bits(rect: &Rect, id: u32) -> (u64, u64, u64, u64, u32) {
(
rect.min_x().to_bits(),
rect.min_y().to_bits(),
rect.max_x().to_bits(),
rect.max_y().to_bits(),
id,
)
}
impl DynamicIndex {
#[doc(hidden)]
pub fn check_invariants(&self, model: &[(Rect, u32)]) {
assert_eq!(self.len(), model.len(), "len differs from the model");
assert_eq!(self.is_empty(), model.is_empty());
let Some(root) = self.root else {
assert!(model.is_empty(), "entries stored but no root");
assert_eq!(self.bounds(), None);
return;
};
let mut stored: Vec<(u64, u64, u64, u64, u32)> = Vec::new();
let mut seen = vec![false; self.arena.len()];
let mut stack = vec![root];
while let Some(slot) = stack.pop() {
assert!(!seen[slot], "slot {slot} is reachable twice");
seen[slot] = true;
let node = &self.arena[slot];
assert!(
node.len() <= Self::NODE_CAPACITY,
"slot {slot} over capacity"
);
if slot == root {
let least = if node.level == 0 { 1 } else { 2 };
assert!(node.len() >= least, "root under its minimum");
} else {
assert!(
node.len() >= Self::MIN_FILL,
"slot {slot} holds {} children, under the minimum fill",
node.len()
);
}
let exact = node.exact_box(self);
assert_eq!(
entry_bits(&node.rect, 0),
entry_bits(&exact, 0),
"slot {slot}: the node box is not the exact union of its children"
);
for child in node.children() {
match *child {
Child::Node(child_slot) => {
assert!(node.level >= 1, "a leaf holds a subtree");
assert_eq!(
self.arena[child_slot].level,
node.level - 1,
"slot {slot}: a child is not one level down"
);
stack.push(child_slot);
}
Child::Entry(rect, id) => {
assert_eq!(node.level, 0, "an internal node holds an entry");
stored.push(entry_bits(&rect, id));
}
}
}
}
for &slot in &self.free {
assert!(!seen[slot], "slot {slot} is both reachable and free");
}
let mut want: Vec<_> = model
.iter()
.map(|(rect, id)| entry_bits(rect, *id))
.collect();
stored.sort_unstable();
want.sort_unstable();
assert_eq!(stored, want, "the stored entries differ from the model");
let union = model
.iter()
.map(|(rect, _)| *rect)
.reduce(|acc, b| acc.union(&b))
.expect("the model is non-empty here");
let got = self.bounds().expect("a non-empty tree has bounds");
assert!(
got == union,
"bounds() {got:?} is not the exact union of the model's boxes {union:?}"
);
}
#[doc(hidden)]
pub fn stored_entries(&self) -> Vec<(Rect, u32)> {
let mut out = Vec::with_capacity(self.len);
let mut stack: Vec<usize> = self.root.into_iter().collect();
while let Some(slot) = stack.pop() {
for child in self.arena[slot].children().rev() {
match *child {
Child::Node(s) => stack.push(s),
Child::Entry(rect, id) => out.push((rect, id)),
}
}
}
out
}
}