249 lines
9.0 KiB
Plaintext
249 lines
9.0 KiB
Plaintext
#! @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>::empty(): slice<T> {
|
||
|
|
return slice { _dat = null, _len = 0 };
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Tells whether the view covers no elements.
|
||
|
|
#! @param this The slice to test.
|
||
|
|
#! @returns Whether its length is zero.
|
||
|
|
pub def slice<T>::is_empty(&this): bool {
|
||
|
|
return 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 the same elements, which is the slice itself.
|
||
|
|
#! @param this The slice to lend.
|
||
|
|
#! @returns A copy of the slice, covering the same elements.
|
||
|
|
pub def slice<T>::slice(&mut this): slice<T> {
|
||
|
|
return this;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Walks the elements a slice covers, giving a reference that reads each in
|
||
|
|
#! turn, which is what `iter(&this)` lends and `for x` and `for &x` walk.
|
||
|
|
pub struct slice<T>::cursor : sized_iterator<&T>, backward_iterator<&T> {
|
||
|
|
var _at: *mut T;
|
||
|
|
var _left: u64;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Gives the next element the slice covers.
|
||
|
|
#! @param this The cursor to advance.
|
||
|
|
#! @returns Maybe a reference to the next element; otherwise, `nothing`.
|
||
|
|
pub def slice<T>::cursor::next(&mut this): maybe<&T> {
|
||
|
|
if this._left == 0 {
|
||
|
|
return nothing {};
|
||
|
|
}
|
||
|
|
|
||
|
|
val here := this._at;
|
||
|
|
this._at = here + 1;
|
||
|
|
this._left = this._left - 1;
|
||
|
|
# Reached as the one element of a view, which is never over null and whose
|
||
|
|
# index is known, so reaching it checks nothing.
|
||
|
|
return &span::<T> { _dat = here as *T, _len = 1 }[0];
|
||
|
|
}
|
||
|
|
|
||
|
|
#! How many elements the cursor has left to give.
|
||
|
|
#! @param this The cursor.
|
||
|
|
#! @returns How many.
|
||
|
|
pub def slice<T>::cursor::remaining(&this): u64 {
|
||
|
|
return this._left;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Gives the last element the cursor has left, from the far end of the slice.
|
||
|
|
#! @param this The cursor to advance from its far end.
|
||
|
|
#! @returns Maybe a reference to the element; otherwise, `nothing`.
|
||
|
|
pub def slice<T>::cursor::next_back(&mut this): maybe<&T> {
|
||
|
|
if this._left == 0 {
|
||
|
|
return nothing {};
|
||
|
|
}
|
||
|
|
|
||
|
|
this._left = this._left - 1;
|
||
|
|
return &span::<T> { _dat = (this._at + this._left) as *T, _len = 1 }[0];
|
||
|
|
}
|
||
|
|
|
||
|
|
#! 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>::mut_cursor : 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 cursor to advance.
|
||
|
|
#! @returns Maybe a reference to the next element; otherwise, `nothing`.
|
||
|
|
pub def slice<T>::mut_cursor::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 cursor has left to give.
|
||
|
|
#! @param this The cursor.
|
||
|
|
#! @returns How many.
|
||
|
|
pub def slice<T>::mut_cursor::remaining(&this): u64 {
|
||
|
|
return this._left;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Gives the last element the cursor has left, from the far end of the slice,
|
||
|
|
#! to be written.
|
||
|
|
#! @param this The cursor to advance from its far end.
|
||
|
|
#! @returns Maybe a reference to the element; otherwise, `nothing`.
|
||
|
|
pub def slice<T>::mut_cursor::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 a cursor that reads the elements the slice covers.
|
||
|
|
#! @param this The slice to walk.
|
||
|
|
#! @returns The cursor, which refers into whatever holds the elements.
|
||
|
|
pub def slice<T>::iter(&this): slice<T>::cursor {
|
||
|
|
return slice::<T>::cursor { _at = this._dat, _left = this._len };
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Lends a cursor that writes the elements the slice covers where they are.
|
||
|
|
#! @param this The slice to walk.
|
||
|
|
#! @returns The cursor, which refers into whatever holds the elements.
|
||
|
|
pub def slice<T>::iter(&mut this): slice<T>::mut_cursor {
|
||
|
|
return slice::<T>::mut_cursor { _at = this._dat, _left = this._len };
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Lends a read-only view of the same elements.
|
||
|
|
#! @param this The slice to lend.
|
||
|
|
#! @returns A `span<T>` over the elements this one 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.
|
||
|
|
pub def slice<T>::view(&this): span<T> {
|
||
|
|
# A slice is never over null while it covers anything, so the view is made as
|
||
|
|
# it is.
|
||
|
|
return span::<T> { _dat = this._dat as *T, _len = this._len };
|
||
|
|
}
|
||
|
|
|
||
|
|
#! 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>::empty()
|
||
|
|
: 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.view();
|
||
|
|
val yours := other.view();
|
||
|
|
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.view().hash();
|
||
|
|
}
|