Struct bitset::BitSet
[−]
[src]
pub struct BitSet { /* fields omitted */ }
Methods
impl BitSet
[src]
fn new() -> Self
fn with_capacity(nbits: usize) -> Self
Create a new BitSet with nbits
bits with all bit initialized by 0
.
Arguments
nbits
- A integer, which value is the bits countBitSet
will hold.
Example
use bitset::BitSet; let bs = BitSet::with_capacity(100);
fn from_u64(v: u64) -> Self
Create a new BitSet from a u64
value, and initialize all bits by 0
.
Arguments
v
- Au64
value.
Example
use bitset::BitSet; let bs = BitSet::from_u64(2); assert!(bs.test(0) == false); assert!(bs.test(1) == true);
fn from_vec64(vec: &Vec<u64>) -> Self
Create a new BitSet from a u64
vec
, and initialize all bits by 0
.
Arguments
vec
- Au64
vector.
Example
use bitset::BitSet; let vec = vec![u64::max_value(), 0, u64::max_value()]; let bs = BitSet::from_vec64(&vec); assert!(bs.test(63) == true); assert!(bs.test(64) == false);
fn size(&self) -> usize
Return the actual bits count.
Example
use bitset::BitSet; let bs = BitSet::with_capacity(100); assert!(bs.size() == 100);
fn count(&self) -> u64
Return the count of 1
.
Example
use bitset::BitSet; let bs = BitSet::with_capacity(100); assert!(bs.count() == 0);
fn test(&self, bit_idx: usize) -> bool
Return if the given bit index has been set to 1
.
Example
use bitset::BitSet; let bs = BitSet::with_capacity(100); assert!(bs.test(99) == false);
fn any(&self) -> bool
Return if there is one bit has been set to 1
in the whole bitset..
Example
use bitset::BitSet; let bs = BitSet::with_capacity(100); assert!(bs.any() == false);
fn none(&self) -> bool
Return if all bits are set to 0
.
Example
use bitset::BitSet; let bs = BitSet::with_capacity(100); assert!(bs.none() == true);
fn set(&mut self, bit_idx: usize, v: bool)
Set the bit specified by bit_idx
to v
, which is true
or false
.
Arguments
bit_idx
- the bit index we want to set.v
- the value we want to set.true
orfalse
.
Example
use bitset::BitSet; let mut bs = BitSet::with_capacity(100); bs.set(99, true); assert!(bs.test(99) == true);
fn reset(&mut self)
Reset all bits to 0
.
Example
use bitset::BitSet; let mut bs = BitSet::with_capacity(100); bs.set(99, true); assert!(bs.test(99) == true); bs.reset(); assert!(bs.test(99) == false);
fn flip(&mut self, bit_idx: usize)
Flip the bit specified by bit_idx
to the reverse value.
If the bit value is true
, then it will be flipped to false
.
The other case is like the same.
Arguments
bit_idx
- the index of the bit we want to flip.
Example
use bitset::BitSet; let mut bs = BitSet::with_capacity(100); assert!(bs.test(99) == false); bs.flip(99); assert!(bs.test(99) == true);
fn flip_all(&mut self)
Flip all bits in the bitset. It may run time-costly.
Example
use bitset::BitSet; let mut bs = BitSet::with_capacity(100); bs.flip_all(); for i in 0..100 { assert!(bs.test(i) == true); } bs.flip_all(); for i in 0..100 { assert!(bs.test(i) == false); }