cli: add safe owning binary heaps

Add std.heap with an exact explicit pure ordering operation, bounded
push, Copy-only peek, moving pop and empty-only free. Sift Copy indices
while complete owners remain in dense initialized storage; repair holes
through complete exchange. Reserve preserves layout on returned failure
and accounts for both old/new buffers and separately owned payloads.

Integrate normative contracts, RFC, references, bundle measurements,
independent heapq/permutation oracles and CLI/package/profile checks.

Validation: 1007 conformance cases; 34 heap GC fixtures and scheduled
stress; unit/Quick Reference, formatting, hosted smoke, 70 CLI checks,
109 source coverage constructs and unchanged AIR reference bundle.
This commit is contained in:
dev committed 2026-10-07 11:41:35 +03:00
1 parent 4114a39846
commit cfece6080c
51 files changed
+2192 -12

No files matched your search

+27 -2
View File
@@ -1,8 +1,8 @@
# Standard library API
Normative contracts: spec 16. Import rules: spec 15 §13. These modules are bundled with the compiler, with no external source dependencies. Allocation authority is explicit for std.vec/std.deque/std.map/std.hashmap/std.hashset new/reserve.
Normative contracts: spec 16. Import rules: spec 15 §13. These modules are bundled with the compiler, with no external source dependencies. Allocation authority is explicit for std.vec/std.deque/std.map/std.hashmap/std.hashset/std.heap new/reserve.
Except std.vec/std.deque/std.map/std.hashmap/std.hashset new/reserve, functions below declare `effects []`, allocate no heap memory and retain no borrows. Pass arrays/slices using explicit `ref` or `mut`. Names are fully qualified; no alias or short-name import exists.
Except std.vec/std.deque/std.map/std.hashmap/std.hashset/std.heap new/reserve, functions below declare `effects []`, allocate no heap memory and retain no borrows. Pass arrays/slices using explicit `ref` or `mut`. Names are fully qualified; no alias or short-name import exists.
| Module | Function signature | Result/failure |
|---|---|---|
@@ -348,3 +348,28 @@ Here HashSet abbreviates the applied type. Duplicate insertion preserves the sto
Only new/reserve allocate with mem.alloc and explicit authority. There is no automatic growth or cleanup. The visible representation is `map: std.hashmap.HashMap<K,bool,fn hash,fn equal>` with every live value true; callers must preserve spec 16 §2.15's complete invariant. Dense extraction/pop order, failure preservation, peak accounting and refunds follow the map. Each capacity slot charges hash + K + bool and two buckets: 51 bytes for u64, or 59 for Key{id:u64,owner:own<u64>}, plus the separately allocated payload. The [owning example](../../examples/std/hashset.aji) returns 42 with heap=mem.alloc:67.
Policies may run for membership and again for insertion, with no runtime dispatch or gained authority. Existing moves/aliases/task loans and Copy/Send/Share checks apply. Callback traps/fuel exhaustion are abnormal outcomes. Worst-case membership/insert/remove/pop is O(N) plus callbacks; growth is O(N²) under collisions. No sorted iteration or native-throughput guarantee is made.
## Owning binary heaps
Import `std.heap` and `std.option`. `BinaryHeap<T,fn before>` stores complete T with an explicit exact `fn(ref<T>,ref<T>) -> bool effects []` operation. before(a,b) means a has higher extraction priority. It must remain a stable strict weak order: irreflexive/transitive, with transitive incomparability. Equivalent items have deterministic extraction for a fixed operation sequence, without a stable tie-order promise. Use `<` for numeric minimum first or `>` for maximum first. Distinct bound operations give distinct nominal types; selection grants no authority.
Every call supplies `::<T,fn before>`; peek additionally requires T: Copy:
| Function arguments | Result |
|---|---|
| new(capacity: usize, heap: cap<mem.alloc>) | NewResult<T,fn before>: ok(BinaryHeap), err(alloc_error) |
| len(value: ref<BinaryHeap>) | usize live count |
| capacity(value: ref<BinaryHeap>) | usize maximum live count |
| reserve(value: mut<BinaryHeap>, capacity: usize, heap: cap<mem.alloc>) | ReserveResult: ok, err(alloc_error) |
| push(value: mut<BinaryHeap>, item: T) | PushResult<T>: ok, full(T) |
| peek(value: ref<BinaryHeap>) | std.option.Option<T> copied root |
| pop(value: mut<BinaryHeap>) | std.option.Option<T> moving root extraction |
| free(value: BinaryHeap) | FreeResult<T,fn before>: ok, not_empty(BinaryHeap) |
Here BinaryHeap abbreviates the applied type. Use move for named linear inputs, mut for push/pop/reserve and ref for queries (ref q through a mut parameter). Full push preserves all state, returns the original input and calls no comparator. Empty pop/peek returns none; nonempty free returns the whole heap. Explicitly consume owning full/pop/not_empty results. Peek retains no borrow and cannot copy an owner; complete owning records, files and sender endpoints use pop. Only new/reserve allocate with mem.alloc and explicit authority; growth is never automatic.
The visible representation is entries: buffer<Entry<T>> / indices: buffer<usize>, Entry<T>{position:usize,value:T}. Both initialized lengths/capacities agree. Indices are a permutation of dense positions; each entry points back to its index position, and no child precedes its parent. Callers must preserve spec 16 §2.16's complete invariant. Sifting swaps Copy indices and inverse positions; pop repairs the dense hole through complete exchange. No default T or implicit destruction is needed.
New allocates empty entries then indices, refunding entries on second-allocation failure. Reserve allocates/copies new indices before growing entries; returned failure preserves both old buffers, values/positions/indices and extraction order, and refunds temporaries. Success preserves the layout and frees old indices explicitly. Peak quota includes both entry/index buffers and all external user allocations; refunds go to recorded authorities. Zero capacity is valid; size overflow returns budget_exceeded. On air64 each capacity slot costs sizeof(T)+16 bytes. The [owning example](../../examples/std/heap.aji) needs 32 bytes for one Item slot plus 8 for its payload: heap=mem.alloc:40.
Existing ownership/alias/task-loan/Send/Share rules apply with no unsafe boundary or new runtime semantics. Callback/fuel traps are abnormal. Abstract push/pop need O(log L) comparisons/swaps, peek O(1), reserve O(L) copies; interpreter prefix-copy/GC costs may add host work. There is no native-throughput guarantee.