#! @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`, 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::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::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::::over((&array) as *T, N)` written #! out. pub def span::over(data: *T, length: u64): span { 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::empty(): span { return span { _dat = null, _len = 0 }; } #! Tells whether the view covers no elements. #! @param this The span to test. #! @returns Whether its length is zero. pub def span::is_empty(&this): bool { return 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::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 = this; return &whole[index]; } #! Lends 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. pub def span::view(&this): span { return 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::cursor : sized_iterator<&T>, backward_iterator<&T> { var _at: *T; var _left: u64; } #! Gives the next element the view covers. #! @param this The cursor to advance. #! @returns Maybe a reference to the next element; otherwise, `nothing`. pub def span::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:: { _dat = here, _len = 1 }[0]; } #! How many elements the cursor has left to give. #! @param this The cursor. #! @returns How many. pub def span::cursor::remaining(&this): u64 { return this._left; } #! Gives the last element the cursor has left, from the far end of the view. #! @param this The cursor to advance from its far end. #! @returns Maybe a reference to the element; otherwise, `nothing`. pub def span::cursor::next_back(&mut this): maybe<&T> { if this._left == 0 { return nothing {}; } this._left = this._left - 1; return &span:: { _dat = this._at + this._left, _len = 1 }[0]; } #! Lends a cursor over the elements the view covers, which `for` walks. #! @param this The span to walk. #! @returns The cursor, which refers into whatever holds the elements. pub def span::iter(&this): span::cursor { return span::::cursor { _at = this._dat, _left = this._len }; } #! 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::get(&this, index: u64): maybe<&T> { # Reached as the view's element, whose check the test answers. return index < this._len ? &span:: { _dat = this._dat, _len = this._len }[index] : nothing {}; } #! Gets the first element the view covers. #! @param this The span to read. #! @returns Maybe the first element; otherwise, `nothing` for an empty view. pub def span::first(&this): maybe<&T> { return this.get(0); } #! Gets the last element the view covers. #! @param this The span to read. #! @returns Maybe the last element; otherwise, `nothing` for an empty view. pub def span::last(&this): maybe<&T> { return 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::sub(&this, from: u64, length: u64): maybe> { if from > this._len || length > this._len - from { return nothing {}; } return length == 0 ? span::::empty() : span::::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::index_of(&this, elem: &T): maybe { if const T is integer && size_of::() == 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::(&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::last_index_of(&this, elem: &T): maybe { if const T is integer && size_of::() == 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::(&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::count_of(&this, elem: &T): u64 { if const T is integer && size_of::() == 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::(&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::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::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::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::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::equals(&this, other: &span): 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::(this._len)) == 0; } for index in 0..this._len { if !equals::(&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::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::(this._len)); } var code := this._len; for index in 0..this._len { code = mix(code, hash(this._dat[index])); } return code; }