cli: add safe owning general-key hash maps

Use explicit static hash/equality functions, dense complete key/value entries
and Copy probe indices. Preserve returned ownership and allocation failure
state without unsafe code or new runtime instructions.

Add normative contracts, API/reference budgets, 33 conformance cases, independent
Copy/owner dict traces and 55 CLI/package/profile checks. Full corpus: 947 passing.
Collection-every-step scheduler stress remains in progress, recorded in RFC/plan.
This commit is contained in:
dev committed 2026-10-07 10:55:09 +03:00
1 parent f02f4bd55b
commit 7bb5bd7931
52 files changed
+7736 -11

No files matched your search

+28 -3
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 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 new/reserve.
Except std.vec/std.deque/std.map 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 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 |
|---|---|---|
@@ -281,7 +281,7 @@ End operations and indexing use constant slot work; construction/growth/free use
## Owning numeric-key maps
Import `std.map`; each generic operation supplies `::<V>`. `U64Map<V>` is always linear, uses exact u64 key equality and supports Copy values, owners, files and sender endpoints. Hashes select probe positions; they never establish key equality. General `HashMap<K,V>` remains Q-037.
Import `std.map`; each generic operation supplies `::<V>`. `U64Map<V>` is always linear, uses exact u64 key equality and supports Copy values, owners, files and sender endpoints. Hashes select probe positions; they never establish key equality. General keys use `std.hashmap` with explicit hash/equality (§2.14).
| Operation | Return / ownership |
|---|---|
@@ -301,3 +301,28 @@ Only new/reserve declare `effects [mem.alloc]` and take allocation authority. No
Representation: `U64Map<V> { storage: buffer<Slot<V>>, length: usize }`, `Slot<V> { key: u64, state: SlotState, value: std.option.Option<V> }`, `SlotState { empty, deleted, occupied }`. Occupied implies some(V), vacancies imply none, keys are unique and reachable, and length equals occupied count. Direct representation writes must preserve spec 16 §2.13's invariant. Capacity N allocates and initializes 2N physical slots, with both enum tags charged. N=0 allocates no bytes. Both whole tables and user allocations count toward growth's peak quota; old storage is refunded to its original authority. Overflowing size returns budget_exceeded.
Lookup traverses deleted slots, compares original keys and terminates after bounded probes, even with no empty slots. Pop drains physical-index order; successful rehash may change this deterministic order. Worst-case probe work is O(N); collision-heavy growth is O(N²). Existing borrow, task-loan, Copy/Send/Share and resource diagnostics apply. The [owning example](../../examples/std/map.aji) returns 42 with heap=mem.alloc:256.
## Owning general-key maps
Import `std.hashmap` and `std.option` for lookup/extraction results. `HashMap<K,V,fn hash,fn equal>` stores complete keys and values, including owning byte buffers, structs, owners, files and channel endpoints. The two declared functions have exactly `fn(ref<K>) -> u64 effects []` / `fn(ref<K>,ref<K>) -> bool effects []`. Equality is stable equivalence and equal keys must hash equally; hash equality alone never identifies a key. Do not change a live key's hash/equality. Different bound functions produce distinct nominal map types. Operations never capture state or gain authority from selection.
Every call supplies `::<K,V,fn hash,fn equal>` (get additionally requires V: Copy, without requiring K: Copy):
| Function arguments | Result |
|---|---|
| new(capacity: usize, heap: cap<mem.alloc>) | NewResult<K,V,fn hash,fn equal>: ok(HashMap), err(alloc_error) |
| len(map: ref<HashMap>) | usize live count |
| capacity(map: ref<HashMap>) | usize maximum live entries |
| reserve(map: mut<HashMap>, capacity: usize, heap: cap<mem.alloc>) | ReserveResult: ok, err(alloc_error) |
| insert(map: mut<HashMap>, key: K, value: V) | InsertResult<K,V>: inserted, replaced(Entry<K,V>), full(Entry<K,V>) |
| contains(map: ref<HashMap>, key: ref<K>) | bool |
| get(map: ref<HashMap>, key: ref<K>) | std.option.Option<V> copied value |
| remove(map: mut<HashMap>, key: ref<K>) | std.option.Option<Entry<K,V>> moving extraction |
| pop(map: mut<HashMap>) | std.option.Option<Entry<K,V>> moving extraction |
| free(map: HashMap) | FreeResult<K,V,fn hash,fn equal>: ok, not_empty(HashMap) |
Here HashMap abbreviates the fully applied type, and Entry<K,V> has key: K/value: V. Pass `ref q` when querying through mut q; ownership transfer of named linear K/V requires move. Replacement returns both old resources and retains the new equivalent key. Full insert returns both original inputs. Explicitly consume every owning result; free returns the nonempty map. get cannot copy an owned V, while contains/remove work without Copy requirements. No borrowed value/cursor escapes.
Only new/reserve allocate and declare mem.alloc with explicit authority. Growth has no implicit trigger and returned failure preserves every old entry/bucket and pop order. A capacity N reserves N StoredEntry slots (hash + full K/V sizes) and 2N Copy Bucket slots (hash + usize + state tag). Live entries form an initialized dense prefix, with a separate probe-index table and cached hashes. Removing a middle entry exchanges the last into its position, repairing the index. Pop drains reverse current dense order; reserve preserves it and clears tombstones. Zero capacity is valid. Peak growth includes both entry buffers, both index buffers and external user owners; refunds follow the recorded allocation capabilities. Capacity-one Key{id:u64}/own<u64> reserves 58 bytes, plus 8 for the owned payload in the [checked example](../../examples/std/hashmap.aji), which runs with heap=mem.alloc:66.
Representation fields are ordinary visible source; manual construction/mutation must preserve the full invariant in spec 16 §2.14. Hash/equality traps and fuel exhaustion are abnormal outcomes. Existing moves, alias/task-loan and Copy/Send/Share rules apply without unsafe or map-specific runtime semantics. Worst-case lookup/insert/remove/pop is O(N) plus callback work, rehash O(N²); no adversarial collision/native-throughput guarantee is made. `std.map.U64Map<V>` retains its existing API for numeric keys.