cli: add safe owning general-key hash sets

Add std.hashset with explicit pure hash/equality policies and complete
owning keys. Duplicate/full insert returns the original input, while
remove/pop and nonempty free preserve explicit resource ownership.

Reuse safe HashMap storage and failure/peak/refund accounting. Integrate
normative contracts, RFC, references, complexity measurements, CLI/profile
checks and independent Copy/owner set traces.

Validation: 973 conformance cases; runtime set GC and schedule stress;
unit/Quick Reference, formatting, hosted smoke, 53 CLI checks, 106 source
coverage constructs and bundle generation.
This commit is contained in:
dev committed 2026-10-07 11:14:53 +03:00
1 parent 7743f515c5
commit 4716cda0f1
43 files changed
+2696 -7

No files matched your search

+24 -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 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 new/reserve.
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.
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.
| Module | Function signature | Result/failure |
|---|---|---|
@@ -326,3 +326,25 @@ Here HashMap abbreviates the fully applied type, and Entry<K,V> has key: K/value
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.
## Owning general-key sets
Import `std.hashset` and `std.option` for extraction results. `HashSet<K,fn hash,fn equal>` uses the map's exact pure `fn(ref<K>) -> u64 effects []` / `fn(ref<K>,ref<K>) -> bool effects []` contracts and stable equality/hash laws. Complete owning keys, including byte buffers and resource-bearing structs, need no Copy constraint. Every operation supplies `::<K,fn hash,fn equal>`:
| Function arguments | Result |
|---|---|
| new(capacity: usize, heap: cap<mem.alloc>) | NewResult<K,fn hash,fn equal>: ok(HashSet), err(alloc_error) |
| len(set: ref<HashSet>) | usize live count |
| capacity(set: ref<HashSet>) | usize maximum live keys |
| reserve(set: mut<HashSet>, capacity: usize, heap: cap<mem.alloc>) | ReserveResult: ok, err(alloc_error) |
| insert(set: mut<HashSet>, key: K) | InsertResult<K>: inserted, present(K), full(K) |
| contains(set: ref<HashSet>, key: ref<K>) | bool |
| remove(set: mut<HashSet>, key: ref<K>) | std.option.Option<K> moving extraction |
| pop(set: mut<HashSet>) | std.option.Option<K> moving extraction |
| free(set: HashSet) | FreeResult<K,fn hash,fn equal>: ok, not_empty(HashSet) |
Here HashSet abbreviates the applied type. Duplicate insertion preserves the stored representative and every entry/bucket, returning the original incoming K as present, even at capacity. Full returns the original new key. Remove/pop return stored keys; free returns a nonempty set. Explicitly consume all owning results. Use `move` for named linear inputs and `ref q` for queries through mut q; contains retains no borrow. Replacement requires explicit remove followed by insert.
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.