Changeset - 166c201a1d43
[Not reviewed]
0 1 0
Christopher Esterhuyse - 5 years ago 2020-02-20 16:52:55
christopher.esterhuyse@gmail.com
bitsets are so great
1 file changed with 1 insertions and 0 deletions:
0 comments (0 inline, 0 general)
src/runtime/experimental/vec_storage.rs
Show inline comments
 
@@ -156,48 +156,49 @@ impl<T> VecStorage<T> {
 
        } else {
 
            unsafe {
 
                // index is within bounds
 
                self.get_occupied_unchecked(i)
 
            }
 
        }
 
    }
 
    pub fn get_occupied_mut(&mut self, i: usize) -> Option<&mut T> {
 
        // SAFE: bitvec bounds ensured by invariant E
 
        if i < self.data.len() && unsafe { self.occupied.contains(i) } {
 
            unsafe {
 
                // 1. index is within bounds
 
                // 2. Invariant A => reading valid ata
 
                Some(&mut *self.data.get_unchecked_mut(i).as_mut_ptr())
 
            }
 
        } else {
 
            None
 
        }
 
    }
 
    pub fn new_reserved(&mut self) -> usize {
 
        if let Some(i) = self.vacant.pop_first() {
 
            i
 
        } else {
 
            let bitsets_need_another_chunk = self.data.len() % usize_bits() == 0;
 
            // every (usize_bits())th time self.data grows by 1, bitsets grow by usize_bits().
 
            if bitsets_need_another_chunk {
 
                self.vacant.0.push(0usize);
 
                self.occupied.0.push(0usize);
 
            }
 
            self.data.push(MaybeUninit::uninit());
 
            self.data.len() - 1
 
        }
 
    }
 
    pub fn occupy_reserved(&mut self, i: usize, t: T) {
 
        // SAFE: bitvec bounds ensured by invariant E
 
        assert!(i < self.data.len());
 
        // element is within bounds
 
        assert!(unsafe { !self.occupied.contains(i) && !self.vacant.contains(i) });
 
        // element is surely reserved
 
        unsafe {
 
            // 1. invariant C => write is within bounds
 
            // 2. i WAS reserved => no initialized data is being overwritten
 
            self.data.get_unchecked_mut(i).as_mut_ptr().write(t);
 
            self.occupied.insert(i);
 
        };
 
    }
 
    pub fn new_occupied(&mut self, t: T) -> usize {
 
        let i = self.new_reserved();
 
        unsafe {
0 comments (0 inline, 0 general)