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
impl DenseBitSet
Sourcepub fn new() -> Self
pub fn new() -> Self
Returns a new empty DenseBitSet
.
§Example
use rust_dense_bitset::DenseBitSet;
let mut bs = DenseBitSet::new();
Sourcepub fn from_integer(i: u64) -> Self
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);
Sourcepub fn from_string(s: &str, base: u32) -> Self
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.
Sourcepub fn to_integer(self) -> u64
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);
Sourcepub fn extract(self, position: usize, length: usize) -> u64
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
).
Sourcepub fn insert(&mut self, position: usize, length: usize, value: u64)
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
)
Sourcepub fn all(self) -> bool
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
Sourcepub fn any(self) -> bool
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
Sourcepub fn none(self) -> bool
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());
Sourcepub fn reverse(self) -> Self
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);
Sourcepub fn rotr(&mut self, shift: u32)
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 );
Sourcepub fn rotl(&mut self, shift: u32)
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 );
Trait Implementations§
Source§impl BitAnd for DenseBitSet
impl BitAnd for DenseBitSet
Source§impl BitAndAssign for DenseBitSet
impl BitAndAssign for DenseBitSet
Source§fn bitand_assign(&mut self, rhs: Self)
fn bitand_assign(&mut self, rhs: Self)
&=
operation. Read moreSource§impl BitOr for DenseBitSet
impl BitOr for DenseBitSet
Source§impl BitOrAssign for DenseBitSet
impl BitOrAssign for DenseBitSet
Source§fn bitor_assign(&mut self, rhs: Self)
fn bitor_assign(&mut self, rhs: Self)
|=
operation. Read moreSource§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.
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)
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
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
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)
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
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
impl BitXor for DenseBitSet
Source§impl BitXorAssign for DenseBitSet
impl BitXorAssign for DenseBitSet
Source§fn bitxor_assign(&mut self, rhs: Self)
fn bitxor_assign(&mut self, rhs: Self)
^=
operation. Read moreSource§impl Clone for DenseBitSet
impl Clone for DenseBitSet
Source§fn clone(&self) -> DenseBitSet
fn clone(&self) -> DenseBitSet
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for DenseBitSet
impl Debug for DenseBitSet
Source§impl Default for DenseBitSet
impl Default for DenseBitSet
Source§fn default() -> DenseBitSet
fn default() -> DenseBitSet
Source§impl Hash for DenseBitSet
impl Hash for DenseBitSet
Source§impl Not for DenseBitSet
impl Not for DenseBitSet
Source§impl PartialEq for DenseBitSet
impl PartialEq for DenseBitSet
Source§impl Shl<usize> for DenseBitSet
impl Shl<usize> for DenseBitSet
Source§impl ShlAssign<usize> for DenseBitSet
impl ShlAssign<usize> for DenseBitSet
Source§fn shl_assign(&mut self, rhs: usize)
fn shl_assign(&mut self, rhs: usize)
<<=
operation. Read moreSource§impl Shr<usize> for DenseBitSet
impl Shr<usize> for DenseBitSet
Source§impl ShrAssign<usize> for DenseBitSet
impl ShrAssign<usize> for DenseBitSet
Source§fn shr_assign(&mut self, rhs: usize)
fn shr_assign(&mut self, rhs: usize)
>>=
operation. Read more