use alloc::vec::Vec;
use search_core::Cost;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum FrontIndex {
None,
#[default]
Blocks,
Tree,
}
pub const DEFAULT_BLOCK: usize = 16;
#[derive(Debug, Clone)]
pub struct IndexedFront<C, const N: usize> {
points: Vec<[C; N]>,
index: Summary<C, N>,
block: usize,
}
#[derive(Debug, Clone)]
enum Summary<C, const N: usize> {
None,
Blocks(Vec<[C; N]>),
Tree {
leaves: usize,
mins: Vec<Option<[C; N]>>,
},
}
impl<C: Cost, const N: usize> IndexedFront<C, N> {
#[must_use]
pub fn new(points: Vec<[C; N]>, index: FrontIndex) -> Self {
Self::with_block_size(points, index, DEFAULT_BLOCK)
}
#[must_use]
pub fn with_block_size(mut points: Vec<[C; N]>, index: FrontIndex, block: usize) -> Self {
assert!(block > 0, "the block size must be positive");
points.sort_unstable();
let index = match index {
FrontIndex::None => Summary::None,
FrontIndex::Blocks => Summary::Blocks(points.chunks(block).map(minimum).collect()),
FrontIndex::Tree => tree(&points),
};
Self {
points,
index,
block,
}
}
#[must_use]
pub fn as_slice(&self) -> &[[C; N]] {
&self.points
}
#[must_use]
pub fn len(&self) -> usize {
self.points.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.points.is_empty()
}
#[must_use]
pub fn weakly_dominates(&self, q: &[C; N]) -> bool {
let prefix = self.points.partition_point(|p| p <= q);
let candidates = &self.points[..prefix];
match &self.index {
Summary::None => candidates.iter().any(|p| below(p, q)),
Summary::Blocks(mins) => candidates
.chunks(self.block)
.zip(mins)
.any(|(block, min)| below_tail(min, q) && block.iter().any(|p| below(p, q))),
Summary::Tree { leaves, mins } => self.tree_dominates(*leaves, mins, prefix, q),
}
}
fn tree_dominates(
&self,
leaves: usize,
mins: &[Option<[C; N]>],
prefix: usize,
q: &[C; N],
) -> bool {
let mut stack = alloc::vec![(1, 0, leaves)];
while let Some((node, first, width)) = stack.pop() {
if first >= prefix {
continue;
}
let Some(min) = &mins[node] else { continue };
if !below_tail(min, q) {
continue;
}
if width == 1 {
if below(&self.points[first], q) {
return true;
}
continue;
}
let half = width / 2;
stack.push((2 * node + 1, first + half, half));
stack.push((2 * node, first, half));
}
false
}
}
fn tree<C: Cost, const N: usize>(points: &[[C; N]]) -> Summary<C, N> {
let leaves = points.len().max(1).next_power_of_two();
let mut mins = alloc::vec![None; 2 * leaves];
for (j, p) in points.iter().enumerate() {
mins[leaves + j] = Some(*p);
}
for node in (1..leaves).rev() {
mins[node] = match (mins[2 * node], mins[2 * node + 1]) {
(Some(a), Some(b)) => Some(core::array::from_fn(|i| a[i].min(b[i]))),
(a, b) => a.or(b),
};
}
Summary::Tree { leaves, mins }
}
fn minimum<C: Cost, const N: usize>(block: &[[C; N]]) -> [C; N] {
block
.iter()
.copied()
.reduce(|a, b| core::array::from_fn(|i| a[i].min(b[i])))
.expect("chunks are never empty")
}
fn below<C: Cost, const N: usize>(p: &[C; N], q: &[C; N]) -> bool {
p.iter().zip(q).all(|(a, b)| a <= b)
}
fn below_tail<C: Cost, const N: usize>(p: &[C; N], q: &[C; N]) -> bool {
p[1..].iter().zip(&q[1..]).all(|(a, b)| a <= b)
}
#[cfg(test)]
mod tests {
use alloc::vec::Vec;
use proptest::prelude::*;
use super::{FrontIndex, IndexedFront};
proptest! {
#[test]
fn indexes_match_scan(
points in prop::collection::vec(prop::array::uniform3(0..20_u32), 0..80),
queries in prop::collection::vec(prop::array::uniform3(0..20_u32), 1..20),
) {
let fronts: Vec<_> = [FrontIndex::None, FrontIndex::Blocks, FrontIndex::Tree]
.into_iter()
.map(|index| IndexedFront::new(points.clone(), index))
.collect();
for q in &queries {
let expected = points.iter().any(|p| (0..3).all(|i| p[i] <= q[i]));
for front in &fronts {
prop_assert_eq!(front.weakly_dominates(q), expected);
}
}
}
}
}