paged-collections 0.2.0

Paged sparse sets, maps and stable-address containers
Documentation

paged-collections

CI crates.io crates.io downloads PyPI PyPI downloads Documentation License

Paged sparse sets, maps, a priority queue and stable-address containers for Rust (no_std + alloc), with Python bindings built on PyO3 and NumPy.

The keyed collections use a paged index. The first write to a page allocates it. The default Flat directory stores a pointer slot for every page number up to the highest one used. Radix and Tree can skip gaps between page numbers, which saves directory space when keys are far apart.

With paged storage, existing elements keep their addresses as the container grows.

Install

cargo add paged-collections             # features: std (default), serde, rayon, concurrent
pip install paged-collections           # Python 3.10+, NumPy 1.24+

Wheels cover CPython 3.10+ (abi3) and free-threaded 3.14t and 3.15t+. See Installation.

Example

Rust:

use paged_collections::direct_map::PagedDirectMap;
use paged_collections::hive::Hive;
use paged_collections::id_alloc::Id;

let mut hive: Hive<&str> = Hive::new();
let a = hive.insert("a");
let b = hive.insert("b");

let mut score: PagedDirectMap<Id, u32> = PagedDirectMap::new();
score.insert(a, 10);
score.insert(b, 20);

// Removing frees the slot; its next occupant gets a new generation, so the
// stale id misses in the hive, and the new id misses the side-table entry
// left by the old one. Side tables are not cleaned up automatically.
hive.remove(a);
let c = hive.insert("c");
assert_eq!(c.index(), a.index());
assert_eq!(hive.get(a), None);
assert_eq!(score.get(c), None);
assert_eq!(score.remove(a), Some(10));

// A direct map iterates in slot order and answers range queries.
let in_range: Vec<u32> = score.range(a..=b).map(|(_, &s)| s).collect();
assert_eq!(in_range, [20]);

Python:

import numpy as np
from paged_collections import sparse_map

m = sparse_map("float64")
m.set_many(np.arange(0, 3_000_000, 1000), 1.0)  # batch insert
m[7] = 2.5
m.get_many([7, 1000])                            # array([2.5, 1. ])

values = m.values_view()   # zero-copy, writeable
values *= 2                # updates the map in place
del values                 # resizing raises BufferError while a view is alive

Containers

Rust Python Paging Purpose
PagedBitSet<K> BitSet index One bit per slot in each allocated page, with key order and range operations
PagedSparseSet<K> SparseSet index Integer set: O(1) insert, remove, contains, clear; dense iteration
PagedSparseMap<K, V> SparseMap, SparseMapF64, ... index Sparse set with a dense value array
PagedDirectMap<K, V> DirectMap, DirectMapF64, ... index Values in place; key order and range queries
PagedPriorityQueue<K, P> PriorityQueue, PriorityQueueF64 index Indexed heap: priority changes and removal by key
PagedVec<T> PagedList, PagedListF64, ... storage Growable vector with stable addresses
PagedDeque<T> PagedDeque storage Double-ended queue with stable addresses
Hive<T> Hive storage Unordered slots with generational ids
IdAllocator IdAllocator, IdMap none Generational ids for use as keys

The rayon feature enables parallel iteration through rayon::prelude.

Concurrent containers

The concurrent feature (no_std compatible) adds thread-safe containers. They grow through shared references (&self), and pages stay at fixed addresses until exclusive access (&mut self), so lookups need no epochs or hazard pointers. The Python classes can be shared between threads, including on free-threaded builds; their batch methods (*_many) release the GIL.

Rust Python Shared operations (&self) Lookups
ConcurrentSparseSet<K> ConcurrentSparseSet insert &K
ConcurrentSparseMap<K, V> ConcurrentSparseMapF64, ... insert &V
ConcurrentDirectMap<K, V> ConcurrentDirectMapF64, ... insert, replace, remove, entry (per-page RwLock) closure, clone or entry guard
AtomicDirectMap (usize -> u64), AtomicIdMap (Id -> u32) — lock-free insert, update, remove copied value
ConcurrentQueue<T> ConcurrentQueue push, pop (FIFO) owned value from pop
ConcurrentPriorityQueue<K, P> ConcurrentPriorityQueue, ConcurrentPriorityQueueF64 push, change priority, remove, relaxed pop, exact pop_exact cloned priority
ConcurrentPagedVec<T> — push &T
ConcurrentHive<T> — insert, remove, take Ref guard
ConcurrentAppendHive<T> — insert &T
ConcurrentIdAllocator — alloc, free

Removal from the sparse set, sparse map and append-only containers, as well as clear, shrink_to_fit and conversion to the sequential types, needs &mut self; in Python, such a call raises RuntimeError if it conflicts with another thread. See Concurrent containers.

Documentation

Layout

paged-collections/
├── crates/
│   ├── paged-collections/         # Rust library (no_std + alloc)
│   ├── paged-collections-python/  # PyO3 bindings for the _core extension
│   └── bench/                     # Rust benchmarks outside the default build
├── python/
│   └── paged_collections/         # Python sources and type stubs
├── tests/                         # Python tests
├── benchmarks/
│   └── python/                    # Python benchmarks
└── docs/
    └── site/                      # mdBook documentation

Development commands are in CONTRIBUTING.md.

License

Licensed under either of

at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.