Repository navigation
Description
Summary
#414 lands real BTreeMap[K, V] and BTreeSet[T] stdlib types with deterministic ordered semantics, Ord enforcement, insert/replace/remove APIs, ordered iteration helpers, and integration with collect, collection literals, and comprehensions. The bootstrap-compatible implementation is intentionally sorted-Vec backed, so lookup and insertion behavior is correct and deterministic but not the O(log n) representation promised by §11.7.
This issue tracks replacing that storage with an actual tree representation without changing the public API.
Spec
§11.7 requires BTreeSet[T] for T: Ord and BTreeMap[K, V] for K: Ord, with O(log n) membership/key lookup. §18.6 places these collections in std.collections.
Current implementation
lib/std/collections.w stores:
BTreeMap[K, V]as sortedVec[(K, V)]BTreeSet[T]as sortedVec[T]
The implementation preserves the observable collection contract:
- keys/elements require
Ord - duplicates replace the existing value/element
- iteration is deterministic and ordered
- literals, comprehensions, and
collect[BTree*]preserve sorted unique storage
The remaining gap is complexity: scans are linear and insertion shifts elements.
Implementation plan
- Replace the internal fields with private tree node storage while keeping the public
BTreeMap/BTreeSetAPI stable. - Implement balanced-tree insertion, lookup, replacement, and removal. Pick one representation and document it in the code before implementing: B-tree node arrays are preferred over binary tree nodes because they match the type name and cache behavior.
- Preserve deterministic in-order traversal for
items,keys,values, andIntoIter. - Keep literals, comprehensions, and
collect[BTree*]routed through normalinsertsemantics or an equivalent builder that preserves invariants. - Avoid exposing internal node types unless a future public cursor/range API requires it.
Test plan
Keep the #414 tests and add coverage that would catch tree-specific edge cases:
- insertion across multiple node splits
- replacement of existing keys after splits
- removal from leaf and internal positions
- ordered iteration after mixed insert/remove operations
- empty, singleton, duplicate, and multi-level cases
If practical, add a non-brittle instrumentation or stress test that exercises many keys in reverse and shuffled orders without depending on wall-clock timing.
Acceptance criteria
- Public API remains source-compatible with Implement BTreeMap and BTreeSet (§11.7, §18.6) #414.
- Lookup and membership are O(log n) by representation, not just by claim.
- Ordered traversal remains deterministic.
- Existing BTree literal/comprehension/collect behavior still passes.
with build,with build :fixpoint,with build :test, andwith build :test-greenpass.
Traceability
Split from #414. Requirement IDs: 11.7.1.46, 11.7.1.47.
Activity
ehartford commentedon Oct 25, 2026
Triage 2026-10-25,
with v0.15.3.0(main a261b65): done by D151 ("BTreeMapandBTreeSetare facades over the M*LIB B+tree slot shape; the compiler no longer knows their layout"), merged 2026-10-22/23 with #937 closed: O(log n) storage, the word shape for integer keys, complexity rowsbtree-map-*/btree-set-*passing,behav_btree_engine.wandbehav_btree_word_keys.