Struct DenseBitSet

Source
pub struct DenseBitSet { /* private fields */ }
Expand description

Provides an efficient and compact BitSet implementation for up to 64 bits.

This structure implements BitSet, Clone, Copy, Default, Debug, Hash, PartialEq, Eq and bit operations.

Implementations§

Source§

impl DenseBitSet

Source

pub fn new() -> Self

Returns a new empty DenseBitSet.

§Example
use rust_dense_bitset::DenseBitSet;

let mut bs = DenseBitSet::new();
Source

pub fn from_integer(i: u64) -> Self

Generates a bitset from an integer (little endian convention).

§Example
use rust_dense_bitset::DenseBitSet;

let bs = DenseBitSet::from_integer(1234567890);
Source

pub fn from_string(s: &str, base: u32) -> Self

Generates a bitset from a string and a base (little endian convention).

The base must be an integer between 2 and 32.

§Example
use rust_dense_bitset::DenseBitSet;

let mut bs1 = DenseBitSet::from_string("101010", 2);
let mut bs2 = DenseBitSet::from_string("2a", 16);

assert_eq!(bs1,bs2);
§Panics

This function will panic if an incorrect base is provided or if invalid characters are found when parsing.

Source

pub fn to_integer(self) -> u64

Returns an integer representing the bitset (little endian convention).

§Example
use rust_dense_bitset::DenseBitSet;

let bs = DenseBitSet::from_integer(1234);

assert_eq!(bs.to_integer(), 1234);
Source

pub fn extract(self, position: usize, length: usize) -> u64

Returns an integer representation of the bitset starting at the given position with given length (little endian convention).

§Example
use rust_dense_bitset::DenseBitSet;

let bs = DenseBitSet::from_integer(0b11110101010010);
let value = bs.extract(5,6);

assert_eq!(value, 42);
§Panics

This function will panic if length is zero or if one tries to access a bit beyond the 64 bit limit (i.e., position + length > 64).

Source

pub fn insert(&mut self, position: usize, length: usize, value: u64)

Returns nothing, mutates the DenseBitSet to insert value at the given position.

Note that value is treated as a length-bit integer (little endian convention); if necessary, value is padded with zeros (or truncated) to be of the correct length.

§Example
use rust_dense_bitset::DenseBitSet;

let mut bs = DenseBitSet::new();
bs.insert(10, 8, 0b10101011);
bs.insert(3,1,1);

assert_eq!(bs.to_integer(), 0b101010110000001000)
§Panics

This function will panic if length is zero, or if one tries to insert a bit beyond the 64 bit limit (i.e. position + length > 64)

Source

pub fn all(self) -> bool

Returns true if and only if all bits are set to true.

§Example
use rust_dense_bitset::DenseBitSet;
use rust_dense_bitset::BitSet;

let mut bs = DenseBitSet::from_integer(u64::max_value());

assert!(bs.all());

bs.set_bit(28,false);
bs.all(); // -> false
Source

pub fn any(self) -> bool

Returns true if at least one of the bits is set to true.

§Example
use rust_dense_bitset::DenseBitSet;
use rust_dense_bitset::BitSet;

let mut bs = DenseBitSet::from_integer(2048);

assert!(bs.any());

bs.set_bit(11,false);
bs.any(); // -> false
Source

pub fn none(self) -> bool

Returns true if all the bits are set to false.

§Example
use rust_dense_bitset::DenseBitSet;
use rust_dense_bitset::BitSet;

let mut bs = DenseBitSet::from_integer(2048);
bs.set_bit(11,false);

assert!(bs.none());
Source

pub fn reverse(self) -> Self

Returns a bit-reversed DenseBitSet.

This method is using a constant time bit reversal algorithm for 64 bits integers.

§Example
use rust_dense_bitset::DenseBitSet;

let bs = DenseBitSet::from_integer(0b11110001);
let bs2 = bs.reverse();

assert_eq!(bs2.to_integer(), 0b1000111100000000000000000000000000000000000000000000000000000000);
Source

pub fn rotr(&mut self, shift: u32)

Right rotation of shift bits.

Shifts the bits to the right, wrapping the truncated bits to the end of the bitset.

The rotation is done in-place, so the bitset needs to be mutable.

§Example
use rust_dense_bitset::DenseBitSet;

let mut bs = DenseBitSet::from_integer(0b111000111000111000111);
bs.rotr(10);

assert_eq!(bs.to_integer(), 0b111000111000000000000000000000000000000000000000000011100011100 );
Source

pub fn rotl(&mut self, shift: u32)

Left rotation of shift bits.

Shifts the bits to the left, wrapping the truncated bits to the beginning of the bitset.

The rotation is done in place, so the bitset needs to be mutable.

§Example
use rust_dense_bitset::DenseBitSet;

let mut bs = DenseBitSet::from_integer(0b111000111000111000111);
bs.rotl(10);

assert_eq!(bs.to_integer(), 0b1110001110001110001110000000000 );
Source

pub fn first_set(self) -> u32

Returns the position of the first set bit (little endian convention)

§Example
let dbs = DenseBitSet::from_integer(256);
println!("{}", dbs.first_set());

Trait Implementations§

Source§

impl BitAnd for DenseBitSet

Source§

type Output = DenseBitSet

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: Self) -> Self

Performs the & operation. Read more
Source§

impl BitAndAssign for DenseBitSet

Source§

fn bitand_assign(&mut self, rhs: Self)

Performs the &= operation. Read more
Source§

impl BitOr for DenseBitSet

Source§

type Output = DenseBitSet

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: Self) -> Self

Performs the | operation. Read more
Source§

impl BitOrAssign for DenseBitSet

Source§

fn bitor_assign(&mut self, rhs: Self)

Performs the |= operation. Read more
Source§

impl BitSet for DenseBitSet

This is a compact implementation of the BitSet trait over a 64-bit word (which is the native word size for many architectures), allowing for efficient operations and compact memory usage.

Modifiers and accessors are boundary checked to ensure that operations remain within that 64 bit range.

Note: The BitSet trait must be in scope in order to use methods from this trait.

Source§

fn set_bit(&mut self, position: usize, value: bool)

Sets the bit at index position to value. The bitset needs to be mutable for this operation to succeed.

§Example
use rust_dense_bitset::DenseBitSet;
use rust_dense_bitset::BitSet;

let mut bs = DenseBitSet::new();
bs.set_bit(25, true); // This sets the bit at index 25 , hence the 26th bit -> 2^25

assert_eq!(bs.to_integer(), 33554432);
Source§

fn get_bit(&self, position: usize) -> bool

Get the bit at index position.

§Example
use rust_dense_bitset::DenseBitSet;
use rust_dense_bitset::BitSet;

let bs = DenseBitSet::from_integer(65536);

assert!(bs.get_bit(16));
Source§

fn get_weight(&self) -> u32

Returns the bitset’s Hamming weight (in other words, the number of bits set to true).

§Example
use rust_dense_bitset::DenseBitSet;
use rust_dense_bitset::BitSet;

let bs = DenseBitSet::from_integer(0b01100100111010);

println!("{}", bs.get_weight()); // -> 7
Source§

fn reset(&mut self)

This resets the bitset to its empty state. (The bitset must be mutable for this operation).

§Example
use rust_dense_bitset::DenseBitSet;
use rust_dense_bitset::BitSet;

let mut bs = DenseBitSet::from_integer(1234567890);
bs.reset();

assert!(bs.none());
Source§

fn to_string(self) -> String

Returns a representation of the bitset as a String.

§Example
use rust_dense_bitset::DenseBitSet;
use rust_dense_bitset::BitSet;

let bs = DenseBitSet::from_integer(68719481088);

println!("{}", bs.to_string()) // -> "0000000000000000000000000001000000000000000000000001000100000000"
Source§

impl BitXor for DenseBitSet

Source§

type Output = DenseBitSet

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: Self) -> Self

Performs the ^ operation. Read more
Source§

impl BitXorAssign for DenseBitSet

Source§

fn bitxor_assign(&mut self, rhs: Self)

Performs the ^= operation. Read more
Source§

impl Clone for DenseBitSet

Source§

fn clone(&self) -> DenseBitSet

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for DenseBitSet

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for DenseBitSet

Source§

fn default() -> DenseBitSet

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

impl Hash for DenseBitSet

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

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

impl Not for DenseBitSet

Source§

type Output = DenseBitSet

The resulting type after applying the ! operator.
Source§

fn not(self) -> Self

Performs the unary ! operation. Read more
Source§

impl PartialEq for DenseBitSet

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Shl<usize> for DenseBitSet

Source§

type Output = DenseBitSet

The resulting type after applying the << operator.
Source§

fn shl(self, rhs: usize) -> Self

Performs the << operation. Read more
Source§

impl ShlAssign<usize> for DenseBitSet

Source§

fn shl_assign(&mut self, rhs: usize)

Performs the <<= operation. Read more
Source§

impl Shr<usize> for DenseBitSet

Source§

type Output = DenseBitSet

The resulting type after applying the >> operator.
Source§

fn shr(self, rhs: usize) -> Self

Performs the >> operation. Read more
Source§

impl ShrAssign<usize> for DenseBitSet

Source§

fn shr_assign(&mut self, rhs: usize)

Performs the >>= operation. Read more
Source§

impl Copy for DenseBitSet

Source§

impl Eq for DenseBitSet

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.