Changeset - 3da401203200
[Not reviewed]
0 1 0
Christopher Esterhuyse - 5 years ago 2020-02-14 17:47:52
christopher.esterhuyse@gmail.com
matrices seem very promising
1 file changed with 42 insertions and 4 deletions:
0 comments (0 inline, 0 general)
src/runtime/ecs.rs
Show inline comments
 
@@ -607,14 +607,13 @@ impl Debug for FlagMatrix {
 
            write!(f, "|\n")?;
 
        }
 
        Ok(())
 
    }
 
}
 

	
 
// invariant: all bits outside of reach of columns or rows BUT inside the
 
// allocation, are zero.
 
// invariant: all bits outside of 0..columns and 0..rows BUT in the allocated space are ZERO
 
struct FlagMatrix {
 
    bytes: *mut u32,
 
    u32s_total: usize,
 
    u32s_per_row: usize,
 
    dims: [usize; 2],
 
}
 
@@ -633,12 +632,41 @@ impl Drop for FlagMatrix {
 
}
 
impl FlagMatrix {
 
    fn get_dims(&self) -> &[usize; 2] {
 
        &self.dims
 
    }
 

	
 
    fn set_entire_row(&mut self, row: usize) {
 
        assert!(row < self.dims[0]);
 
        let mut cols_left = self.dims[1];
 
        unsafe {
 
            let mut ptr = self.bytes.add(self.offset_of_chunk_unchecked([row, 0]));
 
            while cols_left >= 32 {
 
                *ptr = !0u32;
 
                cols_left -= 32;
 
                ptr = ptr.add(1);
 
            }
 
            if cols_left > 0 {
 
                // jagged chunk!
 
                *ptr |= (!0) >> (32 - cols_left);
 
            }
 
        }
 
    }
 
    fn unset_entire_row(&mut self, row: usize) {
 
        assert!(row < self.dims[0]);
 
        let mut cols_left = self.dims[1];
 
        unsafe {
 
            let mut ptr = self.bytes.add(self.offset_of_chunk_unchecked([row, 0]));
 
            while cols_left > 0 {
 
                *ptr = 0u32;
 
                cols_left -= 32;
 
                ptr = ptr.add(1);
 
            }
 
        }
 
    }
 

	
 
    fn reshape(&mut self, new_dims: [usize; 2]) {
 
        dbg!(self.u32s_total, self.u32s_per_row);
 

	
 
        // 1. calc new u32s_per_row
 
        let new_u32s_per_row = match ceiling_to_mul_32(new_dims[1]) / 32 {
 
            min if min > self.u32s_per_row => Some(min * 2),
 
@@ -674,13 +702,22 @@ impl FlagMatrix {
 
                    let o_of = self.offset_of_chunk_unchecked([row, new_min_u32_per_row - 1]);
 
                    unsafe { *self.bytes.add(o_of) &= mask };
 
                }
 
            }
 
        }
 

	
 
        // TODO what about rows?
 
        // 4. if we won't do a new allocation, zero any bit no longer in rows
 
        if new_dims[0] < self.dims[0] && new_u32s_total.is_none() {
 
            // zero all bytes from beginning of first removed row,
 
            // to end of last removed row
 
            unsafe {
 
                self.bytes
 
                    .add(self.offset_of_chunk_unchecked([new_dims[0], 0]))
 
                    .write_bytes(0u8, self.u32s_per_row * (self.dims[0] - new_dims[0]));
 
            }
 
        }
 

	
 
        dbg!(new_u32s_per_row, new_u32s_total);
 
        match [new_u32s_per_row, new_u32s_total] {
 
            [None, None] => { /* do nothing */ }
 
            [None, Some(new_u32s_total)] => {
 
                // realloc only! column alignment is still OK
 
@@ -836,17 +873,18 @@ impl FlagMatrix {
 
        BitChunkIter::new(chunk_iter)
 
    }
 
}
 

	
 
#[test]
 
fn matrix() {
 
    let mut m = FlagMatrix::new([5, 5], [0, 0]);
 
    let mut m = FlagMatrix::new([6, 6], [0, 0]);
 
    for i in 0..5 {
 
        m.set([0, i]);
 
        m.set([i, i]);
 
    }
 
    m.set_entire_row(5);
 
    println!("{:?}", &m);
 
    m.reshape([6, 40]);
 
    let iter = m.col_iter_t1fn(0, &[1, 2, 3]);
 
    for c in iter {
 
        println!("{:?}", c);
 
    }
0 comments (0 inline, 0 general)