Files
sakurac/core/containers/span.sakura
T

397 lines
14 KiB
Plaintext
Raw Normal View History

2026-10-26 10:14:11 -05:00
#! @file span.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the methods of `span`, a read-only view over elements that something
#! else holds, which read and narrow such a view. The writable counterpart is
#! `slice<T>`, which lends a span of itself.
#!
#! `span` is among the types the syntax makes, which the compiler declares
#! itself, as `a[:]` makes one: where the elements begin, and how many there
#! are. A span owns nothing and writes nothing. It is not `owning`, and closing
#! whatever holds the elements leaves the span pointing at memory that is no
#! longer there, the same way a raw pointer does, so keep a span no longer than
#! the thing it views. A view only reads, so it can be taken of anything,
#! including memory that cannot be written such as the text of a literal.
module core;
#! The number of elements the view covers.
#! @param this The span to measure.
#! @returns How many elements it reads, starting at `data`.
@inline
pub val span<T>::length: u64 {
get => this._len;
}
#! The first element the view covers, which is `null` for an empty view.
#! @param this The span to read the address of.
#! @returns Where the elements begin.
@inline
pub val span<T>::data: *T {
get => this._dat;
}
#! Makes a 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 view over one
#! is `array[0:N]`, or `span::<T>::over((&array) as *T, N)` written
#! out.
pub def span<T>::over(data: *T, length: u64): span<T> {
return data == null
? span { _dat = null, _len = 0 }
: span { _dat = data, _len = length };
}
#! Makes a view over nothing, which is the zero value of a span.
#! @returns A view of no elements.
pub def span<T>::blank(): span<T> {
return span { _dat = null, _len = 0 };
}
#! Whether the view covers no elements.
#! @param this The span to test.
#! @returns Whether its length is zero.
@inline
pub val span<T>::empty: bool {
get => this._len == 0;
}
#! Reads the element at an index, which `span[index]` does without calling it.
#! @param this The span to read.
#! @param index The index of the element, below `length`.
#! @returns The element, which refers into whatever holds the elements.
#! @remarks It is what makes a span `indexable`, so generic code bounded by that
#! trait reads a view as it reads any other container. An index past
#! the end stops a checked program, as `span[index]` does.
pub def span<T>::at(&this, index: u64): &T {
# Indexed as a view, it is checked as `span[index]` is, which the compiler does
# itself rather than calling this.
val whole: span<T> = this;
return &whole[index];
}
#! Lends a view of some of the same elements, as `spannable` asks, which
#! `span[start:count]` is without calling it.
#! @param other The span 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 covering those elements, which is the span itself where it
#! covers all of them.
#! @remarks A start or count past the last element stops a checked program, as
#! `span[start:count]` does.
pub def span<T>::span_of(other: &span<T>, start: u64 = 0, count: u64 = ~0u): span<T> {
val whole: span<T> = other;
return count == ~0u ? whole[start:] : whole[start:count];
}
#! A view of the same elements, which is the span itself.
#! @param this The span to lend.
#! @returns A copy of the span, covering the same elements.
@inline
pub val span<T>::span: span<T> {
get => this;
}
#! Walks the elements a view covers, giving a reference to each in turn, which
#! is what `iter` lends and `for` walks.
pub struct span<T>::iterator : sized_iterator<&T>, backward_iterator<&T> {
var _at: *T;
var _left: u64;
}
#! Gives the next element the view covers.
#! @param this The iterator to advance.
#! @returns Maybe a reference to the next element; otherwise, `nothing`.
pub def span<T>::iterator::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, _len = 1 }[0];
}
#! How many elements the iterator has left to give.
#! @param this The iterator.
#! @returns How many.
pub def span<T>::iterator::remaining(&this): u64 {
return this._left;
}
#! Gives the last element the iterator has left, from the far end of the view.
#! @param this The iterator to advance from its far end.
#! @returns Maybe a reference to the element; otherwise, `nothing`.
pub def span<T>::iterator::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, _len = 1 }[0];
}
#! Lends an iterator over the elements the view covers, which `for` walks.
#! @param this The span to walk.
#! @returns The iterator, which refers into whatever holds the elements.
pub def span<T>::iter(&this): span<T>::iterator {
return span::<T>::iterator { _at = this._dat, _left = this._len };
}
#! An iterator over the elements the view covers, as `iter` lends.
#! @param this The span to walk.
#! @returns The iterator, which refers into whatever holds the elements.
@inline
pub val span<T>::iterator: span<T>::iterator {
get => this.iter();
}
#! Gets the element at an index, if the view covers one there.
#! @param this The span to read.
#! @param index The index of the element, below `length`.
#! @returns Maybe the element if the index is one it covers; otherwise,
#! `nothing`.
#! @remarks What it gives refers into whatever holds the elements, so it is only
#! good for as long as the view is.
pub def span<T>::get(&this, index: u64): maybe<&T> {
# Reached as the view's element, whose check the test answers.
return index < this._len
? &span::<T> { _dat = this._dat, _len = this._len }[index]
: nothing {};
}
#! The first element the view covers.
#! @param this The span to read.
#! @returns Maybe the first element; otherwise, `nothing` for an empty view.
pub val span<T>::first: maybe<&T> {
get => this.get(0);
}
#! The last element the view covers.
#! @param this The span to read.
#! @returns Maybe the last element; otherwise, `nothing` for an empty view.
pub val span<T>::last: maybe<&T> {
get => this._len == 0 ? nothing {} : this.get(this._len - 1);
}
#! Narrows the view to a run of the elements it already covers.
#! @param this The span 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 span<T>::sub(&this, from: u64, length: u64): maybe<span<T>> {
if from > this._len || length > this._len - from {
return nothing {};
}
return length == 0
? span::<T>::blank()
: span::<T>::over(this._dat + from, length);
}
#! Finds where the view first covers an element equal to the one given.
#! @param this The span to search.
#! @param elem The element to look for.
#! @returns Maybe the index of the first equal element; otherwise, `nothing`.
#! @remarks Elements are compared with `equals`, so they are `equatable`. Bytes,
#! as text is, are searched eight or sixteen at a time.
pub def span<T>::index_of(&this, elem: &T): maybe<u64> {
if const T is integer && size_of::<T>() == 1 {
val at := find_byte(this._dat as *u8, 0, this._len, *(elem as *u8));
return nothing {} if at == this._len;
return at;
}
for index in 0..this._len {
if equals::<T>(&this._dat[index], elem) {
return index;
}
}
return nothing {};
}
#! Finds where the view last covers an element equal to the one given.
#! @param this The span to search.
#! @param elem The element to look for.
#! @returns Maybe the index of the last equal element; otherwise, `nothing`.
#! @remarks Elements are compared with `equals`, so they are `equatable`. Bytes,
#! as text is, are searched eight at a time.
pub def span<T>::last_index_of(&this, elem: &T): maybe<u64> {
if const T is integer && size_of::<T>() == 1 {
val at := find_last_byte(this._dat as *u8, 0, this._len, *(elem as *u8));
return nothing {} if at == this._len;
return at;
}
var index := this._len;
while index > 0 {
index--;
if equals::<T>(&this._dat[index], elem) {
return index;
}
}
return nothing {};
}
#! Counts the elements of the view equal to the one given.
#! @param this The span to search.
#! @param elem The element to count.
#! @returns How many equal it.
#! @remarks Elements are compared with `equals`, so they are `equatable`. Bytes,
#! as text is, are counted eight at a time, as the lines of text are
#! by counting `'\n'`.
pub def span<T>::count_of(&this, elem: &T): u64 {
if const T is integer && size_of::<T>() == 1 {
return count_byte(this._dat as *u8, 0, this._len, *(elem as *u8));
}
var count: u64 = 0;
for index in 0..this._len {
if equals::<T>(&this._dat[index], elem) {
count++;
}
}
return count;
}
#! Tells whether the view covers an element equal to the one given.
#! @param this The span to search.
#! @param elem The element to look for.
#! @returns Whether any element equals it.
pub def span<T>::contains(&this, elem: &T): bool {
return this.index_of(elem) is some;
}
#! Finds where a value goes among elements kept in order: the first element that
#! is not less than it.
#! @param this The span to search, whose elements are in the order `<` gives.
#! @param value The value to look for.
#! @returns The index of the first element not less than the value, or the
#! length where every element is less.
#! @remarks The elements are `ordered`, and halved until one is found, so a view
#! out of order gives an index that means nothing.
pub def span<T>::lower_bound(&this, value: &T): u64 {
if const !(T is ordered) {
compile_error("`lower_bound` searches by the order `<` gives, so the elements are `ordered`.");
}
var low: u64 = 0;
var high: u64 = this._len;
while low < high {
val middle := low + (high - low) / 2;
if this._dat[middle] < value {
low = middle + 1;
} else {
high = middle;
}
}
return low;
}
#! Finds where a value goes after those equal to it among elements kept in
#! order: the first element greater than it.
#! @param this The span to search, whose elements are in the order `<` gives.
#! @param value The value to look for.
#! @returns The index of the first element greater than the value, or the length
#! where none is.
#! @remarks The elements are `ordered`, and halved until one is found, so a view
#! out of order gives an index that means nothing.
pub def span<T>::upper_bound(&this, value: &T): u64 {
if const !(T is ordered) {
compile_error("`upper_bound` searches by the order `<` gives, so the elements are `ordered`.");
}
var low: u64 = 0;
var high: u64 = this._len;
while low < high {
val middle := low + (high - low) / 2;
if value < this._dat[middle] {
high = middle;
} else {
low = middle + 1;
}
}
return low;
}
#! Finds where elements a test holds of give way to those it does not, among
#! elements kept so that every one it holds of comes first.
#! @param this The span to search.
#! @param held The test, which holds of the elements before the index given
#! and of none from it on.
#! @returns The index of the first element the test does not hold of, or the
#! length where it holds of every one.
#! @remarks Elements are halved until it is found, so the test is called about
#! `log2(length)` times. It finds by a part of each element, such as an
#! offset, where `lower_bound` finds by the whole.
pub def span<T>::partition_point(&this, held: def(&T): bool): u64 {
var low: u64 = 0;
var high: u64 = this._len;
while low < high {
val middle := low + (high - low) / 2;
if held(&this._dat[middle]) {
low = middle + 1;
} else {
high = middle;
}
}
return low;
}
#! Compares one view to another for equality.
#! @param this The span to compare against.
#! @param other The span to compare with.
#! @returns Whether both cover the same number of equal elements, in the same
#! order. Two views of the same elements are equal, and so are two
#! empty views.
pub def span<T>::equals(&this, other: &span<T>): bool {
if this._len != other._len {
return false;
}
if this._dat == other._dat || this._len == 0 {
return true;
}
# Integers are equal where their bytes are, which are compared a word at a time.
if const T is integer {
return memcmp(this._dat as *u8, other._dat as *u8, bytes_of::<T>(this._len)) == 0;
}
for index in 0..this._len {
if !equals::<T>(&this._dat[index], &other._dat[index]) {
return false;
}
}
return true;
}
#! Hashes the view provided.
#! @param this The span to hash.
#! @returns The hash code of the view, which spans of equal elements share.
#! @remarks Elements are hashed by their `hash`, so they are `hashable`.
pub def span<T>::hash(&this): u64 {
# Integers hash as their bytes, a word at a time, which equal ones share.
if const T is integer {
return hash_bytes(this._dat as *u8, bytes_of::<T>(this._len));
}
var code := this._len;
for index in 0..this._len {
code = mix(code, hash(this._dat[index]));
}
return code;
}