Initial Commit
This commit is contained in:
782 files changed
+145156
No files matched your search
@@ -0,0 +1,260 @@
|
||||
#! @file slice.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the methods of `slice`, a writable view over elements that something
|
||||
#! else holds. It is the counterpart of `span<T>`, which only reads, and lends one
|
||||
#! of itself wherever a reader is wanted.
|
||||
#!
|
||||
#! `slice` is among the types the syntax makes, which the compiler declares
|
||||
#! itself. A slice owns nothing: writing through it writes the elements where
|
||||
#! they already live, and closing whatever holds them leaves the slice pointing
|
||||
#! at memory that is no longer there, so keep a slice no longer than the thing
|
||||
#! it views. It converts to a `span<T>` wherever a reader is wanted, which drops
|
||||
#! the right to write.
|
||||
module core;
|
||||
|
||||
|
||||
#! The number of elements the view covers.
|
||||
#! @param this The slice to measure.
|
||||
#! @returns How many elements it covers, starting at `data`.
|
||||
pub val slice<T>::length: u64 {
|
||||
get => this._len;
|
||||
}
|
||||
|
||||
#! The first element the view covers, which is `null` for an empty view.
|
||||
#! @param this The slice to read the address of.
|
||||
#! @returns Where the elements begin.
|
||||
pub val slice<T>::data: *mut T {
|
||||
get => this._dat;
|
||||
}
|
||||
|
||||
#! Makes a writable view over a run of elements.
|
||||
#! @param data The first element, or `null` for a view over nothing.
|
||||
#! @param length The number of elements the view covers.
|
||||
#! @returns The view, which covers nothing where `data` is `null`.
|
||||
#! @remarks A fixed array holds its elements where it stands, so a writable view
|
||||
#! over a `var` one is `array[0:N]`.
|
||||
pub def slice<T>::over(data: *mut T, length: u64): slice<T> {
|
||||
return data == null
|
||||
? slice { _dat = null, _len = 0 }
|
||||
: slice { _dat = data, _len = length };
|
||||
}
|
||||
|
||||
#! Makes a view over nothing, which is the zero value of a slice.
|
||||
#! @returns A view of no elements.
|
||||
pub def slice<T>::blank(): slice<T> {
|
||||
return slice { _dat = null, _len = 0 };
|
||||
}
|
||||
|
||||
#! Whether the view covers no elements.
|
||||
#! @param this The slice to test.
|
||||
#! @returns Whether its length is zero.
|
||||
@inline
|
||||
pub val slice<T>::empty: bool {
|
||||
get => this._len == 0;
|
||||
}
|
||||
|
||||
|
||||
#! Reads the element at an index, which `slice[index]` does without calling it.
|
||||
#! @param this The slice to read.
|
||||
#! @param index The index of the element, below `length`, past which a checked
|
||||
#! program stops.
|
||||
#! @returns The element, which refers into whatever holds the elements.
|
||||
pub def slice<T>::at(&this, index: u64): &T {
|
||||
# Indexed as a view, it is checked as `slice[index]` is, which the compiler does
|
||||
# itself rather than calling this.
|
||||
val whole: slice<T> = this;
|
||||
return &whole[index];
|
||||
}
|
||||
|
||||
#! Reaches the element at an index to write it, which `slice[index] = x` does
|
||||
#! without calling it.
|
||||
#! @param this The slice to write through.
|
||||
#! @param index The index of the element, below `length`, past which a checked
|
||||
#! program stops.
|
||||
#! @returns The element, which refers into whatever holds the elements.
|
||||
pub def slice<T>::at(&mut this, index: u64): &mut T {
|
||||
var whole: slice<T> = this;
|
||||
return &mut whole[index];
|
||||
}
|
||||
|
||||
#! Gets the element at an index, if the slice covers one there.
|
||||
#! @param this The slice to read.
|
||||
#! @param index The index of the element.
|
||||
#! @returns Maybe the element, which refers into whatever holds the elements;
|
||||
#! otherwise, `nothing`.
|
||||
pub def slice<T>::get(&this, index: u64): maybe<&T> {
|
||||
# Reached as the view's element, whose check the test answers.
|
||||
val whole: slice<T> = this;
|
||||
return index < this._len ? &whole[index] : nothing {};
|
||||
}
|
||||
|
||||
#! Gets the element at an index to write it, if the slice covers one there.
|
||||
#! @param this The slice to write through.
|
||||
#! @param index The index of the element.
|
||||
#! @returns Maybe the element, which refers into whatever holds the elements;
|
||||
#! otherwise, `nothing`.
|
||||
pub def slice<T>::get(&mut this, index: u64): maybe<&mut T> {
|
||||
var whole: slice<T> = this;
|
||||
return index < this._len ? &mut whole[index] : nothing {};
|
||||
}
|
||||
|
||||
#! Lends a writable view of some of the same elements, as `sliceable` asks,
|
||||
#! which `slice[start:count]` is without calling it.
|
||||
#! @param other The slice to lend.
|
||||
#! @param start The index of the first element viewed.
|
||||
#! @param count How many elements are viewed, or `~0u` for every one after
|
||||
#! the start.
|
||||
#! @returns A slice covering those elements, which is the slice itself where it
|
||||
#! covers all of them.
|
||||
#! @remarks A start or count past the last element stops a checked program, as
|
||||
#! `slice[start:count]` does.
|
||||
pub def slice<T>::slice_of(other: &mut slice<T>, start: u64 = 0, count: u64 = ~0u): slice<T> {
|
||||
val whole: slice<T> = other;
|
||||
return count == ~0u ? whole[start:] : whole[start:count];
|
||||
}
|
||||
|
||||
#! A writable view of the same elements, which is the slice itself, read of a
|
||||
#! slice that can be written.
|
||||
#! @param this The slice to lend.
|
||||
#! @returns A copy of the slice, covering the same elements.
|
||||
@inline
|
||||
pub val slice<T>::slice: slice<T> {
|
||||
get mut => this;
|
||||
}
|
||||
|
||||
#! Lends a read-only view of some of the same elements, as `spannable` asks.
|
||||
#! @param other The slice to lend.
|
||||
#! @param start The index of the first element viewed.
|
||||
#! @param count How many elements are viewed, or `~0u` for every one after
|
||||
#! the start.
|
||||
#! @returns A `span<T>` over those elements.
|
||||
#! @remarks A start or count past the last element stops a checked program.
|
||||
pub def slice<T>::span_of(other: &slice<T>, start: u64 = 0, count: u64 = ~0u): span<T> {
|
||||
# A slice is never over null while it covers anything, so the view is made as
|
||||
# it is.
|
||||
val whole := span::<T> { _dat = other._dat as *T, _len = other._len };
|
||||
return count == ~0u ? whole[start:] : whole[start:count];
|
||||
}
|
||||
|
||||
#! A read-only view of the same elements.
|
||||
#! @param this The slice to lend.
|
||||
#! @returns A `span<T>` over the elements it covers.
|
||||
#! @remarks A slice converts to a span on its own wherever one is wanted, so
|
||||
#! this is only written where the conversion needs to be named.
|
||||
@inline
|
||||
pub val slice<T>::span: span<T> {
|
||||
get => span::<T> { _dat = this._dat as *T, _len = this._len };
|
||||
}
|
||||
|
||||
#! What `iter(&this)` lends, which reads the elements a slice covers as the
|
||||
#! span it converts to does, and which `for x` and `for &x` walk.
|
||||
pub type slice<T>::iterator = span<T>::iterator;
|
||||
|
||||
#! Walks the elements a slice covers, giving a reference that writes each in
|
||||
#! turn, which is what `iter(&mut this)` lends and `for &mut x` walks.
|
||||
pub struct slice<T>::mutator : sized_iterator<&mut T>, backward_iterator<&mut T> {
|
||||
var _at: *mut T;
|
||||
var _left: u64;
|
||||
}
|
||||
|
||||
#! Gives the next element the slice covers, to be written.
|
||||
#! @param this The mutator to advance.
|
||||
#! @returns Maybe a reference to the next element; otherwise, `nothing`.
|
||||
pub def slice<T>::mutator::next(&mut this): maybe<&mut T> {
|
||||
if this._left == 0 {
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
val here := this._at;
|
||||
this._at = here + 1;
|
||||
this._left = this._left - 1;
|
||||
return &mut here[0];
|
||||
}
|
||||
|
||||
#! How many elements the mutator has left to give.
|
||||
#! @param this The mutator.
|
||||
#! @returns How many.
|
||||
pub def slice<T>::mutator::remaining(&this): u64 {
|
||||
return this._left;
|
||||
}
|
||||
|
||||
#! Gives the last element the mutator has left, from the far end of the slice,
|
||||
#! to be written.
|
||||
#! @param this The mutator to advance from its far end.
|
||||
#! @returns Maybe a reference to the element; otherwise, `nothing`.
|
||||
pub def slice<T>::mutator::next_back(&mut this): maybe<&mut T> {
|
||||
if this._left == 0 {
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
this._left = this._left - 1;
|
||||
return &mut this._at[this._left];
|
||||
}
|
||||
|
||||
#! Lends an iterator that reads the elements the slice covers.
|
||||
#! @param this The slice to walk.
|
||||
#! @returns The iterator, which refers into whatever holds the elements.
|
||||
pub def slice<T>::iter(&this): span<T>::iterator {
|
||||
return this.span.iter();
|
||||
}
|
||||
|
||||
#! Lends a mutator that writes the elements the slice covers where they are.
|
||||
#! @param this The slice to walk.
|
||||
#! @returns The mutator, which refers into whatever holds the elements.
|
||||
pub def slice<T>::iter(&mut this): slice<T>::mutator {
|
||||
return slice::<T>::mutator { _at = this._dat, _left = this._len };
|
||||
}
|
||||
|
||||
#! An iterator that reads the elements the slice covers, as `iter` lends.
|
||||
#! @param this The slice to walk.
|
||||
#! @returns The iterator, which refers into whatever holds the elements.
|
||||
@inline
|
||||
pub val slice<T>::iterator: span<T>::iterator {
|
||||
get => this.span.iter();
|
||||
}
|
||||
|
||||
#! A mutator that writes the elements the slice covers where they are, read of
|
||||
#! a slice that can be written.
|
||||
#! @param this The slice to walk.
|
||||
#! @returns The mutator, which refers into whatever holds the elements.
|
||||
@inline
|
||||
pub val slice<T>::mutator: slice<T>::mutator {
|
||||
get mut => this.iter();
|
||||
}
|
||||
|
||||
#! Narrows the view to a run of the elements it already covers.
|
||||
#! @param this The slice to narrow.
|
||||
#! @param from The index the narrower view starts at, from `0` to `length`.
|
||||
#! @param length The number of elements it covers.
|
||||
#! @returns Maybe the narrower view if this one covers all of it; otherwise,
|
||||
#! `nothing`.
|
||||
pub def slice<T>::sub(&this, from: u64, length: u64): maybe<slice<T>> {
|
||||
if from > this._len || length > this._len - from {
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
return length == 0
|
||||
? slice::<T>::blank()
|
||||
: slice::<T>::over(this._dat + from, length);
|
||||
}
|
||||
|
||||
#! Compares one view to another for equality.
|
||||
#! @param this The slice to compare against.
|
||||
#! @param other The slice to compare with.
|
||||
#! @returns Whether both cover the same number of equal elements, in the same
|
||||
#! order, which is what comparing the spans they lend answers.
|
||||
pub def slice<T>::equals(&this, other: &slice<T>): bool {
|
||||
val mine := this.span;
|
||||
val yours := other.span;
|
||||
return mine.equals(&yours);
|
||||
}
|
||||
|
||||
#! Hashes the view provided.
|
||||
#! @param this The slice to hash.
|
||||
#! @returns The hash code of the view, which it shares with the span it lends.
|
||||
pub def slice<T>::hash(&this): u64 {
|
||||
return this.span.hash();
|
||||
}
|
||||
Reference in new issue
Block a user