Files
dev cfece6080c cli: add safe owning binary heaps
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.
2026-10-07 11:41:35 +03:00

130 lines
7.3 KiB
Rust

module std.heap
import std.option;
struct Entry<T> { position: usize, value: T }
struct BinaryHeap<T, B: fn(ref<T>,ref<T>) -> bool effects []> { entries: buffer<Entry<T>>, indices: buffer<usize> }
enum NewResult<T, B: fn(ref<T>,ref<T>) -> bool effects []> { ok(BinaryHeap<T,fn B>), err(alloc_error) }
enum ReserveResult { ok, err(alloc_error) }
enum PushResult<T> { ok, full(T) }
enum FreeResult<T, B: fn(ref<T>,ref<T>) -> bool effects []> { ok, not_empty(BinaryHeap<T,fn B>) }
fn new<T, B: fn(ref<T>,ref<T>) -> bool effects []>(capacity: usize,heap: cap<mem.alloc>) -> NewResult<T,fn B> effects [mem.alloc] {
match buffer.new<Entry<T>>(capacity,heap) {
err(error) => { return NewResult::<T,fn B>::err(error); }
ok(entries) => {
match buffer.new<usize>(capacity,heap) {
err(error) => { buffer.free(move entries) or trap; return NewResult::<T,fn B>::err(error); }
ok(indices) => { return NewResult::<T,fn B>::ok(BinaryHeap::<T,fn B> { entries: move entries, indices: move indices }); }
}
}
}
}
fn len<T, B: fn(ref<T>,ref<T>) -> bool effects []>(value: ref<BinaryHeap<T,fn B>>) -> usize effects [] {
ref entries = value.entries; return buffer.len(entries);
}
fn capacity<T, B: fn(ref<T>,ref<T>) -> bool effects []>(value: ref<BinaryHeap<T,fn B>>) -> usize effects [] {
ref entries = value.entries; return buffer.capacity(entries);
}
fn reserve<T, B: fn(ref<T>,ref<T>) -> bool effects []>(value: mut<BinaryHeap<T,fn B>>,capacity: usize,heap: cap<mem.alloc>) -> ReserveResult effects [mem.alloc] {
{ ref entries = value.entries; if capacity <= buffer.capacity(entries) { return ReserveResult::ok; } }
match buffer.new<usize>(capacity,heap) {
err(error) => { return ReserveResult::err(error); }
ok(next) => {
{ ref old = value.indices; ref indices = buffer.view(old); mut output = next;
for i in 0usize..indices.len { buffer.push(output,indices[i]) or trap; }
}
{ mut entries = value.entries;
match buffer.reserve(entries,capacity,heap) {
ok() => {}
err(error) => {
{ mut index = next; while buffer.len(index) != 0usize { let unused = buffer.pop(index) or trap; } }
buffer.free(move next) or trap; return ReserveResult::err(error);
}
}
}
let old = value.swap(mut value.indices,move next);
{ mut index = old; while buffer.len(index) != 0usize { let unused = buffer.pop(index) or trap; } }
buffer.free(move old) or trap; return ReserveResult::ok;
}
}
}
fn push<T, B: fn(ref<T>,ref<T>) -> bool effects []>(value: mut<BinaryHeap<T,fn B>>,item: T) -> PushResult<T> effects [] {
var position: usize = 0;
{ ref entries = value.entries; position = buffer.len(entries);
if position == buffer.capacity(entries) { return PushResult::<T>::full(move item); }
}
{ mut entries = value.entries; buffer.push(entries,Entry::<T> { position: position, value: move item }) or trap; }
{ mut indices = value.indices; buffer.push(indices,position) or trap; }
while position != 0usize {
let parent = (position - 1usize) / 2usize;
var child_index: usize = 0; var parent_index: usize = 0;
{ ref storage = value.indices; ref indices = buffer.view(storage); child_index = indices[position]; parent_index = indices[parent]; }
var earlier: bool = false;
{ ref storage = value.entries; ref entries = buffer.view(storage); earlier = B(ref entries[child_index].value,ref entries[parent_index].value); }
if not earlier { break; }
{ mut storage = value.indices; mut indices = buffer.view(storage); indices[parent] = child_index; indices[position] = parent_index; }
{ mut storage = value.entries; mut entries = buffer.view(storage); entries[child_index].position = parent; entries[parent_index].position = position; }
position = parent;
}
return PushResult::<T>::ok;
}
fn peek<T: Copy, B: fn(ref<T>,ref<T>) -> bool effects []>(value: ref<BinaryHeap<T,fn B>>) -> std.option.Option<T> effects [] {
ref index = value.indices;
if buffer.len(index) == 0usize { return std.option.Option::<T>::none; }
ref indices = buffer.view(index); ref storage = value.entries; ref entries = buffer.view(storage);
return std.option.Option::<T>::some(entries[indices[0]].value);
}
fn pop<T, B: fn(ref<T>,ref<T>) -> bool effects []>(value: mut<BinaryHeap<T,fn B>>) -> std.option.Option<T> effects [] {
var length: usize = 0; var removed: usize = 0;
{ ref storage = value.indices; length = buffer.len(storage);
if length == 0usize { return std.option.Option::<T>::none; }
ref indices = buffer.view(storage); removed = indices[0];
}
var replacement: usize = 0;
{ mut storage = value.indices; replacement = buffer.pop(storage) or trap; }
length = length - 1usize;
if length != 0usize {
{ mut storage = value.indices; mut indices = buffer.view(storage); indices[0] = replacement; }
{ mut storage = value.entries; mut entries = buffer.view(storage); entries[replacement].position = 0usize; }
var position: usize = 0;
while position < length / 2usize {
let left = position * 2usize + 1usize; let right = left + 1usize;
var child: usize = left; var child_index: usize = 0; var parent_index: usize = 0;
{ ref index = value.indices; ref indices = buffer.view(index);
ref storage = value.entries; ref entries = buffer.view(storage);
if right < length { if B(ref entries[indices[right]].value,ref entries[indices[left]].value) { child = right; } }
child_index = indices[child]; parent_index = indices[position];
}
var earlier: bool = false;
{ ref storage = value.entries; ref entries = buffer.view(storage); earlier = B(ref entries[child_index].value,ref entries[parent_index].value); }
if not earlier { break; }
{ mut storage = value.indices; mut indices = buffer.view(storage); indices[position] = child_index; indices[child] = parent_index; }
{ mut storage = value.entries; mut entries = buffer.view(storage); entries[child_index].position = position; entries[parent_index].position = child; }
position = child;
}
}
mut storage = value.entries;
let last = buffer.pop(storage) or trap;
if removed == length {
let (position,item) = struct.unpack(move last); return std.option.Option::<T>::some(move item);
}
let position = last.position;
let old = { mut entries = buffer.view(storage); value.swap(mut entries[removed],move last) };
{ mut storage = value.indices; mut indices = buffer.view(storage); indices[position] = removed; }
let (position,item) = struct.unpack(move old); return std.option.Option::<T>::some(move item);
}
fn free<T, B: fn(ref<T>,ref<T>) -> bool effects []>(value: BinaryHeap<T,fn B>) -> FreeResult<T,fn B> effects [] {
var length: usize = 0;
{ ref entries = value.entries; length = buffer.len(entries); }
if length != 0usize { return FreeResult::<T,fn B>::not_empty(move value); }
let (entries,indices) = struct.unpack(move value);
buffer.free(move entries) or trap; buffer.free(move indices) or trap; return FreeResult::<T,fn B>::ok;
}