Skip to content

Replace Vec-backed BTreeMap/BTreeSet with O(log n) tree storage #591

Closed

Description

@ehartford

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 sorted Vec[(K, V)]
  • BTreeSet[T] as sorted Vec[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

  1. Replace the internal fields with private tree node storage while keeping the public BTreeMap/BTreeSet API stable.
  2. 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.
  3. Preserve deterministic in-order traversal for items, keys, values, and IntoIter.
  4. Keep literals, comprehensions, and collect[BTree*] routed through normal insert semantics or an equivalent builder that preserves invariants.
  5. 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, and with build :test-green pass.

Traceability

Split from #414. Requirement IDs: 11.7.1.46, 11.7.1.47.

Activity

  1. ehartford commented on Oct 25, 2026

    @ehartford
    ContributorAuthor

    Triage 2026-10-25, with v0.15.3.0 (main a261b65): done by D151 ("BTreeMap and BTreeSet are 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 rows btree-map-*/btree-set-* passing, behav_btree_engine.w and behav_btree_word_keys.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions