Initial Commit
This commit is contained in:
1625 files changed
+43221
No files matched your search
@@ -0,0 +1,501 @@
|
||||
#! @file deque.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Defines an implementation for a `deque` container. Deques are a structured
|
||||
#! container where data is pushed and popped at either end.
|
||||
module std::containers;
|
||||
|
||||
|
||||
#! A `deque` is a structured data container, a double-ended queue, that stores
|
||||
#! its content in order and is pushed and popped at either end.
|
||||
#!
|
||||
#! They are useful wherever work is taken from both ends, such as undo
|
||||
#! histories, sliding windows, and work-stealing schedulers, and wherever a
|
||||
#! queue must also be indexed.
|
||||
#!
|
||||
#! 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 deque<T>
|
||||
: owning
|
||||
, hashable
|
||||
, indexable<u64, T>
|
||||
, 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 deque.
|
||||
#! @param this The deque to get the length of.
|
||||
#! @returns The length of the deque.
|
||||
pub val deque<T>::length: u64 {
|
||||
get => this._len;
|
||||
}
|
||||
|
||||
#! Provides read only access to the capacity of a deque.
|
||||
#! @param this The deque to get the capacity of.
|
||||
#! @returns The capacity of the deque.
|
||||
pub val deque<T>::capacity: u64 {
|
||||
get => this._cap;
|
||||
}
|
||||
|
||||
#! Indicates if a deque is empty.
|
||||
#! @param this The deque to check.
|
||||
#! @returns Whether the deque is empty.
|
||||
pub val deque<T>::is_empty: bool {
|
||||
get => this._len == 0;
|
||||
}
|
||||
|
||||
#! Indicates if a deque is full.
|
||||
#! @param this The deque to check.
|
||||
#! @returns Whether the deque is full.
|
||||
pub val deque<T>::is_full: bool {
|
||||
get => this._len == this._cap;
|
||||
}
|
||||
|
||||
#! Creates a new deque with no allocator and a default size.
|
||||
#! @returns The created deque container.
|
||||
#! @remarks Will use the built in `raw_allocate`, `raw_release` and `raw_resize`
|
||||
#! for memory management.
|
||||
pub def deque<T>::new(): deque<T> {
|
||||
return deque::<T>::new(4, nothing{});
|
||||
}
|
||||
|
||||
#! Creates a new deque with an optional allocator and a default size.
|
||||
#! @param alloc The optional allocator for the memory backing of the deque.
|
||||
#! @returns The created deque container.
|
||||
pub def deque<T>::new(alloc: maybe<&mut allocator>): deque<T> {
|
||||
return deque::<T>::new(4, alloc);
|
||||
}
|
||||
|
||||
#! Creates a new deque with an optional allocator and a provided size.
|
||||
#! @param size The initial capacity of the deque, which may be none.
|
||||
#! @param alloc The optional allocator for the memory backing of the deque.
|
||||
#! @returns The created deque container, which holds no elements yet.
|
||||
pub def deque<T>::new(size: u64, alloc: maybe<&mut allocator>): deque<T> {
|
||||
var made: deque<T> = deque {
|
||||
_alc = alloc,
|
||||
_dat = null,
|
||||
_hed = 0,
|
||||
_len = 0,
|
||||
_cap = 0
|
||||
};
|
||||
|
||||
made.reserve(size);
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Creates a copy of the provided deque.
|
||||
#! @param other The deque 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 deque<T>::copy_of(other: &deque<T>): deque<T> {
|
||||
var made := deque::<T>::new(other._len, other._alc);
|
||||
for index in 0..other._len {
|
||||
made.push_back(copy::<T>(&other._dat[other.slot(index)]));
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Creates a new deque by moving the data of another deque into it.
|
||||
#! @param other The deque to move elements from, which holds none after.
|
||||
#! @returns The moved deque, 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 deque<T>::move_of(other: &mut deque<T>): deque<T> {
|
||||
val made: deque<T> = deque {
|
||||
_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;
|
||||
}
|
||||
|
||||
#! Gets the element at the provided index, counted from the front.
|
||||
#! @param this The deque 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 deque<T>::at(&this, index: u64): &T {
|
||||
if const checking() {
|
||||
if index >= this._len {
|
||||
past_end(index, this._len);
|
||||
}
|
||||
}
|
||||
return &this._dat[this.slot(index)];
|
||||
}
|
||||
|
||||
#! Gets the element at the provided index, counted from the front.
|
||||
#! @param this The deque 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 deque<T>::at(&mut this, index: u64): &mut T {
|
||||
if const checking() {
|
||||
if index >= this._len {
|
||||
past_end(index, this._len);
|
||||
}
|
||||
}
|
||||
return &mut this._dat[this.slot(index)];
|
||||
}
|
||||
|
||||
#! Gets the element at an index, if the deque holds one there, for an index
|
||||
#! that may be past the end in a program that is right.
|
||||
#! @param this The deque to read.
|
||||
#! @param index The index of the element.
|
||||
#! @returns Maybe the element; otherwise, `nothing`.
|
||||
pub def deque<T>::get(&this, index: u64): maybe<&T> {
|
||||
return nothing {} if index >= this._len;
|
||||
return &this._dat[this.slot(index)];
|
||||
}
|
||||
|
||||
#! Gets the element at an index to write it, if the deque holds one there.
|
||||
#! @param this The deque to write.
|
||||
#! @param index The index of the element.
|
||||
#! @returns Maybe the element; otherwise, `nothing`.
|
||||
pub def deque<T>::get(&mut this, index: u64): maybe<&mut T> {
|
||||
return nothing {} if index >= this._len;
|
||||
return &mut this._dat[this.slot(index)];
|
||||
}
|
||||
|
||||
#! Gets the element at the front of a deque.
|
||||
#! @param this The deque to get the front of, which stops a checked program
|
||||
#! where it is empty.
|
||||
#! @returns A reference to an immutable element.
|
||||
pub def deque<T>::front(&this): &T {
|
||||
if const checking() {
|
||||
if this._len == 0 {
|
||||
fault("The deque is empty.");
|
||||
}
|
||||
}
|
||||
return &this._dat[this._hed];
|
||||
}
|
||||
|
||||
#! Gets the element at the front of a deque.
|
||||
#! @param this The deque to get the front of, which stops a checked program
|
||||
#! where it is empty.
|
||||
#! @returns A reference to a mutable element.
|
||||
pub def deque<T>::front(&mut this): &mut T {
|
||||
if const checking() {
|
||||
if this._len == 0 {
|
||||
fault("The deque is empty.");
|
||||
}
|
||||
}
|
||||
return &mut this._dat[this._hed];
|
||||
}
|
||||
|
||||
#! Gets the element at the back of a deque.
|
||||
#! @param this The deque to get the back of, which stops a checked program
|
||||
#! where it is empty.
|
||||
#! @returns A reference to an immutable element.
|
||||
pub def deque<T>::back(&this): &T {
|
||||
if const checking() {
|
||||
if this._len == 0 {
|
||||
fault("The deque is empty.");
|
||||
}
|
||||
}
|
||||
return &this._dat[this.slot(this._len - 1)];
|
||||
}
|
||||
|
||||
#! Gets the element at the back of a deque.
|
||||
#! @param this The deque to get the back of, which stops a checked program
|
||||
#! where it is empty.
|
||||
#! @returns A reference to a mutable element.
|
||||
pub def deque<T>::back(&mut this): &mut T {
|
||||
if const checking() {
|
||||
if this._len == 0 {
|
||||
fault("The deque is empty.");
|
||||
}
|
||||
}
|
||||
return &mut this._dat[this.slot(this._len - 1)];
|
||||
}
|
||||
|
||||
#! Pushes a new element onto the front of a deque.
|
||||
#! @param this The deque to push the value onto.
|
||||
#! @param value The value to push onto the deque.
|
||||
#! @remarks An allocator that gives no more memory stops the program.
|
||||
pub def deque<T>::push_front(&mut this, value: T): void {
|
||||
if this.is_full {
|
||||
this.grow();
|
||||
}
|
||||
|
||||
this._hed = (this._hed + this._cap - 1) % this._cap;
|
||||
this._dat[this._hed] = move(value);
|
||||
this._len++;
|
||||
}
|
||||
|
||||
#! Pushes a new element onto the back of a deque.
|
||||
#! @param this The deque to push the value onto.
|
||||
#! @param value The value to push onto the deque.
|
||||
#! @remarks An allocator that gives no more memory stops the program.
|
||||
pub def deque<T>::push_back(&mut this, value: T): void {
|
||||
if this.is_full {
|
||||
this.grow();
|
||||
}
|
||||
this._dat[this.slot(this._len)] = move(value);
|
||||
this._len++;
|
||||
}
|
||||
|
||||
#! Pops the element off the front of a deque.
|
||||
#! @param this The deque 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 deque<T>::pop_front(&mut this): void {
|
||||
if const checking() {
|
||||
if this._len == 0 {
|
||||
fault("The deque is empty.");
|
||||
}
|
||||
}
|
||||
this.release(this._hed);
|
||||
this._hed = (this._hed + 1) % this._cap;
|
||||
this._len--;
|
||||
}
|
||||
|
||||
#! Pops the element off the back of a deque.
|
||||
#! @param this The deque 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 deque<T>::pop_back(&mut this): void {
|
||||
if const checking() {
|
||||
if this._len == 0 {
|
||||
fault("The deque is empty.");
|
||||
}
|
||||
}
|
||||
this.release(this.slot(this._len - 1));
|
||||
this._len--;
|
||||
}
|
||||
|
||||
#! Reserves an amount of memory for the number of elements requested for the
|
||||
#! backing memory of the deque provided.
|
||||
#! @param this The deque to reserve elements for.
|
||||
#! @param count The number of elements to reserve for a deque.
|
||||
pub def deque<T>::reserve(&mut this, count: u64): void {
|
||||
this.grow(count);
|
||||
}
|
||||
|
||||
#! Removes all elements from a deque.
|
||||
#! @param this The deque to clear.
|
||||
#! @remarks Elements that close, or hold what does, are closed. The backing data
|
||||
#! is kept but not disposed.
|
||||
pub def deque<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 deque then disposes the backing memory.
|
||||
#! @param this The deque to close.
|
||||
pub def deque<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 deque to another by equality.
|
||||
#! @param this The deque to compare against.
|
||||
#! @param other The deque to compare with.
|
||||
#! @returns Whether both hold equal elements, in the same order.
|
||||
pub def deque<T>::equals(&this, other: &deque<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 deque 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 deque<T>::cursor : 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 deque.
|
||||
#! @param this The cursor to advance.
|
||||
#! @returns Maybe a reference to the next element; otherwise, `nothing`.
|
||||
pub def deque<T>::cursor::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 cursor has left to give.
|
||||
#! @param this The cursor.
|
||||
#! @returns How many.
|
||||
pub def deque<T>::cursor::remaining(&this): u64 {
|
||||
return this._left;
|
||||
}
|
||||
|
||||
#! Gives the last element the cursor has left, from the back of the deque.
|
||||
#! @param this The cursor to advance from its far end.
|
||||
#! @returns Maybe a reference to the element; otherwise, `nothing`.
|
||||
pub def deque<T>::cursor::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 deque 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 deque<T>::mut_cursor : 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 deque, to be written.
|
||||
#! @param this The cursor to advance.
|
||||
#! @returns Maybe a reference to the next element; otherwise, `nothing`.
|
||||
pub def deque<T>::mut_cursor::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 cursor has left to give.
|
||||
#! @param this The cursor.
|
||||
#! @returns How many.
|
||||
pub def deque<T>::mut_cursor::remaining(&this): u64 {
|
||||
return this._left;
|
||||
}
|
||||
|
||||
#! Gives the last element the cursor has left, from the back of the deque, to
|
||||
#! be written.
|
||||
#! @param this The cursor to advance from its far end.
|
||||
#! @returns Maybe a reference to the element; otherwise, `nothing`.
|
||||
pub def deque<T>::mut_cursor::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 a cursor that reads the elements from the front of the deque, which
|
||||
#! `for x in d` walks.
|
||||
#! @param this The deque to walk.
|
||||
#! @returns The cursor, which is only good until the deque changes or closes.
|
||||
pub def deque<T>::iter(&this): deque<T>::cursor {
|
||||
return deque::<T>::cursor { _dat = this._dat, _at = this._hed, _cap = this._cap, _left = this._len };
|
||||
}
|
||||
|
||||
#! Lends a cursor that writes the elements where they are, from the front of the
|
||||
#! deque, which `for &mut x in d` walks.
|
||||
#! @param this The deque to walk.
|
||||
#! @returns The cursor, which is only good until the deque changes or closes.
|
||||
pub def deque<T>::iter(&mut this): deque<T>::mut_cursor {
|
||||
return deque::<T>::mut_cursor { _dat = this._dat, _at = this._hed, _cap = this._cap, _left = this._len };
|
||||
}
|
||||
|
||||
#! Hashes the provided deque.
|
||||
#! @param this The deque to hash.
|
||||
#! @returns The hashed value of the deque.
|
||||
#! @remarks Elements are hashed by their `hash`, so they are `hashable`.
|
||||
pub def deque<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 deque<T>::slot(&this, index: u64): u64 {
|
||||
return (this._hed + index) % this._cap;
|
||||
}
|
||||
|
||||
def deque<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 deque room for more elements, doubling it.
|
||||
def deque<T>::grow(&mut this): void {
|
||||
this.grow(
|
||||
this._cap != 0
|
||||
? this._cap * 2
|
||||
: 4
|
||||
);
|
||||
}
|
||||
|
||||
# Gives the deque 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 deque<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;
|
||||
}
|
||||
Reference in new issue
Block a user