501 lines
15 KiB
Plaintext
501 lines
15 KiB
Plaintext
#! @file list.sakura
|
|||
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
||
|
|
#! @copyright Malunal Studios, LLC.
|
||
|
|
#! @license MIT
|
||
|
|
#!
|
||
|
|
#! Defines an implementation for a `list` container. Lists are a structured
|
||
|
|
#! container that stores each element in a node of its own, linked to the next.
|
||
|
|
module std::containers;
|
||
|
|
|
||
|
|
|
||
|
|
#! A `list` is a structured data container, singly linked, that stores each of
|
||
|
|
#! its elements in a node of its own.
|
||
|
|
#!
|
||
|
|
#! They are useful where elements are added and removed at the front, and where
|
||
|
|
#! an element must never move once it is added, since each keeps its own node
|
||
|
|
#! until it is removed. Reaching an element by index walks the list from the
|
||
|
|
#! front.
|
||
|
|
#!
|
||
|
|
#! It holds its first and last nodes, so pushing onto either end takes the same
|
||
|
|
#! time however long it is, but only the front is popped without walking.
|
||
|
|
#! @tparam T The type of its elements.
|
||
|
|
pub struct list<T>
|
||
|
|
: owning
|
||
|
|
, hashable
|
||
|
|
, indexable<u64, T>
|
||
|
|
, iterable<T>
|
||
|
|
, copyable
|
||
|
|
, moveable
|
||
|
|
, equatable
|
||
|
|
{
|
||
|
|
var _alc: maybe<&mut allocator>;
|
||
|
|
var _hed: *mut list<T>::node;
|
||
|
|
var _tal: *mut list<T>::node;
|
||
|
|
var _len: u64;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! An element of a list, with the node after it.
|
||
|
|
struct list<T>::node {
|
||
|
|
var value: T;
|
||
|
|
var next: *mut list<T>::node;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Walks the elements of a list from its front, giving a reference that reads
|
||
|
|
#! each in turn, which is what `iter(&this)` lends and `for x` and `for &x`
|
||
|
|
#! walk.
|
||
|
|
pub struct list<T>::cursor : sized_iterator<&T> {
|
||
|
|
var _at: *mut list<T>::node;
|
||
|
|
var _left: u64;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Gives the next element of the list.
|
||
|
|
#! @param this The cursor to advance.
|
||
|
|
#! @returns Maybe a reference to the next element; otherwise, `nothing`.
|
||
|
|
pub def list<T>::cursor::next(&mut this): maybe<&T> {
|
||
|
|
if this._at == null {
|
||
|
|
return nothing {};
|
||
|
|
}
|
||
|
|
|
||
|
|
val here := this._at;
|
||
|
|
this._at = (*here).next;
|
||
|
|
this._left = this._left - 1;
|
||
|
|
return &(*here).value;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! How many elements the cursor has left to give.
|
||
|
|
#! @param this The cursor.
|
||
|
|
#! @returns How many.
|
||
|
|
pub def list<T>::cursor::remaining(&this): u64 {
|
||
|
|
return this._left;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Walks the elements of a list from its front, giving a reference that writes
|
||
|
|
#! each in turn, which is what `iter(&mut this)` lends and `for &mut x` walks.
|
||
|
|
pub struct list<T>::mut_cursor : sized_iterator<&mut T> {
|
||
|
|
var _at: *mut list<T>::node;
|
||
|
|
var _left: u64;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Gives the next element of the list, to be written.
|
||
|
|
#! @param this The cursor to advance.
|
||
|
|
#! @returns Maybe a reference to the next element; otherwise, `nothing`.
|
||
|
|
pub def list<T>::mut_cursor::next(&mut this): maybe<&mut T> {
|
||
|
|
if this._at == null {
|
||
|
|
return nothing {};
|
||
|
|
}
|
||
|
|
|
||
|
|
val here := this._at;
|
||
|
|
this._at = (*here).next;
|
||
|
|
this._left = this._left - 1;
|
||
|
|
return &mut (*here).value;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! How many elements the cursor has left to give.
|
||
|
|
#! @param this The cursor.
|
||
|
|
#! @returns How many.
|
||
|
|
pub def list<T>::mut_cursor::remaining(&this): u64 {
|
||
|
|
return this._left;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Provides read only access to the length of a list.
|
||
|
|
#! @param this The list to get the length of.
|
||
|
|
#! @returns The length of the list.
|
||
|
|
pub val list<T>::length: u64 {
|
||
|
|
get => this._len;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Indicates if a list is empty.
|
||
|
|
#! @param this The list to check.
|
||
|
|
#! @returns Whether the list is empty.
|
||
|
|
pub val list<T>::is_empty: bool {
|
||
|
|
get => this._len == 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Creates a new list with no allocator.
|
||
|
|
#! @returns The created list container.
|
||
|
|
#! @remarks Will use the built in `raw_allocate` and `raw_release` for memory
|
||
|
|
#! management.
|
||
|
|
pub def list<T>::new(): list<T> {
|
||
|
|
return list::<T>::new(nothing{});
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Creates a new list with an optional allocator.
|
||
|
|
#! @param alloc The optional allocator for the nodes of the list.
|
||
|
|
#! @returns The created list container, which holds no elements yet.
|
||
|
|
pub def list<T>::new(alloc: maybe<&mut allocator>): list<T> {
|
||
|
|
return list::<T> { _alc = alloc, _hed = null, _tal = null, _len = 0 };
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Creates a copy of the provided list.
|
||
|
|
#! @param other The list to copy.
|
||
|
|
#! @returns The copy, which owns its own nodes and its own elements.
|
||
|
|
#! @remarks Each element is copied by `copyable` where it provides one, so an
|
||
|
|
#! element owning memory is copied rather than shared.
|
||
|
|
pub def list<T>::copy_of(other: &list<T>): list<T> {
|
||
|
|
var made := list::<T>::new(other._alc);
|
||
|
|
var at := other._hed;
|
||
|
|
while at != null {
|
||
|
|
made.push_back(copy::<T>(&(*at).value));
|
||
|
|
at = (*at).next;
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Creates a new list by moving the nodes of another list into it.
|
||
|
|
#! @param other The list to move elements from, which holds none after.
|
||
|
|
#! @returns The moved list, which takes the nodes and allocator with it.
|
||
|
|
#! @remarks Nothing is copied and no element is touched: the nodes move as they
|
||
|
|
#! stand, and the other is left empty rather than dead, so closing it
|
||
|
|
#! afterwards is harmless.
|
||
|
|
pub def list<T>::move_of(other: &mut list<T>): list<T> {
|
||
|
|
val made: list<T> = list {
|
||
|
|
_alc = other._alc,
|
||
|
|
_hed = other._hed,
|
||
|
|
_tal = other._tal,
|
||
|
|
_len = other._len
|
||
|
|
};
|
||
|
|
|
||
|
|
other._hed = null;
|
||
|
|
other._tal = null;
|
||
|
|
other._len = 0;
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Gets the element at the provided index, walking from the front.
|
||
|
|
#! @param this The list to get the element of.
|
||
|
|
#! @param index The index of the element, below `length`, past which a checked
|
||
|
|
#! program stops.
|
||
|
|
#! @returns The element at the given index.
|
||
|
|
pub def list<T>::at(&this, index: u64): &T {
|
||
|
|
if const checking() {
|
||
|
|
if index >= this._len {
|
||
|
|
past_end(index, this._len);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return &(*this.node_at(index)).value;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Gets the element at the provided index, walking from the front.
|
||
|
|
#! @param this The list to get the element of.
|
||
|
|
#! @param index The index of the element, below `length`, past which a checked
|
||
|
|
#! program stops.
|
||
|
|
#! @returns The element at the given index.
|
||
|
|
pub def list<T>::at(&mut this, index: u64): &mut T {
|
||
|
|
if const checking() {
|
||
|
|
if index >= this._len {
|
||
|
|
past_end(index, this._len);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return &mut (*this.node_at(index)).value;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Gets the element at an index, if the list holds one there, for an index
|
||
|
|
#! that may be past the end in a program that is right.
|
||
|
|
#! @param this The list to read.
|
||
|
|
#! @param index The index of the element.
|
||
|
|
#! @returns Maybe the element; otherwise, `nothing`.
|
||
|
|
pub def list<T>::get(&this, index: u64): maybe<&T> {
|
||
|
|
return nothing {} if index >= this._len;
|
||
|
|
return &(*this.node_at(index)).value;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Gets the element at an index to write it, if the list holds one there.
|
||
|
|
#! @param this The list to write.
|
||
|
|
#! @param index The index of the element.
|
||
|
|
#! @returns Maybe the element; otherwise, `nothing`.
|
||
|
|
pub def list<T>::get(&mut this, index: u64): maybe<&mut T> {
|
||
|
|
return nothing {} if index >= this._len;
|
||
|
|
return &mut (*this.node_at(index)).value;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Gets the element at the front of a list.
|
||
|
|
#! @param this The list to get the front of, which stops a checked program
|
||
|
|
#! where it is empty.
|
||
|
|
#! @returns A reference to an immutable element.
|
||
|
|
pub def list<T>::front(&this): &T {
|
||
|
|
if const checking() {
|
||
|
|
if this._len == 0 {
|
||
|
|
fault("The list is empty.");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return &(*this._hed).value;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Gets the element at the front of a list.
|
||
|
|
#! @param this The list to get the front of, which stops a checked program
|
||
|
|
#! where it is empty.
|
||
|
|
#! @returns A reference to a mutable element.
|
||
|
|
pub def list<T>::front(&mut this): &mut T {
|
||
|
|
if const checking() {
|
||
|
|
if this._len == 0 {
|
||
|
|
fault("The list is empty.");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return &mut (*this._hed).value;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Gets the element at the back of a list.
|
||
|
|
#! @param this The list to get the back of, which stops a checked program
|
||
|
|
#! where it is empty.
|
||
|
|
#! @returns A reference to an immutable element.
|
||
|
|
pub def list<T>::back(&this): &T {
|
||
|
|
if const checking() {
|
||
|
|
if this._len == 0 {
|
||
|
|
fault("The list is empty.");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return &(*this._tal).value;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Gets the element at the back of a list.
|
||
|
|
#! @param this The list to get the back of, which stops a checked program
|
||
|
|
#! where it is empty.
|
||
|
|
#! @returns A reference to a mutable element.
|
||
|
|
pub def list<T>::back(&mut this): &mut T {
|
||
|
|
if const checking() {
|
||
|
|
if this._len == 0 {
|
||
|
|
fault("The list is empty.");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return &mut (*this._tal).value;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Pushes a new element onto the front of a list.
|
||
|
|
#! @param this The list to push the value onto.
|
||
|
|
#! @param value The value to push onto the list.
|
||
|
|
#! @remarks An allocator that gives no more memory stops the program.
|
||
|
|
pub def list<T>::push_front(&mut this, value: T): void {
|
||
|
|
val made := this.make(move(value));
|
||
|
|
(*made).next = this._hed;
|
||
|
|
this._hed = made;
|
||
|
|
if this._tal == null {
|
||
|
|
this._tal = made;
|
||
|
|
}
|
||
|
|
this._len++;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Pushes a new element onto the back of a list.
|
||
|
|
#! @param this The list to push the value onto.
|
||
|
|
#! @param value The value to push onto the list.
|
||
|
|
#! @remarks An allocator that gives no more memory stops the program.
|
||
|
|
pub def list<T>::push_back(&mut this, value: T): void {
|
||
|
|
val made := this.make(move(value));
|
||
|
|
if this._tal == null {
|
||
|
|
this._hed = made;
|
||
|
|
} else {
|
||
|
|
(*this._tal).next = made;
|
||
|
|
}
|
||
|
|
this._tal = made;
|
||
|
|
this._len++;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Pops the element off the front of a list.
|
||
|
|
#! @param this The list to pop the value off of, which stops a checked program
|
||
|
|
#! where it is empty.
|
||
|
|
#! @remarks The value is closed where it closes, or holds what does.
|
||
|
|
pub def list<T>::pop_front(&mut this): void {
|
||
|
|
if const checking() {
|
||
|
|
if this._len == 0 {
|
||
|
|
fault("The list is empty.");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
val gone := this._hed;
|
||
|
|
this._hed = (*gone).next;
|
||
|
|
if this._hed == null {
|
||
|
|
this._tal = null;
|
||
|
|
}
|
||
|
|
|
||
|
|
this.release(gone);
|
||
|
|
this._len--;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Adds an element at an index, after the elements before it.
|
||
|
|
#! @param this The list to insert into.
|
||
|
|
#! @param index Where the element goes, from `0` to `length`, where `length`
|
||
|
|
#! appends. An index past that stops a checked program.
|
||
|
|
#! @param value The element to insert.
|
||
|
|
#! @remarks An allocator that gives no more memory stops the program.
|
||
|
|
pub def list<T>::insert_at(&mut this, index: u64, value: T): void {
|
||
|
|
if const checking() {
|
||
|
|
if index > this._len {
|
||
|
|
past_end(index, this._len);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if index == 0 {
|
||
|
|
this.push_front(move(value));
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if index == this._len {
|
||
|
|
this.push_back(move(value));
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
val made := this.make(move(value));
|
||
|
|
val prev := this.node_at(index - 1);
|
||
|
|
(*made).next = (*prev).next;
|
||
|
|
(*prev).next = made;
|
||
|
|
this._len++;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Removes the element at an index.
|
||
|
|
#! @param this The list to remove from.
|
||
|
|
#! @param index The index of the element, below `length`, past which a checked
|
||
|
|
#! program stops.
|
||
|
|
#! @remarks The element is closed where it closes, or holds what does.
|
||
|
|
pub def list<T>::remove_at(&mut this, index: u64): void {
|
||
|
|
if const checking() {
|
||
|
|
if index >= this._len {
|
||
|
|
past_end(index, this._len);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if index == 0 {
|
||
|
|
this.pop_front();
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
this.unlink(this.node_at(index - 1));
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Removes the first element equal to the one given.
|
||
|
|
#! @param this The list to remove from.
|
||
|
|
#! @param elem The element to look for.
|
||
|
|
#! @returns Whether an element equalled it and was removed.
|
||
|
|
#! @remarks The element is closed where it closes, or holds what does.
|
||
|
|
pub def list<T>::remove(&mut this, elem: &T): bool {
|
||
|
|
if this._len == 0 {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
if equals::<T>(&(*this._hed).value, elem) {
|
||
|
|
this.pop_front();
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
var prev := this._hed;
|
||
|
|
while (*prev).next != null {
|
||
|
|
if equals::<T>(&(*(*prev).next).value, elem) {
|
||
|
|
this.unlink(prev);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
prev = (*prev).next;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Tells whether the list holds an element equal to the one given.
|
||
|
|
#! @param this The list to search.
|
||
|
|
#! @param elem The element to look for.
|
||
|
|
#! @returns Whether any element equals it.
|
||
|
|
pub def list<T>::contains(&this, elem: &T): bool {
|
||
|
|
var at := this._hed;
|
||
|
|
while at != null {
|
||
|
|
return true if equals::<T>(&(*at).value, elem);
|
||
|
|
at = (*at).next;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Removes all elements from a list, disposing their nodes.
|
||
|
|
#! @param this The list to clear.
|
||
|
|
#! @remarks Elements that close, or hold what does, are closed.
|
||
|
|
pub def list<T>::clear(&mut this): void {
|
||
|
|
var at := this._hed;
|
||
|
|
while at != null {
|
||
|
|
val next := (*at).next;
|
||
|
|
this.release(at);
|
||
|
|
at = next;
|
||
|
|
}
|
||
|
|
|
||
|
|
this._hed = null;
|
||
|
|
this._tal = null;
|
||
|
|
this._len = 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Removes all elements of a list, disposing their nodes.
|
||
|
|
#! @param this The list to close.
|
||
|
|
pub def list<T>::close(&mut this): void {
|
||
|
|
this.clear();
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Compares one list to another by equality.
|
||
|
|
#! @param this The list to compare against.
|
||
|
|
#! @param other The list to compare with.
|
||
|
|
#! @returns Whether both hold equal elements, in the same order.
|
||
|
|
pub def list<T>::equals(&this, other: &list<T>): bool {
|
||
|
|
if this._len != other._len {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
var mine := this._hed;
|
||
|
|
var theirs := other._hed;
|
||
|
|
while mine != null {
|
||
|
|
return false if !equals::<T>(&(*mine).value, &(*theirs).value);
|
||
|
|
mine = (*mine).next;
|
||
|
|
theirs = (*theirs).next;
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Lends a cursor that reads the elements from the front of the list, which
|
||
|
|
#! `for x in l` walks.
|
||
|
|
#! @param this The list to walk.
|
||
|
|
#! @returns The cursor, which is only good until the list changes or closes.
|
||
|
|
pub def list<T>::iter(&this): list<T>::cursor {
|
||
|
|
return list::<T>::cursor { _at = this._hed, _left = this._len };
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Lends a cursor that writes the elements where they are, from the front of the
|
||
|
|
#! list, which `for &mut x in l` walks.
|
||
|
|
#! @param this The list to walk.
|
||
|
|
#! @returns The cursor, which is only good until the list changes or closes.
|
||
|
|
pub def list<T>::iter(&mut this): list<T>::mut_cursor {
|
||
|
|
return list::<T>::mut_cursor { _at = this._hed, _left = this._len };
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Hashes the provided list.
|
||
|
|
#! @param this The list to hash.
|
||
|
|
#! @returns The hashed value of the list.
|
||
|
|
#! @remarks Elements are hashed by their `hash`, so they are `hashable`.
|
||
|
|
pub def list<T>::hash(&this): u64 {
|
||
|
|
var code := this._len;
|
||
|
|
var at := this._hed;
|
||
|
|
while at != null {
|
||
|
|
code = mix(code, hash::<T>(&(*at).value));
|
||
|
|
at = (*at).next;
|
||
|
|
}
|
||
|
|
return code;
|
||
|
|
}
|
||
|
|
|
||
|
|
# The node at an index, which is below the length.
|
||
|
|
def list<T>::node_at(&this, index: u64): *mut list<T>::node {
|
||
|
|
var at := this._hed;
|
||
|
|
for _ in 0..index {
|
||
|
|
at = (*at).next;
|
||
|
|
}
|
||
|
|
return at;
|
||
|
|
}
|
||
|
|
|
||
|
|
# A node holding the value, from the list's allocator, which stops the program
|
||
|
|
# where it gives no memory, as `acquire` does.
|
||
|
|
def list<T>::make(&mut this, value: T): *mut list<T>::node {
|
||
|
|
val made := acquire::<list<T>::node>(this._alc, 1);
|
||
|
|
(*made).value = move(value);
|
||
|
|
(*made).next = null;
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
def list<T>::unlink(&mut this, prev: *mut list<T>::node): void {
|
||
|
|
val gone := (*prev).next;
|
||
|
|
(*prev).next = (*gone).next;
|
||
|
|
if gone == this._tal {
|
||
|
|
this._tal = prev;
|
||
|
|
}
|
||
|
|
|
||
|
|
this.release(gone);
|
||
|
|
this._len--;
|
||
|
|
}
|
||
|
|
|
||
|
|
def list<T>::release(&mut this, node: *mut list<T>::node): void {
|
||
|
|
close(&mut (*node).value);
|
||
|
|
dispose::<list<T>::node>(this._alc, node, 1);
|
||
|
|
}
|