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 Map

PagedSparseMap<K, V, PAGE = 1024, D = Flat> is a sparse set of keys plus a dense value array kept parallel to the dense keys. The value at position i belongs to the key at position i. Keys and values can be iterated or borrowed as slices without touching the index.

See where sparse and direct maps store their values.

Python: SparseMap holds arbitrary objects. SparseMapF64, SparseMapF32, SparseMapI64, SparseMapI32, SparseMapU64, SparseMapU32 and SparseMapBool store values unboxed; sparse_map(dtype) returns an instance of the class for a NumPy dtype.

Example

from paged_collections import SparseMap, sparse_map

values = sparse_map("float64")
values[7] = 1.5
values[2] = 2.5
values[9] = 3.5
print("get 7 =", values.get(7))
print("removed =", values.pop(2))
print("keys =", list(values))
print("values =", values.values().tolist())
objects = SparseMap()
objects[7] = (4, 5)
print("object =", objects[7])
use paged_collections::sparse_map::PagedSparseMap;

fn main() {
    let mut values: PagedSparseMap<usize, f64> = PagedSparseMap::new();
    values.insert(7, 1.5);
    values.insert(2, 2.5);
    values.insert(9, 3.5);
    println!("get 7 = {}", values.get(7).unwrap());
    println!("removed = {}", values.remove(2).unwrap());
    println!("keys = {:?}", values.keys().collect::<Vec<_>>());
    println!("values = {:?}", values.values().collect::<Vec<_>>());
    let mut objects: PagedSparseMap<usize, (i32, i32)> = PagedSparseMap::new();
    objects.insert(7, (4, 5));
    println!("object = {:?}", objects.get(7).unwrap());
}

Python output:

get 7 = 1.5
removed = 2.5
keys = [7, 9]
values = [1.5, 3.5]
object = (4, 5)

The Rust example prints the same lines.

Complexity

OperationCost
get, removeO(1) with Flat (see directories)
insertas get, amortized over dense-array growth; the first key in a page allocates the page
clearO(1) for the index, plus dropping the values
iteration, key_slice, value_slicedense arrays only
sortO(n log n) comparisons, O(n) swaps, one index write per entry
binary_search_key, binary_search_by, partition_pointO(log n) on entries kept sorted by the caller

Removal swaps the last entry into the removed position, as in the set.

Generational Keys

With plain integer keys the map behaves like HashMap. With generational keys the methods differ when the slot is held by a different key:

MethodSlot held by another generation
insert, get_or_insert_withevicts it
entryreturns Vacant. Inserting through it evicts the other generation in place
replaceevicts it and returns the evicted entry
try_insertrefuses

insert evicts so that get(key) returns the new value afterwards, as with HashMap.

In Rust, entry(key) gives mutable access to an exact key or lets you insert it after a single slot lookup. For example, use *counts.entry(key).or_default() += 1 to update a counter.

Rust and Python

RustPython
insert(k, v)m[k] = v, set_many(keys, values), update(mapping)
get(k), get_mut(k)m[k], get(k, default), get_many(keys, default)
get_or_insert_withsetdefault(k, default)
entry(k)
remove(k)del m[k], pop(k[, default]), popitem(), discard_many(keys)
first_key_value(), last_key_value()first_item(), last_item()
pop_first(), pop_last()pop_first(), pop_last()
next_occupant, prev_occupantnext_occupant(index), prev_occupant(index)
range(a..b)items_range(a, b), keys_range(a, b), values_range(a, b)
contains_key(k)k in m, contains_many(keys)
key_slice()keys() (copy), keys_view() (zero-copy, read-only)
value_slice(), value_slice_mut()typed maps: values() (copy), values_view() (zero-copy; writeable except for SparseMapBool); SparseMap.values() returns a list
sort_keys, sort_by*sort_keys()
allocated_pages()allocated_pages

Python SparseMap, IdMap and their typed variants accept parallel keys and values, one mapping, or one iterable of (key, value) pairs. They validate all input before writing and keep the last value for duplicate keys.

Ordered queries follow the sparse index. first_item() and last_item() return the (key, value) pairs with the smallest and largest slot indices. pop_first() and pop_last() remove and return those pairs by swap removal. All four raise KeyError on an empty map, and ordered pops raise BufferError while a NumPy view is alive.

next_occupant(index) and prev_occupant(index) return the nearest pair at or after, or at or before, the given slot index. They return None when no entry matches. Both accept integers outside the slot domain. A negative index is before the first slot, and an index of 2**32 or higher is past the last slot. For IdMap and its typed variants, pass a slot index and receive a packed ID as the key in the returned pair.

items_range(start=None, stop=None), keys_range and values_range return copies in ascending slot order for the half-open interval [start, stop). None leaves a bound open and integer bounds are clamped to the slot domain. For IdMap, the bounds are slot indices and the returned keys are packed IDs.

Batch methods are described in NumPy Interop.

API: PagedSparseMap.