Changeset - d72289ecc27b
[Not reviewed]
0 1 0
Christopher Esterhuyse - 5 years ago 2020-02-12 18:17:07
christopheresterhuyse@gmail.com
playing around with new predicate=>state,payload storages. toward faster querying but also toward deviation detection by way of intelligible error handling
1 file changed with 8 insertions and 3 deletions:
0 comments (0 inline, 0 general)
src/runtime/polyp.rs
Show inline comments
 
@@ -202,79 +202,84 @@ struct BitSet(Vec<u32>);
 
#[derive(Debug, Default)]
 
struct BitMasks(HashMap<(ChannelId, bool), BitSet>);
 

	
 
struct BitSetAndIter<'a> {
 
    // this value is immutable
 
    // invariant: !sets.is_empty()
 
    sets: &'a [&'a [u32]],
 
    next_u32_index: usize, // invariant: in 0..32 while iterating
 
    next_bit_index: usize,
 
    cached: Option<u32>, // None <=> iterator is done
 
}
 
impl<'a> BitSetAndIter<'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]]) -> Self {
 
        const EMPTY_SINGLETON: &[&[u32]] = &[&[]];
 
        let sets = if sets.is_empty() { EMPTY_SINGLETON } else { sets };
 
        Self { sets, next_u32_index: 0, next_bit_index: 0, cached: Self::nth_u32(sets, 0) }
 
    }
 
    fn nth_u32(sets: &'a [&'a [u32]], index: usize) -> Option<u32> {
 
        sets.iter().fold(Some(!0), |a, b| {
 
            let b = b.get(index)?;
 
            Some(a? & b)
 
        })
 
    }
 
    fn next_chunk(&mut self) {
 
        self.next_bit_index = 0;
 
        self.next_u32_index += 1;
 
        self.cached = Self::nth_u32(self.sets, self.next_u32_index);
 
    }
 
}
 
impl Iterator for BitSetAndIter<'_> {
 
    type Item = usize;
 
    fn next(&mut self) -> Option<Self::Item> {
 
        loop {
 
            println!("LOOP");
 
            // get cached chunk. If none exists, iterator is done.
 
            let mut chunk = self.cached?;
 
            if chunk == 0 {
 
                self.next_chunk();
 
                continue;
 
            }
 
            // this chunk encodes 1+ Items to yield
 
            // shift the contents of chunk until the least significant bit is 1
 

	
 
            #[inline(always)]
 
            fn shifty(chunk: &mut u32, shift_by: usize, next_bit_index: &mut usize) {
 
                if *chunk & ((1 << shift_by) - 1) == 0 {
 
                    *next_bit_index += shift_by;
 
                    *chunk >>= shift_by;
 
                }
 
                println!("{:#032b}", *chunk);
 
            }
 
            shifty(&mut chunk, 16, &mut self.next_bit_index);
 
            shifty(&mut chunk, 08, &mut self.next_bit_index);
 
            shifty(&mut chunk, 04, &mut self.next_bit_index);
 
            shifty(&mut chunk, 02, &mut self.next_bit_index);
 
            shifty(&mut chunk, 01, &mut self.next_bit_index);
 
            // assert(chunk & 1 == 1)
 

	
 
            self.next_bit_index += 1;
 
            self.cached = Some(chunk >> 1);
 
            if chunk > 0 {
 
                let index = self.next_u32_index * 32 + (self.next_bit_index - 1);
 
                // assert((self.next_bit_index-1) < 32)
 
                // because if chunk was shifted 32+ places, its contents would be zero.
 
                return Some(index);
 
            }
 
        }
 
    }
 
}
 

	
 
#[test]
 
fn test_bit_iter() {
 
    static SETS: &[&[u32]] = &[
 
        //
 
        &[0b100011000000100101101],
 
        &[0b100001000000000110100],
 
        &[0b100001000100010100110],
 
        &[0b1, 0b1, 0b1, 0b1, 0b1],
 
        &[0b1, 0b1, 0b1],
 
        &[0b1, 0b1, 0b0, 0b1],
 
        &[0b1, 0b1, 0b1, 0b1],
 
    ];
 
    let indices = BitSetAndIter::new(SETS).collect::<Vec<_>>();
 
    println!("indices {:?}", indices);
 
}
0 comments (0 inline, 0 general)