cli: add safe owning numeric-key maps
Register std.map with bounded U64Map<V>, exact key equality and explicit allocation/growth. Preserve returned ownership, old slots on reserve failure and original-authority refunds; support Copy values, owners and resources. Cover collision/tombstone/wrap behavior, independent dictionary traces, quota/cleanup/borrow/task rules, source/AIR parity and hosted/package CLI. Publish the API, roadmap and measured documentation cost. General key hashing/equality remains Q-037; numeric maps do not satisfy that gate.
This commit is contained in:
1 parent
fc23f1b23a
commit
57603860df
46 files changed
+2430
-15
No files matched your search
+25
-2
@@ -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 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 new/reserve.
|
||||
|
||||
Except std.vec/std.deque 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 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 |
|
||||
|---|---|---|
|
||||
@@ -278,3 +278,26 @@ Import std.deque; every operation supplies `::<T>`. Deque<T> is always linear an
|
||||
Indices count from the front. Full pushes, out-of-bounds replaces and nonempty free preserve the queue and return ownership. Allocation failure also preserves physical slots/head/length. Successful growth preserves logical order, resets head and charges the old+new capacities at the peak; no shrink occurs. Every slot is an Option<T> (tag plus payload), including free slots: capacity 3 of own<u64> charges 27 bytes. Empty free explicitly drains internal none slots; it never destroys a user's T. Callers pop and consume resources before freeing; match move on linear result values and import std.option to name pop/get variants.
|
||||
|
||||
End operations and indexing use constant slot work; construction/growth/free use O(capacity) work. Interpreter representation costs remain those of buffer primitives. Deque<T> fields are ordinarily visible; direct representation writes/construction must preserve spec 16 §2.11's ring invariant. Library calls maintain it. [Executable owning deque](../../examples/std/deque.aji) returns 42 with heap=mem.alloc:1024. There is no iterator, returned borrow, trait hierarchy or implicit allocation/cleanup. Existing alias, task-loan and Copy/Send/Share checks apply.
|
||||
|
||||
## 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.
|
||||
|
||||
| Operation | Return / ownership |
|
||||
|---|---|
|
||||
| `new<V>(capacity: usize, heap: cap<mem.alloc>)` | `NewResult<V>::ok(U64Map<V>)` / `err(alloc_error)` |
|
||||
| `len<V>(map: ref<U64Map<V>>) -> usize` | entry count |
|
||||
| `capacity<V>(map: ref<U64Map<V>>) -> usize` | maximum live entries before a new key returns full |
|
||||
| `reserve<V>(map: mut<U64Map<V>>, capacity: usize, heap: cap<mem.alloc>)` | `ReserveResult::ok` / `err(alloc_error)`; grow explicitly, preserve every slot on returned failure |
|
||||
| `insert<V>(map: mut<U64Map<V>>, key: u64, value: V)` | `InsertResult<V>::inserted` / `replaced(V)` / `full(V)` |
|
||||
| `contains<V>(map: ref<U64Map<V>>, key: u64) -> bool` | presence without copying V |
|
||||
| `get<V: Copy>(map: ref<U64Map<V>>, key: u64)` | `std.option.Option<V>` copied lookup |
|
||||
| `remove<V>(map: mut<U64Map<V>>, key: u64)` | `std.option.Option<V>` moved removal |
|
||||
| `pop<V>(map: mut<U64Map<V>>)` | `std.option.Option<Entry<V>>`; Entry has key: u64, value: V |
|
||||
| `free<V>(map: U64Map<V>)` | `FreeResult<V>::ok` / `not_empty(U64Map<V>)` |
|
||||
|
||||
Only new/reserve declare `effects [mem.alloc]` and take allocation authority. No operation implicitly grows. Full insertion returns the exact input; replacement works even when full and returns the old value. Callers must explicitly consume owning results and drain before free. Use `ref map` for reads and `mut map` for mutation, including explicit shared reborrows from mut parameters.
|
||||
|
||||
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.
|
||||
Reference in new issue
Block a user