1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use core::ptr::{RawPtr, set_memory};

// TODO: make an actual bitmap.

/// A vector of 2-bit values.
pub struct Bitv {
    pub storage: *mut u32
}

impl Bitv {
    #[inline]
    pub fn get(&self, i: uint) -> u8 {
        let w = (i / 16) as int;
        let b = (i % 16) * 2;
        unsafe {
            (*self.storage.offset(w) as uint >> b) as u8 & 3
        }
    }

    #[inline]
    pub fn set(&self, i: uint, x: u8) {
        let w = (i / 16) as int;
        let b = (i % 16) * 2;
        unsafe {
            *self.storage.offset(w) = *self.storage.offset(w) & !(3 << b) | (x as u32 << b)
        }
    }

    #[inline]
    fn as_mut_ptr(&self) -> *mut u8 {
        self.storage as *mut u8
    }

    pub fn clear(&self, capacity: uint) {
        unsafe {
            set_memory(self.as_mut_ptr(), 0, capacity / 4);
        }
    }
}