cli: add safe owning numeric-key maps
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.
This commit is contained in:
1 parent
fc23f1b23a
commit
57603860df
46 files changed
+2430
-15
No files matched your search
@@ -0,0 +1,62 @@
|
||||
// expect: run
|
||||
// result: 42
|
||||
// phase: 6
|
||||
// spec: docs/spec/16-standard-library.md §2.13
|
||||
// grant: heap=mem.alloc:1024
|
||||
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::<u64>(4, heap);
|
||||
{ mut q = map;
|
||||
inserted::<u64>(std.map.insert::<u64>(q, 0, 1));
|
||||
inserted::<u64>(std.map.insert::<u64>(q, 8, 2));
|
||||
inserted::<u64>(std.map.insert::<u64>(q, 16, 3));
|
||||
inserted::<u64>(std.map.insert::<u64>(q, 24, 4));
|
||||
assert(extracted::<u64>(std.map.remove::<u64>(q, 8)) == 2u64);
|
||||
match std.map.insert::<u64>(q, 16, 42) {
|
||||
std.map.InsertResult::<u64>::replaced(previous) => { assert(previous == 3u64); }
|
||||
std.map.InsertResult::<u64>::inserted => { trap; }
|
||||
std.map.InsertResult::<u64>::full(returned) => { trap; }
|
||||
}
|
||||
assert(std.map.len::<u64>(ref q) == 3usize);
|
||||
inserted::<u64>(std.map.insert::<u64>(q, 32, 5));
|
||||
assert(extracted::<u64>(std.map.get::<u64>(ref q, 24)) == 4u64);
|
||||
assert(extracted::<u64>(std.map.get::<u64>(ref q, 16)) == 42u64);
|
||||
assert(extracted::<u64>(std.map.get::<u64>(ref q, 32)) == 5u64);
|
||||
assert(not std.map.contains::<u64>(ref q, 8));
|
||||
match std.map.reserve::<u64>(q, 7, heap) { std.map.ReserveResult::ok => {}, std.map.ReserveResult::err(error) => { trap; } }
|
||||
for key in 0u64..5u64 {
|
||||
if key != 1u64 { let removed = extracted::<u64>(std.map.remove::<u64>(q, key * 8u64)); }
|
||||
}
|
||||
assert(std.map.len::<u64>(ref q) == 0usize);
|
||||
}
|
||||
released::<u64>(move map);
|
||||
return 42;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user