Files
std/containers/runs.sakura
T
2026-11-14 15:32:55 -06:00

186 lines
7.1 KiB
Plaintext

#! @file runs.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Defines an implementation for a `runs` container. Runs hold many short lists,
#! each a run of elements one after another in one block, named by an ID.
module std::containers;
#! A `runs` holds many lists of elements, each a run of them one after another
#! in one block, which a `u32` ID names.
#!
#! They are useful wherever many short lists are made and kept, such as the
#! parts of each type, the parameters of each function, or the children of each
#! node, which a vector each would scatter over the heap.
#!
#! The elements of every run are held in one vector, in the order the runs were
#! made, and where each run begins in another. A run is made by adding a view of
#! its elements, or by pushing them one at a time and then ending it. Runs are
#! never changed, and none is removed but by `rewind` and `clear`, so an ID is
#! good for as long as its run is. ID 0 is the empty run, which every empty view
#! added is given.
#! @tparam T The type of the elements.
pub struct runs<T>
: owning
, copyable
, moveable
{
var _items: vector<T>;
var _start: vector<u64>;
}
#! Provides read only access to how many runs there are.
#! @param this The runs to count.
#! @returns The number of runs, the empty one included, which is one more than
#! the newest's ID.
pub val runs<T>::count: u64 {
get => this._start.length - 1;
}
#! Provides read only access to how many elements the runs hold together.
#! @param this The runs to count the elements of.
#! @returns The number of elements, those of a run still being pushed included.
pub val runs<T>::size: u64 {
get => this._items.length;
}
#! Creates new runs with no allocator, holding only the empty run.
#! @returns The created runs.
#! @remarks Will use the built in `raw_allocate`, `raw_release` and `raw_resize`
#! for memory management.
pub def runs<T>::new(): runs<T> {
return runs::<T>::new(nothing{});
}
#! Creates new runs with an optional allocator, holding only the empty run.
#! @param alloc The optional allocator for the memory backing of the runs.
#! @returns The created runs.
pub def runs<T>::new(alloc: maybe<&mut allocator>): runs<T> {
var made := runs::<T> { _items = vector::<T>::new(alloc), _start = vector::<u64>::new(alloc) };
made._start.resize(2);
return made;
}
#! Builds runs holding copies of another's, each with the same ID, with its
#! allocator.
#! @param other The runs to copy from.
#! @returns The copy, which owns its own memory and elements.
#! @remarks Elements are copied by `copyable` where they provide one.
pub def runs<T>::copy_of(other: &runs<T>): runs<T> {
return runs::<T> { _items = vector::<T>::copy_of(&other._items), _start = vector::<u64>::copy_of(&other._start) };
}
#! Creates new runs by moving the data of others into them.
#! @param other The runs to move from, which hold nothing after, not even the
#! empty run, until they are cleared.
#! @returns The moved runs, which take the memory and allocator with them.
pub def runs<T>::move_of(other: &mut runs<T>): runs<T> {
return runs::<T> { _items = move(&mut other._items), _start = move(&mut other._start) };
}
#! Adds a run of copies of elements.
#! @param this The runs to add to.
#! @param items The elements of the run, in order.
#! @returns The run's ID, or 0, the empty run's, where there are no elements.
#! @remarks Elements are copied as a vector's `extend` copies them. Elements
#! pushed and not yet ended begin the run. An allocator that gives no
#! more memory stops the program.
pub def runs<T>::add(&mut this, items: span<T>): u32 {
this._items.extend(items);
return this.end();
}
#! Adds an element to the run being made, which `end` ends.
#! @param this The runs to add to.
#! @param item The element, which follows those pushed before it.
pub def runs<T>::push(&mut this, item: T): void {
this._items.append(move(item));
}
#! Ends the run being made, of the elements pushed since the last run ended.
#! @param this The runs to end the run of.
#! @returns The run's ID, or 0, the empty run's, where none was pushed.
pub def runs<T>::end(&mut this): u32 {
val open := this._start.data[this._start.length - 1];
return 0 if this._items.length == open;
val id := this._start.length - 1;
this._start.append(this._items.length);
return id as u32;
}
#! How many elements a run holds.
#! @param this The runs.
#! @param id The run's ID, below `count`, past which a checked program
#! stops.
#! @returns The number of its elements.
pub def runs<T>::length(&this, id: u32): u64 {
expects(id as u64 + 1 < this._start.length);
return this._start.data[id as u64 + 1] - this._start.data[id];
}
#! Reads an element of a run.
#! @param this The runs.
#! @param id The run's ID, below `count`, past which a checked program
#! stops.
#! @param index The element's place in the run, below its length, past which a
#! checked program stops.
#! @returns The element, which is only good until the runs grow or close.
pub def runs<T>::at(&this, id: u32, index: u64): &T {
expects(id as u64 + 1 < this._start.length);
val start := this._start.data[id];
val length := this._start.data[id as u64 + 1] - start;
expects(index < length);
return &this._items.data[start + index];
}
#! Lends a view of a run's elements.
#! @param this The runs.
#! @param id The run's ID, below `count`, past which a checked program
#! stops.
#! @returns The view, which is only good until the runs grow or close, so a loop
#! that adds runs while it walks one reads it by `at` instead.
pub def runs<T>::view(&this, id: u32): span<T> {
val next := id as u64 + 1;
expects(next < this._start.length);
val start := this._start.data[id];
return this._items[start:this._start.data[next] - start];
}
#! Removes the runs from an ID on, so that the next run made is given it.
#! @param this The runs to remove from.
#! @param count How many runs to keep, from 1, which keeps only the empty one,
#! to `count`, which keeps them all.
#! @remarks Elements removed are closed where they close, or hold what does, and
#! so are those pushed and not yet ended.
pub def runs<T>::rewind(&mut this, count: u64): void {
expects(count != 0 && count <= this.count);
this._items.resize(this._start.data[count]);
this._start.resize(count + 1);
}
#! Removes every run but the empty one, keeping the memory.
#! @param this The runs to empty.
#! @remarks Elements are closed where they close, or hold what does.
pub def runs<T>::clear(&mut this): void {
this._items.clear();
this._start.clear();
this._start.resize(2);
}
#! Removes every run and gives back the memory the runs hold.
#! @param this The runs to close.
#! @remarks Elements are closed where they close, or hold what does. Closed runs
#! hold not even the empty run, until they are cleared.
pub def runs<T>::close(&mut this): void {
this._items.close();
this._start.close();
}
# Stops the program where an ID names no run.
def runs<T>::check(&this, id: u32): void {
expects(id as u64 + 1 < this._start.length);
}