Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Sparse Set

PagedSparseSet<K, PAGE = 1024, D = Flat> (Python: SparseSet) stores keys in a contiguous dense array and finds them through a paged sparse index from slot index to dense position.

Example

from paged_collections import SparseSet

keys = SparseSet([3, 7, 11])
keys.add(19)
print("inserted =", list(keys))
keys.remove(7)
print("contains 7 =", 7 in keys)
print("dense =", list(keys))
other = SparseSet([11, 23])
print("union =", sorted(keys | other))
print("intersection =", sorted(keys & other))
print("difference =", sorted(keys - other))
keys.clear()
print("cleared len =", len(keys))
use paged_collections::sparse_set::PagedSparseSet;

fn main() {
    let mut keys: PagedSparseSet<usize> = PagedSparseSet::new();
    keys.extend([3, 7, 11]);
    keys.insert(19);
    println!("inserted = {:?}", keys.as_slice());
    keys.remove(7);
    println!("contains 7 = {}", keys.contains(7));
    println!("dense = {:?}", keys.as_slice());
    keys.clear();
    println!("cleared len = {}", keys.len());
}

Python output:

inserted = [3, 7, 11, 19]
contains 7 = False
dense = [3, 19, 11]
union = [3, 11, 19, 23]
intersection = [11]
difference = [3, 19]
cleared len = 0

The Rust example prints the same lines without the set algebra.

Complexity

OperationCost
insert, remove, contains, get, takeO(1) with Flat; O(depth) with Radix; O(log pages) with Tree
clearO(1): pages are kept, stale slots are ignored
iteration, as_slicedense array only, O(len)
sortO(n log n) comparisons, then one index write per key

Lookups follow Briggs and Torczon. A lookup accepts a slot only if it points inside the dense array to a key with the same slot index. This check lets clear leave the index pages untouched.

Order

Iteration follows the dense array in insertion order until a removal. remove swaps the last key into the removed position (O(1)), changing the order. sort and its variants reorder the dense array. Sorted order then lasts until the next removal or insertion out of order.

Removing 9 from the dense keys 11, 9, 2 moves the last key, 2, into the middle slot.
Swap removal updates the sparse index for the moved key. Iteration now yields 11, then 2.

Generational Keys

The set holds at most one key per slot index. With generational keys, insert refuses a key whose slot is held by another generation and returns false; replace evicts the other generation; occupant(index) returns the key holding a slot. get(key) borrows the stored key and take(key) removes and returns it. Both require an exact generation match. See Ids and Side Tables.

Rust and Python

RustPythonNotes
insert(k) -> booladd(k), add_many(keys) -> int
remove(k) -> booldiscard(k), remove(k) (raises KeyError), discard_many(keys)swap removal
contains(k)k in s, contains_many(keys) -> ndarray[bool]
get(k)borrows the stored key
take(k)returns the stored key by swap removal
as_slice()to_array() (copy), view() (zero-copy, read-only)
iter()iter(s)dense order
first(), last()first(), last()smallest and largest keys
pop_first(), pop_last()pop_first(), pop_last()remove and return the smallest and largest keys, respectively
next_occupant, prev_occupantnext_occupant(index), prev_occupant(index)nearest key at or after, or at or before, index
sort*sort()
position(k)dense position
clear() / reset()clear()reset frees pages
allocated_pages()allocated_pages
union, intersection, difference, symmetric_difference, the *_update forms, | & - ^ and in-place operators, issubset, issuperset, isdisjoint, < <= > >=Python set protocol

Python keys are integers in 0 <= key < 2**32. Batch lookups (contains_many, discard_many) treat out-of-range integers as absent; add and add_many raise for them.

Ordered queries use the sparse index, and next_occupant and prev_occupant require a valid key and return None when no key matches. first(), last(), pop_first() and pop_last() raise KeyError on an empty set. Ordered pops use swap removal and raise BufferError while a view() is alive.

Set Algebra

  • Operators take SparseSet operands; methods accept any iterable of keys.
  • Results use the receiver’s page size, keep the receiver’s surviving keys in order, then append new keys in operand iteration order.
  • difference_update and -= use swap removal; other in-place operations keep the receiver’s order.
  • In-place operations raise BufferError while a view() is alive.

API: PagedSparseSet.