pub struct Bits<T: Deref<Target = [u8]>>(_);
Expand description

Bits stored as a sequence of bytes (most significant bit first).

Implementations§

All of the bytes stored in the byte sequence: not just the ones actually used.

The number of bits used in the storage.

The used bytes of the byte sequence: bear in mind some of the bits in the last byte may be unused.

Deconstruct the bits storage to get back what it was constructed from.

Get the byte at a specific index.

Returns None for out-of-bounds.

use indexed_bitvec_core::*;
let bits = Bits::from(vec![0xFE, 0xFE], 15).unwrap();
assert_eq!(bits.get(0), Some(true));
assert_eq!(bits.get(7), Some(false));
assert_eq!(bits.get(14), Some(true));
assert_eq!(bits.get(15), None);

Count the set bits (O(n)).

use indexed_bitvec_core::*;
let bits = Bits::from(vec![0xFE, 0xFE], 15).unwrap();
assert_eq!(bits.count_ones(), 14);
assert_eq!(bits.count_zeros(), 1);
assert_eq!(bits.count_ones() + bits.count_zeros(), bits.used_bits());

Count the unset bits (O(n)).

use indexed_bitvec_core::*;
let bits = Bits::from(vec![0xFE, 0xFE], 15).unwrap();
assert_eq!(bits.count_ones(), 14);
assert_eq!(bits.count_zeros(), 1);
assert_eq!(bits.count_ones() + bits.count_zeros(), bits.used_bits());

Count the set bits before a position in the bits (O(n)).

Returns None it the index is out of bounds.

use indexed_bitvec_core::*;
let bits = Bits::from(vec![0xFE, 0xFE], 15).unwrap();
assert!((0..bits.used_bits()).all(|idx|
    bits.rank_ones(idx).unwrap()
    + bits.rank_zeros(idx).unwrap()
    == (idx as u64)));
assert_eq!(bits.rank_ones(7), Some(7));
assert_eq!(bits.rank_zeros(7), Some(0));
assert_eq!(bits.rank_ones(8), Some(7));
assert_eq!(bits.rank_zeros(8), Some(1));
assert_eq!(bits.rank_ones(9), Some(8));
assert_eq!(bits.rank_zeros(9), Some(1));
assert_eq!(bits.rank_ones(15), None);

Count the unset bits before a position in the bits (O(n)).

Returns None it the index is out of bounds.

use indexed_bitvec_core::*;
let bits = Bits::from(vec![0xFE, 0xFE], 15).unwrap();
assert!((0..bits.used_bits()).all(|idx|
    bits.rank_ones(idx).unwrap()
    + bits.rank_zeros(idx).unwrap()
    == (idx as u64)));
assert_eq!(bits.rank_ones(7), Some(7));
assert_eq!(bits.rank_zeros(7), Some(0));
assert_eq!(bits.rank_ones(8), Some(7));
assert_eq!(bits.rank_zeros(8), Some(1));
assert_eq!(bits.rank_ones(9), Some(8));
assert_eq!(bits.rank_zeros(9), Some(1));
assert_eq!(bits.rank_ones(15), None);

Find the position of a set bit by its rank (O(n)).

Returns None if no suitable bit is found. It is always the case otherwise that rank_ones(result) == Some(target_rank) and get(result) == Some(true).

use indexed_bitvec_core::*;
let bits = Bits::from(vec![0xFE, 0xFE], 15).unwrap();
assert_eq!(bits.select_ones(6), Some(6));
assert_eq!(bits.select_ones(7), Some(8));
assert_eq!(bits.select_zeros(0), Some(7));
assert_eq!(bits.select_zeros(1), None);

Find the position of an unset bit by its rank (O(n)).

Returns None if no suitable bit is found. It is always the case otherwise that rank_zeros(result) == Some(target_rank) and get(result) == Some(false).

use indexed_bitvec_core::*;
let bits = Bits::from(vec![0xFE, 0xFE], 15).unwrap();
assert_eq!(bits.select_ones(6), Some(6));
assert_eq!(bits.select_ones(7), Some(8));
assert_eq!(bits.select_zeros(0), Some(7));
assert_eq!(bits.select_zeros(1), None);

Create a reference to these same bits.

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

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

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

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.