Initial Commit
This commit is contained in:
1625 files changed
+43618
No files matched your search
@@ -0,0 +1,544 @@
|
||||
#! @file chain.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Defines an implementation for a `chain` container. Chains are a structured
|
||||
#! container that stores each element in a node of its own, linked to the nodes
|
||||
#! before and after it.
|
||||
module std::containers;
|
||||
|
||||
|
||||
#! A `chain` is a structured data container, doubly linked, that stores each of
|
||||
#! its elements in a node of its own.
|
||||
#!
|
||||
#! They are useful where elements are added and removed at either end, 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 chain from
|
||||
#! whichever end is nearer.
|
||||
#! @tparam T The type of its elements.
|
||||
pub struct chain<T>
|
||||
: owning
|
||||
, hashable
|
||||
, indexable<u64, T>
|
||||
, assignable<u64, T>
|
||||
, iterable<T>
|
||||
, copyable
|
||||
, moveable
|
||||
, equatable
|
||||
{
|
||||
var _alc: maybe<&mut allocator>;
|
||||
var _hed: *mut chain<T>::node;
|
||||
var _tal: *mut chain<T>::node;
|
||||
var _len: u64;
|
||||
}
|
||||
|
||||
#! An element of a chain, with the nodes before and after it.
|
||||
struct chain<T>::node {
|
||||
var value: T;
|
||||
var prev: *mut chain<T>::node;
|
||||
var next: *mut chain<T>::node;
|
||||
}
|
||||
|
||||
#! Walks the elements of a chain 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 chain<T>::iterator : sized_iterator<&T>, backward_iterator<&T> {
|
||||
var _at: *mut chain<T>::node;
|
||||
var _end: *mut chain<T>::node;
|
||||
var _left: u64;
|
||||
}
|
||||
|
||||
#! Gives the next element of the chain.
|
||||
#! @param this The iterator to advance.
|
||||
#! @returns Maybe a reference to the next element; otherwise, `nothing`.
|
||||
pub def chain<T>::iterator::next(&mut this): maybe<&T> {
|
||||
if this._left == 0 {
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
val here := this._at;
|
||||
this._at = (*here).next;
|
||||
this._left = this._left - 1;
|
||||
return &(*here).value;
|
||||
}
|
||||
|
||||
#! How many elements the iterator has left to give.
|
||||
#! @param this The iterator.
|
||||
#! @returns How many.
|
||||
pub def chain<T>::iterator::remaining(&this): u64 {
|
||||
return this._left;
|
||||
}
|
||||
|
||||
#! Gives the last element the iterator has left, from the back of the chain.
|
||||
#! @param this The iterator to advance from its far end.
|
||||
#! @returns Maybe a reference to the element; otherwise, `nothing`.
|
||||
pub def chain<T>::iterator::next_back(&mut this): maybe<&T> {
|
||||
if this._left == 0 {
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
val here := this._end;
|
||||
this._end = (*here).prev;
|
||||
this._left = this._left - 1;
|
||||
return &(*here).value;
|
||||
}
|
||||
|
||||
#! Walks the elements of a chain 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 chain<T>::mutator : sized_iterator<&mut T>, backward_iterator<&mut T> {
|
||||
var _at: *mut chain<T>::node;
|
||||
var _end: *mut chain<T>::node;
|
||||
var _left: u64;
|
||||
}
|
||||
|
||||
#! Gives the next element of the chain, to be written.
|
||||
#! @param this The mutator to advance.
|
||||
#! @returns Maybe a reference to the next element; otherwise, `nothing`.
|
||||
pub def chain<T>::mutator::next(&mut this): maybe<&mut T> {
|
||||
if this._left == 0 {
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
val here := this._at;
|
||||
this._at = (*here).next;
|
||||
this._left = this._left - 1;
|
||||
return &mut (*here).value;
|
||||
}
|
||||
|
||||
#! How many elements the mutator has left to give.
|
||||
#! @param this The mutator.
|
||||
#! @returns How many.
|
||||
pub def chain<T>::mutator::remaining(&this): u64 {
|
||||
return this._left;
|
||||
}
|
||||
|
||||
#! Gives the last element the mutator has left, from the back of the chain, to
|
||||
#! be written.
|
||||
#! @param this The mutator to advance from its far end.
|
||||
#! @returns Maybe a reference to the element; otherwise, `nothing`.
|
||||
pub def chain<T>::mutator::next_back(&mut this): maybe<&mut T> {
|
||||
if this._left == 0 {
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
val here := this._end;
|
||||
this._end = (*here).prev;
|
||||
this._left = this._left - 1;
|
||||
return &mut (*here).value;
|
||||
}
|
||||
|
||||
#! Provides read only access to the length of a chain.
|
||||
#! @param this The chain to get the length of.
|
||||
#! @returns The length of the chain.
|
||||
pub val chain<T>::length: u64 {
|
||||
get => this._len;
|
||||
}
|
||||
|
||||
#! Indicates if a chain is empty.
|
||||
#! @param this The chain to check.
|
||||
#! @returns Whether the chain is empty.
|
||||
pub val chain<T>::empty: bool {
|
||||
get => this._len == 0;
|
||||
}
|
||||
|
||||
#! Creates a new chain with no allocator.
|
||||
#! @returns The created chain container.
|
||||
#! @remarks Will use the built in `raw_allocate` and `raw_release` for memory
|
||||
#! management.
|
||||
pub def chain<T>::new(): chain<T> {
|
||||
return chain::<T>::new(nothing{});
|
||||
}
|
||||
|
||||
#! Creates a new chain with an optional allocator.
|
||||
#! @param alloc The optional allocator for the nodes of the chain.
|
||||
#! @returns The created chain container, which holds no elements yet.
|
||||
pub def chain<T>::new(alloc: maybe<&mut allocator>): chain<T> {
|
||||
return chain::<T> { _alc = alloc, _hed = null, _tal = null, _len = 0 };
|
||||
}
|
||||
|
||||
#! Creates a copy of the provided chain.
|
||||
#! @param other The chain 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 chain<T>::copy_of(other: &chain<T>): chain<T> {
|
||||
var made := chain::<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 chain by moving the nodes of another chain into it.
|
||||
#! @param other The chain to move elements from, which holds none after.
|
||||
#! @returns The moved chain, 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 chain<T>::move_of(other: &mut chain<T>): chain<T> {
|
||||
val made: chain<T> = chain {
|
||||
_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 nearer end.
|
||||
#! @param this The chain 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 chain<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 nearer end.
|
||||
#! @param this The chain 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 chain<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 chain holds one there, for an index
|
||||
#! that may be past the end in a program that is right.
|
||||
#! @param this The chain to read.
|
||||
#! @param index The index of the element.
|
||||
#! @returns Maybe the element; otherwise, `nothing`.
|
||||
pub def chain<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 chain holds one there.
|
||||
#! @param this The chain to write.
|
||||
#! @param index The index of the element.
|
||||
#! @returns Maybe the element; otherwise, `nothing`.
|
||||
pub def chain<T>::get(&mut this, index: u64): maybe<&mut T> {
|
||||
return nothing {} if index >= this._len;
|
||||
return &mut (*this.node_at(index)).value;
|
||||
}
|
||||
|
||||
#! The element at the front of a chain, which a chain that can be written
|
||||
#! writes through.
|
||||
#! @param this The chain to read, which stops a checked program where it is
|
||||
#! empty.
|
||||
#! @returns A reference to the element.
|
||||
pub val chain<T>::first: &T {
|
||||
get {
|
||||
if const checking() {
|
||||
if this._len == 0 {
|
||||
fault("The chain is empty.");
|
||||
}
|
||||
}
|
||||
return &(*this._hed).value;
|
||||
}
|
||||
get mut {
|
||||
if const checking() {
|
||||
if this._len == 0 {
|
||||
fault("The chain is empty.");
|
||||
}
|
||||
}
|
||||
return &mut (*this._hed).value;
|
||||
}
|
||||
}
|
||||
|
||||
#! The element at the back of a chain, which a chain that can be written writes
|
||||
#! through.
|
||||
#! @param this The chain to read, which stops a checked program where it is
|
||||
#! empty.
|
||||
#! @returns A reference to the element.
|
||||
pub val chain<T>::last: &T {
|
||||
get {
|
||||
if const checking() {
|
||||
if this._len == 0 {
|
||||
fault("The chain is empty.");
|
||||
}
|
||||
}
|
||||
return &(*this._tal).value;
|
||||
}
|
||||
get mut {
|
||||
if const checking() {
|
||||
if this._len == 0 {
|
||||
fault("The chain is empty.");
|
||||
}
|
||||
}
|
||||
return &mut (*this._tal).value;
|
||||
}
|
||||
}
|
||||
|
||||
#! Pushes a new element onto the front of a chain.
|
||||
#! @param this The chain to push the value onto.
|
||||
#! @param value The value to push onto the chain.
|
||||
#! @remarks An allocator that gives no more memory stops the program.
|
||||
pub def chain<T>::push_front(&mut this, value: T): void {
|
||||
this.link(null, this._hed, move(value));
|
||||
}
|
||||
|
||||
#! Pushes a new element onto the back of a chain.
|
||||
#! @param this The chain to push the value onto.
|
||||
#! @param value The value to push onto the chain.
|
||||
#! @remarks An allocator that gives no more memory stops the program.
|
||||
pub def chain<T>::push_back(&mut this, value: T): void {
|
||||
this.link(this._tal, null, move(value));
|
||||
}
|
||||
|
||||
#! Pops the element off the front of a chain.
|
||||
#! @param this The chain 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 chain<T>::pop_front(&mut this): void {
|
||||
if const checking() {
|
||||
if this._len == 0 {
|
||||
fault("The chain is empty.");
|
||||
}
|
||||
}
|
||||
this.unlink(this._hed);
|
||||
}
|
||||
|
||||
#! Pops the element off the back of a chain.
|
||||
#! @param this The chain 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 chain<T>::pop_back(&mut this): void {
|
||||
if const checking() {
|
||||
if this._len == 0 {
|
||||
fault("The chain is empty.");
|
||||
}
|
||||
}
|
||||
this.unlink(this._tal);
|
||||
}
|
||||
|
||||
#! Adds an element at an index, after the elements before it.
|
||||
#! @param this The chain 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 chain<T>::insert_at(&mut this, index: u64, value: T): void {
|
||||
if const checking() {
|
||||
if index > this._len {
|
||||
past_end(index, this._len);
|
||||
}
|
||||
}
|
||||
if index == this._len {
|
||||
this.push_back(move(value));
|
||||
return;
|
||||
}
|
||||
|
||||
val next := this.node_at(index);
|
||||
this.link((*next).prev, next, move(value));
|
||||
}
|
||||
|
||||
#! Removes the element at an index.
|
||||
#! @param this The chain 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 chain<T>::remove_at(&mut this, index: u64): void {
|
||||
if const checking() {
|
||||
if index >= this._len {
|
||||
past_end(index, this._len);
|
||||
}
|
||||
}
|
||||
this.unlink(this.node_at(index));
|
||||
}
|
||||
|
||||
#! Removes the first element equal to the one given.
|
||||
#! @param this The chain 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 chain<T>::remove(&mut this, elem: &T): bool {
|
||||
var at := this._hed;
|
||||
while at != null {
|
||||
if equals::<T>(&(*at).value, elem) {
|
||||
this.unlink(at);
|
||||
return true;
|
||||
}
|
||||
at = (*at).next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#! Tells whether the chain holds an element equal to the one given.
|
||||
#! @param this The chain to search.
|
||||
#! @param elem The element to look for.
|
||||
#! @returns Whether any element equals it.
|
||||
pub def chain<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 chain, disposing their nodes.
|
||||
#! @param this The chain to clear.
|
||||
#! @remarks Elements that close, or hold what does, are closed.
|
||||
pub def chain<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 chain, disposing their nodes.
|
||||
#! @param this The chain to close.
|
||||
pub def chain<T>::close(&mut this): void {
|
||||
this.clear();
|
||||
}
|
||||
|
||||
#! Compares one chain to another by equality.
|
||||
#! @param this The chain to compare against.
|
||||
#! @param other The chain to compare with.
|
||||
#! @returns Whether both hold equal elements, in the same order.
|
||||
pub def chain<T>::equals(&this, other: &chain<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 an iterator that reads the elements from the front of the chain, which
|
||||
#! `for x in c` walks.
|
||||
#! @param this The chain to walk.
|
||||
#! @returns The iterator, which is only good until the chain changes or closes.
|
||||
pub def chain<T>::iter(&this): chain<T>::iterator {
|
||||
return chain::<T>::iterator { _at = this._hed, _end = this._tal, _left = this._len };
|
||||
}
|
||||
|
||||
#! Lends a mutator that writes the elements where they are, from the front of
|
||||
#! the chain, which `for &mut x in c` walks.
|
||||
#! @param this The chain to walk.
|
||||
#! @returns The mutator, which is only good until the chain changes or closes.
|
||||
pub def chain<T>::iter(&mut this): chain<T>::mutator {
|
||||
return chain::<T>::mutator { _at = this._hed, _end = this._tal, _left = this._len };
|
||||
}
|
||||
|
||||
#! An iterator that reads the elements from the front of the chain, as `iter`
|
||||
#! lends.
|
||||
#! @param this The chain to walk.
|
||||
#! @returns The iterator, which is only good until the chain changes or closes.
|
||||
pub val chain<T>::iterator: chain<T>::iterator {
|
||||
get => this.iter();
|
||||
}
|
||||
|
||||
#! A mutator that writes the elements where they are, read of a chain that can
|
||||
#! be written.
|
||||
#! @param this The chain to walk.
|
||||
#! @returns The mutator, which is only good until the chain changes or closes.
|
||||
pub val chain<T>::mutator: chain<T>::mutator {
|
||||
get mut => this.iter();
|
||||
}
|
||||
|
||||
#! Hashes the provided chain.
|
||||
#! @param this The chain to hash.
|
||||
#! @returns The hashed value of the chain.
|
||||
#! @remarks Elements are hashed by their `hash`, so they are `hashable`.
|
||||
pub def chain<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, walked to from the nearer end.
|
||||
def chain<T>::node_at(&this, index: u64): *mut chain<T>::node {
|
||||
if index < this._len / 2 {
|
||||
var at := this._hed;
|
||||
for _ in 0..index {
|
||||
at = (*at).next;
|
||||
}
|
||||
return at;
|
||||
}
|
||||
|
||||
var at := this._tal;
|
||||
for _ in 0..(this._len - 1 - index) {
|
||||
at = (*at).prev;
|
||||
}
|
||||
return at;
|
||||
}
|
||||
|
||||
def chain<T>::link(
|
||||
&mut this,
|
||||
prev: *mut chain<T>::node,
|
||||
next: *mut chain<T>::node,
|
||||
value: T
|
||||
): void {
|
||||
# An allocator that gives no memory stops the program, as `acquire` does.
|
||||
val made := acquire::<chain<T>::node>(this._alc, 1);
|
||||
(*made).value = move(value);
|
||||
(*made).prev = prev;
|
||||
(*made).next = next;
|
||||
if prev == null {
|
||||
this._hed = made;
|
||||
} else {
|
||||
(*prev).next = made;
|
||||
}
|
||||
if next == null {
|
||||
this._tal = made;
|
||||
} else {
|
||||
(*next).prev = made;
|
||||
}
|
||||
this._len++;
|
||||
}
|
||||
|
||||
def chain<T>::unlink(&mut this, node: *mut chain<T>::node): void {
|
||||
if (*node).prev == null {
|
||||
this._hed = (*node).next;
|
||||
} else {
|
||||
(*(*node).prev).next = (*node).next;
|
||||
}
|
||||
if (*node).next == null {
|
||||
this._tal = (*node).prev;
|
||||
} else {
|
||||
(*(*node).next).prev = (*node).prev;
|
||||
}
|
||||
|
||||
this.release(node);
|
||||
this._len--;
|
||||
}
|
||||
|
||||
def chain<T>::release(&mut this, node: *mut chain<T>::node): void {
|
||||
close(&mut (*node).value);
|
||||
dispose::<chain<T>::node>(this._alc, node, 1);
|
||||
}
|
||||
Reference in new issue
Block a user