diff options
Diffstat (limited to 'engine/src/util.rs')
| -rw-r--r-- | engine/src/util.rs | 344 |
1 files changed, 288 insertions, 56 deletions
diff --git a/engine/src/util.rs b/engine/src/util.rs index a505a38..fc4c1c8 100644 --- a/engine/src/util.rs +++ b/engine/src/util.rs @@ -1,18 +1,265 @@ -macro_rules! try_option { - ($expr: expr) => { - match $expr { - Ok(value) => value, - Err(err) => { - return Some(Err(err.into())); - } +use std::fmt::Debug; + +use crate::ecs::util::StreamingIterator; + +pub trait OptionExt<T> +{ + /// Substitute for the currently experimental function + /// [`Option::get_or_try_insert_with`]. + /// See <https://github.com/rust-lang/rust/issues/143648> + fn get_or_try_insert_with_fn<Err>( + &mut self, + func: impl FnOnce() -> Result<T, Err>, + ) -> Result<&mut T, Err>; +} + +impl<T> OptionExt<T> for Option<T> +{ + fn get_or_try_insert_with_fn<Err>( + &mut self, + func: impl FnOnce() -> Result<T, Err>, + ) -> Result<&mut T, Err> + { + if self.is_none() { + *self = Some(func()?); } - }; + + Ok(unsafe { self.as_mut().unwrap_unchecked() }) + } +} + +#[derive(Debug)] +pub struct BitArray<const SIZE: usize, const BITS_PER_ITEM: usize> +{ + inner: [u8; SIZE], +} + +impl<const SIZE: usize, const BITS_PER_ITEM: usize> BitArray<SIZE, BITS_PER_ITEM> +{ + const ITEM_MASK: u8 = !(u8::MAX << BITS_PER_ITEM); + + pub fn new() -> Self + { + assert!(BITS_PER_ITEM > 1); + assert!(BITS_PER_ITEM <= 8); + assert_eq!(BITS_PER_ITEM % 2, 0); + + Self { inner: [0; SIZE] } + } + + pub fn get(&self, item_index: usize) -> u8 + { + let bit_index = item_index * BITS_PER_ITEM; + + let byte_index = bit_index / 8; + + let bit_index_in_byte = bit_index - (byte_index * 8); + + (self.inner[byte_index] >> (bit_index_in_byte)) & Self::ITEM_MASK + } + + #[tracing::instrument(skip(self))] + pub fn clear_and_set(&mut self, item_index: usize, item_bits: u8, clear_mask: u8) + { + let item_bits = item_bits & Self::ITEM_MASK; + + let clear_mask = clear_mask & Self::ITEM_MASK; + + let bit_index = item_index * BITS_PER_ITEM; + + let byte_index = bit_index / 8; + + let bit_index_in_byte = bit_index - (byte_index * 8); + + tracing::trace!( + bit_index, + byte_index, + bit_index_in_byte, + "Setting item bits" + ); + + self.inner[byte_index] &= !(clear_mask << bit_index_in_byte); + + self.inner[byte_index] |= item_bits << bit_index_in_byte; + } + + pub fn iter_occupied(&self) -> BitArrayOccupiedIter<'_, BITS_PER_ITEM> + { + BitArrayOccupiedIter { + inner: self.inner.iter().enumerate().peekable(), + mask: u8::MAX, + } + } + + pub fn iter_occupied_mut(&mut self) -> BitArrayOccupiedMutIter<'_, BITS_PER_ITEM> + { + BitArrayOccupiedMutIter { + inner: self.inner.iter_mut().enumerate().peekable(), + mask: u8::MAX, + } + } + + pub fn bytes_mut(&mut self) -> &mut [u8] + { + &mut self.inner + } +} + +impl<const SIZE: usize, const BITS_PER_ITEM: usize> Default + for BitArray<SIZE, BITS_PER_ITEM> +{ + fn default() -> Self + { + Self::new() + } } -use std::mem::ManuallyDrop; -use std::ops::{Deref, DerefMut}; +pub struct BitArrayOccupiedIter<'a, const BITS_PER_ITEM: usize> +{ + inner: std::iter::Peekable<std::iter::Enumerate<std::slice::Iter<'a, u8>>>, + mask: u8, +} + +impl<const BITS_PER_ITEM: usize> BitArrayOccupiedIter<'_, BITS_PER_ITEM> +{ + const ITEM_MASK: u8 = !(u8::MAX << BITS_PER_ITEM); +} + +impl<const BITS_PER_ITEM: usize> Iterator for BitArrayOccupiedIter<'_, BITS_PER_ITEM> +{ + type Item = (usize, u8); + + fn next(&mut self) -> Option<Self::Item> + { + let (byte_masked, byte_index, item_bit_index_in_byte) = loop { + let current = self.inner.peek().and_then(|(byte_index, byte)| { + let byte_masked = **byte & self.mask; + + let lowest_one = byte_masked.lowest_one()?; + + let item_bit_index_in_byte = match lowest_one as usize % BITS_PER_ITEM { + 0 => lowest_one as usize, + remainder => lowest_one as usize - remainder, + }; + + Some((byte_masked, byte_index, item_bit_index_in_byte)) + }); + + let Some((byte_masked, byte_index, item_bit_index_in_byte)) = current else { + let _ = self.inner.next()?; + + self.mask = u8::MAX; + + continue; + }; + + break (byte_masked, byte_index, item_bit_index_in_byte); + }; + + let item_bits = (byte_masked >> (item_bit_index_in_byte)) & Self::ITEM_MASK; + + self.mask &= (!Self::ITEM_MASK) << item_bit_index_in_byte; -pub(crate) use try_option; + let item_index_in_byte = item_bit_index_in_byte / BITS_PER_ITEM; + + let prev_bytes_item_cnt = (byte_index * 8) / BITS_PER_ITEM; + + let index = prev_bytes_item_cnt + item_index_in_byte; + + Some((index, item_bits)) + } +} + +pub struct BitArrayOccupiedMutIter<'a, const BITS_PER_ITEM: usize> +{ + inner: std::iter::Peekable<std::iter::Enumerate<std::slice::IterMut<'a, u8>>>, + mask: u8, +} + +impl<const BITS_PER_ITEM: usize> BitArrayOccupiedMutIter<'_, BITS_PER_ITEM> +{ + const ITEM_MASK: u8 = !(u8::MAX << BITS_PER_ITEM); +} + +impl<const BITS_PER_ITEM: usize> StreamingIterator + for BitArrayOccupiedMutIter<'_, BITS_PER_ITEM> +{ + type Item<'a> + = BitArrayItemMut<'a, BITS_PER_ITEM> + where + Self: 'a; + + fn streaming_next(&mut self) -> Option<Self::Item<'_>> + { + let (byte_masked, byte_index, item_bit_index_in_byte) = loop { + let current = self.inner.peek().and_then(|(byte_index, byte)| { + let byte_masked = **byte & self.mask; + + let lowest_one = byte_masked.lowest_one()?; + + let item_bit_index_in_byte = match lowest_one as usize % BITS_PER_ITEM { + 0 => lowest_one as usize, + remainder => lowest_one as usize - remainder, + }; + + Some((byte_masked, byte_index, item_bit_index_in_byte)) + }); + + let Some((byte_masked, byte_index, item_bit_index_in_byte)) = current else { + let _ = self.inner.next()?; + + self.mask = u8::MAX; + + continue; + }; + + break (byte_masked, byte_index, item_bit_index_in_byte); + }; + + let item_bits = (byte_masked >> (item_bit_index_in_byte)) & Self::ITEM_MASK; + + self.mask &= (!Self::ITEM_MASK) << item_bit_index_in_byte; + + let item_index_in_byte = item_bit_index_in_byte / BITS_PER_ITEM; + + let prev_bytes_item_cnt = (byte_index * 8) / BITS_PER_ITEM; + + let index = prev_bytes_item_cnt + item_index_in_byte; + + Some(BitArrayItemMut { + index, + bits: item_bits, + byte: self.inner.peek_mut().unwrap().1, + bit_index_in_byte: item_bit_index_in_byte, + }) + } +} + +pub struct BitArrayItemMut<'a, const BITS_PER_ITEM: usize> +{ + pub index: usize, + pub bits: u8, + byte: &'a mut u8, + bit_index_in_byte: usize, +} + +impl<const BITS_PER_ITEM: usize> BitArrayItemMut<'_, BITS_PER_ITEM> +{ + const ITEM_MASK: u8 = !(u8::MAX << BITS_PER_ITEM); + + pub fn clear_and_set(&mut self, new_bits: u8, clear_mask: u8) + { + let new_bits = new_bits & Self::ITEM_MASK; + + let clear_mask = clear_mask & Self::ITEM_MASK; + + *self.byte &= !(clear_mask << self.bit_index_in_byte); + + *self.byte |= new_bits << self.bit_index_in_byte; + + self.bits = new_bits; + } +} macro_rules! or { (($($tt: tt)+) else ($($else_tt: tt)*)) => { @@ -26,6 +273,18 @@ macro_rules! or { pub(crate) use or; +#[macro_export] +macro_rules! expand_map_opt { + ($in: tt, no_occurance=($($no_occurance: tt)*), occurance=($($occurance: tt)*)) => { + $($occurance)* + }; + + (, no_occurance=($($no_occurance: tt)*), occurance=($($occurance: tt)*)) => { + $($no_occurance)* + }; +} + +#[macro_export] macro_rules! builder { ( $(#[doc = $doc: literal])* @@ -37,7 +296,8 @@ macro_rules! builder { $visibility: vis struct $name: ident { $( - $(#[$field_attr: meta])* + $(#[doc = $field_doc: literal])* + $(#[builder(skip_generate_fn$($field_skip_generate_fn: tt)?)])? $field_visibility: vis $field: ident: $field_type: ty, )* } @@ -47,7 +307,7 @@ macro_rules! builder { $visibility struct $name { $( - $(#[$field_attr])* + $(#[doc = $field_doc])* $field_visibility $field: $field_type, )* } @@ -63,16 +323,23 @@ macro_rules! builder { impl $builder_name { $( - #[must_use] - $visibility fn $field(mut self, $field: $field_type) -> Self - { - self.$field = $field; - self - } + $crate::expand_map_opt!( + $(true $($field_skip_generate_fn)?)?, + no_occurance=( + #[must_use] + $visibility fn $field(mut self, $field: $field_type) -> Self + { + self.$field = $field; + self + } + ), + occurance=() + ); )* #[must_use] - $visibility fn build(self) -> $name { + $visibility const fn build(self) -> $name + { $name { $( $field: self.$field, @@ -83,6 +350,7 @@ macro_rules! builder { impl From<$name> for $builder_name { + #[allow(unused_variables)] fn from(built: $name) -> Self { Self { @@ -94,39 +362,3 @@ macro_rules! builder { } }; } - -pub(crate) use builder; - -/// Wrapper that ensures the contained value will never be dropped. -#[derive(Debug)] -pub struct NeverDrop<Value> -{ - value: ManuallyDrop<Value>, -} - -impl<Value> NeverDrop<Value> -{ - #[must_use] - pub fn new(value: Value) -> Self - { - Self { value: ManuallyDrop::new(value) } - } -} - -impl<Value> Deref for NeverDrop<Value> -{ - type Target = Value; - - fn deref(&self) -> &Self::Target - { - &self.value - } -} - -impl<Value> DerefMut for NeverDrop<Value> -{ - fn deref_mut(&mut self) -> &mut Self::Target - { - &mut self.value - } -} |
