pub struct Bitset { /* private fields */ }Expand description
A dense bitset
Implementations§
Source§impl Bitset
impl Bitset
Sourcepub fn as_mut_slice(&mut self) -> &mut [u64]
pub fn as_mut_slice(&mut self) -> &mut [u64]
Access the raw underlying slice
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Create a new bitset
Does not allocate
§Examples
use croaring::Bitset;
let bitset = Bitset::new();
assert_eq!(bitset.capacity(), 0);
assert_eq!(bitset.size_in_bits(), 0);Sourcepub fn with_size(size: usize) -> Self
pub fn with_size(size: usize) -> Self
Create a new bitset of the specified size
§Examples
use croaring::Bitset;
let bitset = Bitset::with_size(100);
// Actual size/capacity may be rounded up
assert!(bitset.capacity() >= 100);
assert!(bitset.size_in_bits() >= 100);Sourcepub fn fill(&mut self, value: bool)
pub fn fill(&mut self, value: bool)
Set all bits to zero
§Examples
use croaring::Bitset;
let mut bitset = Bitset::with_size(64);
bitset.fill(true);
assert!(bitset.get(1));
assert!(bitset.get(63));
// Bitset size stays the same
assert!(!bitset.get(64));
bitset.fill(false);
assert!(!bitset.get(1));
assert!(!bitset.get(63));
assert!(!bitset.get(64));Sourcepub const fn size_in_bytes(&self) -> usize
pub const fn size_in_bytes(&self) -> usize
How many bytes of memory the backend buffer uses
Sourcepub const fn size_in_bits(&self) -> usize
pub const fn size_in_bits(&self) -> usize
How many bits can be accessed
Sourcepub const fn size_in_words(&self) -> usize
pub const fn size_in_words(&self) -> usize
How many 64-bit words of memory the backend buffer uses
Sourcepub fn resize_words(&mut self, new_array_size: usize, value: bool)
pub fn resize_words(&mut self, new_array_size: usize, value: bool)
Resize the bitset to contain new_array_size 64-bit words
New bits are set to value
§Examples
use croaring::Bitset;
let mut bitset = Bitset::new();
bitset.resize_words(1, false);
bitset.resize_words(2, true);
assert_eq!(bitset.iter().collect::<Vec<_>>(), (64..128).collect::<Vec<_>>());Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Attempts to recover unused memory by shrinking capacity to fit the highest set bit
§Examples
use croaring::Bitset;
let mut bitset = Bitset::new();
bitset.set(63);
bitset.set(1000);
assert!(bitset.size_in_bits() > 1000);
bitset.set_to_value(1000, false);
bitset.shrink_to_fit();
// The next highest bit is at index 63
assert_eq!(bitset.size_in_bits(), 64);Sourcepub fn set(&mut self, i: usize)
pub fn set(&mut self, i: usize)
Set the ith bit
Will resize the bitset if needed, any other newly added bits will be initialized to zero
§Examples
use croaring::Bitset;
let mut bitset = Bitset::new();
bitset.set(1);
bitset.set(2);
bitset.set(100);
assert_eq!(bitset.iter().collect::<Vec<_>>(), vec![1, 2, 100]);Sourcepub fn set_to_value(&mut self, i: usize, value: bool)
pub fn set_to_value(&mut self, i: usize, value: bool)
Set the ith bit to value
Will resize the bitset if needed, any other newly added bits will be initialized to zero
§Examples
use croaring::Bitset;
let mut bitset = Bitset::new();
bitset.set_to_value(1, true);
bitset.set_to_value(2, true);
bitset.set_to_value(100, true);
bitset.set_to_value(1, false);
assert_eq!(bitset.iter().collect::<Vec<_>>(), vec![2, 100]);Sourcepub const fn get(&self, i: usize) -> bool
pub const fn get(&self, i: usize) -> bool
Get the value of the ith bit
If the bit is out of bounds, returns false
§Examples
use croaring::Bitset;
let mut bitset = Bitset::new();
bitset.set(1);
bitset.set(2);
assert!(bitset.get(1));
assert!(bitset.get(2));
assert!(!bitset.get(3));Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Check if the bitset is empty
§Examples
use croaring::Bitset;
let mut bitset = Bitset::new();
assert!(bitset.is_empty());
bitset.set(100);
assert!(!bitset.is_empty());Sourcepub fn count(&self) -> usize
pub fn count(&self) -> usize
Count of number of set bits
§Examples
use croaring::Bitset;
let mut bitset: Bitset = [1, 2, 3, 100].into_iter().collect();
assert_eq!(bitset.count(), 4);Sourcepub fn minimum(&self) -> usize
pub fn minimum(&self) -> usize
Index of the first set bit, or usize::MAX if the bitset has no set bits
§Examples
use croaring::Bitset;
let mut bitset = Bitset::new();
// minimum returns 0 if the bitset is empty
assert_eq!(bitset.minimum(), usize::MAX);
bitset.set(100);
assert_eq!(bitset.minimum(), 100);Sourcepub fn maximum(&self) -> usize
pub fn maximum(&self) -> usize
Index of the last set bit, or zero if the bitset has no set bits
§Examples
use croaring::Bitset;
let mut bitset = Bitset::new();
// maximum returns 0 if the bitset is empty
assert_eq!(bitset.maximum(), 0);
bitset.set(100);
assert_eq!(bitset.maximum(), 100);
bitset.set(1000);
assert_eq!(bitset.maximum(), 1000);Sourcepub fn union_count(&self, other: &Self) -> usize
pub fn union_count(&self, other: &Self) -> usize
The size of the hypothetical union of self and other
§Examples
use croaring::Bitset;
let mut bitset1: Bitset = [1, 2, 3, 100].into_iter().collect();
let bitset2: Bitset = [2, 3, 4, 5].into_iter().collect();
assert_eq!(bitset1.union_count(&bitset2), 6);
bitset1 |= &bitset2;
assert_eq!(bitset1.count(), 6);Sourcepub fn intersection_count(&self, other: &Self) -> usize
pub fn intersection_count(&self, other: &Self) -> usize
The size of the hypothetical intersection of self and other
§Examples
use croaring::Bitset;
let mut bitset1: Bitset = [1, 2, 3, 100].into_iter().collect();
let bitset2: Bitset = [2, 3, 4, 5].into_iter().collect();
assert_eq!(bitset1.intersection_count(&bitset2), 2);
bitset1 &= &bitset2;
assert_eq!(bitset1.count(), 2);Sourcepub fn is_disjoint(&self, other: &Self) -> bool
pub fn is_disjoint(&self, other: &Self) -> bool
Return true if self and other contain no common elements
§Examples
use croaring::Bitset;
let bitset1: Bitset = [1, 2, 3, 100].into_iter().collect();
let bitset2: Bitset = [2, 3, 4, 5].into_iter().collect();
assert!(!bitset1.is_disjoint(&bitset2));
let bitset3: Bitset = [4, 5, 6, 7].into_iter().collect();
assert!(bitset1.is_disjoint(&bitset3));Empty bitsets are always disjoint
use croaring::Bitset;
let bitset1 = Bitset::new();
let bitset2 = Bitset::new();
assert!(bitset1.is_disjoint(&bitset2));Sourcepub fn has_intersect(&self, other: &Self) -> bool
pub fn has_intersect(&self, other: &Self) -> bool
Return true if self and other contain at least one common element
§Examples
use croaring::Bitset;
let bitset1: Bitset = [1, 2, 3, 100].into_iter().collect();
let bitset2: Bitset = [2, 3, 4, 5].into_iter().collect();
assert!(bitset1.has_intersect(&bitset2));
let bitset3: Bitset = [4, 5, 6, 7].into_iter().collect();
assert!(!bitset1.has_intersect(&bitset3));Sourcepub fn is_superset(&self, other: &Self) -> bool
pub fn is_superset(&self, other: &Self) -> bool
Return true if self is a superset of other
§Examples
use croaring::Bitset;
let bitset1: Bitset = [1, 2, 3, 100].into_iter().collect();
let bitset2: Bitset = [2, 3].into_iter().collect();
assert!(bitset1.is_superset(&bitset2));
let bitset3: Bitset = [4, 5, 6, 7].into_iter().collect();
assert!(!bitset1.is_superset(&bitset3));Sourcepub fn difference_count(&self, other: &Self) -> usize
pub fn difference_count(&self, other: &Self) -> usize
The size of the hypothetical difference of self and other
§Examples
use croaring::Bitset;
let mut bitset1: Bitset = [1, 2, 3, 100].into_iter().collect();
let bitset2: Bitset = [2, 3, 4, 5].into_iter().collect();
assert_eq!(bitset1.difference_count(&bitset2), 2);
bitset1 -= &bitset2;
assert_eq!(bitset1.count(), 2);Sourcepub fn symmetric_difference_count(&self, other: &Self) -> usize
pub fn symmetric_difference_count(&self, other: &Self) -> usize
The size of the hypothetical symmetric difference (xor) of self and other
§Examples
use croaring::Bitset;
let mut bitset1: Bitset = [1, 2, 3, 100].into_iter().collect();
let bitset2: Bitset = [2, 3, 4, 5].into_iter().collect();
assert_eq!(bitset1.symmetric_difference_count(&bitset2), 4);
bitset1 ^= &bitset2;
assert_eq!(bitset1.count(), 4);Sourcepub fn as_raw(&self) -> &bitset_t
pub fn as_raw(&self) -> &bitset_t
Expose the raw CRoaring bitset
This allows calling raw CRoaring functions on the bitset.
Sourcepub fn as_raw_mut(&mut self) -> &mut bitset_t
pub fn as_raw_mut(&mut self) -> &mut bitset_t
Expose the raw CRoaring bitset mutably
This allows calling raw CRoaring functions on the bitset.
Trait Implementations§
Source§impl BitAndAssign<&Bitset> for Bitset
impl BitAndAssign<&Bitset> for Bitset
Source§fn bitand_assign(&mut self, rhs: &Bitset)
fn bitand_assign(&mut self, rhs: &Bitset)
&= operation. Read moreSource§impl BitOrAssign<&Bitset> for Bitset
impl BitOrAssign<&Bitset> for Bitset
Source§fn bitor_assign(&mut self, rhs: &Bitset)
fn bitor_assign(&mut self, rhs: &Bitset)
|= operation. Read moreSource§impl BitXorAssign<&Bitset> for Bitset
impl BitXorAssign<&Bitset> for Bitset
Source§fn bitxor_assign(&mut self, rhs: &Bitset)
fn bitxor_assign(&mut self, rhs: &Bitset)
^= operation. Read moreSource§impl Extend<usize> for Bitset
impl Extend<usize> for Bitset
Source§fn extend<T: IntoIterator<Item = usize>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = usize>>(&mut self, iter: T)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl FromIterator<usize> for Bitset
impl FromIterator<usize> for Bitset
Source§impl<'a> IntoIterator for &'a Bitset
impl<'a> IntoIterator for &'a Bitset
Source§impl ShlAssign<usize> for Bitset
impl ShlAssign<usize> for Bitset
Source§fn shl_assign(&mut self, shift: usize)
fn shl_assign(&mut self, shift: usize)
<<= operation. Read moreSource§impl ShrAssign<usize> for Bitset
impl ShrAssign<usize> for Bitset
Source§fn shr_assign(&mut self, shift: usize)
fn shr_assign(&mut self, shift: usize)
>>= operation. Read moreSource§impl SubAssign<&Bitset> for Bitset
impl SubAssign<&Bitset> for Bitset
Source§fn sub_assign(&mut self, rhs: &Bitset)
fn sub_assign(&mut self, rhs: &Bitset)
-= operation. Read more