Changeset - 3fdb178e94b2
[Not reviewed]
0 1 0
Christopher Esterhuyse - 5 years ago 2020-02-14 14:55:43
christopher.esterhuyse@gmail.com
matrices seem very promising
1 file changed with 5 insertions and 76 deletions:
0 comments (0 inline, 0 general)
src/runtime/ecs.rs
Show inline comments
 
@@ -608,13 +608,13 @@ fn ceiling_to_mul_32(value: usize) -> usize {
 
    (value + 31) & !31
 
}
 
impl Drop for FlagMatrix {
 
    fn drop(&mut self) {
 
        let layout = Self::layout_for(self.u32s_total);
 
        unsafe {
 
            //?
 
            // ?
 
            std::alloc::dealloc(self.bytes as *mut u8, layout);
 
        }
 
    }
 
}
 
impl FlagMatrix {
 
    fn get_dims(&self) -> &[usize; 2] {
 
@@ -637,16 +637,14 @@ impl FlagMatrix {
 
        };
 

	
 
        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)] => {
 
                assert!(new_u32s_total > self.u32s_total);
 
                // realloc only!
 
                dbg!("REALLOC ONLY");
 
                println!("BEFORE {:?}", self.bytes);
 
                // assert!(new_u32s_total > self.u32s_total);
 
                let old_layout = Self::layout_for(self.u32s_total);
 
                let new_layout = Self::layout_for(new_u32s_total);
 
                let new_bytes = unsafe {
 
                    let new_bytes = std::alloc::alloc(new_layout) as *mut u32;
 
                    // copy the previous total
 
                    self.bytes.copy_to_nonoverlapping(new_bytes, self.u32s_total);
 
@@ -660,15 +658,14 @@ impl FlagMatrix {
 
                };
 
                self.bytes = new_bytes;
 
                println!("AFTER {:?}", self.bytes);
 
                self.u32s_total = new_u32s_total;
 
            }
 
            [Some(new_u32s_per_row), None] => {
 
                assert!(new_u32s_per_row > self.u32s_per_row);
 
                // shift only!
 
                dbg!("SHIFT ONLY");
 
                // assert!(new_u32s_per_row > self.u32s_per_row);
 
                for r in (0..self.dims[0]).rev() {
 
                    // iterate in REVERSE order because new row[n] may overwrite old row[n+m]
 
                    unsafe {
 
                        let src = self.bytes.add(r * self.u32s_per_row);
 
                        let dest = self.bytes.add(r * new_u32s_per_row);
 
                        // copy the used prefix
 
@@ -678,16 +675,15 @@ impl FlagMatrix {
 
                            .write_bytes(0u8, new_u32s_per_row - self.u32s_per_row);
 
                    }
 
                }
 
                self.u32s_per_row = new_u32s_per_row;
 
            }
 
            [Some(new_u32s_per_row), Some(new_u32s_total)] => {
 
                assert!(new_u32s_total > self.u32s_total);
 
                assert!(new_u32s_per_row > self.u32s_per_row);
 
                // alloc AND shift!
 
                dbg!("BOTH");
 
                // assert!(new_u32s_total > self.u32s_total);
 
                // assert!(new_u32s_per_row > self.u32s_per_row);
 
                let old_layout = Self::layout_for(self.u32s_total);
 
                let new_layout = Self::layout_for(new_u32s_total);
 
                let new_bytes = unsafe { std::alloc::alloc(new_layout) as *mut u32 };
 
                for r in 0..self.dims[0] {
 
                    // iterate forwards over rows!
 
                    unsafe {
 
@@ -791,79 +787,12 @@ impl FlagMatrix {
 
        });
 
        // 3. return an unsafe iterator over column indices
 
        BitChunkIter::new(chunk_iter).filter(move |&x| x < self.dims[1])
 
    }
 
}
 

	
 
#[derive(Debug, Copy, Clone)]
 
enum ColumnCombinator<'a> {
 
    Row(usize),
 
    True,
 
    False,
 
    And(&'a ColumnCombinator<'a>, &'a ColumnCombinator<'a>),
 
    Or(&'a ColumnCombinator<'a>, &'a ColumnCombinator<'a>),
 
    Not(&'a ColumnCombinator<'a>),
 
}
 
struct FlaggedColumnIter<'a> {
 
    flag_matrix: &'a FlagMatrix,
 
    next_column_chunk: usize,
 
    combinator: &'a ColumnCombinator<'a>,
 
}
 
impl<'a> FlaggedColumnIter<'a> {
 
    fn new(flag_matrix: &'a FlagMatrix, combinator: &'a ColumnCombinator<'a>) -> Self {
 
        Self { flag_matrix, combinator, next_column_chunk: 0 }
 
    }
 
    /// #Safety: bounds on self.next_column_chunk have been checked with self.flag_matrix
 
    /// retrieves the column chunk at self.next_column_chunk
 
    unsafe fn combine(&self, c: &ColumnCombinator) -> u32 {
 
        use ColumnCombinator as Cc;
 
        match c {
 
            Cc::Row(row) => self.flag_matrix.copy_chunk_unchecked(*row, self.next_column_chunk),
 
            Cc::False => 0u32,
 
            Cc::True => !0u32,
 
            Cc::And(a, b) => self.combine(a) & self.combine(b),
 
            Cc::Or(a, b) => self.combine(a) | self.combine(b),
 
            Cc::Not(a) => !self.combine(a),
 
        }
 
    }
 
}
 
impl<'a> Iterator for FlaggedColumnIter<'a> {
 
    type Item = u32;
 
    fn next(&mut self) -> Option<Self::Item> {
 
        struct CombineCtx<'a> {
 
            flag_matrix: &'a FlagMatrix,
 
            nth_col_chunk: usize,
 
        }
 
        if self.next_column_chunk >= self.flag_matrix.u32s_per_row {
 
            None
 
        } else {
 
            let x = unsafe { self.combine(self.combinator) };
 
            self.next_column_chunk += 1;
 
            Some(x)
 
        }
 
    }
 
}
 

	
 
struct ColumnIter<'a> {
 
    bit_chunk_iter: BitChunkIter<FlaggedColumnIter<'a>>,
 
}
 
impl<'a> ColumnIter<'a> {
 
    fn new(m: &'a FlagMatrix, combinator: &'a ColumnCombinator) -> Self {
 
        let iter = FlaggedColumnIter::new(m, combinator);
 
        let bit_chunk_iter = BitChunkIter::new(iter);
 
        Self { bit_chunk_iter }
 
    }
 
}
 
impl<'a> Iterator for ColumnIter<'a> {
 
    type Item = usize;
 
    fn next(&mut self) -> Option<Self::Item> {
 
        let v: Option<usize> = self.bit_chunk_iter.next();
 
        v.filter(|&x| x < self.bit_chunk_iter.chunk_iter.flag_matrix.dims[1])
 
    }
 
}
 

	
 
#[test]
 
fn matrix() {
 
    let mut m = FlagMatrix::new([5, 5], [0, 0]);
 
    for i in 0..5 {
 
        m.set([0, i]);
 
        m.set([i, i]);
0 comments (0 inline, 0 general)