Skip to main content

SparseMap

Struct SparseMap 

Source
pub struct SparseMap { /* private fields */ }
Expand description

A sparse, compressed, run-length-encoded bitset over u64 indices.

See the crate-level documentation for the data model.

Implementations§

Source§

impl SparseMap

Source

pub fn iter(&self) -> Iter<'_> ⓘ

Returns an iterator over the set bits, in ascending order.

use sparsemap::SparseMap;
let m: SparseMap = [3, 1, 4, 1, 5].into_iter().collect();
assert_eq!(m.iter().collect::<Vec<_>>(), vec![1, 3, 4, 5]);
Source

pub fn to_vec(&self) -> Vec<u64>

Collects the set bits into a Vec<u64>, ascending.

Allocates; provided for parity with the C sm_to_array.

Source§

impl SparseMap

Source

pub fn union(&self, other: &SparseMap) -> SparseMap

Returns the union (self ∪ other): bits set in either map.

Source

pub fn intersection(&self, other: &SparseMap) -> SparseMap

Returns the intersection (self ∩ other): bits set in both maps.

Source

pub fn difference(&self, other: &SparseMap) -> SparseMap

Returns the difference (self \ other): bits set in self but not in other.

Source

pub fn symmetric_difference(&self, other: &SparseMap) -> SparseMap

Returns the symmetric difference (self △ other): bits set in exactly one map.

Source

pub fn intersects(&self, other: &SparseMap) -> bool

Returns true if self and other share at least one bit.

Source

pub fn is_subset(&self, other: &SparseMap) -> bool

Returns true if every bit set in self is also set in other.

Source

pub fn is_superset(&self, other: &SparseMap) -> bool

Returns true if every bit set in other is also set in self.

Source

pub fn shifted(&self, offset: i64) -> SparseMap

Returns a copy with every bit shifted by offset (positive = toward higher indices). Bits that would move below zero or above u64::MAX are dropped, matching the C sm_offset.

Source§

impl SparseMap

Source

pub fn to_bytes(&self) -> Vec<u8> ⓘ

Serializes the map into the C-compatible wire format (version 2: 8-byte chunk-start offsets, addressing the full 64-bit universe).

Source

pub fn from_bytes(buf: &[u8]) -> Result<SparseMap, DecodeError>

Deserializes a buffer produced by SparseMap::to_bytes or by the C sm_serialize.

§Errors

Returns a DecodeError for any malformed input rather than panicking; arbitrary bytes are safe to feed in.

Source§

impl SparseMap

Source

pub const fn new() -> Self

Creates an empty map.

Source

pub fn is_empty(&self) -> bool

Returns true if the map contains no set bits.

Source

pub fn clear(&mut self)

Removes all bits, retaining no allocation.

Source

pub fn cardinality(&self) -> u64

Returns the number of set bits (the cardinality).

Runs are counted in O(1), so this is cheap even for maps dominated by long runs.

Source

pub fn len(&self) -> u64

Returns the number of set bits.

An alias for cardinality, named to match the Rust collection convention (and roaring::RoaringBitmap), so the type drops in for code written against those APIs.

Source

pub fn contains(&self, idx: u64) -> bool

Returns true if bit idx is set.

Source

pub fn insert(&mut self, idx: u64) -> bool

Sets bit idx. Returns true if the bit was newly set, false if it was already set (mirroring alloc::collections::BTreeSet::insert).

Source

pub fn remove(&mut self, idx: u64) -> bool

Clears bit idx. Returns true if the bit had been set.

Source

pub fn min(&self) -> Option<u64>

Returns the smallest set bit, or None if the map is empty.

Source

pub fn max(&self) -> Option<u64>

Returns the largest set bit, or None if the map is empty.

Source

pub fn rank(&self, idx: u64) -> u64

Returns the number of set bits strictly less than idx (the standard succinct-structure rank).

rank(0) is always 0; rank(u64::MAX) + contains(u64::MAX) as u64 is the cardinality.

Source

pub fn select(&self, n: u64) -> Option<u64>

Returns the position of the n-th set bit (0-based), or None if there are n or fewer set bits.

select(0) is min.

Source

pub fn insert_range(&mut self, start: u64, end: u64)

Sets every bit in the half-open range [start, end).

Source

pub fn remove_range(&mut self, start: u64, end: u64)

Clears every bit in the half-open range [start, end).

Source

pub fn span(&self, start: u64, len: u64, value: bool) -> Option<u64>

Returns the start of the first run of at least len consecutive bits all equal to value, beginning the search at start, or None if there is no such run.

Trait Implementations§

Source§

impl BitAnd for &SparseMap

Source§

type Output = SparseMap

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: &SparseMap) -> SparseMap

Performs the & operation. Read more
Source§

impl BitAndAssign<&SparseMap> for SparseMap

Source§

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

Performs the &= operation. Read more
Source§

impl BitOr for &SparseMap

Source§

type Output = SparseMap

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: &SparseMap) -> SparseMap

Performs the | operation. Read more
Source§

impl BitOrAssign<&SparseMap> for SparseMap

Source§

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

Performs the |= operation. Read more
Source§

impl BitXor for &SparseMap

Source§

type Output = SparseMap

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: &SparseMap) -> SparseMap

Performs the ^ operation. Read more
Source§

impl BitXorAssign<&SparseMap> for SparseMap

Source§

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

Performs the ^= operation. Read more
Source§

impl Clone for SparseMap

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for SparseMap

Source§

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

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

impl Default for SparseMap

Source§

fn default() -> Self

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

impl Eq for SparseMap

Source§

impl<'a> Extend<&'a u64> for SparseMap

Source§

fn extend<I: IntoIterator<Item = &'a u64>>(&mut self, iter: I)

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

fn extend_one(&mut self, item: T)

🔬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 Extend<u64> for SparseMap

Source§

fn extend<I: IntoIterator<Item = u64>>(&mut self, iter: I)

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

fn extend_one(&mut self, item: T)

🔬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<u64> for SparseMap

Source§

fn from_iter<I: IntoIterator<Item = u64>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl Hash for SparseMap

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'a> IntoIterator for &'a SparseMap

Source§

type Item = u64

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a>

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

fn into_iter(self) -> Iter<'a> ⓘ

Creates an iterator from a value. Read more
Source§

impl PartialEq for SparseMap

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for SparseMap

Source§

impl Sub for &SparseMap

Source§

type Output = SparseMap

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: &SparseMap) -> SparseMap

Performs the - operation. Read more
Source§

impl SubAssign<&SparseMap> for SparseMap

Source§

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

Performs the -= operation. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 T
where 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 T
where T: Clone,

Source§

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 T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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.