#! @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 : owning , hashable , sliceable , viewable , iterable , 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::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::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::is_empty: bool { get => this._len == 0; } #! Indicates if a stack is full. #! @param this The stack to check. #! @returns Whether the stack is full. pub val stack::is_full: bool { get => this._len == this._cap; } #! 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::new(): stack { return stack::::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::new(alloc: maybe<&mut allocator>): stack { return stack:: { _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::new(size: u64, alloc: maybe<&mut allocator>): stack { var made := stack::::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::copy_of(other: &stack): stack { var made := stack::::new(other._len, other._alc); for index in 0..other._len { made.push(copy::(&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::move_of(other: &mut stack): stack { val made: stack = 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. #! @param this The stack container to slice. #! @returns A slice of the elements of the provided stack container. pub def stack::slice(&mut this): slice { return slice::::over(this._dat, this._len); } #! 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::slice_of(other: &mut stack): slice { return slice::::over(other._dat, other._len); } #! Creates a view of the data of a stack container. #! @param this The stack container to view. #! @returns A span of the elements of the provided stack container. pub def stack::view(&this): span { return span::::over(this._dat, this._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::push(&mut this, value: T): void { if this.is_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::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::(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::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::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::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::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::(this._len - count)); } } else { memset((this._dat + this._len) as *mut u8, 0, bytes_of::(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::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::(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::close(&mut this): void { if const T is closeable { for index in 0..this._len { close(&mut this._dat[index]); } } dispose::(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::equals(&this, other: &stack): bool { if this._len != other._len { return false; } for index in 0..this._len { return false if !equals::( &this._dat[index], &other._dat[index] ); } return true; } #! What `iter(&this)` lends, by the name `iterable` asks for it by. pub type stack::iterator = stack::cursor; #! What `iter(&this)` lends, which reads the elements from the bottom of the #! stack. pub type stack::cursor = span::cursor; #! What `iter(&mut this)` lends, which writes the elements where the stack holds #! them. pub type stack::mutator = slice::mutator; #! Lends a cursor 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 cursor, which is only good until the stack grows, shrinks, or #! closes. pub def stack::iter(&this): span::cursor { return this.view().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::iter(&mut this): slice::mutator { var whole := this.slice(); return whole.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::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::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::grow(&mut this, size: u64): void { if size <= this._cap { return; } this._dat = this._dat == null ? acquire::(this._alc, size) : resize::(this._alc, this._dat, this._cap, size); this._cap = size; }