Add std.heap with an exact explicit pure ordering operation, bounded push, Copy-only peek, moving pop and empty-only free. Sift Copy indices while complete owners remain in dense initialized storage; repair holes through complete exchange. Reserve preserves layout on returned failure and accounts for both old/new buffers and separately owned payloads. Integrate normative contracts, RFC, references, bundle measurements, independent heapq/permutation oracles and CLI/package/profile checks. Validation: 1007 conformance cases; 34 heap GC fixtures and scheduled stress; unit/Quick Reference, formatting, hosted smoke, 70 CLI checks, 109 source coverage constructs and unchanged AIR reference bundle.
32 lines
2.8 KiB
Rust
32 lines
2.8 KiB
Rust
// expect: run
|
|
// result: 42
|
|
// phase: 6
|
|
// spec: docs/spec/16-standard-library.md §2.16
|
|
// grant: heap=mem.alloc:207
|
|
module test.heap
|
|
import std.heap;
|
|
import std.option;
|
|
fn opened<T, B: fn(ref<T>,ref<T>) -> bool effects []>(capacity: usize,heap: cap<mem.alloc>) -> std.heap.BinaryHeap<T,fn B> effects [mem.alloc] {
|
|
match std.heap.new::<T,fn B>(capacity,heap) { std.heap.NewResult::<T,fn B>::ok(value) => { return move value; }, std.heap.NewResult::<T,fn B>::err(error) => { trap; } }
|
|
}
|
|
fn pushed<T>(result: std.heap.PushResult<T>) -> void effects [] { match move result { std.heap.PushResult::<T>::ok => {}, std.heap.PushResult::<T>::full(item) => { trap; } } }
|
|
fn extracted<T>(result: std.option.Option<T>) -> T effects [] { match move result { std.option.Option::<T>::some(item) => { return move item; }, std.option.Option::<T>::none => { trap; } } }
|
|
fn released<T, B: fn(ref<T>,ref<T>) -> bool effects []>(value: std.heap.BinaryHeap<T,fn B>) -> void effects [] { match std.heap.free::<T,fn B>(move value) { std.heap.FreeResult::<T,fn B>::ok => {}, std.heap.FreeResult::<T,fn B>::not_empty(value) => { trap; } } }
|
|
fn before(a: ref<u64>,b: ref<u64>) -> bool effects [] { return *a < *b; }
|
|
fn after(a: ref<u64>,b: ref<u64>) -> bool effects [] { return *a > *b; }
|
|
fn check(heap: cap<mem.alloc>) -> void effects [mem.alloc] {
|
|
let value = opened::<u64,fn before>(3,heap);
|
|
{ mut q = value; pushed::<u64>(std.heap.push::<u64,fn before>(q,9)); pushed::<u64>(std.heap.push::<u64,fn before>(q,1)); pushed::<u64>(std.heap.push::<u64,fn before>(q,5)); }
|
|
var positions: [usize;3] = [0usize;3]; var values: [u64;3] = [0u64;3]; var indices: [usize;3] = [0usize;3];
|
|
{ ref q = value; ref storage = q.entries; ref entries = buffer.view(storage); ref index = q.indices; ref current = buffer.view(index);
|
|
for i in 0usize..3usize { positions[i] = entries[i].position; values[i] = entries[i].value; indices[i] = current[i]; } }
|
|
{ mut q = value; match std.heap.reserve::<u64,fn before>(q,4,heap) { std.heap.ReserveResult::err(error) => {}, std.heap.ReserveResult::ok => { trap; } } }
|
|
{ ref q = value; assert(std.heap.capacity::<u64,fn before>(q) == 3usize); ref storage = q.entries; ref entries = buffer.view(storage); ref index = q.indices; ref current = buffer.view(index);
|
|
for i in 0usize..3usize { assert(positions[i] == entries[i].position); assert(values[i] == entries[i].value); assert(indices[i] == current[i]); } }
|
|
{ mut q = value; assert(extracted::<u64>(std.heap.pop::<u64,fn before>(q)) == 1u64); assert(extracted::<u64>(std.heap.pop::<u64,fn before>(q)) == 5u64); assert(extracted::<u64>(std.heap.pop::<u64,fn before>(q)) == 9u64); }
|
|
released::<u64,fn before>(move value);
|
|
}
|
|
fn main(heap: cap<mem.alloc>) -> u64 effects [mem.alloc] {
|
|
let first = cap.narrow.budget(heap,103usize) or trap; check(first); let second = cap.narrow.budget(heap,104usize) or trap; check(second); return 42;
|
|
}
|