Struct croaring::bitset::Bitset

source ·
#[repr(transparent)]
pub struct Bitset { /* private fields */ }
Expand description

A dense bitset

Implementations§

source§

impl Bitset

source

pub const fn as_slice(&self) -> &[u64]

Access the raw underlying slice

source

pub fn as_mut_slice(&mut self) -> &mut [u64]

Access the raw underlying slice

source

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);
source

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);
source

pub const fn capacity(&self) -> usize

Capacity in bits

source

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));
source

pub const fn size_in_bytes(&self) -> usize

How many bytes of memory the backend buffer uses

source

pub const fn size_in_bits(&self) -> usize

How many bits can be accessed

source

pub const fn size_in_words(&self) -> usize

How many 64-bit words of memory the backend buffer uses

source

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<_>>());
source

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);
source

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]);
source

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]);
source

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));
source

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);
source

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);
source

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);
source

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);
source

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);
source

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));
source

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));
source

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));
source

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);
source

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);
source

pub fn as_raw(&self) -> &bitset_t

Expose the raw CRoaring bitset

This allows calling raw CRoaring functions on the bitset.

source

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.

source§

impl Bitset

source

pub const fn iter(&self) -> BitsetIterator<'_>

Returns an iterator over the set bits in the bitset

Trait Implementations§

source§

impl BitAndAssign<&Bitset> for Bitset

source§

fn bitand_assign(&mut self, rhs: &Bitset)

Performs the &= operation. Read more
source§

impl BitOrAssign<&Bitset> for Bitset

source§

fn bitor_assign(&mut self, rhs: &Bitset)

Performs the |= operation. Read more
source§

impl BitXorAssign<&Bitset> for Bitset

source§

fn bitxor_assign(&mut self, rhs: &Bitset)

Performs the ^= operation. Read more
source§

impl Clone for Bitset

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Bitset

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Bitset

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl Drop for Bitset

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl Extend<usize> for Bitset

source§

fn extend<T: IntoIterator<Item = usize>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
source§

impl FromIterator<usize> for Bitset

source§

fn from_iter<T: IntoIterator<Item = usize>>(iter: T) -> Self

Creates a value from an iterator. Read more
source§

impl<'a> IntoIterator for &'a Bitset

§

type Item = usize

The type of the elements being iterated over.
§

type IntoIter = BitsetIterator<'a>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl ShlAssign<usize> for Bitset

source§

fn shl_assign(&mut self, shift: usize)

Performs the <<= operation. Read more
source§

impl ShrAssign<usize> for Bitset

source§

fn shr_assign(&mut self, shift: usize)

Performs the >>= operation. Read more
source§

impl SubAssign<&Bitset> for Bitset

source§

fn sub_assign(&mut self, rhs: &Bitset)

Performs the -= operation. Read more
source§

impl Send for Bitset

source§

impl Sync for Bitset

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.