Changeset - 3740bd8ebfdd
[Not reviewed]
0 1 0
Christopher Esterhuyse - 5 years ago 2020-02-14 11:46:13
christopher.esterhuyse@gmail.com
trying out a matrix representation for components
1 file changed with 1 insertions and 43 deletions:
0 comments (0 inline, 0 general)
src/runtime/ecs.rs
Show inline comments
 
@@ -72,15 +72,12 @@ impl BitSet {
 
                self.0.pop();
 
            }
 
        }
 
    }
 
}
 

	
 
#[derive(Debug, Default)]
 
struct BitMasks(HashMap<(ChannelId, bool), BitSet>);
 

	
 
struct BitChunkIter<I: Iterator<Item = u32>> {
 
    chunk_iter: I,
 
    next_bit_index: usize,
 
    cached: Option<u32>, // None <=> iterator is done
 
}
 

	
 
@@ -155,59 +152,20 @@ impl Iterator for AndChunkIter<'_> {
 
            let b = *b.get(old_chunk_index)?;
 
            Some(a & b)
 
        })
 
    }
 
}
 

	
 
/// Returns an iterator over chunks for bits in range 0..bits_to_go but skipping
 
/// indices for which ANY of the given bitsets has a 1
 
struct NoneChunkIter<'a> {
 
    // this value is not overwritten during iteration
 
    // invariant: !sets.is_empty()
 
    sets: &'a [&'a [u32]],
 
    next_chunk_index: usize,
 
    bits_to_go: usize,
 
}
 
impl<'a> NoneChunkIter<'a> {
 
    /// a set of bitsets. the u32s of each are in ascending order of significant digits
 
    /// i.e., &[0,1] means {1<<32, 0} while &[0,1] is identical to &[1] and means {1}.
 
    fn new(sets: &'a [&'a [u32]], max_bit: usize) -> Self {
 
        let sets = if sets.is_empty() { &[&[] as &[_]] } else { sets };
 
        Self { sets, next_chunk_index: 0, bits_to_go: max_bit }
 
    }
 
}
 
impl Iterator for NoneChunkIter<'_> {
 
    type Item = u32;
 
    fn next(&mut self) -> Option<u32> {
 
        let neutral = match self.bits_to_go {
 
            0 => None,
 
            x @ 1..=31 => Some(!0u32 >> (32 - x)),
 
            _ => Some(!0u32),
 
        };
 
        self.bits_to_go = self.bits_to_go.saturating_sub(32);
 

	
 
        let old_chunk_index = self.next_chunk_index;
 
        self.next_chunk_index += 1;
 

	
 
        self.sets.iter().fold(neutral, move |a, b| {
 
            let a = a?;
 
            let b = *b.get(old_chunk_index)?;
 
            Some(a & !b)
 
        })
 
    }
 
}
 

	
 
#[test]
 
fn test_bit_iter() {
 
    static SETS: &[&[u32]] = &[
 
        //
 
        &[0b101001, 0b101001],
 
        &[0b100001, 0b101001],
 
    ];
 
    let _ = BitChunkIter::new(AndChunkIter::new(SETS));
 
    let iter = BitChunkIter::new(NoneChunkIter::new(SETS, 9));
 
    let iter = BitChunkIter::new(AndChunkIter::new(SETS));
 
    let indices = iter.collect::<Vec<_>>();
 
    println!("indices {:?}", indices);
 
}
 

	
 
enum Entity {
 
    Payload(Payload),
0 comments (0 inline, 0 general)