onecodex / mmap-bitvec

Rust implementation of a mmap-based bit vector
MIT License
6 stars 0 forks source link

More natural indexing for BitVec #1

Open bovee opened 7 years ago

bovee commented 7 years ago

It would be nice to write bitvec[1] = true rather than bitvec.set(1, true).

This is essentially blocked on: https://github.com/rust-lang/rfcs/issues/997

The standard library bit vector has a hacky workaround for getting (and no way to set) indices, but this won't work for us for ranges: https://doc.rust-lang.org/1.2.0/src/collections/bit.rs.html#168-180

bovee commented 6 years ago

The "hacky workaround " above would look like:

impl Index<usize> for BitVec {
    // fairly hacky; it would be nice to have Index<Range>, IndexMut<usize>,
    // and IndexMut<Range> methods too, but since these have to return
    // references that's a little tricky
    type Output = bool;
    fn index(&self, idx: usize) -> &Self::Output {
        const TRUE: &'static bool = &true;
        const FALSE: &'static bool = &false;
        match self.get(idx) {
            true => TRUE,
            false => FALSE,
        }
    }
}

but there's not way to have a compatible setter and this is gross to start with (and slower than the get( method we already have).