pub const SECTION_VOLUME: usize = 4096;
#[inline]
#[must_use]
pub const fn block_index(x: i32, y: i32, z: i32) -> usize {
(((y & 15) << 8) | ((z & 15) << 4) | (x & 15)) as usize
}
#[derive(Clone)]
pub struct Indexed {
idx: [u8; SECTION_VOLUME],
palette: [u16; 256],
len: u16,
non_air: u16,
}
#[derive(Clone)]
pub struct Direct {
states: [u16; SECTION_VOLUME],
non_air: u16,
}
#[derive(Clone)]
pub enum Section {
Uniform(u16),
Indexed(Box<Indexed>),
Direct(Box<Direct>),
}
impl std::fmt::Debug for Section {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Section::Uniform(s) => f.debug_tuple("Uniform").field(s).finish(),
Section::Indexed(b) => f
.debug_struct("Indexed")
.field("palette", &&b.palette[..usize::from(b.len)])
.field("non_air", &b.non_air)
.finish(),
Section::Direct(b) => f
.debug_struct("Direct")
.field("non_air", &b.non_air)
.finish(),
}
}
}
impl Section {
#[inline]
#[must_use]
pub fn get(&self, i: usize) -> u16 {
let i = i & (SECTION_VOLUME - 1);
match self {
Section::Uniform(s) => *s,
Section::Indexed(b) => b.palette[usize::from(b.idx[i])],
Section::Direct(b) => b.states[i],
}
}
#[must_use]
pub fn non_air(&self) -> u16 {
match self {
Section::Uniform(_) => SECTION_VOLUME as u16,
Section::Indexed(b) => b.non_air,
Section::Direct(b) => b.non_air,
}
}
#[must_use]
pub fn heap_bytes(&self) -> usize {
match self {
Section::Uniform(_) => 0,
Section::Indexed(_) => size_of::<Indexed>(),
Section::Direct(_) => size_of::<Direct>(),
}
}
pub fn for_each(&self, mut f: impl FnMut(usize, u16)) {
match self {
Section::Uniform(s) => (0..SECTION_VOLUME).for_each(|i| f(i, *s)),
Section::Indexed(b) => {
for (i, &p) in b.idx.iter().enumerate() {
f(i, b.palette[usize::from(p)]);
}
}
Section::Direct(b) => {
for (i, &s) in b.states.iter().enumerate() {
f(i, s);
}
}
}
}
pub fn set(&mut self, i: usize, state: u16) -> bool {
let i = i & (SECTION_VOLUME - 1);
match self {
Section::Uniform(u) => {
if *u == state {
return true;
}
let mut b = Box::new(Indexed {
idx: [0; SECTION_VOLUME],
palette: [0; 256],
len: 2,
non_air: SECTION_VOLUME as u16,
});
b.palette[0] = *u;
b.palette[1] = state;
b.idx[i] = 1;
if state == 0 {
b.non_air -= 1;
}
*self = Section::Indexed(b);
true
}
Section::Indexed(b) => {
let old = b.palette[usize::from(b.idx[i])];
if old == state {
return true;
}
let p = match b.find_or_insert(state) {
Some(p) => p,
None => {
let mut d = Box::new(Direct {
states: [0; SECTION_VOLUME],
non_air: b.non_air,
});
for (dst, &p) in d.states.iter_mut().zip(b.idx.iter()) {
*dst = b.palette[usize::from(p)];
}
*self = Section::Direct(d);
return self.set(i, state);
}
};
b.idx[i] = p;
adjust(&mut b.non_air, old, state);
b.non_air != 0
}
Section::Direct(b) => {
let old = b.states[i];
b.states[i] = state;
adjust(&mut b.non_air, old, state);
b.non_air != 0
}
}
}
#[must_use]
pub fn single(i: usize, state: u16) -> Option<Section> {
if state == 0 {
return None;
}
let mut b = Box::new(Indexed {
idx: [0; SECTION_VOLUME],
palette: [0; 256],
len: 2,
non_air: 1,
});
b.palette[1] = state;
b.idx[i & (SECTION_VOLUME - 1)] = 1;
Some(Section::Indexed(b))
}
}
#[inline]
fn adjust(non_air: &mut u16, old: u16, new: u16) {
match (old == 0, new == 0) {
(true, false) => *non_air += 1,
(false, true) => *non_air -= 1,
_ => {}
}
}
impl Indexed {
fn find_or_insert(&mut self, state: u16) -> Option<u8> {
let len = usize::from(self.len);
if let Some(p) = self.palette[..len].iter().position(|&s| s == state) {
return Some(p as u8);
}
if len == 256 {
self.compact();
}
let len = usize::from(self.len);
if len == 256 {
return None;
}
self.palette[len] = state;
self.len += 1;
Some(len as u8)
}
fn compact(&mut self) {
let mut used = [false; 256];
for &p in &self.idx {
used[usize::from(p)] = true;
}
let mut remap = [0u8; 256];
let mut palette = [0u16; 256];
let mut n = 0usize;
for p in 0..usize::from(self.len) {
if used[p] {
remap[p] = n as u8;
palette[n] = self.palette[p];
n += 1;
}
}
for p in &mut self.idx {
*p = remap[usize::from(*p)];
}
self.palette = palette;
self.len = n as u16;
}
}
#[cfg_attr(not(any(feature = "v1_8", feature = "v1_16_5")), allow(dead_code))]
pub(crate) struct SectionBuilder {
lookup: Vec<u16>,
}
#[cfg_attr(not(any(feature = "v1_8", feature = "v1_16_5")), allow(dead_code))]
impl SectionBuilder {
pub(crate) fn new(state_space: usize) -> Self {
SectionBuilder {
lookup: vec![0; state_space],
}
}
pub(crate) fn build(&mut self, states: &[u16; SECTION_VOLUME]) -> Option<Section> {
let space = self.lookup.len();
let mut non_air = 0u16;
for &s in states {
non_air += u16::from(s != 0);
}
if non_air == 0 {
return None;
}
let mut ix = Box::new(Indexed {
idx: [0; SECTION_VOLUME],
palette: [0; 256],
len: 0,
non_air,
});
let mut len = 0usize;
let mut overflow = false;
for (dst, &s) in ix.idx.iter_mut().zip(states.iter()) {
let s = if usize::from(s) < space { s } else { 0 };
let slot = &mut self.lookup[usize::from(s)];
if *slot == 0 {
if len == 256 {
overflow = true;
break;
}
ix.palette[len] = s;
len += 1;
*slot = len as u16;
}
*dst = (*slot - 1) as u8;
}
self.reset(&ix.palette[..len]);
if overflow {
let mut d = Box::new(Direct {
states: *states,
non_air: 0,
});
let mut n = 0u16;
for s in &mut d.states {
if usize::from(*s) >= space {
*s = 0;
}
n += u16::from(*s != 0);
}
d.non_air = n;
return (n != 0).then_some(Section::Direct(d));
}
if len == 1 {
return Some(Section::Uniform(ix.palette[0]));
}
if ix.palette[..len].contains(&0) {
let air = ix.palette[..len].iter().position(|&s| s == 0).unwrap_or(0) as u8;
ix.non_air = ix.idx.iter().filter(|&&p| p != air).count() as u16;
if ix.non_air == 0 {
return None;
}
}
ix.len = len as u16;
Some(Section::Indexed(ix))
}
#[cfg_attr(not(feature = "v1_16_5"), allow(dead_code))]
pub(crate) fn build_packed(
&mut self,
remap: &[u16],
bits: u32,
data: &[u8],
) -> Option<Section> {
debug_assert!((4..=8).contains(&bits));
let reachable = 1usize << bits;
let mut palette = [0u16; 256];
let mut len = 0usize;
let mut to_palette = [0u8; 256];
let space = self.lookup.len();
for (w, slot_out) in to_palette.iter_mut().enumerate().take(reachable) {
let s = remap.get(w).copied().unwrap_or(0);
let s = if usize::from(s) < space { s } else { 0 };
let slot = &mut self.lookup[usize::from(s)];
if *slot == 0 {
palette[len] = s;
len += 1;
*slot = len as u16;
}
*slot_out = (*slot - 1) as u8;
if w >= remap.len() {
let air = *slot_out;
to_palette[w..reachable].fill(air);
break;
}
}
self.reset(&palette[..len]);
let mut ix = Box::new(Indexed {
idx: [0; SECTION_VOLUME],
palette: [0; 256],
len: 0,
non_air: 0,
});
let mut hist = [0u16; 256];
crate::packed::unpack_be(bits, data, SECTION_VOLUME, |i, v| {
let p = to_palette[(v & 255) as usize];
ix.idx[i] = p;
hist[usize::from(p)] += 1;
});
let mut remap2 = [0u8; 256];
let mut n = 0usize;
let mut non_air = 0u16;
for p in 0..len {
if hist[p] != 0 {
remap2[p] = n as u8;
ix.palette[n] = palette[p];
if palette[p] != 0 {
non_air += hist[p];
}
n += 1;
}
}
if non_air == 0 {
return None;
}
if n == 1 {
return Some(Section::Uniform(ix.palette[0]));
}
if n != len {
for p in &mut ix.idx {
*p = remap2[usize::from(*p)];
}
}
ix.len = n as u16;
ix.non_air = non_air;
Some(Section::Indexed(ix))
}
fn reset(&mut self, palette: &[u16]) {
for &s in palette {
self.lookup[usize::from(s)] = 0;
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn check_eq(s: &Option<Section>, expect: &[u16]) {
for (i, &e) in expect.iter().enumerate() {
let got = s.as_ref().map_or(0, |s| s.get(i));
assert_eq!(got, e, "index {i}");
}
let non_air = expect.iter().filter(|&&v| v != 0).count();
assert_eq!(s.as_ref().map_or(0, |s| usize::from(s.non_air())), non_air);
}
#[test]
fn build_variants() {
let mut b = SectionBuilder::new(4096);
assert!(b.build(&[0; 4096]).is_none());
assert!(matches!(b.build(&[16; 4096]), Some(Section::Uniform(16))));
let v: Vec<u16> = (0..4096).map(|i| (i % 7) as u16 * 16).collect();
let s = b.build(v.as_slice().try_into().unwrap());
assert!(matches!(s, Some(Section::Indexed(_))));
check_eq(&s, &v);
let v: Vec<u16> = (0..4096).map(|i| (i % 300) as u16).collect();
let s = b.build(v.as_slice().try_into().unwrap());
assert!(matches!(s, Some(Section::Direct(_))));
check_eq(&s, &v);
assert!(b.lookup.iter().all(|&x| x == 0));
}
#[test]
fn set_transitions() {
let mut b = SectionBuilder::new(4096);
let mut expect = vec![1u16; 4096];
let mut s = b.build(&[1; 4096]);
for (i, e) in expect.iter_mut().enumerate() {
let v = (i % 400) as u16;
*e = v;
let alive = s.as_mut().unwrap().set(i, v);
assert!(alive);
}
check_eq(&s, &expect);
assert!(matches!(s, Some(Section::Direct(_))));
for i in 0..4096usize {
let alive = s.as_mut().unwrap().set(i, 0);
assert_eq!(alive, i != 4095);
}
}
#[test]
fn palette_compaction_keeps_indexed() {
let mut b = SectionBuilder::new(4096);
let mut s = b.build(&[1; 4096]).unwrap();
for v in 2..1000u16 {
s.set(0, v);
}
assert!(matches!(s, Section::Indexed(_)), "{s:?}");
assert_eq!(s.get(0), 999);
assert_eq!(s.get(1), 1);
}
#[test]
fn single() {
let s = Section::single(block_index(1, 2, 3), 5).unwrap();
assert_eq!(s.get(block_index(1, 2, 3)), 5);
assert_eq!(s.get(0), 0);
assert_eq!(s.non_air(), 1);
assert!(Section::single(0, 0).is_none());
}
}