Changeset - 833d72fe1c06
[Not reviewed]
0 2 0
Christopher Esterhuyse - 5 years ago 2020-02-15 18:58:59
christopher.esterhuyse@gmail.com
newtypes
2 files changed with 29 insertions and 9 deletions:
0 comments (0 inline, 0 general)
Cargo.toml
Show inline comments
 
@@ -8,12 +8,13 @@ authors = [
 
edition = "2018"
 

	
 
[dependencies]
 
# hibitset = "0.6.2"
 

	
 
# runtime stuff
 
derive_more = "0.99.2"
 
getrandom = "0.1.14" # tiny crate. used to guess controller-id
 
take_mut = "0.2.2"
 
maplit = "1.0.2" # convenience macros
 
indexmap = "1.3.0" # hashsets/hashmaps with efficient arbitrary element removal
 

	
 
# network stuff
src/runtime/bits.rs
Show inline comments
 
@@ -201,20 +201,21 @@ impl BitMatrix {
 
    fn test(&self, at: Pair) -> bool {
 
        self.assert_within_bounds(at);
 
        let [o_of, o_in] = self.offsets_unchecked(at);
 
        unsafe { (*self.buffer.add(o_of) & 1 << o_in) != 0 }
 
    }
 

	
 
    fn batch_mut<'a, 'b>(&mut self, mut chunk_mut_fn: impl FnMut(&'b mut [u32])) {
 
    fn batch_mut<'a, 'b>(&mut self, mut chunk_mut_fn: impl FnMut(&'b mut [BitChunk])) {
 
        let row_chunks = Self::row_chunks(self.bounds.property) as usize;
 
        let column_chunks = Self::column_chunks(self.bounds.entity);
 
        let mut ptr = self.buffer;
 
        for _row in 0..column_chunks {
 
            let slice;
 
            unsafe {
 
                slice = std::slice::from_raw_parts_mut(ptr, row_chunks);
 
                let slicey = std::slice::from_raw_parts_mut(ptr, row_chunks);
 
                slice = std::mem::transmute(slicey);
 
                ptr = ptr.add(row_chunks);
 
            }
 
            chunk_mut_fn(slice);
 
        }
 
        if let Some(mask) = Self::last_row_chunk_mask(self.bounds.entity) {
 
            // TODO TEST
 
@@ -226,28 +227,33 @@ impl BitMatrix {
 
                    ptr = ptr.add(1);
 
                }
 
            }
 
        }
 
    }
 

	
 
    /// given:
 
    /// 1. a buffer to work with
 
    /// 2. a _fold function_ for combining the properties of a given entity
 
    ///    and returning a new derived property (working )
 
    fn iter_entities_where<'a, 'b>(
 
        &'a self,
 
        buf: &'b mut Vec<u32>,
 
        mut fold_fn: impl FnMut(&'b [u32]) -> u32,
 
    ) -> impl Iterator<Item = u32> + 'b {
 
        mut fold_fn: impl FnMut(&'b [BitChunk]) -> BitChunk,
 
    ) -> BitChunkIter<std::vec::Drain<'b, u32>> {
 
        let buf_start = buf.len();
 
        let row_chunks = Self::row_chunks(self.bounds.property) as usize;
 
        let column_chunks = Self::column_chunks(self.bounds.entity);
 
        let mut ptr = self.buffer;
 
        for _row in 0..column_chunks {
 
            let slice;
 
            unsafe {
 
                slice = std::slice::from_raw_parts(ptr, row_chunks);
 
                let slicey = std::slice::from_raw_parts(ptr, row_chunks);
 
                slice = std::mem::transmute(slicey);
 
                ptr = ptr.add(row_chunks);
 
            }
 
            buf.push(fold_fn(slice));
 
            buf.push(fold_fn(slice).0);
 
        }
 
        if let Some(mask) = Self::last_row_chunk_mask(self.bounds.entity) {
 
            *buf.iter_mut().last().unwrap() &= mask;
 
        }
 
        BitChunkIter::new(buf.drain(buf_start..))
 
    }
 
@@ -259,28 +265,41 @@ impl BitMatrix {
 
            // 2. size is always a multiple of 4 and 4-aligned
 
            std::alloc::Layout::from_size_align_unchecked(4 * total_chunks.max(1), 4)
 
        }
 
    }
 
}
 

	
 
// TODO newtypes for:
 
// 1. BitChunk(u32) for fold fn
 
// 1. Entity(u32) and Property(u32)
 

	
 
use derive_more::*;
 
#[derive(Copy, Clone, BitAnd, Not, BitOr, BitXor, BitAndAssign, BitOrAssign, BitXorAssign)]
 
struct BitChunk(u32);
 
const TRUE: BitChunk = BitChunk(!0);
 
const FALSE: BitChunk = BitChunk(0);
 

	
 
#[test]
 
fn matrix_test() {
 
    let mut m = BitMatrix::new(Pair { entity: 50, property: 3 });
 
    m.set([2, 0].into());
 
    m.set([40, 1].into());
 
    m.set([40, 2].into());
 
    m.set([40, 0].into());
 
    println!("{:?}", &m);
 

	
 
    m.batch_mut(|p| p[0] = !0);
 
    m.batch_mut(|p| {
 
        p[0] = FALSE | p[1];
 
        p[2] ^= TRUE;
 
    });
 
    println!("{:?}", &m);
 

	
 
    let mut buf = vec![];
 
    for index in m.iter_entities_where(&mut buf, move |p| p[0] ^ p[1] ^ p[2]) {
 
    for index in m.iter_entities_where(&mut buf, move |p| TRUE ^ p[1] ^ p[2]) {
 
        println!("index {}", index);
 
    }
 
    for index in m.iter_entities_where(&mut buf, move |p| (p[0] | p[1]) & p[2]) {
 
    for index in m.iter_entities_where(&mut buf, move |p| p[0] & !p[2]) {
 
        println!("index {}", index);
 
    }
 
}
 

	
 
// TODO something still a bit screwy with 1s where theere should be zeroes
0 comments (0 inline, 0 general)