A bitvector stored as a sequence of bytes (most significant bit first).
All of the bytes stored in the byte sequence: not just the ones actually used.
The number of bits 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::Bits;
let bits = Bits::from_bytes(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::Bits;
let bits = Bits::from_bytes(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.len());
Count the unset bits (O(n)).
use indexed_bitvec::Bits;
let bits = Bits::from_bytes(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.len());
Count the set bits before a position in the bits (O(n)).
Returns None
it the index is out of bounds.
use indexed_bitvec::Bits;
let bits = Bits::from_bytes(vec![0xFE, 0xFE], 15).unwrap();
assert!((0..bits.len()).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::Bits;
let bits = Bits::from_bytes(vec![0xFE, 0xFE], 15).unwrap();
assert!((0..bits.len()).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::Bits;
let bits = Bits::from_bytes(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::Bits;
let bits = Bits::from_bytes(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.
Set the byte at a specific index.
Returns an error if the index is out of bounds.
use indexed_bitvec::Bits;
let mut bits = Bits::from_bytes(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);
assert!(bits.set(0, false).is_ok());
assert_eq!(bits.get(0), Some(false));
assert!(bits.set(0, true).is_ok());
assert_eq!(bits.get(0), Some(true));
assert!(bits.set(7, false).is_ok());
assert_eq!(bits.get(7), Some(false));
assert!(bits.set(14, false).is_ok());
assert_eq!(bits.get(14), Some(false));
assert!(bits.set(15, false).is_err());
Add a specific bit to the end of the vector.
This will enlarge the used section of bits (corresponding
to Bits::len
) and overwrite any content already in the
newly used storage.
use indexed_bitvec::Bits;
let mut bits = Bits::from_bytes(vec![0x80], 0).unwrap();
assert_eq!(0x80, bits.all_bytes()[0]);
assert_eq!(None, bits.get(0));
bits.push(false);
assert_eq!(0x00, bits.all_bytes()[0]);
assert_eq!(Some(false), bits.get(0));
bits.push(true);
assert_eq!(0x40, bits.all_bytes()[0]);
assert_eq!(Some(true), bits.get(1));
for _ in 0..6 { bits.push(false) };
assert_eq!(0x40, bits.all_bytes()[0]);
assert_eq!(1, bits.all_bytes().len());
bits.push(true);
assert_eq!(2, bits.all_bytes().len());
assert_eq!(0x80, bits.all_bytes()[1]);
assert_eq!(Some(true), bits.get(8));
type Item = bool
The type of the elements being iterated over.
type IntoIter = BitIterator<T>
Which kind of iterator are we turning this into?
This method returns an ordering between self
and other
values if one exists. Read more
This method tests less than (for self
and other
) and is used by the <
operator. Read more
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
This method tests for self
and other
values to be equal, and is used by ==
. Read more
This method tests for !=
.
Performs copy-assignment from source
. Read more
This method returns an Ordering
between self
and other
. Read more
fn max(self, other: Self) -> Self | 1.21.0 [src] |
Compares and returns the maximum of two values. Read more
fn min(self, other: Self) -> Self | 1.21.0 [src] |
Compares and returns the minimum of two values. Read more
Formats the value using the given formatter. Read more
Serialize this value into the given Serde serializer. Read more
Deserialize this value from the given Serde deserializer. Read more
type Item = <I as Iterator>::Item
The type of the elements being iterated over.
type IntoIter = I
Which kind of iterator are we turning this into?
type Owned = T
Creates owned data from borrowed data, usually by cloning. Read more
🔬 This is a nightly-only experimental API. (toowned_clone_into
)
recently added
Uses borrowed data to replace owned data, usually by cloning. Read more
type Error = !
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
type Error = <U as TryFrom<T>>::Error
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static