Struct BitSet

Source
pub struct BitSet { /* private fields */ }

Implementations§

Source§

impl BitSet

Source

pub fn new() -> Self

Create a new BitSet with ZERO bit.

§Example
use bitset::BitSet;

let bs = BitSet::new();
Source

pub 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 count BitSet will hold.
§Example
use bitset::BitSet;

let bs = BitSet::with_capacity(100);
Source

pub fn from_u64(v: u64) -> Self

Create a new BitSet from a u64 value, and initialize all bits by 0.

§Arguments
  • v - A u64 value.
§Example
use bitset::BitSet;

let bs = BitSet::from_u64(2);
assert!(bs.test(0) == false);
assert!(bs.test(1) == true);
Source

pub fn from_vec64(vec: &Vec<u64>) -> Self

Create a new BitSet from a u64 vec, and initialize all bits by 0.

§Arguments
  • vec - A u64 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);
Source

pub fn size(&self) -> usize

Return the actual bits count.

§Example
use bitset::BitSet;
 
let bs = BitSet::with_capacity(100);
assert!(bs.size() == 100);
Source

pub fn count(&self) -> u64

Return the count of 1.

§Example
use bitset::BitSet;

let bs = BitSet::with_capacity(100);
assert!(bs.count() == 0);
Source

pub 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);
Source

pub 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);
Source

pub 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);
Source

pub 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 or false.
§Example
use bitset::BitSet;

let mut bs = BitSet::with_capacity(100);
bs.set(99, true);
assert!(bs.test(99) == true);
Source

pub 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);
Source

pub 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);
Source

pub 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);
}

Trait Implementations§

Source§

impl Default for BitSet

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl Freeze for BitSet

§

impl RefUnwindSafe for BitSet

§

impl Send for BitSet

§

impl Sync for BitSet

§

impl Unpin for BitSet

§

impl UnwindSafe for BitSet

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.