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

+25 -1
View File
@@ -10,7 +10,7 @@ APIs SHOULD offer one canonical operation for a common need, small documentation
## 2. Bootstrap module registry
The built-in registry contains `std.bytes`, `std.encoding.base64`, `std.encoding.hex`, `std.fmt`, `std.hash`, `std.hashmap`, `std.map`, `std.math`, `std.option`, `std.result`, `std.utf8`, `std.vec` and `std.deque`. They are implemented in safe `.aji`; their stable source paths are `std/bytes.aji`, `std/encoding/base64.aji`, `std/encoding/hex.aji`, `std/fmt.aji`, `std/hash.aji`, `std/hashmap.aji`, `std/map.aji`, `std/math.aji`, `std/option.aji`, `std/result.aji`, `std/utf8.aji`, `std/vec.aji` and `std/deque.aji`, respectively. Spec 15 §13 defines imports; the reference API tables are in `reference/std.md`. All operations are deterministic in every profile and have no concurrency effects. Modules other than std.vec/std.deque/std.map/std.hashmap declare effects [] and allocate no heap memory. std.vec/std.deque/std.map/std.hashmap new/reserve declare mem.alloc and require explicit allocation authority (§2.10–§2.11, §2.13–§2.14); their other functions allocate no heap. Ordinary interpreter fuel limits apply.
The built-in registry contains `std.bytes`, `std.encoding.base64`, `std.encoding.hex`, `std.fmt`, `std.hash`, `std.hashmap`, `std.hashset`, `std.map`, `std.math`, `std.option`, `std.result`, `std.utf8`, `std.vec` and `std.deque`. They are implemented in safe `.aji`; their stable source paths are `std/bytes.aji`, `std/encoding/base64.aji`, `std/encoding/hex.aji`, `std/fmt.aji`, `std/hash.aji`, `std/hashmap.aji`, `std/hashset.aji`, `std/map.aji`, `std/math.aji`, `std/option.aji`, `std/result.aji`, `std/utf8.aji`, `std/vec.aji` and `std/deque.aji`, respectively. Spec 15 §13 defines imports; the reference API tables are in `reference/std.md`. All operations are deterministic in every profile and have no concurrency effects. Modules other than std.vec/std.deque/std.map/std.hashmap/std.hashset declare effects [] and allocate no heap memory. std.vec/std.deque/std.map/std.hashmap/std.hashset new/reserve declare mem.alloc and require explicit allocation authority (§2.10–§2.11, §2.13–§2.15); their other functions allocate no heap. Ordinary interpreter fuel limits apply.
### 2.1 `std.math`
@@ -261,3 +261,27 @@ Removal exchanges the last dense entry into the removed entry's position when ne
New checks 2N overflow, allocates the empty entry buffer, then the fully initialized bucket buffer. A returned second-allocation failure MUST free the temporary empty entry buffer and refund its recorded authority. Reserve≤N is an unchanged no-op with its declared mut/effect/capability requirements. Growth MUST allocate/initialize a new bucket buffer before calling ownership-preserving buffer.reserve on the dense entries. Any returned failure MUST free temporary metadata, preserve both original buffers, every key/value/hash/index/state and pop order, and refund all temporary charges. After entries reserve succeeds, only cached hashes are used to rebuild the new buckets; no further recoverable allocation or callback occurs. Swap in the new buckets and explicitly free the old Copy metadata buffer. Peak quota includes old/new entry buffers, old/new bucket buffers and separately charged user owners. All old storage refunds go to their recorded authorities, even when growth uses another capability. Arithmetic/allocation-size overflow returns alloc_error.budget_exceeded; no shrink occurs.
Each StoredEntry slot charges hash plus full K/V slot sizes, including uninitialized capacity; each Bucket charges hash, usize and state tag. Unused capacity reserves bytes but contains no live K/V. There is no tag/default key allocation and no implicit destruction. Existing initialized buffers, aggregate transfer/exchange, borrows/task loans, bounds, effects, Copy/Send/Share and quota verification suffice; no new AIR instruction/feature, source form, verifier/runtime state or diagnostic is added. Generic callback contracts are RFC 0029. Valid bookkeeping does not trap with a valid representation and successful callbacks. Existing API/AIR meanings remain unchanged, including std.map.U64Map. Deterministic/hosted behavior agrees. Probe operations are O(N) worst-case plus callback work, pop/removal repair O(N), reserve O(N²) worst-case collision work; buffer-prefix interpreter copying may add host cost. No adversarial collision or native throughput promise is made.
### 2.15 `std.hashset`: bounded owning general-key set
The registry MUST include std.hashset at std/hashset.aji, ordinary safe source. HashSet<K,H,E> binds the same exact pure ref-key hash/equality contracts and stable algebraic laws as std.hashmap (§2.14), with explicit type/application arguments `K,fn hash,fn equal`. It owns complete arbitrary K, including owning byte buffers, structs, resources and endpoints subject to existing member/Send/Share rules. Its representation is one field map: std.hashmap.HashMap<K,bool,H,E>, with every live value true; all underlying dense/probe invariants apply. No default key, implicit destruction or library access privilege is added.
Nine operations use the three ordered arguments above:
| Operation | Result / transfer |
|---|---|
| new(capacity: usize, heap: cap<mem.alloc>) | NewResult<K,H,E>: ok(HashSet<K,H,E>), err(alloc_error) |
| len(set: ref<HashSet<K,H,E>>) -> usize | live count |
| capacity(set: ref<HashSet<K,H,E>>) -> usize | maximum live keys |
| reserve(set: mut<HashSet<K,H,E>>, capacity: usize, heap: cap<mem.alloc>) | ReserveResult: ok, err(alloc_error) |
| insert(set: mut<HashSet<K,H,E>>, key: K) | InsertResult<K>: inserted, present(original_input_K), full(original_input_K) |
| contains(set: ref<HashSet<K,H,E>>, key: ref<K>) -> bool | exact membership |
| remove(set: mut<HashSet<K,H,E>>, key: ref<K>) -> std.option.Option<K> | moving extraction of the stored key |
| pop(set: mut<HashSet<K,H,E>>) -> std.option.Option<K> | moving extraction of last dense key |
| free(set: HashSet<K,H,E>) | FreeResult<K,H,E>: ok, not_empty(HashSet<K,H,E>) |
Only new/reserve declare mem.alloc and take explicit heap authority. Insert checks membership before transferring to the underlying map: duplicate insertion MUST preserve the existing stored key and every entry/bucket, returning the exact input key as present, even when full. New-key full also preserves the set and returns its exact input. This gives one canonical insert contract; replacement requires explicit remove/insert by the caller. No automatic growth occurs. Owning present/full/remove/pop/not_empty results MUST be consumed. contains returns no borrow. Missing remove and nonempty free preserve all state. Pop order is the underlying reverse current dense order; removal may move the last key to a hole, and reserve preserves the order.
New/reserve failure, peak quota/refund, zero capacity, probing, collision equality, callback/fuel traps and visible representation invariants are exactly §2.14's rules. Each capacity slot charges hash plus complete K plus bool, and two buckets: for K=u64, N charges 51N bytes; separately owned K allocations remain separately charged. insert MAY evaluate H/E for membership and again for bounded map insertion; these pure stable operations grant no authority and cannot be inferred away. Once membership says absent, map replacement is unreachable under the valid representation/hash/equality laws; invalid laws may trap under ordinary abnormal-path semantics.
Existing buffers/aggregates/borrows, static callable identity, effects, Copy/Send/Share and ownership verification suffice. No new syntax, diagnostic, feature/opcode, verifier/runtime state, implicit allocation/destructor or trusted boundary is added. Deterministic/hosted behavior agrees and existing APIs/AIR meanings remain unchanged. Worst-case insertion/membership/removal/pop is O(N) plus callback work; growth is O(N²) worst-case collision work with existing interpreter prefix-copy costs. No sorted output, adversarial collision bound or native throughput promise is added.