use alloc::vec::Vec;
use core::cmp::Ordering;
use crate::Rect;
use crate::dynamic::DynamicIndex;
use crate::dynamic::node::{Child, DNode};
use crate::static_index::str_order;
fn key(v: f64) -> f64 {
if v.is_nan() { f64::INFINITY } else { v }
}
fn cmp_key(a: f64, b: f64) -> Ordering {
key(a).total_cmp(&key(b))
}
fn area(r: &Rect) -> f64 {
(r.max_x() - r.min_x()) * (r.max_y() - r.min_y())
}
fn margin(r: &Rect) -> f64 {
(r.max_x() - r.min_x()) + (r.max_y() - r.min_y())
}
fn intersection_area(a: &Rect, b: &Rect) -> f64 {
if !a.intersects(b) {
return 0.0;
}
let w = a.max_x().min(b.max_x()) - a.min_x().max(b.min_x());
let h = a.max_y().min(b.max_y()) - a.min_y().max(b.min_y());
w * h
}
fn center_distance_sq(a: &Rect, b: &Rect) -> f64 {
let (ax, ay) = a.center();
let (bx, by) = b.center();
let (dx, dy) = (ax - bx, ay - by);
dx * dx + dy * dy
}
fn area_enlargement(own: &Rect, added: &Rect) -> f64 {
area(&own.union(added)) - area(own)
}
impl DynamicIndex {
pub fn insert(&mut self, rect: Rect, id: u32) {
self.len += 1;
if self.root.is_none() {
let leaf = DNode::with_first(0, Child::Entry(rect, id), rect);
self.root = Some(self.allocate(leaf));
return;
}
let mut reinserted: u64 = 0;
self.place(Child::Entry(rect, id), 0, &mut reinserted);
}
pub(crate) fn place(&mut self, child: Child, level: u32, reinserted: &mut u64) {
let child_rect = child.rect(self);
let path = self.choose_path(&child_rect, level);
let target = *path.last().expect("the tree has a root");
self.arena[target].push(child);
self.recompute_boxes(&path);
if self.arena[target].len() > Self::NODE_CAPACITY {
self.treat_overflow(&path, reinserted);
}
}
fn choose_path(&self, rect: &Rect, level: u32) -> Vec<usize> {
let mut slot = self.root.expect("the tree has a root");
let mut path = Vec::with_capacity(8);
path.push(slot);
while self.arena[slot].level > level {
slot = self.choose_child(slot, rect);
path.push(slot);
}
path
}
fn choose_child(&self, slot: usize, rect: &Rect) -> usize {
let node = &self.arena[slot];
let boxes: Vec<Rect> = node.children().map(|c| c.rect(self)).collect();
let children_are_leaves = node.level == 1;
let mut best: Option<(usize, f64, f64, f64)> = None;
for (i, own) in boxes.iter().enumerate() {
let grown = own.union(rect);
let overlap_cost = if children_are_leaves {
let mut cost = 0.0f64;
for (j, other) in boxes.iter().enumerate() {
if i != j {
cost += intersection_area(&grown, other) - intersection_area(own, other);
}
}
cost
} else {
0.0
};
let enlargement = area_enlargement(own, rect);
let own_area = area(own);
let better = match &best {
None => true,
Some((_, bo, be, ba)) => {
cmp_key(overlap_cost, *bo)
.then_with(|| cmp_key(enlargement, *be))
.then_with(|| cmp_key(own_area, *ba))
== Ordering::Less
}
};
if better {
best = Some((i, overlap_cost, enlargement, own_area));
}
}
let (at, ..) = best.expect("a node is never empty");
match self.arena[slot].child(at) {
Child::Node(child_slot) => child_slot,
Child::Entry(..) => unreachable!("descent stops above the entries"),
}
}
pub(crate) fn recompute_boxes(&mut self, path: &[usize]) {
for &slot in path.iter().rev() {
self.arena[slot].rect = self.arena[slot].exact_box(self);
}
}
fn treat_overflow(&mut self, path: &[usize], reinserted: &mut u64) {
let slot = *path.last().expect("path is never empty");
let level = self.arena[slot].level;
let is_root = path.len() == 1;
let bit = 1u64 << level.min(63);
if !is_root && *reinserted & bit == 0 {
*reinserted |= bit;
self.reinsert(path, reinserted);
} else {
self.split(path, reinserted);
}
}
fn reinsert(&mut self, path: &[usize], reinserted: &mut u64) {
let slot = *path.last().expect("path is never empty");
let level = self.arena[slot].level;
let node_box = self.arena[slot].rect;
let mut order: Vec<(f64, usize)> = self.arena[slot]
.children()
.enumerate()
.map(|(i, c)| (center_distance_sq(&c.rect(self), &node_box), i))
.collect();
order.sort_by(|a, b| cmp_key(b.0, a.0).then_with(|| a.1.cmp(&b.1)));
let mut removed: Vec<Child> = Vec::with_capacity(Self::REINSERT_COUNT);
let mut far: Vec<usize> = order[..Self::REINSERT_COUNT].iter().map(|e| e.1).collect();
far.sort_unstable_by(|a, b| b.cmp(a));
for at in far {
removed.push(self.arena[slot].remove(at));
}
self.recompute_boxes(path);
let mut by_distance: Vec<(f64, usize)> = removed
.iter()
.enumerate()
.map(|(i, c)| (center_distance_sq(&c.rect(self), &node_box), i))
.collect();
by_distance.sort_by(|a, b| cmp_key(a.0, b.0).then_with(|| a.1.cmp(&b.1)));
for (_, i) in by_distance {
self.place(removed[i], level, reinserted);
}
}
fn split(&mut self, path: &[usize], reinserted: &mut u64) {
let slot = *path.last().expect("path is never empty");
let level = self.arena[slot].level;
let (first, second) = self.split_groups(slot);
self.arena[slot].set_children(&first);
self.arena[slot].rect = self.arena[slot].exact_box(self);
let second_box = boxes_union(&second, self);
let mut sibling = DNode::with_first(level, second[0], second_box);
for child in &second[1..] {
sibling.push(*child);
}
let sibling_slot = self.allocate(sibling);
if path.len() == 1 {
let old_root_box = self.arena[slot].rect;
let root = DNode::with_first(level + 1, Child::Node(slot), old_root_box);
let root_slot = self.allocate(root);
self.arena[root_slot].push(Child::Node(sibling_slot));
self.arena[root_slot].rect = self.arena[root_slot].exact_box(self);
self.root = Some(root_slot);
return;
}
let parent_path = &path[..path.len() - 1];
let parent = *parent_path.last().expect("a non-root node has a parent");
self.arena[parent].push(Child::Node(sibling_slot));
self.recompute_boxes(parent_path);
if self.arena[parent].len() > Self::NODE_CAPACITY {
self.treat_overflow(parent_path, reinserted);
}
}
fn split_groups(&self, slot: usize) -> (Vec<Child>, Vec<Child>) {
let children: Vec<Child> = self.arena[slot].children().copied().collect();
let m = Self::MIN_FILL;
let splits = children.len() + 1 - 2 * m;
let sorted_on = |by_y: bool| -> Vec<Child> {
let mut order: Vec<(usize, Child)> = children.iter().copied().enumerate().collect();
order.sort_by(|(ia, a), (ib, b)| {
let (ra, rb) = (a.rect(self), b.rect(self));
let (lo_a, hi_a, lo_b, hi_b) = if by_y {
(ra.min_y(), ra.max_y(), rb.min_y(), rb.max_y())
} else {
(ra.min_x(), ra.max_x(), rb.min_x(), rb.max_x())
};
lo_a.total_cmp(&lo_b)
.then_with(|| hi_a.total_cmp(&hi_b))
.then_with(|| ia.cmp(ib))
});
order.into_iter().map(|(_, c)| c).collect()
};
let margin_sum = |sorted: &[Child]| -> f64 {
let mut sum = 0.0f64;
for k in m..=sorted.len() - m {
let (a, b) = sorted.split_at(k);
sum += margin(&boxes_union(a, self)) + margin(&boxes_union(b, self));
}
sum
};
let on_x = sorted_on(false);
let on_y = sorted_on(true);
let sorted = if cmp_key(margin_sum(&on_y), margin_sum(&on_x)) == Ordering::Less {
on_y
} else {
on_x
};
let mut best: Option<(usize, f64, f64)> = None;
for k in m..m + splits {
let (a, b) = sorted.split_at(k);
let (box_a, box_b) = (boxes_union(a, self), boxes_union(b, self));
let overlap = intersection_area(&box_a, &box_b);
let total_area = area(&box_a) + area(&box_b);
let better = match &best {
None => true,
Some((_, bo, ba)) => {
cmp_key(overlap, *bo).then_with(|| cmp_key(total_area, *ba)) == Ordering::Less
}
};
if better {
best = Some((k, overlap, total_area));
}
}
let (k, ..) = best.expect("at least one distribution exists");
let (a, b) = sorted.split_at(k);
(a.to_vec(), b.to_vec())
}
}
fn boxes_union(children: &[Child], index: &DynamicIndex) -> Rect {
let mut boxes = children.iter().map(|c| c.rect(index));
let first = boxes.next().expect("a split group is never empty");
boxes.fold(first, |acc, r| acc.union(&r))
}
impl DynamicIndex {
#[must_use]
pub fn bulk_load(mut entries: Vec<(Rect, u32)>) -> DynamicIndex {
let mut index = DynamicIndex::new();
index.len = entries.len();
if entries.is_empty() {
return index;
}
str_order(&mut entries, |e| e.0, |e| u64::from(e.1));
let mut level: Vec<usize> = chunk_sizes(entries.len())
.into_iter()
.scan(0usize, |taken, size| {
let chunk = &entries[*taken..*taken + size];
*taken += size;
Some(index.load_node(0, chunk.iter().map(|&(r, id)| Child::Entry(r, id))))
})
.collect();
let mut height = 1u32;
while level.len() > 1 {
let mut items: Vec<(Rect, usize)> =
level.iter().map(|&s| (index.arena[s].rect, s)).collect();
str_order(&mut items, |n| n.0, |_| 0);
level = chunk_sizes(items.len())
.into_iter()
.scan(0usize, |taken, size| {
let chunk = &items[*taken..*taken + size];
*taken += size;
Some(index.load_node(height, chunk.iter().map(|&(_, s)| Child::Node(s))))
})
.collect();
height += 1;
}
index.root = Some(level[0]);
index
}
fn load_node(&mut self, level: u32, mut children: impl Iterator<Item = Child>) -> usize {
let first = children.next().expect("a chunk is never empty");
let mut node = DNode::with_first(level, first, first.rect(self));
for child in children {
node.push(child);
}
node.rect = node.exact_box(self);
self.allocate(node)
}
}
fn chunk_sizes(n: usize) -> Vec<usize> {
let (cap, min) = (DynamicIndex::NODE_CAPACITY, DynamicIndex::MIN_FILL);
if n <= cap {
return alloc::vec![n];
}
let mut sizes = Vec::with_capacity(n.div_ceil(cap));
let full = n / cap;
let rest = n % cap;
for _ in 0..full {
sizes.push(cap);
}
if rest > 0 {
if rest < min {
let moved = min - rest;
let last_full = sizes.len() - 1;
sizes[last_full] = cap - moved;
sizes.push(rest + moved);
} else {
sizes.push(rest);
}
}
sizes
}