module std.heap import std.option; struct Entry { position: usize, value: T } struct BinaryHeap,ref) -> bool effects []> { entries: buffer>, indices: buffer } enum NewResult,ref) -> bool effects []> { ok(BinaryHeap), err(alloc_error) } enum ReserveResult { ok, err(alloc_error) } enum PushResult { ok, full(T) } enum FreeResult,ref) -> bool effects []> { ok, not_empty(BinaryHeap) } fn new,ref) -> bool effects []>(capacity: usize,heap: cap) -> NewResult effects [mem.alloc] { match buffer.new>(capacity,heap) { err(error) => { return NewResult::::err(error); } ok(entries) => { match buffer.new(capacity,heap) { err(error) => { buffer.free(move entries) or trap; return NewResult::::err(error); } ok(indices) => { return NewResult::::ok(BinaryHeap:: { entries: move entries, indices: move indices }); } } } } } fn len,ref) -> bool effects []>(value: ref>) -> usize effects [] { ref entries = value.entries; return buffer.len(entries); } fn capacity,ref) -> bool effects []>(value: ref>) -> usize effects [] { ref entries = value.entries; return buffer.capacity(entries); } fn reserve,ref) -> bool effects []>(value: mut>,capacity: usize,heap: cap) -> ReserveResult effects [mem.alloc] { { ref entries = value.entries; if capacity <= buffer.capacity(entries) { return ReserveResult::ok; } } match buffer.new(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,ref) -> bool effects []>(value: mut>,item: T) -> PushResult effects [] { var position: usize = 0; { ref entries = value.entries; position = buffer.len(entries); if position == buffer.capacity(entries) { return PushResult::::full(move item); } } { mut entries = value.entries; buffer.push(entries,Entry:: { 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::::ok; } fn peek,ref) -> bool effects []>(value: ref>) -> std.option.Option effects [] { ref index = value.indices; if buffer.len(index) == 0usize { return std.option.Option::::none; } ref indices = buffer.view(index); ref storage = value.entries; ref entries = buffer.view(storage); return std.option.Option::::some(entries[indices[0]].value); } fn pop,ref) -> bool effects []>(value: mut>) -> std.option.Option effects [] { var length: usize = 0; var removed: usize = 0; { ref storage = value.indices; length = buffer.len(storage); if length == 0usize { return std.option.Option::::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::::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::::some(move item); } fn free,ref) -> bool effects []>(value: BinaryHeap) -> FreeResult effects [] { var length: usize = 0; { ref entries = value.entries; length = buffer.len(entries); } if length != 0usize { return FreeResult::::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::::ok; }