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

3
.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
/target/
Cargo.lock

14
.gitlab-ci.yml Normal file
View File

@@ -0,0 +1,14 @@
image: "rust:latest"
variables:
CARGO_HOME: $CI_PROJECT_DIR/cargo
test:cargo:
script:
- rustc --version && cargo --version
- time cargo test --verbose --jobs 1 --release
cache:
paths:
- target/
- cargo/

6
Cargo.toml Normal file
View File

@@ -0,0 +1,6 @@
[package]
name = "bit_set"
version = "0.1.0"
authors = ["logaritmisk <anders.e.olsson@gmail.com>"]
[dependencies]

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);
}
}