Files
aji/tests/conformance/source/hashset/duplicate_state.aji
T
dev 4716cda0f1 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.
2026-10-07 11:14:53 +03:00

45 lines
3.0 KiB
Rust

// expect: run
// result: 42
// phase: 6
// spec: docs/spec/16-standard-library.md §2.15
// grant: heap=mem.alloc:59
module test.hashset
import std.hashset;
import std.option;
import std.hashmap;
fn opened<K, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>,ref<K>) -> bool effects []>(capacity: usize,heap: cap<mem.alloc>) -> std.hashset.HashSet<K,fn H,fn E> effects [mem.alloc] {
match std.hashset.new::<K,fn H,fn E>(capacity,heap) { std.hashset.NewResult::<K,fn H,fn E>::ok(set) => { return move set; }, std.hashset.NewResult::<K,fn H,fn E>::err(error) => { trap; } }
}
fn inserted<K>(result: std.hashset.InsertResult<K>) -> void effects [] {
match move result { std.hashset.InsertResult::<K>::inserted => {}, std.hashset.InsertResult::<K>::present(key) => { trap; }, std.hashset.InsertResult::<K>::full(key) => { trap; } }
}
fn extracted<K>(result: std.option.Option<K>) -> K effects [] { match move result { std.option.Option::<K>::some(key) => { return move key; }, std.option.Option::<K>::none => { trap; } } }
fn released<K, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>,ref<K>) -> bool effects []>(set: std.hashset.HashSet<K,fn H,fn E>) -> void effects [] {
match std.hashset.free::<K,fn H,fn E>(move set) { std.hashset.FreeResult::<K,fn H,fn E>::ok => {}, std.hashset.FreeResult::<K,fn H,fn E>::not_empty(set) => { trap; } }
}
struct Key { id: u64, marker: u64 }
fn hash(key: ref<Key>) -> u64 effects [] { return 3; }
fn equal(a: ref<Key>,b: ref<Key>) -> bool effects [] { return a.id == b.id; }
fn bucket(q: ref<std.hashset.HashSet<Key,fn hash,fn equal>>,index: usize) -> std.hashmap.Bucket effects [] {
ref storage = q.map.buckets; ref buckets = buffer.view(storage); return buckets[index];
}
fn state(value: std.hashmap.BucketState) -> u64 effects [] {
match value { std.hashmap.BucketState::empty => { return 0; }, std.hashmap.BucketState::deleted => { return 1; }, std.hashmap.BucketState::occupied => { return 2; } }
}
fn main(heap: cap<mem.alloc>) -> u64 effects [mem.alloc] {
let set = opened::<Key,fn hash,fn equal>(1,heap);
{ mut q = set; inserted::<Key>(std.hashset.insert::<Key,fn hash,fn equal>(q,Key { id: 1,marker: 42 })); }
let first = bucket(ref set,0usize);
let second = bucket(ref set,1usize);
{ mut q = set;
match std.hashset.insert::<Key,fn hash,fn equal>(q,Key { id: 1,marker: 99 }) {
std.hashset.InsertResult::<Key>::present(key) => { assert(key.marker == 99u64); },
std.hashset.InsertResult::<Key>::inserted => { trap; }, std.hashset.InsertResult::<Key>::full(key) => { trap; }
} }
{ ref q = set; ref storage = q.map.buckets; ref buckets = buffer.view(storage);
assert(buckets[0].hash == first.hash); assert(buckets[0].index == first.index); assert(state(buckets[0].state) == state(first.state));
assert(buckets[1].hash == second.hash); assert(buckets[1].index == second.index); assert(state(buckets[1].state) == state(second.state)); }
{ mut q = set; let key = extracted::<Key>(std.hashset.pop::<Key,fn hash,fn equal>(q)); assert(key.marker == 42u64); }
released::<Key,fn hash,fn equal>(move set); return 42;
}