pub trait BoxSliceExt {
// Required methods
fn from_bit_siter(t: impl ExactSizeIterator<Item = bool>) -> Self;
fn zeros(len: usize) -> Self;
fn ones(len: usize) -> Self;
fn count_ones(&self) -> u32;
fn conjunction_assign(&mut self, other: &Self);
fn union_assign(&mut self, other: &Self);
fn difference_assign(&mut self, other: &Self);
fn subset(&self, other: &Self) -> bool;
fn get(&self, index: usize) -> bool;
}
Expand description
A trait defining extended operations on box slices, particularly focused on bit manipulation.
This trait provides methods to create a new instance from an iterator of boolean values, initialize slices filled with zeros or ones, and count the number of ones in the slice. It also includes methods to perform bitwise conjunction, union, and difference operations in place, while offering functionality to check if a slice is a subset of another and to access the boolean value at a specific index. These operations facilitate efficient manipulation and querying of boxed slices as bit arrays.
Required Methods§
fn from_bit_siter(t: impl ExactSizeIterator<Item = bool>) -> Self
fn zeros(len: usize) -> Self
fn ones(len: usize) -> Self
fn count_ones(&self) -> u32
fn conjunction_assign(&mut self, other: &Self)
fn union_assign(&mut self, other: &Self)
fn difference_assign(&mut self, other: &Self)
fn subset(&self, other: &Self) -> bool
fn get(&self, index: usize) -> bool
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementations on Foreign Types§
Source§impl BoxSliceExt for Box<[u128]>
impl BoxSliceExt for Box<[u128]>
Source§fn zeros(len: usize) -> Self
fn zeros(len: usize) -> Self
Creates a boxed slice of u128
integers, initializing each value to zero.
It achieves this by calculating the necessary length in terms of u128
blocks to cover the specified len
in bits and then mapping over this range, collecting zeros into a boxed slice.
The process involves using a ceiling division to ensure sufficient space is allocated, corresponding to the bit width of u128
.
Source§fn ones(len: usize) -> Self
fn ones(len: usize) -> Self
Creates a new boxed slice of u128
integers each filled with the value u128::MAX
, with partial bit masking applied to the last element if the specified length does not directly align with a multiple of u128
’s bit size.
This function initializes a vector by determining the number of u128
elements required via ceiling division, fills it with maximum possible 128-bit unsigned integers, and then collects this into a boxed slice.
If the intended length in bits leaves a remainder that isn’t a full u128
, the final element is appropriately masked to ensure only the requisite bits are set to one, maintaining the defined length.
Source§fn count_ones(&self) -> u32
fn count_ones(&self) -> u32
Provides a method that calculates the total number of one-bits in a boxed slice of u128
integers.
It iterates over each u128
value in the slice, applies the count_ones
method to obtain the number of one-bits for each element, and sums these counts to return the cumulative total as a u32
.
This operation enables efficient bit counting across potentially large numeric sequences.
Source§fn conjunction_assign(&mut self, other: &Self)
fn conjunction_assign(&mut self, other: &Self)
Performs an in-place bitwise conjunction between two slices of u128
.
This operation modifies the elements of the caller slice by iterating through it, paired with elements from the other
slice, applying a bitwise AND operation, and storing the result back into the caller slice elements.
Each element in the slice is processed in sequence such that their corresponding positions between the two slices are combined using the AND operation.
Source§fn union_assign(&mut self, other: &Self)
fn union_assign(&mut self, other: &Self)
Performs an in-place union operation between two boxed slices.
This method iterates over each element of the boxed slice and the corresponding element of another boxed slice, applying the bitwise OR operation.
The result of this operation updates the elements of the boxed slice on which the method is called (self
).
This allows for the combination of elements in two slices, storing the union of each pair of elements from the slices into the first slice.
This operation assumes both slices have the same length, as it utilizes the zip
method to combine elements from self
and other
.
Source§fn difference_assign(&mut self, other: &Self)
fn difference_assign(&mut self, other: &Self)
Calculates the difference between the current boxed slice of u128
integers and another slice, assigning the result to the current instance.
This method iterates through both slices in parallel, applying a bitwise AND with the negation operation on each corresponding pair of elements, thereby effectively subtracting the bit representation of the elements from other
out of the current slice’s elements.
Source§fn subset(&self, other: &Self) -> bool
fn subset(&self, other: &Self) -> bool
Determines if one boxed slice of u128
integers is a subset of another.
This functionality is executed by comparing each integer in the other
slice with the corresponding integer in self
slice.
The method utilizes bitwise AND operation to ascertain that every integer in self
is contained within the same indexed position in other
.
The method returns true only if this condition holds for every pair of integers throughout the slices, indicating that self
is indeed a subset of other
.
Source§fn from_bit_siter(t: impl ExactSizeIterator<Item = bool>) -> Self
fn from_bit_siter(t: impl ExactSizeIterator<Item = bool>) -> Self
Constructs a boxed slice of u128
integers from an iterator of boolean values.
The function takes an ExactSizeIterator
of bool
items and initializes a Vec<u128>
large enough to represent each boolean value as a bit within u128
segments.
It calculates the necessary number of u128
elements using a ceiling division based on the iterator’s length divided by 128.
As it iterates over the boolean items, it appropriately sets bits in the u128
segments depending on their indexes.
This results in a bit-packed representation of the iterator’s boolean sequence, which is then converted into a boxed slice for returned storage efficiency.
Source§fn get(&self, index: usize) -> bool
fn get(&self, index: usize) -> bool
Returns the boolean value of the bit at the given index for a boxed slice of u128
integers.
This function calculates the position of the bit within the slice by dividing the index
by 128 to locate the correct u128
integer and then using the modulus operation to determine the bit position within that integer.
It then uses bitwise operations to isolate the bit at the specified index and checks whether it is set, returning true
if the bit is 1
and false
otherwise.