Initial commit.

This commit is contained in:
2017-12-18 17:08:40 +01:00
commit bd009b0d37
4 changed files with 79 additions and 0 deletions

56
src/lib.rs Normal file
View File

@@ -0,0 +1,56 @@
pub struct BitSet {
data: Box<[u64]>,
}
impl BitSet {
pub fn with_capacity(max: usize) -> BitSet {
BitSet {
data: vec![0; max / 64 + if max % 64 == 0 { 0 } else { 1 }].into_boxed_slice()
}
}
pub fn capacity(&self) -> u64 {
self.data.len() as u64 * 64
}
pub fn insert(&mut self, value: usize) {
let block = value / 64 as usize;
let bit = 2u64.pow((value - (block * 64)) as u32);
self.data[block] |= bit;
}
pub fn remove(&mut self, value: usize) {
let block = value / 64 as usize;
let bit = 2u64.pow((value - (block * 64)) as u32);
self.data[block] &= !bit;
}
pub fn contains(&self, value: usize) -> bool {
let block = value / 64 as usize;
let bit = 2u64.pow((value - (block * 64)) as u32);
self.data[block] & bit == bit
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test() {
let mut set = BitSet::with_capacity(100);
assert_eq!(set.contains(0), false);
assert_eq!(set.contains(10), false);
set.insert(0);
set.insert(10);
assert_eq!(set.contains(0), true);
assert_eq!(set.contains(10), true);
}
}