Files
2026-11-14 15:32:55 -06:00

431 lines
13 KiB
Plaintext

#! @file queue.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Defines an implementation for a `queue` container. Queues are a structured
#! container where data is pushed onto the back and popped off the front, in a
#! first-in-first-out (FIFO) manner.
module std::containers;
#! A `queue` is a structured data container that stores its content in a first-
#! in-first-out (FIFO) manner.
#!
#! They are useful wherever work is handled in the order it arrives, such as
#! event loops, breadth-first searches, and buffers between a producer and a
#! consumer.
#!
#! Its elements live in one block used as a ring: the front is at `_hed`, and
#! each element after it is at the next index, wrapping to the start of the
#! block.
#! @tparam T The type of its elements.
pub struct queue<T>
: owning
, hashable
, iterable<T>
, copyable
, moveable
, equatable
{
var _alc: maybe<&mut allocator>;
var _dat: *mut T;
var _hed: u64;
var _len: u64;
var _cap: u64;
}
#! Provides read only access to the length of a queue.
#! @param this The queue to get the length of.
#! @returns The length of the queue.
pub val queue<T>::length: u64 {
get => this._len;
}
#! Provides read only access to the capacity of a queue.
#! @param this The queue to get the capacity of.
#! @returns The capacity of the queue.
pub val queue<T>::capacity: u64 {
get => this._cap;
}
#! Indicates if a queue is empty.
#! @param this The queue to check.
#! @returns Whether the queue is empty.
pub val queue<T>::empty: bool {
get => this._len == 0;
}
#! Indicates if a queue is full.
#! @param this The queue to check.
#! @returns Whether the queue is full.
pub val queue<T>::full: bool {
get => this._len == this._cap;
}
#! Creates a new queue with no allocator and a default size.
#! @returns The created queue container.
#! @remarks Will use the built in `raw_allocate`, `raw_release` and `raw_resize`
#! for memory management.
pub def queue<T>::new(): queue<T> {
return queue::<T>::new(4, nothing{});
}
#! Creates a new queue with an optional allocator and a default size.
#! @param alloc The optional allocator for the memory backing of the queue.
#! @returns The created queue container.
pub def queue<T>::new(alloc: maybe<&mut allocator>): queue<T> {
return queue::<T>::new(4, alloc);
}
#! Creates a new queue with an optional allocator and a provided size.
#! @param size The initial capacity of the queue, which may be none.
#! @param alloc The optional allocator for the memory backing of the queue.
#! @returns The created queue container, which holds no elements yet.
pub def queue<T>::new(size: u64, alloc: maybe<&mut allocator>): queue<T> {
var made: queue<T> = queue {
_alc = alloc,
_dat = null,
_hed = 0,
_len = 0,
_cap = 0
};
made.reserve(size);
return made;
}
#! Creates a copy of the provided queue.
#! @param other The queue to copy.
#! @returns The copy, which owns its own memory 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 queue<T>::copy_of(other: &queue<T>): queue<T> {
var made := queue::<T>::new(other._len, other._alc);
for index in 0..other._len {
made.push(copy::<T>(&other._dat[other.slot(index)]));
}
return made;
}
#! Creates a new queue by moving the data of another queue into it.
#! @param other The queue to move elements from, which holds none after.
#! @returns The moved queue, 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 queue<T>::move_of(other: &mut queue<T>): queue<T> {
val made: queue<T> = queue {
_alc = other._alc,
_dat = other._dat,
_hed = other._hed,
_len = other._len,
_cap = other._cap
};
other._dat = null;
other._hed = 0;
other._len = 0;
other._cap = 0;
return made;
}
#! Pushes a new element onto the back of a queue.
#! @param this The queue to push the value onto.
#! @param value The value to push onto the queue.
#! @remarks An allocator that gives no more memory stops the program.
pub def queue<T>::push(&mut this, value: T): void {
if this.full {
this.grow();
}
this._dat[this.slot(this._len)] = move(value);
this._len++;
}
#! Pops the element off the front of a queue.
#! @param this The queue 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 queue<T>::pop(&mut this): void {
expects(!this.empty, "The queue is empty.");
this.release(this._hed);
this._hed = (this._hed + 1) % this._cap;
this._len--;
}
#! Peeks the element at the front of a queue without removing it.
#! @param this The queue to peek the front of, which stops a checked program
#! where it is empty.
#! @returns A reference to an immutable element.
pub def queue<T>::peek(&this): &T {
expects(!this.empty, "The queue is empty.");
return &this._dat[this._hed];
}
#! Peeks the element at the front of a queue without removing it.
#! @param this The queue to peek the front of, which stops a checked program
#! where it is empty.
#! @returns A reference to a mutable element.
pub def queue<T>::peek(&mut this): &mut T {
expects(!this.empty, "The queue is empty.");
return &mut this._dat[this._hed];
}
#! The element at the front of a queue, as `peek` reads it, which a queue that
#! can be written writes through.
#! @param this The queue to read, which stops a checked program where it is
#! empty.
#! @returns A reference to the element.
pub val queue<T>::first: &T {
get => this.peek();
get mut => this.peek();
}
#! The element at the back of a queue, pushed last, which a queue that can be
#! written writes through.
#! @param this The queue to read, which stops a checked program where it is
#! empty.
#! @returns A reference to the element.
pub val queue<T>::last: &T {
get {
expects(!this.empty, "The queue is empty.");
return &this._dat[this.slot(this._len - 1)];
}
get mut {
expects(!this.empty, "The queue is empty.");
return &mut this._dat[this.slot(this._len - 1)];
}
}
#! Reserves an amount of memory for the number of elements requested for the
#! backing memory of the queue provided.
#! @param this The queue to reserve elements for.
#! @param count The number of elements to reserve for a queue.
pub def queue<T>::reserve(&mut this, count: u64): void {
this.grow(count);
}
#! Removes all elements from a queue.
#! @param this The queue to clear.
#! @remarks Elements that close, or hold what does, are closed. The backing data
#! is kept but not disposed.
pub def queue<T>::clear(&mut this): void {
for index in 0..this._len {
this.release(this.slot(index));
}
this._hed = 0;
this._len = 0;
}
#! Removes all elements of a queue then disposes the backing memory.
#! @param this The queue to close.
pub def queue<T>::close(&mut this): void {
if const T is closeable {
for index in 0..this._len {
close(&mut this._dat[this.slot(index)]);
}
}
this._hed = 0;
this._len = 0;
if this._dat != null {
dispose::<T>(this._alc, this._dat, this._cap);
}
this._dat = null;
this._cap = 0;
}
#! Compares one queue to another by equality.
#! @param this The queue to compare against.
#! @param other The queue to compare with.
#! @returns Whether both hold equal elements, in the same order.
pub def queue<T>::equals(&this, other: &queue<T>): bool {
if this._len != other._len {
return false;
}
for index in 0..this._len {
return false if !equals::<T>(
&this._dat[this.slot(index)],
&other._dat[other.slot(index)]
);
}
return true;
}
#! Walks the elements of a queue 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 queue<T>::iterator : sized_iterator<&T>, backward_iterator<&T> {
var _dat: *mut T;
var _at: u64;
var _cap: u64;
var _left: u64;
}
#! Gives the next element of the queue.
#! @param this The iterator to advance.
#! @returns Maybe a reference to the next element; otherwise, `nothing`.
pub def queue<T>::iterator::next(&mut this): maybe<&T> {
if this._left == 0 {
return nothing {};
}
val here := this._at;
this._at = (here + 1) % this._cap;
this._left = this._left - 1;
return &this._dat[here];
}
#! How many elements the iterator has left to give.
#! @param this The iterator.
#! @returns How many.
pub def queue<T>::iterator::remaining(&this): u64 {
return this._left;
}
#! Gives the last element the iterator has left, from the back of the queue.
#! @param this The iterator to advance from its far end.
#! @returns Maybe a reference to the element; otherwise, `nothing`.
pub def queue<T>::iterator::next_back(&mut this): maybe<&T> {
if this._left == 0 {
return nothing {};
}
this._left = this._left - 1;
return &this._dat[(this._at + this._left) % this._cap];
}
#! Walks the elements of a queue 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 queue<T>::mutator : sized_iterator<&mut T>, backward_iterator<&mut T> {
var _dat: *mut T;
var _at: u64;
var _cap: u64;
var _left: u64;
}
#! Gives the next element of the queue, to be written.
#! @param this The mutator to advance.
#! @returns Maybe a reference to the next element; otherwise, `nothing`.
pub def queue<T>::mutator::next(&mut this): maybe<&mut T> {
if this._left == 0 {
return nothing {};
}
val here := this._at;
this._at = (here + 1) % this._cap;
this._left = this._left - 1;
return &mut this._dat[here];
}
#! How many elements the mutator has left to give.
#! @param this The mutator.
#! @returns How many.
pub def queue<T>::mutator::remaining(&this): u64 {
return this._left;
}
#! Gives the last element the mutator has left, from the back of the queue, to
#! be written.
#! @param this The mutator to advance from its far end.
#! @returns Maybe a reference to the element; otherwise, `nothing`.
pub def queue<T>::mutator::next_back(&mut this): maybe<&mut T> {
if this._left == 0 {
return nothing {};
}
this._left = this._left - 1;
return &mut this._dat[(this._at + this._left) % this._cap];
}
#! Lends an iterator that reads the elements from the front of the queue, which
#! `for x in q` walks.
#! @param this The queue to walk.
#! @returns The iterator, which is only good until the queue changes or closes.
pub def queue<T>::iter(&this): queue<T>::iterator {
return queue::<T>::iterator { _dat = this._dat, _at = this._hed, _cap = this._cap, _left = this._len };
}
#! Lends a mutator that writes the elements where they are, from the front of
#! the queue, which `for &mut x in q` walks.
#! @param this The queue to walk.
#! @returns The mutator, which is only good until the queue changes or closes.
pub def queue<T>::iter(&mut this): queue<T>::mutator {
return queue::<T>::mutator { _dat = this._dat, _at = this._hed, _cap = this._cap, _left = this._len };
}
#! An iterator that reads the elements from the front of the queue, as `iter`
#! lends.
#! @param this The queue to walk.
#! @returns The iterator, which is only good until the queue changes or closes.
pub val queue<T>::iterator: queue<T>::iterator {
get => this.iter();
}
#! A mutator that writes the elements where they are, read of a queue that can
#! be written.
#! @param this The queue to walk.
#! @returns The mutator, which is only good until the queue changes or closes.
pub val queue<T>::mutator: queue<T>::mutator {
get mut => this.iter();
}
#! Hashes the provided queue.
#! @param this The queue to hash.
#! @returns The hashed value of the queue.
#! @remarks Elements are hashed by their `hash`, so they are `hashable`.
pub def queue<T>::hash(&this): u64 {
var code := this._len;
for index in 0..this._len {
code = mix(code, hash::<T>(&this._dat[this.slot(index)]));
}
return code;
}
def queue<T>::slot(&this, index: u64): u64 {
return (this._hed + index) % this._cap;
}
def queue<T>::release(&mut this, slot: u64): void {
close(&mut this._dat[slot]);
# The slot is emptied where writing over it would close what it held.
if const T is closeable {
memset((this._dat + slot) as *mut u8, 0, bytes_of::<T>(1));
}
}
# Gives the queue room for more elements, doubling it.
def queue<T>::grow(&mut this): void {
this.grow(
this._cap != 0
? this._cap * 2
: 4
);
}
# Gives the queue room for a number of elements, where it has less, with them
# from its first slot. An allocator that gives no memory stops the program, as
# `acquire` does.
def queue<T>::grow(&mut this, size: u64): void {
if size <= this._cap {
return;
}
val block := acquire::<T>(this._alc, size);
val first := this._cap - this._hed;
val lead := this._len < first ? this._len : first;
memcpy(block as *mut u8, (this._dat + this._hed) as *mut u8, bytes_of::<T>(lead));
memcpy((block + lead) as *mut u8, this._dat as *mut u8, bytes_of::<T>(this._len - lead));
if this._dat != null {
dispose::<T>(this._alc, this._dat, this._cap);
}
this._dat = block;
this._hed = 0;
this._cap = size;
}