use crate::tier2::bio::BitReader;
const UNRESOLVED: u32 = u32::MAX;
const MAX_DEPTH: usize = 33;
#[derive(Debug, Clone, Copy)]
struct Node {
parent: Option<usize>,
value: u32,
low: u32,
}
#[derive(Debug, Clone)]
pub struct TagTree {
width: u32,
height: u32,
nodes: Vec<Node>,
}
struct Level {
width: u32,
height: u32,
offset: usize,
}
impl TagTree {
pub fn new(width: u32, height: u32) -> Self {
let width = width.max(1);
let height = height.max(1);
let mut levels = Vec::new();
let (mut lw, mut lh, mut offset) = (width, height, 0usize);
loop {
levels.push(Level {
width: lw,
height: lh,
offset,
});
offset += lw as usize * lh as usize;
if lw == 1 && lh == 1 {
break;
}
lw = lw.div_ceil(2);
lh = lh.div_ceil(2);
}
let mut nodes = vec![
Node {
parent: None,
value: UNRESOLVED,
low: 0,
};
offset
];
for pair in levels.windows(2) {
let (cur, parent) = (&pair[0], &pair[1]);
for y in 0..cur.height {
for x in 0..cur.width {
let child = cur.offset + (y as usize * cur.width as usize + x as usize);
let par = parent.offset
+ ((y / 2) as usize * parent.width as usize + (x / 2) as usize);
nodes[child].parent = Some(par);
}
}
}
TagTree {
width,
height,
nodes,
}
}
pub fn read(&mut self, x: u32, y: u32, threshold: u32, bio: &mut BitReader) -> Option<u32> {
debug_assert!(
x < self.width && y < self.height,
"leaf ({x}, {y}) out of range"
);
let leaf = y as usize * self.width as usize + x as usize;
let mut path = [0usize; MAX_DEPTH];
let mut depth = 0;
let mut idx = leaf;
while let Some(parent) = self.nodes[idx].parent {
path[depth] = idx;
depth += 1;
idx = parent;
}
let mut low = 0;
loop {
let node = &mut self.nodes[idx];
low = low.max(node.low);
while low < threshold && low < node.value {
if bio.read_bit() == 1 {
node.value = low;
} else {
low += 1;
}
}
node.low = low;
if depth == 0 {
break;
}
depth -= 1;
idx = path[depth];
}
let value = self.nodes[leaf].value;
(value < threshold).then_some(value)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn bits(seq: &[u8]) -> Vec<u8> {
let mut bytes = vec![0u8; seq.len().div_ceil(8)];
for (i, &b) in seq.iter().enumerate() {
if b == 1 {
bytes[i / 8] |= 0x80 >> (i % 8);
}
}
bytes
}
#[test]
fn single_leaf_value_zero() {
let data = bits(&[1]);
let mut bio = BitReader::new(&data);
let mut t = TagTree::new(1, 1);
assert_eq!(t.read(0, 0, 1, &mut bio), Some(0));
}
#[test]
fn single_leaf_value_three() {
let data = bits(&[0, 0, 0, 1]);
let mut bio = BitReader::new(&data);
let mut t = TagTree::new(1, 1);
assert_eq!(t.read(0, 0, 4, &mut bio), Some(3));
}
#[test]
fn rising_threshold_resolves_incrementally() {
let data = bits(&[0, 0, 1]);
let mut bio = BitReader::new(&data);
let mut t = TagTree::new(1, 1);
assert_eq!(t.read(0, 0, 1, &mut bio), None); assert_eq!(t.read(0, 0, 2, &mut bio), None); assert_eq!(t.read(0, 0, 3, &mut bio), Some(2)); }
#[test]
fn state_retained_across_separate_readers() {
let mut t = TagTree::new(1, 1);
let first = bits(&[0, 0]); let mut bio1 = BitReader::new(&first);
assert_eq!(t.read(0, 0, 2, &mut bio1), None);
let second = bits(&[1]); let mut bio2 = BitReader::new(&second);
assert_eq!(t.read(0, 0, 3, &mut bio2), Some(2));
}
#[test]
fn two_leaves_share_a_root() {
let data = bits(&[1, 1, 0, 1]);
let mut bio = BitReader::new(&data);
let mut t = TagTree::new(2, 1);
assert_eq!(t.read(1, 0, 2, &mut bio), Some(0));
assert_eq!(t.read(0, 0, 2, &mut bio), Some(1));
}
#[test]
fn inclusion_across_layers() {
let data = bits(&[1, 1]);
let mut bio = BitReader::new(&data);
let mut t = TagTree::new(2, 2);
assert_eq!(t.read(0, 0, 1, &mut bio), Some(0));
}
#[test]
fn odd_dimensions_build_and_read() {
for (w, h) in [(1, 1), (3, 2), (5, 5), (7, 3), (1, 9)] {
let mut t = TagTree::new(w, h);
let data = bits(&[0; 64]);
let mut bio = BitReader::new(&data);
let _ = t.read(w - 1, h - 1, 8, &mut bio);
}
}
}