std1.0.0-beta-7 SafeC docs

Module collections::btree

#include <std/collections/btree.h> source

BTree<V>: B-tree ordered map, unsigned long keys

?&BTree<int> tq = std::alloc(sizeof(struct BTree<int>));  // ~pool-sized; keep it off the stack
&BTree<int> t = std::expect_ref(tq, "oom");
t.init();
t.insert(42UL, 7);
int v = t.get(42UL).default(0);

Deterministic O(log n) lookups with no heap use after construction: nodes come from a fixed in-struct pool of BTREE_POOL_SIZE nodes (minimum degree BTREE_ORDER, so up to BTREE_MAX_KEYS keys per node), and nodes freed by a merge go on a free list for reuse. Values are stored by value; get() hands out a '?&V' into the pool, valid until the next insert()/remove().

The tree owns its values, which may own something themselves (String, Vec, ...): insert() takes the value by value (overwriting a key drops the old value; one it can't store is dropped), remove() moves it out, and clear() drops every value still in the tree. The tree lives in place (usually behind a reference), so it has no drop() of its own: clear() a tree whose values own resources before releasing its memory.

Structs

BTreeNode
BTree

Macros

BTREE_ORDERmin keys per non-root node
BTREE_MAX_KEYS2*ORDER - 1
BTREE_MAX_CHILD2*ORDER
BTREE_POOL_SIZEmax nodes in the pool