pub struct BitVector { /* private fields */ }
Expand description

Bit vector in a plain format, supporting some utilities such as update, chunking, and predecessor queries.

This is a yet another Rust port of succinct::bit_vector.

Examples

use sucds::BitVector;

let bv = BitVector::from_bits(&[true, false, false, true]);

assert_eq!(bv.get_bit(0), true);
assert_eq!(bv.get_bit(1), false);
assert_eq!(bv.get_bit(2), false);
assert_eq!(bv.get_bit(3), true);

assert_eq!(bv.predecessor1(2), Some(0));
assert_eq!(bv.predecessor0(2), Some(2));
assert_eq!(bv.successor1(1), Some(3));
assert_eq!(bv.successor0(1), Some(1));

let mut bytes = vec![];
let size = bv.serialize_into(&mut bytes).unwrap();
let other = BitVector::deserialize_from(&bytes[..]).unwrap();
assert_eq!(bv, other);
assert_eq!(size, bytes.len());
assert_eq!(size, bv.size_in_bytes());

Implementations

Creates a new empty BitVector.

Creates a new BitVector of len bits.

Creates a new BitVector that capa bits are reserved.

Serializes the data structure into the writer, returning the number of serialized bytes.

Arguments
  • writer: std::io::Write variable.

Deserializes the data structure from the reader.

Arguments
  • reader: std::io::Read variable.

Returns the number of bytes to serialize the data structure.

Creates a new BitVector from input bitset bits.

Arguments
  • bits: List of bits.
Examples
use sucds::BitVector;

let bv = BitVector::from_bits(&[true, false, false, true]);
assert_eq!(bv.get_bit(0), true);
assert_eq!(bv.get_bit(1), false);
assert_eq!(bv.get_bit(2), false);
assert_eq!(bv.get_bit(3), true);

Gets the pos-th bit.

Arguments
  • pos: Bit position.
Examples
use sucds::BitVector;

let bv = BitVector::from_bits(&[true, false, false, true]);
assert_eq!(bv.get_bit(0), true);
assert_eq!(bv.get_bit(1), false);
assert_eq!(bv.get_bit(2), false);
assert_eq!(bv.get_bit(3), true);

Sets the pos-th bit to bit.

Arguments
  • pos: Bit position.
  • bit: Set bit.
Examples
use sucds::BitVector;

let mut bv = BitVector::from_bits(&[false, true, true, false]);
bv.set_bit(0, true);
bv.set_bit(2, false);
assert_eq!(bv.get_bit(0), true);
assert_eq!(bv.get_bit(1), true);
assert_eq!(bv.get_bit(2), false);
assert_eq!(bv.get_bit(3), false);

Pushes bit at the end.

Arguments
  • bit: Pushed bit.
Examples
use sucds::BitVector;

let mut bv = BitVector::new();
bv.push_bit(true);
bv.push_bit(false);
assert_eq!(bv.get_bit(0), true);
assert_eq!(bv.get_bit(1), false);

Gets the len bits starting at the pos-th bit.

Arguments
  • pos: Starting bit position.
  • len: Number of bits.
Examples
use sucds::BitVector;

let bv = BitVector::from_bits(&[true, false, true, false, true]);
assert_eq!(bv.get_bits(1, 4), 0b1010);

Sets the len bits starting at the pos-th bit to bits.

Arguments
  • pos: Starting bit position.
  • bits: Set bits.
  • len: Number of bits.
Examples
use sucds::BitVector;

let mut bv = BitVector::with_len(5);
bv.set_bits(1, 0b1010, 4);
assert_eq!(bv.get_bits(1, 4), 0b1010);

Pushes bits of len bits at the end.

Arguments
  • bits: Pushed bits.
  • len: Number of bits.
Examples
use sucds::BitVector;

let mut bv = BitVector::new();
bv.push_bits(0b1, 1);
bv.push_bits(0b1010, 4);
assert_eq!(bv.get_bits(1, 4), 0b1010);

Gets the largest bit position pred such that pred <= pos and the pred-th bit is set. If not found, None is returned.

Arguments
  • pos: Bit position.
Examples
use sucds::BitVector;

let bv = BitVector::from_bits(&[false, true, false, true]);
assert_eq!(bv.predecessor1(3), Some(3));
assert_eq!(bv.predecessor1(2), Some(1));
assert_eq!(bv.predecessor1(1), Some(1));
assert_eq!(bv.predecessor1(0), None);

Gets the smallest bit position succ such that succ >= pos and the succ-th bit is set. If not found, None is returned.

Arguments
  • pos: Bit position.
Examples
use sucds::BitVector;

let bv = BitVector::from_bits(&[true, false, true, false]);
assert_eq!(bv.successor1(0), Some(0));
assert_eq!(bv.successor1(1), Some(2));
assert_eq!(bv.successor1(2), Some(2));
assert_eq!(bv.successor1(3), None);

Gets the largest bit position pred such that pred <= pos and the pred-th bit is not set. If not found, None is returned.

Arguments
  • pos: Bit position.
Examples
use sucds::BitVector;

let bv = BitVector::from_bits(&[true, false, true, false]);
assert_eq!(bv.predecessor0(3), Some(3));
assert_eq!(bv.predecessor0(2), Some(1));
assert_eq!(bv.predecessor0(1), Some(1));
assert_eq!(bv.predecessor0(0), None);

Gets the smallest bit position succ such that succ >= pos and the succ-th bit is not set. If not found, None is returned.

Arguments
  • pos: Bit position.
Examples
use sucds::BitVector;

let bv = BitVector::from_bits(&[false, true, false, true]);
assert_eq!(bv.successor0(0), Some(0));
assert_eq!(bv.successor0(1), Some(2));
assert_eq!(bv.successor0(2), Some(2));
assert_eq!(bv.successor0(3), None);

Creates an iterator for enumerating positions of set bits.

Examples
use sucds::BitVector;

let bv = BitVector::from_bits(&[false, true, false, false, true]);
let mut it = bv.unary_iter(1);

assert_eq!(it.next(), Some(1));
assert_eq!(it.next(), Some(4));
assert_eq!(it.next(), None);

Gets get_bits(pos, 64) but it can extend further len(), padding with zeros.

Gets the slice of raw words.

Gets the number of words.

Gets the number of bits.

Checks if the vector is empty.

Shrinks the capacity of the vector as much as possible.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.