Register std.map with bounded U64Map<V>, exact key equality and explicit allocation/growth. Preserve returned ownership, old slots on reserve failure and original-authority refunds; support Copy values, owners and resources. Cover collision/tombstone/wrap behavior, independent dictionary traces, quota/cleanup/borrow/task rules, source/AIR parity and hosted/package CLI. Publish the API, roadmap and measured documentation cost. General key hashing/equality remains Q-037; numeric maps do not satisfy that gate.
71 lines
3.1 KiB
Rust
71 lines
3.1 KiB
Rust
// expect: run
|
|
// result: 42
|
|
// phase: 6
|
|
// spec: docs/spec/16-standard-library.md §2.13
|
|
// grant: heap=mem.alloc:195
|
|
module test.map
|
|
import std.map;
|
|
import std.option;
|
|
fn opened<V>(capacity: usize, heap: cap<mem.alloc>) -> std.map.U64Map<V> effects [mem.alloc] {
|
|
match std.map.new::<V>(capacity, heap) {
|
|
std.map.NewResult::<V>::ok(map) => { return move map; }
|
|
std.map.NewResult::<V>::err(error) => { trap; }
|
|
}
|
|
}
|
|
fn inserted<V>(result: std.map.InsertResult<V>) -> void effects [] {
|
|
match move result {
|
|
std.map.InsertResult::<V>::inserted => {}
|
|
std.map.InsertResult::<V>::replaced(old) => { trap; }
|
|
std.map.InsertResult::<V>::full(value) => { trap; }
|
|
}
|
|
}
|
|
fn released<V>(map: std.map.U64Map<V>) -> void effects [] {
|
|
match std.map.free::<V>(move map) {
|
|
std.map.FreeResult::<V>::ok => {}
|
|
std.map.FreeResult::<V>::not_empty(returned) => { trap; }
|
|
}
|
|
}
|
|
fn extracted<V>(value: std.option.Option<V>) -> V effects [] {
|
|
match move value {
|
|
std.option.Option::<V>::some(item) => { return move item; }
|
|
std.option.Option::<V>::none => { trap; }
|
|
}
|
|
}
|
|
fn main(heap: cap<mem.alloc>) -> u64 effects [mem.alloc] {
|
|
let map = opened::<own<u64>>(2, heap);
|
|
let a = own.new(7u64, heap) or trap; let b = own.new(42u64, heap) or trap;
|
|
{ mut q = map;
|
|
inserted::<own<u64>>(std.map.insert::<own<u64>>(q, 0, move a));
|
|
inserted::<own<u64>>(std.map.insert::<own<u64>>(q, 8, move b));
|
|
var keys: [u64; 4] = zero; var occupied: [bool; 4] = zero;
|
|
{ ref storage = q.storage; ref slots = buffer.view(storage);
|
|
for i in 0usize..4usize { ref slot = slots[i]; keys[i] = slot.key;
|
|
occupied[i] = match slot.state { std.map.SlotState::occupied => { true }, std.map.SlotState::empty => { false }, std.map.SlotState::deleted => { trap; } };
|
|
}
|
|
}
|
|
match std.map.reserve::<own<u64>>(q, 3, heap) {
|
|
std.map.ReserveResult::err(error) => { match error { alloc_error::budget_exceeded => {}, alloc_error::out_of_memory => { trap; } } }
|
|
std.map.ReserveResult::ok => { trap; }
|
|
}
|
|
match std.map.reserve::<own<u64>>(q, (0usize -% 1usize) / 2usize + 1usize, heap) {
|
|
std.map.ReserveResult::err(error) => { match error { alloc_error::budget_exceeded => {}, alloc_error::out_of_memory => { trap; } } }
|
|
std.map.ReserveResult::ok => { trap; }
|
|
}
|
|
assert(std.map.len::<own<u64>>(ref q) == 2usize); assert(std.map.capacity::<own<u64>>(ref q) == 2usize);
|
|
{ ref storage = q.storage; ref slots = buffer.view(storage);
|
|
for i in 0usize..4usize { ref slot = slots[i]; assert(slot.key == keys[i]);
|
|
let state = match slot.state { std.map.SlotState::occupied => { true }, std.map.SlotState::empty => { false }, std.map.SlotState::deleted => { trap; } };
|
|
assert(state == occupied[i]);
|
|
}
|
|
}
|
|
for i in 0usize..4usize { if occupied[i] {
|
|
let entry = extracted::<std.map.Entry<own<u64>>>(std.map.pop::<own<u64>>(q));
|
|
let (key, item) = struct.unpack(move entry); assert(key == keys[i]);
|
|
let value = own.take(move item); assert(value == if key == 0u64 { 7u64 } else { 42u64 });
|
|
} }
|
|
}
|
|
released::<own<u64>>(move map);
|
|
return 42;
|
|
}
|
|
|