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.
54 lines
3.6 KiB
Rust
54 lines
3.6 KiB
Rust
// expect: run
|
|
// result: 42
|
|
// phase: 6
|
|
// spec: docs/spec/16-standard-library.md §2.14
|
|
// grant: heap=mem.alloc:1024
|
|
module test.hashmap
|
|
import std.hashmap;
|
|
import std.option;
|
|
fn opened<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(capacity: usize, heap: cap<mem.alloc>) -> std.hashmap.HashMap<K, V, fn H, fn E> effects [mem.alloc] {
|
|
match std.hashmap.new::<K, V, fn H, fn E>(capacity, heap) {
|
|
std.hashmap.NewResult::<K, V, fn H, fn E>::ok(map) => { return move map; }
|
|
std.hashmap.NewResult::<K, V, fn H, fn E>::err(error) => { trap; }
|
|
}
|
|
}
|
|
fn inserted<K, V>(result: std.hashmap.InsertResult<K,V>) -> void effects [] {
|
|
match move result { std.hashmap.InsertResult::<K,V>::inserted => {}, std.hashmap.InsertResult::<K,V>::full(value) => { trap; }, std.hashmap.InsertResult::<K,V>::replaced(value) => { trap; } }
|
|
}
|
|
fn extracted<T>(value: std.option.Option<T>) -> T effects [] {
|
|
match move value { std.option.Option::<T>::some(item) => { return move item; }, std.option.Option::<T>::none => { trap; } }
|
|
}
|
|
fn released<K, V, H: fn(ref<K>) -> u64 effects [], E: fn(ref<K>, ref<K>) -> bool effects []>(map: std.hashmap.HashMap<K, V, fn H, fn E>) -> void effects [] {
|
|
match std.hashmap.free::<K, V, fn H, fn E>(move map) { std.hashmap.FreeResult::<K, V, fn H, fn E>::ok => {}, std.hashmap.FreeResult::<K, V, fn H, fn E>::not_empty(value) => { trap; } }
|
|
}
|
|
struct Key { id: 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 main(heap: cap<mem.alloc>) -> u64 effects [mem.alloc] {
|
|
let map = opened::<Key, u64, fn hash, fn equal>(2, heap);
|
|
{ mut q = map;
|
|
inserted::<Key,u64>(std.hashmap.insert::<Key, u64, fn hash, fn equal>(q, Key { id: 1 }, 11));
|
|
inserted::<Key,u64>(std.hashmap.insert::<Key, u64, fn hash, fn equal>(q, Key { id: 2 }, 22));
|
|
match std.hashmap.insert::<Key, u64, fn hash, fn equal>(q, Key { id: 3 }, 33) {
|
|
std.hashmap.InsertResult::<Key,u64>::full(entry) => { assert(entry.key.id == 3u64); assert(entry.value == 33u64); },
|
|
std.hashmap.InsertResult::<Key,u64>::inserted => { trap; }, std.hashmap.InsertResult::<Key,u64>::replaced(old) => { trap; }
|
|
}
|
|
match std.hashmap.insert::<Key, u64, fn hash, fn equal>(q, Key { id: 2 }, 42) {
|
|
std.hashmap.InsertResult::<Key,u64>::replaced(old) => { assert(old.key.id == 2u64); assert(old.value == 22u64); },
|
|
std.hashmap.InsertResult::<Key,u64>::inserted => { trap; }, std.hashmap.InsertResult::<Key,u64>::full(entry) => { trap; }
|
|
}
|
|
let key = Key { id: 1 };
|
|
let removed = extracted::<std.hashmap.Entry<Key,u64>>(std.hashmap.remove::<Key, u64, fn hash, fn equal>(q, ref key));
|
|
assert(removed.key.id == 1u64); assert(removed.value == 11u64);
|
|
inserted::<Key,u64>(std.hashmap.insert::<Key, u64, fn hash, fn equal>(q, Key { id: 3 }, 33));
|
|
match std.hashmap.reserve::<Key, u64, fn hash, fn equal>(q, 4, heap) { std.hashmap.ReserveResult::ok => {}, std.hashmap.ReserveResult::err(error) => { trap; } }
|
|
}
|
|
{ ref q = map; let key = Key { id: 2 }; assert(std.hashmap.contains::<Key, u64, fn hash, fn equal>(q, ref key));
|
|
assert(extracted::<u64>(std.hashmap.get::<Key, u64, fn hash, fn equal>(q, ref key)) == 42u64);
|
|
assert(std.hashmap.len::<Key, u64, fn hash, fn equal>(q) == 2usize); assert(std.hashmap.capacity::<Key, u64, fn hash, fn equal>(q) == 4usize);
|
|
}
|
|
{ mut q = map; let last = extracted::<std.hashmap.Entry<Key,u64>>(std.hashmap.pop::<Key, u64, fn hash, fn equal>(q)); assert(last.key.id == 3u64);
|
|
let first = extracted::<std.hashmap.Entry<Key,u64>>(std.hashmap.pop::<Key, u64, fn hash, fn equal>(q)); assert(first.value == 42u64); }
|
|
released::<Key, u64, fn hash, fn equal>(move map); return 42;
|
|
}
|