Initial commit.
This commit is contained in:
56
src/lib.rs
Normal file
56
src/lib.rs
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user