Ordered key-value container implemented as an AVL tree.
Known issue: constructing an AVLMap fails compilation with «copy», Name was not found. The module also uses unavailable names isMoved and move in insert, recursive in clear, ASSIGN, debugPrint, and each, times in debugPrint, and print and printList in debugPrint without importing them; the API remains unavailable until the module is repaired. Its clear implementation frees node storage without destroying stored key and value objects (AvlMap.mpl:331-346); repair that cleanup before using managed keys or values.
AVLMap
Construct with key and value schemas in front of AVLMap: Int32 Int32 AVLMap.
Fields
AVL_MAP:virtualmap marker field.keyType:virtualRefto the key schema.valueType:virtualRefto the value schema.nodeType:virtualRefto the nodeDictwith fieldsvalue,key,balance,left, andright;asNodeinterprets a node address as aRefto this node.root: root-node address stored asNatx; this is the only field with run-time storage.
Public methods
INIT (--): initializes an empty map.find (key -- result): returns the lookup result for one key.insert (key value --): inserts one new key-value pair.erase (key --): removes the pair at one key.clear (--): removes all pairs and resets the root.ASSIGN (other --): replaces the map contents with a cloned copy ofother.DIE (--): callsclear; it frees node storage without destroying stored key and value objects.debugPrint (--): prints one debug view of the tree.
Ordering and key semantics
- Keys are compared through
=and<. insertrequires an absent key;eraserequires a present key. The source checks these preconditions withassert; run-time checks are disabled by-ndebug, so callers must not rely on rejection in that mode.debugPrintvisits the stored pairs in ascending key order.- The module has no size field or method.
- A newly initialized map stores
0nxinrootand therefore has no nodes.
Storage and lifetime
- Each valid
insertallocates one new node;insertis unavailable in the current module, so see the known issue for its unresolved names. erasedestroys and frees exactly one stored node.clearfrees every stored node's storage without destroying the stored key and value objects (theirDIEis not run) and resetsrootto the empty state;DIEandASSIGNuseclear.ASSIGNdeep-clones the whole tree. The new map does not share node storage with the source map.
find result and returned refs
success: reports whether the key was found.value:Refto the stored value whensuccessisTRUE; the Ref is mutable only through a mutable map path such as@m(throughmit is immutable), and when mutable, writing through it replaces the stored value.- When
successisFALSE,valueis a nil ref ofvalueType. - Value refs remain valid across
insertanderaseof another key. Erasing the referenced pair invalidates its ref;clear,ASSIGN, andDIEinvalidate all value refs.
each (source body --)
Pops a map (or a view of it) and a body and calls the body once per stored pair in ascending key order; the tree structure is unchanged, but the body may replace stored values only through a mutable source map.
sourcemust provide theAVL_MAPmarker field.- The body receives one record with fields
keyandvalue. keyis an immutable borrowedRefto the stored key, not a copy.valueis aRefto the stored value; the Ref is mutable only through a mutable map path such as@m(throughmit is immutable), and when mutable, writing through it replaces the stored value.- Value refs remain valid across
insertanderaseof another key. Erasing the referenced pair invalidates its ref;clear,ASSIGN, andDIEinvalidate all value refs.
See also
- HashTable: Hash-based map and set module.
- algorithm: Collection interfaces, comparison helpers, iteration adapters, and view slicing utilities.
- control: Foundational schema aliases, low-level runtime bindings, predicates, stack helpers, control combinators, and numeric utilities.
- Array: Growable array.
- Pool: Pooled storage with reusable numeric keys.