198 lines
6.6 KiB
Plaintext
198 lines
6.6 KiB
Plaintext
#! @file heap.sakura
|
|||
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
||
|
|
#! @copyright Malunal Studios, LLC.
|
||
|
|
#! @license MIT
|
||
|
|
#!
|
||
|
|
#! Defines an implementation for a `heap` container. Heaps keep the least of the
|
||
|
|
#! values they hold at hand, adding a value and taking the least each in time
|
||
|
|
#! that grows with the logarithm of how many there are.
|
||
|
|
module std::containers;
|
||
|
|
|
||
|
|
|
||
|
|
#! A `heap` holds values so that the least of them, by the order `<` gives, is
|
||
|
|
#! always first, which `peek` reads and `pop` removes.
|
||
|
|
#!
|
||
|
|
#! They are useful wherever the next of many things to do is the least of them,
|
||
|
|
#! such as the soonest event, the cheapest path found so far, or the interval
|
||
|
|
#! that begins first.
|
||
|
|
#!
|
||
|
|
#! Its values are held in one vector as a binary tree, each no greater than the
|
||
|
|
#! two below it: those below the value at index `i` are at `2i + 1` and `2i + 2`.
|
||
|
|
#! Values equal to each other come out in no particular order.
|
||
|
|
#! @tparam T The type of its values, which are `ordered`.
|
||
|
|
pub struct heap<T>
|
||
|
|
: owning
|
||
|
|
, copyable
|
||
|
|
, moveable
|
||
|
|
{
|
||
|
|
var _val: vector<T>;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Provides read only access to the length of a heap.
|
||
|
|
#! @param this The heap to get the length of.
|
||
|
|
#! @returns The number of values it holds.
|
||
|
|
pub val heap<T>::length: u64 {
|
||
|
|
get => this._val.length;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Indicates if a heap is empty.
|
||
|
|
#! @param this The heap to check.
|
||
|
|
#! @returns Whether the heap holds no values.
|
||
|
|
pub val heap<T>::is_empty: bool {
|
||
|
|
get => this._val.length == 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Creates a new heap with no allocator, which takes memory once something is
|
||
|
|
#! pushed.
|
||
|
|
#! @returns The created heap, which holds no values yet.
|
||
|
|
#! @remarks Will use the built in `raw_allocate`, `raw_release` and `raw_resize`
|
||
|
|
#! for memory management.
|
||
|
|
pub def heap<T>::new(): heap<T> {
|
||
|
|
return heap::<T>::new(nothing{});
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Creates a new heap with an optional allocator, which takes memory once
|
||
|
|
#! something is pushed.
|
||
|
|
#! @param alloc The optional allocator for the memory backing of the heap.
|
||
|
|
#! @returns The created heap, which holds no values yet.
|
||
|
|
pub def heap<T>::new(alloc: maybe<&mut allocator>): heap<T> {
|
||
|
|
if const !(T is ordered) {
|
||
|
|
compile_error("A heap keeps its least value first, by the order `<` gives, so its values are `ordered`.");
|
||
|
|
}
|
||
|
|
return heap::<T> { _val = vector::<T>::new(alloc) };
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Creates a new heap with an optional allocator and room for some values.
|
||
|
|
#! @param size The number of values to make room for, which may be none.
|
||
|
|
#! @param alloc The optional allocator for the memory backing of the heap.
|
||
|
|
#! @returns The created heap, which holds no values yet.
|
||
|
|
pub def heap<T>::new(size: u64, alloc: maybe<&mut allocator>): heap<T> {
|
||
|
|
var made := heap::<T>::new(alloc);
|
||
|
|
made._val.reserve(size);
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Builds a heap holding copies of another's values, with its allocator.
|
||
|
|
#! @param other The heap to copy from.
|
||
|
|
#! @returns The copy, which owns its own memory and values.
|
||
|
|
#! @remarks Values are copied by `copyable` where they provide one.
|
||
|
|
pub def heap<T>::copy_of(other: &heap<T>): heap<T> {
|
||
|
|
return heap::<T> { _val = vector::<T>::copy_of(&other._val) };
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Creates a new heap by moving the data of another heap into it.
|
||
|
|
#! @param other The heap to move from, which holds nothing after.
|
||
|
|
#! @returns The moved heap, which takes the memory and allocator with it.
|
||
|
|
pub def heap<T>::move_of(other: &mut heap<T>): heap<T> {
|
||
|
|
return heap::<T> { _val = move(&mut other._val) };
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Adds a value.
|
||
|
|
#! @param this The heap to add the value to.
|
||
|
|
#! @param value The value to add.
|
||
|
|
#! @remarks The value rises past each value above it that is greater. An
|
||
|
|
#! allocator that gives no more memory stops the program.
|
||
|
|
pub def heap<T>::push(&mut this, value: T): void {
|
||
|
|
if const !(T is ordered) {
|
||
|
|
compile_error("A heap keeps its least value first, by the order `<` gives, so its values are `ordered`.");
|
||
|
|
}
|
||
|
|
this._val.append(move(value));
|
||
|
|
|
||
|
|
var at := this._val.length - 1;
|
||
|
|
val data := this._val.data;
|
||
|
|
while at > 0 {
|
||
|
|
val parent := (at - 1) / 2;
|
||
|
|
break if data[parent] <= data[at];
|
||
|
|
heap::<T>::swap(data, parent, at);
|
||
|
|
at = parent;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Reads the least value.
|
||
|
|
#! @param this The heap to read, which holds a value, or else a checked
|
||
|
|
#! program stops.
|
||
|
|
#! @returns The least value, which is only good until the heap changes or
|
||
|
|
#! closes.
|
||
|
|
pub def heap<T>::peek(&this): &T {
|
||
|
|
if const checking() {
|
||
|
|
if this._val.length == 0 {
|
||
|
|
past_end(0, 0);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return &this._val.data[0];
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Removes the least value.
|
||
|
|
#! @param this The heap to remove from, which holds a value, or else a checked
|
||
|
|
#! program stops.
|
||
|
|
#! @remarks The value is closed where it closes, or holds what does, and not
|
||
|
|
#! given back, so read it with `peek` first. The last value takes its
|
||
|
|
#! place and sinks past each value below it that is less.
|
||
|
|
pub def heap<T>::pop(&mut this): void {
|
||
|
|
if const !(T is ordered) {
|
||
|
|
compile_error("A heap keeps its least value first, by the order `<` gives, so its values are `ordered`.");
|
||
|
|
}
|
||
|
|
if const checking() {
|
||
|
|
if this._val.length == 0 {
|
||
|
|
past_end(0, 0);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
val data := this._val.data;
|
||
|
|
val last := this._val.length - 1;
|
||
|
|
heap::<T>::swap(data, 0, last);
|
||
|
|
this._val.truncate();
|
||
|
|
|
||
|
|
val count := this._val.length;
|
||
|
|
var at: u64 = 0;
|
||
|
|
while 2 * at + 1 < count {
|
||
|
|
var child := 2 * at + 1;
|
||
|
|
if child + 1 < count && data[child + 1] < data[child] {
|
||
|
|
child++;
|
||
|
|
}
|
||
|
|
break if data[at] <= data[child];
|
||
|
|
heap::<T>::swap(data, at, child);
|
||
|
|
at = child;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Lends a view of the values, in the order the heap holds them, which is not
|
||
|
|
#! the order they come out in.
|
||
|
|
#! @param this The heap to view.
|
||
|
|
#! @returns The view, which is only good until the heap changes or closes.
|
||
|
|
pub def heap<T>::view(&this): span<T> {
|
||
|
|
return this._val.view();
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Removes every value, keeping the memory the heap holds them in.
|
||
|
|
#! @param this The heap to empty.
|
||
|
|
#! @remarks Values are closed where they close, or hold what does.
|
||
|
|
pub def heap<T>::clear(&mut this): void {
|
||
|
|
this._val.clear();
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Removes every value and gives back the memory the heap holds.
|
||
|
|
#! @param this The heap to close.
|
||
|
|
#! @remarks Values are closed where they close, or hold what does.
|
||
|
|
pub def heap<T>::close(&mut this): void {
|
||
|
|
this._val.close();
|
||
|
|
}
|
||
|
|
|
||
|
|
# Swaps two values byte for byte, so that neither is closed or copied.
|
||
|
|
def heap<T>::swap(data: *mut T, first: u64, second: u64): void {
|
||
|
|
return if first == second;
|
||
|
|
if const !(T is closeable) {
|
||
|
|
val held: T = data[first];
|
||
|
|
data[first] = data[second];
|
||
|
|
data[second] = held;
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
val a := (data + first) as *mut u8;
|
||
|
|
val b := (data + second) as *mut u8;
|
||
|
|
for at in 0..size_of::<T>() {
|
||
|
|
val held: u8 = a[at];
|
||
|
|
a[at] = b[at];
|
||
|
|
b[at] = held;
|
||
|
|
}
|
||
|
|
}
|