#! @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`, 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` 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::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::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::over(data: *mut T, length: u64): slice { 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::blank(): slice { 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::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::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 = 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::at(&mut this, index: u64): &mut T { var whole: slice = 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::get(&this, index: u64): maybe<&T> { # Reached as the view's element, whose check the test answers. val whole: slice = 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::get(&mut this, index: u64): maybe<&mut T> { var whole: slice = 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::slice_of(other: &mut slice, start: u64 = 0, count: u64 = ~0u): slice { val whole: slice = 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::slice: slice { 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` over those elements. #! @remarks A start or count past the last element stops a checked program. pub def slice::span_of(other: &slice, start: u64 = 0, count: u64 = ~0u): span { # A slice is never over null while it covers anything, so the view is made as # it is. val whole := span:: { _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` 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::span: span { get => span:: { _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::iterator = span::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::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::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::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::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::iter(&this): span::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::iter(&mut this): slice::mutator { return slice::::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::iterator: span::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::mutator: slice::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::sub(&this, from: u64, length: u64): maybe> { if from > this._len || length > this._len - from { return nothing {}; } return length == 0 ? slice::::blank() : slice::::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::equals(&this, other: &slice): 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::hash(&this): u64 { return this.span.hash(); }