pub struct DenseBitSet { /* fields omitted */ }
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.
Returns a new empty DenseBitSet
.
use rust_dense_bitset::DenseBitSet;
let mut bs = DenseBitSet::new();
Generates a bitset from an integer (little endian convention).
use rust_dense_bitset::DenseBitSet;
let bs = DenseBitSet::from_integer(1234567890);
Generates a bitset from a string and a base (little endian convention).
The base
must be an integer between 2 and 32.
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);
This function will panic if an incorrect base
is provided or if invalid
characters are found when parsing.
Returns an integer representing the bitset (little endian convention).
use rust_dense_bitset::DenseBitSet;
let bs = DenseBitSet::from_integer(1234);
assert_eq!(bs.to_integer(), 1234);
Returns an integer representation of the bitset starting at the given position
with given length
(little endian convention).
use rust_dense_bitset::DenseBitSet;
let bs = DenseBitSet::from_integer(0b11110101010010);
let value = bs.extract(5,6);
assert_eq!(value, 42);
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
).
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.
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)
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
)
Returns true
if and only if all bits are set to true
.
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();
Returns true
if at least one of the bits is set to true
.
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();
Returns true
if all the bits are set to false
.
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());
Returns a bit-reversed DenseBitSet
.
This method is using a constant time bit reversal algorithm for 64 bits integers.
use rust_dense_bitset::DenseBitSet;
let bs = DenseBitSet::from_integer(0b11110001);
let bs2 = bs.reverse();
assert_eq!(bs2.to_integer(), 0b1000111100000000000000000000000000000000000000000000000000000000);
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.
use rust_dense_bitset::DenseBitSet;
let mut bs = DenseBitSet::from_integer(0b111000111000111000111);
bs.rotr(10);
assert_eq!(bs.to_integer(), 0b111000111000000000000000000000000000000000000000000011100011100 );
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.
use rust_dense_bitset::DenseBitSet;
let mut bs = DenseBitSet::from_integer(0b111000111000111000111);
bs.rotl(10);
assert_eq!(bs.to_integer(), 0b1110001110001110001110000000000 );
Returns the position of the first set bit (little endian convention)
let dbs = DenseBitSet::from_integer(256);
println!("{}", dbs.first_set());
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.
Sets the bit at index position
to value
.
The bitset needs to be mutable for this operation to succeed.
use rust_dense_bitset::DenseBitSet;
use rust_dense_bitset::BitSet;
let mut bs = DenseBitSet::new();
bs.set_bit(25, true);
assert_eq!(bs.to_integer(), 33554432);
Get the bit at index position
.
use rust_dense_bitset::DenseBitSet;
use rust_dense_bitset::BitSet;
let bs = DenseBitSet::from_integer(65536);
assert!(bs.get_bit(16));
Returns the bitset's Hamming weight (in other words, the number of bits set to true).
use rust_dense_bitset::DenseBitSet;
use rust_dense_bitset::BitSet;
let bs = DenseBitSet::from_integer(0b01100100111010);
println!("{}", bs.get_weight());
This resets the bitset to its empty state.
(The bitset must be mutable for this operation).
use rust_dense_bitset::DenseBitSet;
use rust_dense_bitset::BitSet;
let mut bs = DenseBitSet::from_integer(1234567890);
bs.reset();
assert!(bs.none());
Returns a representation of the bitset as a String
.
use rust_dense_bitset::DenseBitSet;
use rust_dense_bitset::BitSet;
let bs = DenseBitSet::from_integer(68719481088);
println!("{}", bs.to_string())
Returns the "default value" for a type. Read more
This method tests for self
and other
values to be equal, and is used by ==
. Read more
This method tests for !=
.
Performs copy-assignment from source
. Read more
Formats the value using the given formatter. Read more
Feeds this value into the given [Hasher
]. Read more
Feeds a slice of this type into the given [Hasher
]. Read more
The resulting type after applying the !
operator.
Performs the unary !
operation.
The resulting type after applying the &
operator.
Performs the &
operation.
The resulting type after applying the |
operator.
Performs the |
operation.
The resulting type after applying the ^
operator.
Performs the ^
operation.
The resulting type after applying the <<
operator.
Performs the <<
operation.
The resulting type after applying the >>
operator.
Performs the >>
operation.
Performs the &=
operation.
Performs the |=
operation.
Performs the ^=
operation.
Performs the <<=
operation.
Performs the >>=
operation.
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
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static