Files
aji/examples/std/hashmap.aji
dev 7bb5bd7931 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.
2026-10-07 10:55:09 +03:00

36 lines
1.6 KiB
Rust

module demo.hashmap
import std.hashmap;
import std.option;
struct Key { id: u64 }
fn hash(key: ref<Key>) -> u64 effects [] { return key.id; }
fn equal(a: ref<Key>, b: ref<Key>) -> bool effects [] { return a.id == b.id; }
fn main(heap: cap<mem.alloc>) -> u64 effects [mem.alloc] {
let map = match std.hashmap.new::<Key,own<u64>,fn hash,fn equal>(1,heap) {
std.hashmap.NewResult::<Key,own<u64>,fn hash,fn equal>::ok(value) => { move value },
std.hashmap.NewResult::<Key,own<u64>,fn hash,fn equal>::err(error) => { trap; }
};
let value = own.new(42u64,heap) or trap;
{ mut q = map;
match std.hashmap.insert::<Key,own<u64>,fn hash,fn equal>(q,Key { id: 7 },move value) {
std.hashmap.InsertResult::<Key,own<u64>>::inserted => {}
std.hashmap.InsertResult::<Key,own<u64>>::replaced(old) => { trap; }
std.hashmap.InsertResult::<Key,own<u64>>::full(returned) => { trap; }
}
let key = Key { id: 7 };
match std.hashmap.remove::<Key,own<u64>,fn hash,fn equal>(q,ref key) {
std.option.Option::<std.hashmap.Entry<Key,own<u64>>>::some(entry) => {
let (stored_key,owner) = struct.unpack(move entry);
assert(stored_key.id == 7u64); assert(own.take(move owner) == 42u64);
}
std.option.Option::<std.hashmap.Entry<Key,own<u64>>>::none => { trap; }
}
}
match std.hashmap.free::<Key,own<u64>,fn hash,fn equal>(move map) {
std.hashmap.FreeResult::<Key,own<u64>,fn hash,fn equal>::ok => {}
std.hashmap.FreeResult::<Key,own<u64>,fn hash,fn equal>::not_empty(returned) => { trap; }
}
return 42;
}