Expand description

bitmac

This library provides implementation of bitmap with custom bit accessing and resizing strategy.

Features

Resizing strategy

The library provides several resizing strategy.

MinimalRequiredStrategy

Strategy MinimumRequiredStrategy resizes to minimum required bytes.

let mut bitmap = Bitmap::<Vec<u8>, MinimumRequiredStrategy, LSB>::default();

bitmap.set(0, true);
assert_eq!(bitmap.as_bytes().len(), 1);

bitmap.set(15, true);
assert_eq!(bitmap.as_bytes().len(), 2);
FixedStrategy

Strategy FixedStrategy advances size by fixed steps.

let mut bitmap = Bitmap::<Vec<u8>, _, LSB>::with_resizing_strategy(FixedStrategy(3));

bitmap.set(0, true);
assert_eq!(bitmap.as_bytes().len(), 3);

bitmap.set(15, true);
assert_eq!(bitmap.as_bytes().len(), 3);

bitmap.set(24, true);
assert_eq!(bitmap.as_bytes().len(), 6);
StaticStrategy

Strategy StaticStrategy never increases the size, returns an error if an increase is required.

Useful for const containers ([u8; N]).

let mut bitmap = Bitmap::<_, _, LSB>::new([0u8; 2], StaticStrategy);

bitmap.set(0, true);
assert_eq!(bitmap.as_bytes().len(), 2);

bitmap.set(15, true);
assert_eq!(bitmap.as_bytes().len(), 2);

assert!(bitmap.try_set(24, true).is_err());
assert_eq!(bitmap.as_bytes().len(), 2);

You can implement your own ResizingStrategy.

BitAccess

Trait BitAccess provides functions for accessing to bit in byte. The bytes in a bitmap can be stored in LSB or MSB order.

LSB

In LSB order, the 0th bit of the bitmap is the least significant bit.

For example:

let bitmap = Bitmap::<_, _, LSB>::new([0b0000_0001], StaticStrategy);
assert!(bitmap.get(0));
MSB

In MSB order, the 0th bit of the bitmap is the most significant bit.

For example:

let bitmap = Bitmap::<_, _, MSB>::new([0b0000_0001], StaticStrategy);
assert!(bitmap.get(7));
DynBitAccess

DynBitAccess can be configured in runtime (LSB or MSB)

For example:

let bitmap = Bitmap::<_, _, DynBitAccess>::from_parts([0b0000_0001], StaticStrategy, DynBitAccess::LSB);
assert!(bitmap.get(0));

let bitmap = Bitmap::<_, _, DynBitAccess>::from_parts([0b0000_0001], StaticStrategy, DynBitAccess::MSB);
assert!(bitmap.get(7));

Bitmap

Bitmap that owns the container. Container can dynamically grow if needed.

You can use any container that implements the AsRef<[u8]> trait for read-only access and the ContainerMut trait for write access

Usage example:

let mut bitmap = Bitmap::<Vec<u8>, MinimumRequiredStrategy, LSB>::default();

assert_eq!(bitmap.as_bytes().len(), 0);

assert!(!bitmap.get(0));
assert!(!bitmap.get(7));
assert!(!bitmap.get(300));

bitmap.set(15, true);
assert_eq!(bitmap.as_bytes().len(), 2);
assert!(bitmap.get(15));

BitmapRef

BitmapRef is bitmap that borrows bytes.

Helpful if you have already allocated bytes and you want to just look at them as bitmap, without modifications.

Usage example:

let bitmap = BitmapRef::<'_, LSB>::from_bytes(&[0b0000_1000, 0b0000_0001]);

assert_eq!(bitmap.get(3), true);
assert_eq!(bitmap.get(8), true);

assert_eq!(bitmap.get(1), false);
assert_eq!(bitmap.get(7), false);
assert_eq!(bitmap.get(300), false);

assert_eq!(bitmap.as_bytes().len(), 2);

BitmapRefMut

BitmapRefMut is bitmap that borrows mutable bytes.

Helpful if you have already allocated bytes and you want to just look at them as bitmap and modify it. Cannot increase the number of bytes.

Usage example:

let bitmap = BitmapRef::<'_, LSB>::from_bytes(&[0b0000_1000, 0b0000_0001]);

assert_eq!(bitmap.get(3), true);
assert_eq!(bitmap.get(8), true);

assert_eq!(bitmap.get(1), false);
assert_eq!(bitmap.get(7), false);
assert_eq!(bitmap.get(300), false);

assert_eq!(bitmap.as_bytes().len(), 2);

Re-exports

pub use bit_access::*;
pub use bitmap::*;
pub use bitmap_ref::*;
pub use container::*;
pub use error::*;
pub use resizing_strategy::*;

Modules