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 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 zero 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(), 0);
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 moreimpl Send for Bitset
impl Sync for Bitset
Auto Trait Implementations§
impl Freeze for Bitset
impl RefUnwindSafe for Bitset
impl Unpin for Bitset
impl UnwindSafe for Bitset
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit)