Skip to content

Latest commit

 

History

History
46 lines (35 loc) · 1.56 KB

README.md

File metadata and controls

46 lines (35 loc) · 1.56 KB

mmap-bitvec

ci Crates.io Version

mmap-bitvec is a library for working with bit-vectors backed by memory-mapped files. Included is a simple Bloom filter built on top of such bit-vectors.

Examples

Using a memory-mapped bit-vector:

    // Build an in-memory bit-vector with a capacity of 128 bits.
    use mmap_bitvec::{MmapBitVec, BitVector};

    let mut bitvec = MmapBitVec::from_memory(128).unwrap();
    bitvec.set(2, true);
    assert!(bitvec.get(2));
    assert_eq!(bitvec.get_range(0..8), 0b00100000);

    // Write the bit-vector to disk, passing in `None` where an optional magic bytes arg can be set
    let dir = tempfile::tempdir().unwrap();
    bitvec.save_to_disk(dir.path().join("test"), None, &[])
        .unwrap();
    let f = MmapBitVec::open(dir.path().join("test"), None, false).unwrap();
    assert_eq!(f.get(2), true);

Using the Bloom filter:

    use mmap_bitvec::{BloomFilter};
    // Create a Bloom filter with a capacity of 100 bits that uses 2 hash functions on each insert.
    let mut filter = BloomFilter::new(Some("./test.bloom"), 100, 2).unwrap();
    let (a, b) = (1, 2);
    assert!(!filter.contains(a));
    assert!(!filter.contains(b));

    filter.insert(b);
    assert!(!filter.contains(a));
    assert!(filter.contains(b));

License

This project is licensed under the MIT license.