Initial Commit
This commit is contained in:
1625 files changed
+43592
No files matched your search
@@ -0,0 +1,394 @@
|
||||
#! @file stack.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Defines an implementation for a `stack` container. Stacks are a structured
|
||||
#! container where data is pushed and popped from the memory region in a last-
|
||||
#! in-first-out (LIFO) manner.
|
||||
module std::containers;
|
||||
|
||||
|
||||
#! A `stack` is a structured data container that stores its content in a last-
|
||||
#! in-first-out (LIFO) manner.
|
||||
#!
|
||||
#! They are useful in situations where data needs to be tracked in that exact
|
||||
#! LIFO manner. Situations like that often include things like lexers, parsers,
|
||||
#! GUI layers, etc.
|
||||
#! @tparam T The type of its elements.
|
||||
pub struct stack<T>
|
||||
: owning
|
||||
, hashable
|
||||
, sliceable<T>
|
||||
, spannable<T>
|
||||
, iterable<T>
|
||||
, copyable
|
||||
, moveable
|
||||
, equatable
|
||||
{
|
||||
var _alc: maybe<&mut allocator>;
|
||||
var _dat: *mut T;
|
||||
var _len: u64;
|
||||
var _cap: u64;
|
||||
}
|
||||
|
||||
#! Provides read only access to the length of a stack.
|
||||
#! @param this The stack to get the length of.
|
||||
#! @returns The length of the stack.
|
||||
pub val stack<T>::length: u64 {
|
||||
get => this._len;
|
||||
}
|
||||
|
||||
#! Provides read only access to the capacity of a stack.
|
||||
#! @param this The stack to get the capacity of.
|
||||
#! @returns The capacity of the stack.
|
||||
pub val stack<T>::capacity: u64 {
|
||||
get => this._cap;
|
||||
}
|
||||
|
||||
#! Indicates if a stack is empty.
|
||||
#! @param this The stack to check.
|
||||
#! @returns Whether the stack is empty.
|
||||
pub val stack<T>::empty: bool {
|
||||
get => this._len == 0;
|
||||
}
|
||||
|
||||
#! Indicates if a stack is full, so the next push makes it grow.
|
||||
#! @param this The stack to check.
|
||||
#! @returns Whether the stack is full.
|
||||
pub val stack<T>::full: bool {
|
||||
get => this._len == this._cap;
|
||||
}
|
||||
|
||||
#! A view of the elements, from the bottom of the stack to its top.
|
||||
#! @param this The stack to view.
|
||||
#! @returns A span of its elements, which is only good until the stack grows,
|
||||
#! shrinks, or closes.
|
||||
pub val stack<T>::span: span<T> {
|
||||
get => stack::<T>::span_of(this);
|
||||
}
|
||||
|
||||
#! A writable view of the elements, read of a stack that can be written.
|
||||
#! @param this The stack to view.
|
||||
#! @returns A slice of its elements, which is only good until the stack grows,
|
||||
#! shrinks, or closes.
|
||||
pub val stack<T>::slice: slice<T> {
|
||||
get mut => stack::<T>::slice_of(this);
|
||||
}
|
||||
|
||||
#! The element at the bottom of the stack, which a stack that can be written
|
||||
#! writes through.
|
||||
#! @param this The stack to read, which holds an element, or else a checked
|
||||
#! program stops.
|
||||
#! @returns The element, which is only good until the stack grows, shrinks, or
|
||||
#! closes.
|
||||
pub val stack<T>::first: &T {
|
||||
get => &this.span[0];
|
||||
get mut => &mut this.slice[0];
|
||||
}
|
||||
|
||||
#! The element on top of the stack, as `peek` reads it, which a stack that can
|
||||
#! be written writes through.
|
||||
#! @param this The stack to read, which holds an element, or else a checked
|
||||
#! program stops.
|
||||
#! @returns The element, which is only good until the stack grows, shrinks, or
|
||||
#! closes.
|
||||
pub val stack<T>::last: &T {
|
||||
get => &this.span[this._len - 1];
|
||||
get mut => &mut this.slice[this._len - 1];
|
||||
}
|
||||
|
||||
#! Creates a new stack with no allocator, which takes memory once something is
|
||||
#! added.
|
||||
#! @returns The created stack container.
|
||||
#! @remarks Will use the built in `raw_allocate`, `raw_release` and `raw_resize`
|
||||
#! for memory management.
|
||||
pub def stack<T>::new(): stack<T> {
|
||||
return stack::<T>::new(nothing{});
|
||||
}
|
||||
|
||||
#! Creates a new stack with an optional allocator, which takes memory once
|
||||
#! something is added.
|
||||
#! @param alloc The optional allocator for the memory backing of the stack.
|
||||
#! @returns The created stack container.
|
||||
pub def stack<T>::new(alloc: maybe<&mut allocator>): stack<T> {
|
||||
return stack::<T> { _alc = alloc, _dat = null, _len = 0, _cap = 0 };
|
||||
}
|
||||
|
||||
#! Creates a new stack with an optional allocator and a provided size.
|
||||
#! @param size The initial capacity of the stack.
|
||||
#! @param alloc The optional allocator for the memory backing of the stack.
|
||||
#! @returns The created stack container.
|
||||
pub def stack<T>::new(size: u64, alloc: maybe<&mut allocator>): stack<T> {
|
||||
var made := stack::<T>::new(alloc);
|
||||
made.reserve(size);
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Creates a copy of the provided stack.
|
||||
#! @param other The stack to copy.
|
||||
#! @returns The created stack container.
|
||||
pub def stack<T>::copy_of(other: &stack<T>): stack<T> {
|
||||
var made := stack::<T>::new(other._len, other._alc);
|
||||
for index in 0..other._len {
|
||||
made.push(copy::<T>(&other._dat[index]));
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Creates a new stack by moving the data of another stack into it.
|
||||
#! @param other The stack to move elements from, which holds none after.
|
||||
#! @returns The moved stack, which takes the memory and allocator with it.
|
||||
#! @remarks Nothing is copied and no element is touched: the memory moves as it
|
||||
#! stands, and the other is left empty rather than dead, so closing it
|
||||
#! afterwards is harmless.
|
||||
pub def stack<T>::move_of(other: &mut stack<T>): stack<T> {
|
||||
val made: stack<T> = stack {
|
||||
_alc = other._alc,
|
||||
_dat = other._dat,
|
||||
_len = other._len,
|
||||
_cap = other._cap
|
||||
};
|
||||
|
||||
other._dat = null;
|
||||
other._len = 0;
|
||||
other._cap = 0;
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Creates a slice of the data of a stack container, as `sliceable` asks.
|
||||
#! @param other The stack container to slice, which can be written.
|
||||
#! @returns A slice of the elements of the provided stack container.
|
||||
pub def stack<T>::slice_of(other: &mut stack<T>): slice<T> {
|
||||
return slice::<T>::over(other._dat, other._len);
|
||||
}
|
||||
|
||||
#! Creates a view of the data of a stack container, as `spannable` asks.
|
||||
#! @param other The stack container to view.
|
||||
#! @returns A span of the elements of the provided stack container.
|
||||
pub def stack<T>::span_of(other: &stack<T>): span<T> {
|
||||
return span::<T>::over(other._dat, other._len);
|
||||
}
|
||||
|
||||
#! Pushes a new element onto a stack.
|
||||
#! @param this The stack to push the value onto.
|
||||
#! @param value The value to push onto a stack.
|
||||
#! @remarks An allocator that gives no more memory stops the program.
|
||||
pub def stack<T>::push(&mut this, value: T): void {
|
||||
if this.full {
|
||||
this.grow();
|
||||
}
|
||||
this._dat[this._len++] = move(value);
|
||||
}
|
||||
|
||||
#! Pops an old value off of a stack.
|
||||
#! @param this The stack 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 stack<T>::pop(&mut this): void {
|
||||
if const checking() {
|
||||
if this._len == 0 {
|
||||
fault("The stack is empty.");
|
||||
}
|
||||
}
|
||||
close(&mut this._dat[this._len - 1]);
|
||||
|
||||
# The slot is emptied where pushing would close what it held.
|
||||
if const T is closeable {
|
||||
memset((this._dat + (this._len - 1)) as *mut u8, 0, bytes_of::<T>(1));
|
||||
}
|
||||
this._len--;
|
||||
}
|
||||
|
||||
#! Peeks the element on top of a stack without removing it.
|
||||
#! @param this The stack to peek the top of, which stops a checked program
|
||||
#! where it is empty.
|
||||
#! @returns A reference to an immutable element.
|
||||
pub def stack<T>::peek(&this): &T {
|
||||
if const checking() {
|
||||
if this._len == 0 {
|
||||
fault("The stack is empty.");
|
||||
}
|
||||
}
|
||||
return &this._dat[this._len - 1];
|
||||
}
|
||||
|
||||
#! Peeks the element on top of a stack without removing it.
|
||||
#! @param this The stack to peek the top of, which stops a checked program
|
||||
#! where it is empty.
|
||||
#! @returns A reference to a mutable element.
|
||||
pub def stack<T>::peek(&mut this): &mut T {
|
||||
if const checking() {
|
||||
if this._len == 0 {
|
||||
fault("The stack is empty.");
|
||||
}
|
||||
}
|
||||
return &mut this._dat[this._len - 1];
|
||||
}
|
||||
|
||||
#! Reserves an amount of memory for the number of elements requested for the
|
||||
#! backing memory of the stack provided.
|
||||
#! @param this The stack to reserve elements for.
|
||||
#! @param count The number of elements to reserve for a stack.
|
||||
#! @remarks A stack with room for that many already is left as it is, so this
|
||||
#! never shrinks it.
|
||||
pub def stack<T>::reserve(&mut this, count: u64): void {
|
||||
this.grow(count);
|
||||
}
|
||||
|
||||
#! Reserves an amount of memory for the number of elements requested for the
|
||||
#! backing memory of the stack provided, then makes those new elements
|
||||
#! available.
|
||||
#! @param this The stack to resize.
|
||||
#! @param count The number of elements to resize a stack to.
|
||||
#! @remarks New elements are zeroed. Elements past a smaller count are removed,
|
||||
#! and closed where they close, or hold what does. An allocator that
|
||||
#! gives no more memory stops the program.
|
||||
pub def stack<T>::resize(&mut this, count: u64): void {
|
||||
# Growing doubles at least, so resizing a little at a time copies little.
|
||||
if count > this._cap {
|
||||
this.grow(count > this._cap * 2 ? count : this._cap * 2);
|
||||
}
|
||||
|
||||
if count < this._len {
|
||||
if const T is closeable {
|
||||
for index in count..this._len {
|
||||
close(&mut this._dat[index]);
|
||||
}
|
||||
}
|
||||
|
||||
# The slots past the length are kept empty where pushing would close them.
|
||||
if const T is closeable {
|
||||
memset((this._dat + count) as *mut u8, 0, bytes_of::<T>(this._len - count));
|
||||
}
|
||||
}
|
||||
else {
|
||||
memset((this._dat + this._len) as *mut u8, 0, bytes_of::<T>(count - this._len));
|
||||
}
|
||||
this._len = count;
|
||||
}
|
||||
|
||||
#! Removes all elements from a stack.
|
||||
#! @param this The stack to clear.
|
||||
#! @remarks Elements that close, or hold what does, are closed. Once all are
|
||||
#! closed, the backing data will be cleared but not disposed.
|
||||
pub def stack<T>::clear(&mut this): void {
|
||||
if const T is closeable {
|
||||
for index in 0..this._len {
|
||||
close(&mut this._dat[index]);
|
||||
}
|
||||
}
|
||||
|
||||
if const T is closeable {
|
||||
memset(this._dat as *mut u8, 0, bytes_of::<T>(this._len));
|
||||
}
|
||||
this._len = 0;
|
||||
}
|
||||
|
||||
#! Removes all elements of a stack then disposes the backing memory.
|
||||
#! @param this The stack to close.
|
||||
pub def stack<T>::close(&mut this): void {
|
||||
if const T is closeable {
|
||||
for index in 0..this._len {
|
||||
close(&mut this._dat[index]);
|
||||
}
|
||||
}
|
||||
|
||||
dispose::<T>(this._alc, this._dat, this._cap);
|
||||
this._dat = null;
|
||||
this._len = 0;
|
||||
this._cap = 0;
|
||||
}
|
||||
|
||||
#! Compares one stack to another by equality.
|
||||
#! @param this The stack to compare against.
|
||||
#! @param other The stack to compare with.
|
||||
#! @returns Whether the stacks are equal by value.
|
||||
pub def stack<T>::equals(&this, other: &stack<T>): bool {
|
||||
if this._len != other._len {
|
||||
return false;
|
||||
}
|
||||
|
||||
for index in 0..this._len {
|
||||
return false if !equals::<T>(
|
||||
&this._dat[index],
|
||||
&other._dat[index]
|
||||
);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#! What `iter(&this)` lends, which reads the elements from the bottom of the
|
||||
#! stack.
|
||||
pub type stack<T>::iterator = span<T>::iterator;
|
||||
|
||||
#! What `iter(&mut this)` lends, which writes the elements where the stack holds
|
||||
#! them.
|
||||
pub type stack<T>::mutator = slice<T>::mutator;
|
||||
|
||||
#! Lends an iterator that reads the elements from the bottom of the stack to
|
||||
#! its top, which `for x in s` walks.
|
||||
#! @param this The stack to walk.
|
||||
#! @returns The iterator, which is only good until the stack grows, shrinks, or
|
||||
#! closes.
|
||||
pub def stack<T>::iter(&this): span<T>::iterator {
|
||||
return this.span.iter();
|
||||
}
|
||||
|
||||
#! Lends a mutator that writes the elements where they are, from the bottom of
|
||||
#! the stack to its top, which `for &mut x in s` walks.
|
||||
#! @param this The stack to walk.
|
||||
#! @returns The mutator, which is only good until the stack grows, shrinks, or
|
||||
#! closes.
|
||||
pub def stack<T>::iter(&mut this): slice<T>::mutator {
|
||||
var whole := this.slice;
|
||||
return whole.iter();
|
||||
}
|
||||
|
||||
#! An iterator that reads the elements from the bottom of the stack to its top,
|
||||
#! as `iter` lends.
|
||||
#! @param this The stack to walk.
|
||||
#! @returns The iterator, which is only good until the stack grows, shrinks, or
|
||||
#! closes.
|
||||
pub val stack<T>::iterator: span<T>::iterator {
|
||||
get => this.iter();
|
||||
}
|
||||
|
||||
#! A mutator that writes the elements where they are, read of a stack that can
|
||||
#! be written.
|
||||
#! @param this The stack to walk.
|
||||
#! @returns The mutator, which is only good until the stack grows, shrinks, or
|
||||
#! closes.
|
||||
pub val stack<T>::mutator: slice<T>::mutator {
|
||||
get mut => this.iter();
|
||||
}
|
||||
|
||||
#! Hashes the provided stack.
|
||||
#! @param this The stack to hash.
|
||||
#! @returns The hashed value of the stack.
|
||||
#! @remarks Elements are hashed by their `hash`, so they are `hashable`.
|
||||
pub def stack<T>::hash(&this): u64 {
|
||||
var code := this._len;
|
||||
for index in 0..this._len {
|
||||
code = mix(code, hash(this._dat[index]));
|
||||
}
|
||||
return code;
|
||||
}
|
||||
|
||||
# Gives the stack room for more elements, doubling it.
|
||||
def stack<T>::grow(&mut this): void {
|
||||
this.grow(this._cap == 0 ? 4 : this._cap * 2);
|
||||
}
|
||||
|
||||
# Gives the stack room for a number of elements, where it has less. An allocator
|
||||
# that gives no memory stops the program, as `acquire` and `resize` do.
|
||||
def stack<T>::grow(&mut this, size: u64): void {
|
||||
if size <= this._cap {
|
||||
return;
|
||||
}
|
||||
|
||||
this._dat = this._dat == null
|
||||
? acquire::<T>(this._alc, size)
|
||||
: resize::<T>(this._alc, this._dat, this._cap, size);
|
||||
this._cap = size;
|
||||
}
|
||||
Reference in new issue
Block a user