Struct bv::BitVec[][src]

pub struct BitVec<Block: BlockType = usize> { /* fields omitted */ }
Expand description

A bit-vector, akin to Vec<bool> but packed.

BitVec stores its bits in an array of Blocks, where Block is given as a type parameter that defaults to usize. You might find that a different Block size is preferable, but only benchmarking will tell.

Several useful methods are exported in traits, rather than inherent to BitVec. In particular, see:

You will likely want to use these traits (or bv::*) when you use BitVec.

Examples

use bv::BitVec;

let mut bv: BitVec = BitVec::new();
assert_eq!(bv.len(), 0);

bv.push(true);
bv.push(false);
bv.push(true);
assert_eq!(bv.len(), 3);

assert_eq!(bv[0], true);
assert_eq!(bv[1], false);
assert_eq!(bv[2], true);

Implementations

Creates a new, empty bit-vector with a capacity of one block.

Examples

use bv::BitVec;

let mut bv: BitVec = BitVec::new();
assert_eq!(bv.len(), 0);

bv.push(true);
bv.push(false);
bv.push(true);
assert_eq!(bv.len(), 3);

assert_eq!(bv[0], true);
assert_eq!(bv[1], false);
assert_eq!(bv[2], true);

Creates a new, empty bit-vector with the given bit capacity.

Examples

use bv::BitVec;

let mut bv: BitVec<u16> = BitVec::with_capacity(20);
assert_eq!(bv.capacity(), 32);

Creates a new, empty bit-vector with the given block capacity.

Examples

use bv::BitVec;

let mut bv: BitVec<u16> = BitVec::with_block_capacity(8);
assert_eq!(bv.capacity(), 128);

Creates a new bit-vector of size len, filled with all 0s or 1s depending on value.

Examples

use bv::*;

let mut bv: BitVec<u64> = BitVec::new_fill(false, 100);

assert_eq!( bv.get(0), false );
assert_eq!( bv.len(), 100 );

Creates a new BitVec from any value implementing the Bits trait with the same block type.

The number of bits in the bit-vector.

Examples

use bv::BitVec;

let mut bv: BitVec = BitVec::new();
assert_eq!(bv.len(), 0);
bv.push(false);
assert_eq!(bv.len(), 1);
bv.push(false);
assert_eq!(bv.len(), 2);
bv.push(false);
assert_eq!(bv.len(), 3);

The number of blocks used by this bit-vector.

Examples

use bv::*;

let mut bv: BitVec<u64> = BitVec::new_fill(false, 100);

assert_eq!( bv.len(), 100 );
assert_eq!( bv.block_len(), 2 );

The capacity of the bit-vector in bits.

This is the number of bits that can be held without reallocating.

Examples

use bv::*;

let bv: BitVec<u64> = bit_vec![false; 100];

assert_eq!( bv.len(), 100 );
assert_eq!( bv.capacity(), 128 );

Note that this example holds because bit_vec! does not introduces excess capacity.

The capacity of the bit-vector in blocks.

Examples

use bv::*;

let bv: BitVec<u64> = BitVec::with_capacity(250);

assert_eq!( bv.len(), 0 );
assert_eq!( bv.block_len(), 0 );
assert_eq!( bv.capacity(), 256 );
assert_eq!( bv.block_capacity(), 4 );

Note that this example holds because bit_vec! does not introduces excess capacity.

Adjust the capacity to hold at least additional additional bits.

May reserve more to avoid frequent reallocations.

Examples

use bv::*;

let mut bv: BitVec<u32> = bit_vec![ false, false, true ];
assert_eq!( bv.capacity(), 32 );
bv.reserve(100);
assert!( bv.capacity() >= 103 );

Adjust the capacity to hold at least additional additional blocks.

May reserve more to avoid frequent reallocations.

Examples

use bv::*;

let mut bv: BitVec<u32> = bit_vec![ false, false, true ];
assert_eq!( bv.block_capacity(), 1 );
bv.block_reserve(3);
assert!( bv.block_capacity() >= 4 );

Adjust the capacity to hold at least additional additional bits.

Examples

use bv::*;

let mut bv: BitVec<u32> = bit_vec![ false, false, true ];
assert_eq!( bv.capacity(), 32 );
bv.reserve_exact(100);
assert_eq!( bv.capacity(), 128 );

Adjusts the capacity to at least additional blocks beyond those used.

Examples

use bv::*;

let mut bv: BitVec<u32> = bit_vec![ false, false, true ];
assert_eq!( bv.block_capacity(), 1 );
bv.block_reserve_exact(3);
assert_eq!( bv.block_capacity(), 4 );

Shrinks the capacity of the vector as much as possible.

Examples

use bv::BitVec;

let mut bv: BitVec<u8> = BitVec::new();

for i in 0 .. 23 {
    bv.push(i % 3 == 0);
}

assert!(bv.capacity() >= 24);

bv.shrink_to_fit();
assert_eq!(bv.capacity(), 24);

Converts the vector into Box<[Block]>.

Note that this will not drop any excess capacity.

Examples

use bv::*;

let bv: BitVec<u8> = bit_vec![true, true, false, false, true, false, true, false];
let bs = bv.into_boxed_slice();

assert!( bs.len() >= 1 );
assert_eq!( bs[0], 0b01010011 );

Shortens the vector, keeping the first len elements and dropping the rest.

If len is greater than the vector’s current length, this has no effect.

Note that this method has no effect on the capacity of the bit-vector.

Examples

use bv::*;

let mut v1: BitVec = bit_vec![ true, true, false, false ];
let     v2: BitVec = bit_vec![ true, true ];

assert_ne!( v1, v2 );

v1.truncate(2);

assert_eq!( v1, v2 );

Resizes the bit-vector, filling with value if it has to grow.

Examples

use bv::*;

let     v1: BitVec = bit_vec![ true, true, false, false ];
let mut v2: BitVec = bit_vec![ true, true ];
let mut v3: BitVec = bit_vec![ true, true ];

v2.resize(4, false);
v3.resize(4, true);

assert_eq!( v1, v2 );
assert_ne!( v1, v3 );

Gets a slice to a BitVec.

Examples

use bv::*;

let bv: BitVec = bit_vec![true, false, true];
let slice = bv.as_slice();

assert_eq!( slice.len(), 3 );
assert_eq!( slice[0], true );
assert_eq!( slice[1], false );
assert_eq!( slice[2], true );

Gets a mutable slice to a BitVec.

Examples

use bv::*;

let mut bv: BitVec = bit_vec![true, false, true];

{
    let mut slice = bv.as_mut_slice();
    slice.set_bit(1, true);
}

assert_eq!( bv[1], true );

Gets the value of the bit at the given position.

This is an alias for Bits::get_bit.

Panics

If the position is out of bounds.

Sets the value of the bit at the given position.

This is an alias for BitsMut::set_bit.

Panics

If the position is out of bounds.

Adds the given bool to the end of the bit-vector.

Examples

use bv::*;

let mut bv0: BitVec = bit_vec![ ];
let     bv1: BitVec = bit_vec![ true ];
let     bv2: BitVec = bit_vec![ true, false ];
let     bv3: BitVec = bit_vec![ true, false, true ];

assert_ne!( bv0, bv1 );
assert_ne!( bv0, bv2 );
assert_ne!( bv0, bv3 );

bv0.push(true);
assert_eq!( bv0, bv1 );

bv0.push(false);
assert_eq!( bv0, bv2 );

bv0.push(true);
assert_eq!( bv0, bv3 );

Removes and returns the last element of the bit-vector, or None if empty.

Examples

use bv::*;

let mut bv: BitVec = bit_vec![ true, false, true ];
assert_eq!( bv.pop(), Some(true) );
assert_eq!( bv.pop(), Some(false) );
assert_eq!( bv.pop(), Some(true) );
assert_eq!( bv.pop(), None );

Removes all elements from the bit-vector.

Does not change the capacity.

Examples

use bv::*;

let mut bv: BitVec<u32> = bit_vec![ true ];
assert_eq!( bv.len(), 1 );
assert_eq!( bv.capacity(), 32 );
bv.clear();
assert_eq!( bv.len(), 0 );
assert_eq!( bv.capacity(), 32 );

Does the bit-vector have no elements?

Examples

use bv::*;

let mut bv: BitVec<u32> = bit_vec![ true ];
assert!( !bv.is_empty() );
bv.clear();
assert!(  bv.is_empty() );

Trait Implementations

The type of the slice produced.

Slices or re-slices the given object. Read more

The type of the slice produced.

Slices or re-slices the given object. Read more

The type of the slice produced.

Slices or re-slices the given object. Read more

The type of the slice produced.

Slices or re-slices the given object. Read more

The type of the slice produced.

Slices or re-slices the given object. Read more

The type of the slice produced.

Slices or re-slices the given object. Read more

The type of the slice produced.

Slices or re-slices the given object. Read more

The type of the slice produced.

Slices or re-slices the given object. Read more

The type of the slice produced.

Slices or re-slices the given object. Read more

The type of the slice produced.

Slices or re-slices the given object. Read more

The type of the slice produced.

Slices or re-slices the given object. Read more

The type of the slice produced.

Slices or re-slices the given object. Read more

The underlying block type used to store the bits of the vector.

The length of the slice in bits.

Gets the bit at position Read more

Gets the block at position, masked as necessary. Read more

Gets the block at position, without masking. Read more

The length of the slice in blocks.

Gets count bits starting at bit index start, interpreted as a little-endian integer. Read more

Copies the bits into a new allocated BitVec. Read more

Sets the bit at position to value. Read more

Sets the block at position to value. Read more

Sets count bits starting at bit index start, interpreted as a little-endian integer. Read more

Adds the given bit to the end of the bit vector.

Removes and returns the last bit, if any.

Pushes value 0 or more times until the size of the bit vector is block-aligned. Read more

Pushes the given block onto the end of the bit vector. Read more

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

Deserialize this value from the given Serde deserializer. Read more

Performs the conversion.

Performs the conversion.

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

The returned type after indexing.

Performs the indexing (container[index]) operation. Read more

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

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

This method tests for !=.

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

Serialize this value into the given Serde serializer. 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

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)

recently added

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.