Initial Commit
This commit is contained in:
789 files changed
+149185
No files matched your search
@@ -0,0 +1,349 @@
|
||||
#! @file decimal.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the conversion of decimal text to the nearest 64-bit float, which
|
||||
#! `real::parse` reads numbers with.
|
||||
#!
|
||||
#! Most numbers have few enough digits, and a small enough exponent, that one
|
||||
#! multiplication or division of two exact floats gives the nearest float. The
|
||||
#! rest are converted as Go's `strconv` converts them: the number is held as
|
||||
#! decimal digits, and multiplied and divided by powers of two, which a decimal
|
||||
#! holds exactly, until its 53 bits can be read off and rounded.
|
||||
|
||||
module core;
|
||||
|
||||
# Digits a decimal holds; past them, only whether any was not zero matters.
|
||||
const decimal_capacity: u64 = 800;
|
||||
|
||||
# Digits a shift by 60 bits can add, which is how far one shift goes.
|
||||
const decimal_room: u64 = 19;
|
||||
|
||||
# The largest integer every integer below which a float holds exactly.
|
||||
const decimal_exact: u64 = 9007199254740992;
|
||||
|
||||
# How far to shift a decimal whose point is a number of digits from where it should
|
||||
# be, so the shift moves it by no more than that.
|
||||
const decimal_shifts: u64[9] = [1, 3, 6, 9, 13, 16, 19, 23, 26];
|
||||
|
||||
# A number as decimal digits, each from 0 to 9, with the point `point` digits from
|
||||
# the first, which may be before it or past the last.
|
||||
struct decimal_digits {
|
||||
var digits: u8[decimal_capacity + decimal_room];
|
||||
var count: u64;
|
||||
var point: i64;
|
||||
var truncated: bool; #! Whether a digit past those held was not zero.
|
||||
}
|
||||
|
||||
# The float nearest the number some digits write, which may hold a `.`, times ten
|
||||
# to an exponent, or `nothing` where it is too large for one.
|
||||
def decimal_digits::nearest(written: strview, exponent: i64): maybe<f64> {
|
||||
# The first 19 digits after the leading zeros, and whether those after were all
|
||||
# zero, which is all one exact multiplication needs.
|
||||
var mantissa := 0u;
|
||||
var held := 0u;
|
||||
var scale := exponent;
|
||||
var exact: bool = true;
|
||||
var dotted: bool = false;
|
||||
for byte in written {
|
||||
if byte == '.' {
|
||||
dotted = true;
|
||||
continue;
|
||||
}
|
||||
if held == 0 && byte == '0' {
|
||||
if dotted {
|
||||
scale--;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if held < 19 {
|
||||
mantissa = mantissa * 10 + byte.digit_value;
|
||||
held++;
|
||||
if dotted {
|
||||
scale--;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if byte != '0' {
|
||||
exact = false;
|
||||
}
|
||||
if !dotted {
|
||||
scale++;
|
||||
}
|
||||
}
|
||||
|
||||
if mantissa == 0 {
|
||||
return 0.0;
|
||||
}
|
||||
if exact && mantissa <= decimal_exact && scale >= -22 && scale <= 22 {
|
||||
val whole := mantissa as f64;
|
||||
return scale < 0 ? whole / decimal_digits::power_of_ten(-scale) : whole * decimal_digits::power_of_ten(scale);
|
||||
}
|
||||
|
||||
var number: decimal_digits;
|
||||
number.read(written, exponent);
|
||||
return number.to_float();
|
||||
}
|
||||
|
||||
# Reads the digits some text writes, without their leading zeros.
|
||||
def decimal_digits::read(&mut this, written: strview, exponent: i64): void {
|
||||
var dotted: bool = false;
|
||||
for byte in written {
|
||||
if byte == '.' {
|
||||
dotted = true;
|
||||
continue;
|
||||
}
|
||||
if this.count == 0 && byte == '0' {
|
||||
if dotted {
|
||||
this.point--;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if !dotted {
|
||||
this.point++;
|
||||
}
|
||||
if this.count < decimal_capacity {
|
||||
this.digits[this.count] = byte.digit_value as u8;
|
||||
this.count++;
|
||||
} else if byte != '0' {
|
||||
this.truncated = true;
|
||||
}
|
||||
}
|
||||
|
||||
this.point += exponent;
|
||||
this.trim();
|
||||
}
|
||||
|
||||
# The float nearest the number, as Go's `strconv` finds it, or `nothing` where it
|
||||
# is too large for one.
|
||||
def decimal_digits::to_float(&mut this): maybe<f64> {
|
||||
if this.count == 0 || this.point < -330 {
|
||||
return 0.0;
|
||||
}
|
||||
if this.point > 310 {
|
||||
return nothing{};
|
||||
}
|
||||
|
||||
# Halve or double the number, counting how often, until it is in [0.5, 1).
|
||||
var exponent: i64 = 0;
|
||||
while this.point > 0 {
|
||||
val by := decimal_digits::shift_for(this.point);
|
||||
this.shift(-by);
|
||||
exponent += by;
|
||||
}
|
||||
while this.point < 0 || (this.point == 0 && this.digits[0] < 5) {
|
||||
val by := decimal_digits::shift_for(-this.point);
|
||||
this.shift(by);
|
||||
exponent -= by;
|
||||
}
|
||||
|
||||
# A float's significand is in [1, 2), and below the smallest exponent a float
|
||||
# holds, the number is subnormal, with fewer bits.
|
||||
exponent--;
|
||||
if exponent < -1022 {
|
||||
val by := -1022 - exponent;
|
||||
this.shift(-by);
|
||||
exponent += by;
|
||||
}
|
||||
if exponent > 1023 {
|
||||
return nothing{};
|
||||
}
|
||||
|
||||
# Its 53 bits, rounded, which may round up to one bit more.
|
||||
this.shift(53);
|
||||
var mantissa := this.rounded();
|
||||
if mantissa == decimal_exact {
|
||||
mantissa = mantissa >> 1;
|
||||
exponent++;
|
||||
if exponent > 1023 {
|
||||
return nothing{};
|
||||
}
|
||||
}
|
||||
return (mantissa as f64) * decimal_digits::power_of_two(exponent - 52);
|
||||
}
|
||||
|
||||
# Multiplies the number by two to a power, which divides it where the power is
|
||||
# negative, sixty bits at a time.
|
||||
def decimal_digits::shift(&mut this, by: i64): void {
|
||||
if this.count == 0 {
|
||||
return;
|
||||
}
|
||||
|
||||
var left := by;
|
||||
while left > 60 {
|
||||
this.shift_left(60);
|
||||
left -= 60;
|
||||
}
|
||||
while left < -60 {
|
||||
this.shift_right(60);
|
||||
left += 60;
|
||||
}
|
||||
if left > 0 {
|
||||
this.shift_left(left as u64);
|
||||
}
|
||||
if left < 0 {
|
||||
this.shift_right((-left) as u64);
|
||||
}
|
||||
}
|
||||
|
||||
# Multiplies the number by two to a power of at most 60, writing the digits from
|
||||
# the last into the room past those held, then moving them to the front.
|
||||
def decimal_digits::shift_left(&mut this, by: u64): void {
|
||||
var read := this.count;
|
||||
var write := this.count + decimal_room;
|
||||
var carry := 0u;
|
||||
while read > 0 {
|
||||
read--;
|
||||
carry += (this.digits[read] as u64) << by;
|
||||
val quotient := carry / 10;
|
||||
write--;
|
||||
this.digits[write] = (carry - quotient * 10) as u8;
|
||||
carry = quotient;
|
||||
}
|
||||
while carry > 0 {
|
||||
val quotient := carry / 10;
|
||||
write--;
|
||||
this.digits[write] = (carry - quotient * 10) as u8;
|
||||
carry = quotient;
|
||||
}
|
||||
|
||||
val made := this.count + decimal_room - write;
|
||||
val first := (&mut this.digits) as *mut u8;
|
||||
memcpy(first, first + write, made);
|
||||
for k in decimal_capacity..made {
|
||||
if this.digits[k] != 0 {
|
||||
this.truncated = true;
|
||||
}
|
||||
}
|
||||
|
||||
this.point += (made - this.count) as i64;
|
||||
this.count = made < decimal_capacity ? made : decimal_capacity;
|
||||
this.trim();
|
||||
}
|
||||
|
||||
# Divides the number by two to a power of at most 60.
|
||||
def decimal_digits::shift_right(&mut this, by: u64): void {
|
||||
var read := 0u;
|
||||
var write := 0u;
|
||||
var carry := 0u;
|
||||
|
||||
# Enough of the first digits to divide, counting zeros past the last as digits.
|
||||
while (carry >> by) == 0 {
|
||||
if read >= this.count {
|
||||
if carry == 0 {
|
||||
this.count = 0;
|
||||
return;
|
||||
}
|
||||
while (carry >> by) == 0 {
|
||||
carry = carry * 10;
|
||||
read++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
carry = carry * 10 + this.digits[read];
|
||||
read++;
|
||||
}
|
||||
|
||||
this.point -= (read as i64) - 1;
|
||||
val mask := (1u << by) - 1;
|
||||
while read < this.count {
|
||||
val next := this.digits[read];
|
||||
this.digits[write] = (carry >> by) as u8;
|
||||
write++;
|
||||
carry = (carry & mask) * 10 + next;
|
||||
read++;
|
||||
}
|
||||
while carry > 0 {
|
||||
val digit := carry >> by;
|
||||
carry = (carry & mask) * 10;
|
||||
if write < decimal_capacity {
|
||||
this.digits[write] = digit as u8;
|
||||
write++;
|
||||
} else if digit > 0 {
|
||||
this.truncated = true;
|
||||
}
|
||||
}
|
||||
|
||||
this.count = write;
|
||||
this.trim();
|
||||
}
|
||||
|
||||
# The integer part of the number, rounded by the digits after the point, halfway
|
||||
# going to an even integer.
|
||||
def decimal_digits::rounded(&this): u64 {
|
||||
if this.point > 20 {
|
||||
return 0xFFFFFFFFFFFFFFFF;
|
||||
}
|
||||
|
||||
var whole := 0u;
|
||||
var at: i64 = 0;
|
||||
while at < this.point && at < (this.count as i64) {
|
||||
whole = whole * 10 + this.digits[at as u64];
|
||||
at++;
|
||||
}
|
||||
while at < this.point {
|
||||
whole = whole * 10;
|
||||
at++;
|
||||
}
|
||||
if this.rounds_up() {
|
||||
whole++;
|
||||
}
|
||||
return whole;
|
||||
}
|
||||
|
||||
# Whether the digits after the point round the integer part up: past halfway, or
|
||||
# exactly halfway with an odd integer part, or halfway with digits left out past it.
|
||||
def decimal_digits::rounds_up(&this): bool {
|
||||
if this.point < 0 || this.point >= (this.count as i64) {
|
||||
return false;
|
||||
}
|
||||
|
||||
val at := this.point as u64;
|
||||
if this.digits[at] == 5 && at + 1 == this.count {
|
||||
if this.truncated {
|
||||
return true;
|
||||
}
|
||||
return at > 0 && this.digits[at - 1] % 2 == 1;
|
||||
}
|
||||
return this.digits[at] >= 5;
|
||||
}
|
||||
|
||||
# Drops the zeros after the last digit that is not one.
|
||||
def decimal_digits::trim(&mut this): void {
|
||||
while this.count > 0 && this.digits[this.count - 1] == 0 {
|
||||
this.count--;
|
||||
}
|
||||
if this.count == 0 {
|
||||
this.point = 0;
|
||||
}
|
||||
}
|
||||
|
||||
def decimal_digits::shift_for(digits: i64): i64 {
|
||||
return digits < 9 ? decimal_shifts[digits as u64] as i64 : 27;
|
||||
}
|
||||
|
||||
# Ten to a power from 0 to 22, which a float holds exactly.
|
||||
def decimal_digits::power_of_ten(power: i64): f64 {
|
||||
var made: f64 = 1;
|
||||
for _ in 0..power {
|
||||
made *= 10;
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
# Two to a power from -1074 to 1023, which a float holds exactly.
|
||||
def decimal_digits::power_of_two(power: i64): f64 {
|
||||
var made: f64 = 1;
|
||||
var left := power;
|
||||
while left > 0 {
|
||||
made *= 2;
|
||||
left--;
|
||||
}
|
||||
while left < 0 {
|
||||
made *= 0.5;
|
||||
left++;
|
||||
}
|
||||
return made;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
#! @file pair.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains `pair`, two values kept together, as `enumerate` gives each element
|
||||
#! with its index and `zip` the elements of two iterators side by side.
|
||||
|
||||
module core;
|
||||
|
||||
|
||||
#! Two values kept together, each named for where it stands.
|
||||
#! @tparam A The type of the first.
|
||||
#! @tparam B The type of the second.
|
||||
pub struct pair<A, B> {
|
||||
pub var first: A; #! The first value.
|
||||
pub var second: B; #! The second value.
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
@@ -0,0 +1,199 @@
|
||||
#! @file sort.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the sorts of slices and vectors: `sort`, by the order `<` gives, and
|
||||
#! `sort_by`, by a function saying whether one element comes before another.
|
||||
#! Both are stable, keeping elements neither comes before in the order they were
|
||||
#! in, so what is sorted comes out the same however many times it is sorted.
|
||||
module core;
|
||||
|
||||
|
||||
#! Puts the elements in order, least first, by the order `<` gives.
|
||||
#! @param this The slice to sort, whose elements are `ordered`.
|
||||
#! @remarks The sort is stable: elements equal to each other keep the order they
|
||||
#! were in. Runs of a few are sorted by insertion and then merged, which
|
||||
#! takes memory for as many elements again while it sorts. Elements
|
||||
#! are moved byte for byte, so nothing is closed or copied.
|
||||
pub def slice<T>::sort(&mut this): void {
|
||||
sort_elements::<T, natural_order<T>>(this._dat, this._len, &natural_order::<T> {});
|
||||
}
|
||||
|
||||
#! Puts the elements in an order a function gives.
|
||||
#! @param this The slice to sort.
|
||||
#! @param before Whether one element comes before another, which holds of no
|
||||
#! element and itself, as `<` does.
|
||||
#! @remarks The sort is stable, as `sort` is: elements neither comes before keep
|
||||
#! the order they were in.
|
||||
pub def slice<T>::sort_by(&mut this, before: def(&T, &T): bool): void {
|
||||
sort_elements::<T, given_order<T>>(this._dat, this._len, &given_order::<T> { before_of = before });
|
||||
}
|
||||
|
||||
#! Puts the elements in order, least first, by the order `<` gives.
|
||||
#! @param this The vector to sort, whose elements are `ordered`.
|
||||
#! @remarks The sort is stable, as a slice's `sort` is.
|
||||
pub def vector<T>::sort(&mut this): void {
|
||||
var whole := this.slice;
|
||||
whole.sort();
|
||||
}
|
||||
|
||||
#! Puts the elements in an order a function gives.
|
||||
#! @param this The vector to sort.
|
||||
#! @param before Whether one element comes before another, which holds of no
|
||||
#! element and itself, as `<` does.
|
||||
#! @remarks The sort is stable, as a slice's `sort_by` is.
|
||||
pub def vector<T>::sort_by(&mut this, before: def(&T, &T): bool): void {
|
||||
var whole := this.slice;
|
||||
whole.sort_by(before);
|
||||
}
|
||||
|
||||
# What a sort orders elements by: whether one comes before another.
|
||||
trait sort_order<T> {
|
||||
def before(&this, first: &T, second: &T): bool;
|
||||
}
|
||||
|
||||
# The order `<` gives.
|
||||
struct natural_order<T> : sort_order<T> {}
|
||||
|
||||
def natural_order<T>::before(&this, first: &T, second: &T): bool {
|
||||
if const !(T is ordered) {
|
||||
compile_error("`sort` puts elements in the order `<` gives, so they are `ordered`. `sort_by` takes an order of its own.");
|
||||
}
|
||||
return first < second;
|
||||
}
|
||||
|
||||
# The order a function gives.
|
||||
struct given_order<T> : sort_order<T> {
|
||||
var before_of: def(&T, &T): bool;
|
||||
}
|
||||
|
||||
def given_order<T>::before(&this, first: &T, second: &T): bool {
|
||||
return this.before_of(first, second);
|
||||
}
|
||||
|
||||
# How many elements a run sorted by insertion holds, before runs are merged.
|
||||
const sort_run: u64 = 16;
|
||||
|
||||
# Sorts elements in place, stably: runs of a few by insertion, then each pair of
|
||||
# runs merged, back and forth between the elements and as many slots again.
|
||||
def sort_elements<T, O>(data: *mut T, count: u64, order: &O): void where O: sort_order<T> {
|
||||
return if count < 2;
|
||||
|
||||
var at: u64 = 0;
|
||||
while at < count {
|
||||
insert_sorted::<T, O>(data + at, count - at < sort_run ? count - at : sort_run, order);
|
||||
at += sort_run;
|
||||
}
|
||||
return if count <= sort_run;
|
||||
|
||||
# The spare slots hold the elements a while, so they are cleared where the
|
||||
# elements close, as `acquire` clears them.
|
||||
val spare := acquire::<T>(nothing {}, count);
|
||||
var from := data;
|
||||
var to := spare;
|
||||
var width := sort_run;
|
||||
while width < count {
|
||||
var low: u64 = 0;
|
||||
while low < count {
|
||||
val middle := low + width < count ? low + width : count;
|
||||
val high := low + 2 * width < count ? low + 2 * width : count;
|
||||
merge_runs::<T, O>(from, low, middle, high, to, order);
|
||||
low = high;
|
||||
}
|
||||
val swap := from;
|
||||
from = to;
|
||||
to = swap;
|
||||
width *= 2;
|
||||
}
|
||||
|
||||
if from != data {
|
||||
move_elements::<T>(data, from, count);
|
||||
}
|
||||
dispose::<T>(nothing {}, spare, count);
|
||||
}
|
||||
|
||||
# Sorts a few elements in place by insertion, which keeps equal ones in order.
|
||||
# Where each goes is found while it is still in its place, so the order is only
|
||||
# asked about elements in the block: one held aside in a local would be put on
|
||||
# the heap each time, where the order is a function's.
|
||||
def insert_sorted<T, O>(data: *mut T, count: u64, order: &O): void where O: sort_order<T> {
|
||||
if const T is closeable {
|
||||
# What closes is held aside byte for byte, in a slot of its own, so nothing is
|
||||
# closed or copied as it moves.
|
||||
val held := acquire::<T>(nothing {}, 1);
|
||||
for i in 1..count {
|
||||
val j := insertion_place::<T, O>(data, i, order);
|
||||
continue if j == i;
|
||||
move_elements::<T>(held, data + i, 1);
|
||||
var k := i;
|
||||
while k > j {
|
||||
move_elements::<T>(data + k, data + k - 1, 1);
|
||||
k--;
|
||||
}
|
||||
move_elements::<T>(data + j, held, 1);
|
||||
}
|
||||
dispose::<T>(nothing {}, held, 1);
|
||||
return;
|
||||
}
|
||||
|
||||
for i in 1..count {
|
||||
val j := insertion_place::<T, O>(data, i, order);
|
||||
continue if j == i;
|
||||
val held: T = data[i];
|
||||
var k := i;
|
||||
while k > j {
|
||||
data[k] = data[k - 1];
|
||||
k--;
|
||||
}
|
||||
data[j] = held;
|
||||
}
|
||||
}
|
||||
|
||||
# Where an element goes among those before it, which are in order: after the
|
||||
# last it does not come before.
|
||||
def insertion_place<T, O>(data: *mut T, at: u64, order: &O): u64 where O: sort_order<T> {
|
||||
var j := at;
|
||||
while j > 0 && order.before(&data[at], &data[j - 1]) {
|
||||
j--;
|
||||
}
|
||||
return j;
|
||||
}
|
||||
|
||||
# Merges two runs next to each other, each in order, into the same places of
|
||||
# another block, taking from the first where neither comes before the other.
|
||||
def merge_runs<T, O>(from: *mut T, low: u64, middle: u64, high: u64, to: *mut T, order: &O): void where O: sort_order<T> {
|
||||
var left := low;
|
||||
var right := middle;
|
||||
var out := low;
|
||||
while left < middle && right < high {
|
||||
if order.before(&from[right], &from[left]) {
|
||||
move_elements::<T>(to + out, from + right, 1);
|
||||
right++;
|
||||
} else {
|
||||
move_elements::<T>(to + out, from + left, 1);
|
||||
left++;
|
||||
}
|
||||
out++;
|
||||
}
|
||||
|
||||
if left < middle {
|
||||
move_elements::<T>(to + out, from + left, middle - left);
|
||||
out += middle - left;
|
||||
}
|
||||
if right < high {
|
||||
move_elements::<T>(to + out, from + right, high - right);
|
||||
}
|
||||
}
|
||||
|
||||
# Moves elements byte for byte, which neither closes what they land on nor copies
|
||||
# what they hold, as a sort only puts them in other places.
|
||||
def move_elements<T>(to: *mut T, from: *mut T, count: u64): void {
|
||||
if const !(T is closeable) {
|
||||
for i in 0..count {
|
||||
to[i] = from[i];
|
||||
}
|
||||
return;
|
||||
}
|
||||
memcpy(to as *mut u8, from as *u8, bytes_of::<T>(count));
|
||||
}
|
||||
@@ -0,0 +1,397 @@
|
||||
#! @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;
|
||||
}
|
||||
@@ -0,0 +1,468 @@
|
||||
#! @file string.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the methods of `string`, which owns the bytes of text it holds, and
|
||||
#! the functions that build one and give it back.
|
||||
#!
|
||||
#! `string` is among the types the syntax makes, which the compiler declares
|
||||
#! itself, since interpolation, as `$"{count} items"`, builds one. A string is
|
||||
#! `owning`, so the memory goes back where its scope ends. Text that is only read
|
||||
#! is a `strview`, which owns nothing: a literal such as `"hello"` is one, and so
|
||||
#! is what `span` gives. A string whose allocator is `nothing` holds memory from
|
||||
#! `raw_allocate`, and one made with an allocator gives its memory back to that
|
||||
#! allocator. Either way the zero value of a string holds no memory, so closing
|
||||
#! one that was never built does nothing.
|
||||
module core;
|
||||
|
||||
|
||||
#! The number of bytes the string holds.
|
||||
#! @param this The string to measure.
|
||||
#! @returns How many bytes of text it holds, which is no more than its capacity.
|
||||
pub val string::length: u64 {
|
||||
get => this._len;
|
||||
}
|
||||
|
||||
#! The number of bytes the string has room for.
|
||||
#! @param this The string to measure.
|
||||
#! @returns How many bytes it can hold before it has to grow.
|
||||
pub val string::capacity: u64 {
|
||||
get => this._cap;
|
||||
}
|
||||
|
||||
#! The allocator the string takes its memory from, or `nothing` where it takes
|
||||
#! it from `raw_allocate`, so what is made from it can take memory alike.
|
||||
#! @param this The string.
|
||||
#! @returns The allocator, or `nothing`.
|
||||
pub val string::alloc: maybe<&mut allocator> {
|
||||
get => this._alc;
|
||||
}
|
||||
|
||||
#! Whether the string holds no text.
|
||||
#! @param this The string to check.
|
||||
#! @returns Whether its length is zero.
|
||||
pub val string::empty: bool {
|
||||
get => this._len == 0;
|
||||
}
|
||||
|
||||
#! Whether the string has no room left, so the next byte added makes it grow.
|
||||
#! @param this The string to check.
|
||||
#! @returns Whether its length is its capacity.
|
||||
pub val string::full: bool {
|
||||
get => this._len == this._cap;
|
||||
}
|
||||
|
||||
#! Makes a string that holds no text yet.
|
||||
#! @param alloc An optional allocator for the string to take memory from. A
|
||||
#! string given none takes it from `raw_allocate`.
|
||||
#! @returns The empty string, which holds no memory until it is written to.
|
||||
pub def string::new(alloc: maybe<&mut allocator>): string {
|
||||
return string { _alc = alloc, _dat = null, _len = 0, _cap = 0 };
|
||||
}
|
||||
|
||||
#! Makes a string holding a copy of the text given.
|
||||
#! @param text The text to copy, such as a literal or a view of one.
|
||||
#! @param alloc An optional allocator for the string to take memory from.
|
||||
#! @returns The string, which holds no text where there was no memory for it.
|
||||
#! @remarks An allocator that gives no memory stops the program.
|
||||
pub def string::from(text: strview, alloc: maybe<&mut allocator>): string {
|
||||
var made := string::new(alloc);
|
||||
made.append(text);
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Views some of the text a string holds, without copying it, as `spannable`
|
||||
#! asks, which `s[start:count]` calls.
|
||||
#! @param other The string to view.
|
||||
#! @param start The index of the first byte viewed.
|
||||
#! @param count How many bytes are viewed, or `~0u` for every one after the
|
||||
#! start.
|
||||
#! @returns A view of those bytes, which is only good until the string grows,
|
||||
#! shrinks, or closes.
|
||||
#! @remarks A start or count past the last byte stops a checked program.
|
||||
pub def string::span_of(other: &string, start: u64 = 0, count: u64 = ~0u): strview {
|
||||
val whole := strview::over(other._dat as *u8, other._len);
|
||||
return count == ~0u ? whole[start:] : whole[start:count];
|
||||
}
|
||||
|
||||
#! Lends a writable view of some of the bytes a string holds, without copying
|
||||
#! them, as `sliceable` asks, which `s[start:count]` calls where `s` can be
|
||||
#! written.
|
||||
#! @param other The string to view, which can be written.
|
||||
#! @param start The index of the first byte viewed.
|
||||
#! @param count How many bytes are viewed, or `~0u` for every one after the
|
||||
#! start.
|
||||
#! @returns A view of those bytes, which writes them where they are, and is only
|
||||
#! good until the string grows, shrinks, or closes.
|
||||
#! @remarks A start or count past the last byte stops a checked program.
|
||||
pub def string::slice_of(other: &mut string, start: u64 = 0, count: u64 = ~0u): slice<u8> {
|
||||
val whole := slice::<u8>::over(other._dat, other._len);
|
||||
return count == ~0u ? whole[start:] : whole[start:count];
|
||||
}
|
||||
|
||||
#! A view of the text the string holds, which copies nothing.
|
||||
#! @param this The string to view.
|
||||
#! @returns A view of its bytes, which is only good until the string grows,
|
||||
#! shrinks, or closes.
|
||||
@inline
|
||||
pub val string::span: strview {
|
||||
get => strview::over(this._dat as *u8, this._len);
|
||||
}
|
||||
|
||||
#! A writable view of the bytes the string holds, read of a string that can be
|
||||
#! written.
|
||||
#! @param this The string to view.
|
||||
#! @returns A view of its bytes, which writes them where they are, and is only
|
||||
#! good until the string grows, shrinks, or closes.
|
||||
@inline
|
||||
pub val string::slice: slice<u8> {
|
||||
get mut => slice::<u8>::over(this._dat, this._len);
|
||||
}
|
||||
|
||||
#! Reads the byte at an index, which `s[index]` calls.
|
||||
#! @param this The string to read.
|
||||
#! @param index The index of the byte, below `length`, past which a checked
|
||||
#! program stops.
|
||||
#! @returns The byte, which is only good until the string grows, shrinks, or
|
||||
#! closes.
|
||||
pub def string::at(&this, index: u64): &u8 {
|
||||
# Indexed as a view of the bytes, it is checked as a view's index is.
|
||||
return &span::<u8> { _dat = this._dat, _len = this._len }[index];
|
||||
}
|
||||
|
||||
#! Reaches the byte at an index to write it, which `s[index] = b` calls.
|
||||
#! @param this The string to write.
|
||||
#! @param index The index of the byte, below `length`, past which a checked
|
||||
#! program stops.
|
||||
#! @returns The byte, which is only good until the string grows, shrinks, or
|
||||
#! closes.
|
||||
pub def string::at(&mut this, index: u64): &mut u8 {
|
||||
return &mut slice::<u8> { _dat = this._dat, _len = this._len }[index];
|
||||
}
|
||||
|
||||
#! Gets the byte at an index, if the string holds one there, for an index that
|
||||
#! may be past the end in a program that is right.
|
||||
#! @param this The string to read.
|
||||
#! @param index The index of the byte.
|
||||
#! @returns Maybe the byte, which is only good until the string grows, shrinks,
|
||||
#! or closes; otherwise, `nothing`.
|
||||
pub def string::get(&this, index: u64): maybe<&u8> {
|
||||
# Reached as a view's byte, whose check the test answers.
|
||||
return index < this._len
|
||||
? &span::<u8> { _dat = this._dat, _len = this._len }[index]
|
||||
: nothing {};
|
||||
}
|
||||
|
||||
#! Gets the byte at an index to write it, if the string holds one there.
|
||||
#! @param this The string to write.
|
||||
#! @param index The index of the byte.
|
||||
#! @returns Maybe the byte, which is only good until the string grows, shrinks,
|
||||
#! or closes; otherwise, `nothing`.
|
||||
pub def string::get(&mut this, index: u64): maybe<&mut u8> {
|
||||
return index < this._len
|
||||
? &mut slice::<u8> { _dat = this._dat, _len = this._len }[index]
|
||||
: nothing {};
|
||||
}
|
||||
|
||||
#! Makes room for at least a number of bytes, keeping the text it holds.
|
||||
#! @param this The string to make room in.
|
||||
#! @param capacity The number of bytes it should have room for.
|
||||
#! @remarks A string already that large is left as it is, and an allocator that
|
||||
#! gives no more memory stops the program.
|
||||
pub def string::reserve(&mut this, capacity: u64): void {
|
||||
if capacity <= this._cap {
|
||||
return;
|
||||
}
|
||||
|
||||
this.recapacity(capacity);
|
||||
}
|
||||
|
||||
#! Makes the string hold a number of bytes, adding zero bytes after the last or
|
||||
#! removing those past it, as a reader filling the room it made does.
|
||||
#! @param this The string to resize.
|
||||
#! @param length How many bytes it holds after.
|
||||
#! @remarks The memory is kept where it shrinks, and an allocator that gives no
|
||||
#! more memory stops the program where it grows.
|
||||
pub def string::resize(&mut this, length: u64): void {
|
||||
if length > this._cap {
|
||||
this.grow(length);
|
||||
}
|
||||
if length > this._len {
|
||||
memset(this._dat + this._len, 0, length - this._len);
|
||||
}
|
||||
this._len = length;
|
||||
}
|
||||
|
||||
#! Adds a byte after the last.
|
||||
#! @param this The string to append to.
|
||||
#! @param byte The byte to append.
|
||||
#! @remarks An allocator that gives no more memory stops the program.
|
||||
pub def string::push(&mut this, byte: u8): void {
|
||||
if this._len == this._cap {
|
||||
this.grow(this._len + 1);
|
||||
}
|
||||
|
||||
this._dat[this._len] = byte;
|
||||
this._len++;
|
||||
}
|
||||
|
||||
#! Adds the bytes of some text after the last.
|
||||
#! @param this The string to append to.
|
||||
#! @param text The text to append, which may view this string's own bytes.
|
||||
#! @remarks An allocator that gives no more memory stops the program.
|
||||
pub def string::append(&mut this, text: strview): void {
|
||||
val count := text.length;
|
||||
if count == 0 {
|
||||
return;
|
||||
}
|
||||
|
||||
if this._len + count > this._cap {
|
||||
this.grow(this._len + count);
|
||||
}
|
||||
|
||||
memcpy(this._dat + this._len, text.data, count);
|
||||
this._len += count;
|
||||
}
|
||||
|
||||
#! Removes every byte, keeping the memory the string holds them in.
|
||||
#! @param this The string to empty.
|
||||
pub def string::clear(&mut this): void {
|
||||
this._len = 0;
|
||||
}
|
||||
|
||||
#! Gives back the memory the string holds, leaving it empty.
|
||||
#! @param this The string to close out.
|
||||
pub def string::close(&mut this): void {
|
||||
if this._dat == null {
|
||||
return;
|
||||
}
|
||||
|
||||
match this._alc {
|
||||
some(owner) => owner.dispose(this._dat, this._cap);
|
||||
none => raw_release(this._dat);
|
||||
}
|
||||
|
||||
this._dat = null;
|
||||
this._len = 0;
|
||||
this._cap = 0;
|
||||
}
|
||||
|
||||
#! Compares one string to another for equality.
|
||||
#! @param this The string to compare against.
|
||||
#! @param other The string to compare with.
|
||||
#! @returns Whether both hold the same bytes, in the same order.
|
||||
pub def string::equals(&this, other: &string): bool {
|
||||
return this.span.equals(&other.span);
|
||||
}
|
||||
|
||||
#! Compares one string with another, by their bytes and then by their length.
|
||||
#! @param this The string to compare against.
|
||||
#! @param other The string to compare with.
|
||||
#! @retval -1 Where this string orders before the other.
|
||||
#! @retval 0 Where they hold the same bytes.
|
||||
#! @retval 1 Where this string orders after the other.
|
||||
pub def string::compare(&this, other: &string): i64 {
|
||||
return this.span.compare(other.span);
|
||||
}
|
||||
|
||||
#! What `iter(&this)` lends, which reads the bytes where the string holds them.
|
||||
pub type string::iterator = span<u8>::iterator;
|
||||
|
||||
#! What `iter(&mut this)` lends, which writes the bytes where the string holds
|
||||
#! them.
|
||||
pub type string::mutator = slice<u8>::mutator;
|
||||
|
||||
#! Lends an iterator that reads the bytes, in order, which `for byte in s`
|
||||
#! walks.
|
||||
#! @param this The string to walk.
|
||||
#! @returns The iterator, which is only good until the string grows, shrinks, or
|
||||
#! closes.
|
||||
pub def string::iter(&this): span<u8>::iterator {
|
||||
return this.span.iter();
|
||||
}
|
||||
|
||||
#! Lends a mutator that writes the bytes where they are, which `for &mut byte
|
||||
#! in s` walks.
|
||||
#! @param this The string to walk.
|
||||
#! @returns The mutator, which is only good until the string grows, shrinks, or
|
||||
#! closes.
|
||||
pub def string::iter(&mut this): slice<u8>::mutator {
|
||||
var whole := this.slice;
|
||||
return whole.iter();
|
||||
}
|
||||
|
||||
#! An iterator that reads the bytes, in order, as `iter` lends.
|
||||
#! @param this The string to walk.
|
||||
#! @returns The iterator, which is only good until the string grows, shrinks, or
|
||||
#! closes.
|
||||
@inline
|
||||
pub val string::iterator: span<u8>::iterator {
|
||||
get => this.span.iter();
|
||||
}
|
||||
|
||||
#! A mutator that writes the bytes where they are, read of a string that can be
|
||||
#! written.
|
||||
#! @param this The string to walk.
|
||||
#! @returns The mutator, which is only good until the string grows, shrinks, or
|
||||
#! closes.
|
||||
@inline
|
||||
pub val string::mutator: slice<u8>::mutator {
|
||||
get mut => this.iter();
|
||||
}
|
||||
|
||||
#! Hashes the string provided.
|
||||
#! @param this The string to hash.
|
||||
#! @returns The hash code of the text it holds, which equal strings share.
|
||||
pub def string::hash(&this): u64 {
|
||||
return this.span.hash();
|
||||
}
|
||||
|
||||
#! Builds a string holding the same text as another, with its allocator.
|
||||
#! @param other The string to copy text from.
|
||||
#! @returns The copy, which owns memory of its own.
|
||||
pub def string::copy_of(other: &string): string {
|
||||
return string::from(other.span, other._alc);
|
||||
}
|
||||
|
||||
#! Builds a string holding the text of another, leaving the other empty.
|
||||
#! @param other The string to move text from, which holds nothing after.
|
||||
#! @returns The moved string, which takes the memory and the allocator with it.
|
||||
#! @remarks Nothing is copied: the memory moves as it stands, and the other is
|
||||
#! left empty rather than dead, so closing it afterwards is harmless.
|
||||
pub def string::move_of(other: &mut string): string {
|
||||
var made: string = string {
|
||||
_alc = other._alc,
|
||||
_dat = other._dat,
|
||||
_len = other._len,
|
||||
_cap = other._cap
|
||||
};
|
||||
|
||||
other._dat = null;
|
||||
other._len = 0;
|
||||
other._cap = 0;
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Views a run of the text, from an index for a number of bytes.
|
||||
#! @param this The string to narrow.
|
||||
#! @param start Where the narrower view begins, from `0` to `length`.
|
||||
#! @param count How many bytes it covers, cut short by the end of the text.
|
||||
#! @returns A view of that run, which is empty where it begins past the end, and
|
||||
#! is only good until the string grows, shrinks, or closes.
|
||||
pub def string::substr(&this, start: u64, count: u64): strview {
|
||||
return this.span.substr(start, count);
|
||||
}
|
||||
|
||||
#! Tells whether the text begins with some other text.
|
||||
#! @param this The string to read.
|
||||
#! @param prefix The text to look for at its beginning.
|
||||
#! @returns Whether it begins with the prefix, which empty text always does.
|
||||
pub def string::starts_with(&this, prefix: strview): bool {
|
||||
return this.span.starts_with(prefix);
|
||||
}
|
||||
|
||||
#! Tells whether the text ends with some other text.
|
||||
#! @param this The string to read.
|
||||
#! @param suffix The text to look for at its end.
|
||||
#! @returns Whether it ends with the suffix, which empty text always does.
|
||||
pub def string::ends_with(&this, suffix: strview): bool {
|
||||
return this.span.ends_with(suffix);
|
||||
}
|
||||
|
||||
#! Makes a string of the text without the whitespace at its beginning.
|
||||
#! @param this The string to read.
|
||||
#! @returns A new string holding what is left, from the same allocator.
|
||||
pub def string::ltrim(&this): string {
|
||||
return string::from(this.span.ltrim(), this._alc);
|
||||
}
|
||||
|
||||
#! Makes a string of the text without the whitespace at its end.
|
||||
#! @param this The string to read.
|
||||
#! @returns A new string holding what is left, from the same allocator.
|
||||
pub def string::rtrim(&this): string {
|
||||
return string::from(this.span.rtrim(), this._alc);
|
||||
}
|
||||
|
||||
#! Makes a string of the text without the whitespace at either end.
|
||||
#! @param this The string to read.
|
||||
#! @returns A new string holding what is left, from the same allocator.
|
||||
pub def string::trim(&this): string {
|
||||
return string::from(this.span.trim(), this._alc);
|
||||
}
|
||||
|
||||
#! Compares the text with other text, by their bytes and then by their length.
|
||||
#! @param this The string to compare against.
|
||||
#! @param other The text to compare with.
|
||||
#! @retval -1 Where this string orders before the other.
|
||||
#! @retval 0 Where they hold the same bytes.
|
||||
#! @retval 1 Where this string orders after the other.
|
||||
pub def string::compare(&this, other: strview): i64 {
|
||||
return this.span.compare(other);
|
||||
}
|
||||
|
||||
#! Makes text of the string with every letter in upper case.
|
||||
#! @param this The string to read.
|
||||
#! @returns A new string holding the converted text.
|
||||
pub def string::upper(&this): string {
|
||||
return this.span.upper();
|
||||
}
|
||||
|
||||
#! Makes text of the string with every letter in lower case.
|
||||
#! @param this The string to read.
|
||||
#! @returns A new string holding the converted text.
|
||||
pub def string::lower(&this): string {
|
||||
return this.span.lower();
|
||||
}
|
||||
|
||||
#! Makes text of a width, with the string's text at its end and a byte before
|
||||
#! it.
|
||||
#! @param this The string to read.
|
||||
#! @param width How many bytes the text should hold.
|
||||
#! @param fill The byte to put before it.
|
||||
#! @returns A new string holding the padded text, which is a copy of this one
|
||||
#! where it is already that wide.
|
||||
pub def string::lpad(&this, width: u64, fill: u8): string {
|
||||
return this.span.lpad(width, fill);
|
||||
}
|
||||
|
||||
#! Makes text of a width, with the string's text at its beginning and a byte
|
||||
#! after it.
|
||||
#! @param this The string to read.
|
||||
#! @param width How many bytes the text should hold.
|
||||
#! @param fill The byte to put after it.
|
||||
#! @returns A new string holding the padded text, which is a copy of this one
|
||||
#! where it is already that wide.
|
||||
pub def string::rpad(&this, width: u64, fill: u8): string {
|
||||
return this.span.rpad(width, fill);
|
||||
}
|
||||
|
||||
# Gives the string room for more bytes than it holds, which is what it needs
|
||||
# before more are added.
|
||||
def string::grow(&mut this, wanted: u64): void {
|
||||
var capacity := this._cap == 0 ? 16u : this._cap;
|
||||
while capacity < wanted {
|
||||
capacity *= 2;
|
||||
}
|
||||
|
||||
this.recapacity(capacity);
|
||||
}
|
||||
|
||||
# Moves the bytes to memory of another size, keeping what fits. An allocator that
|
||||
# gives no memory stops the program, as `resize` does.
|
||||
def string::recapacity(&mut this, capacity: u64): void {
|
||||
if capacity == this._cap {
|
||||
return;
|
||||
}
|
||||
|
||||
if capacity == 0 {
|
||||
this.close();
|
||||
return;
|
||||
}
|
||||
|
||||
this._dat = resize::<u8>(this._alc, this._dat, this._cap, capacity);
|
||||
this._cap = capacity;
|
||||
if this._len > capacity {
|
||||
this._len = capacity;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,301 @@
|
||||
#! @file strview.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains `strview`, the view of text that is only read, and the methods of
|
||||
#! text: narrowing, splitting, finding, trimming, comparing, changing case,
|
||||
#! padding, and reading numbers.
|
||||
#!
|
||||
#! Anything that only reads text takes a `strview`, so a literal, a view of one,
|
||||
#! and the text a `string` lends are all read by the same function, since a
|
||||
#! string lends a view of itself wherever one is wanted. Anything that makes new
|
||||
#! text gives a `string`, which owns the memory it is written into and gives it
|
||||
#! back where its scope ends.
|
||||
#!
|
||||
#! `strview` is an alias of `span<u8>`, among the types the syntax makes, which
|
||||
#! the compiler declares itself, so it converts to and from one implicitly and
|
||||
#! has its methods too. The methods of text are declared on the
|
||||
#! alias because a method cannot be declared on one instance of a generic type:
|
||||
#! `def span<u8>::trim` would declare a type parameter named `u8`.
|
||||
|
||||
import core::swar;
|
||||
module core;
|
||||
|
||||
|
||||
#! Views a run of the text, from an index for a number of bytes.
|
||||
#! @param this The text to narrow.
|
||||
#! @param start Where the narrower view begins, from `0` to `length`.
|
||||
#! @param count How many bytes it covers, cut short by the end of the text.
|
||||
#! @returns A view of that run, which is empty where it begins past the end.
|
||||
@inline
|
||||
pub def strview::substr(&this, start: u64, count: u64): strview {
|
||||
if start >= this.length {
|
||||
return strview::blank();
|
||||
}
|
||||
|
||||
val left := this.length - start;
|
||||
return this.sub(start, count < left ? count : left).some;
|
||||
}
|
||||
|
||||
#! Tells whether the text begins with some other text.
|
||||
#! @param this The text to read.
|
||||
#! @param prefix The text to look for at its beginning.
|
||||
#! @returns Whether it begins with the prefix, which empty text always does.
|
||||
pub def strview::starts_with(&this, prefix: strview): bool {
|
||||
return
|
||||
this.length >= prefix.length &&
|
||||
this.substr(0, prefix.length).equals(&prefix);
|
||||
}
|
||||
|
||||
#! Tells whether the text ends with some other text.
|
||||
#! @param this The text to read.
|
||||
#! @param suffix The text to look for at its end.
|
||||
#! @returns Whether it ends with the suffix, which empty text always does.
|
||||
pub def strview::ends_with(&this, suffix: strview): bool {
|
||||
return
|
||||
this.length >= suffix.length &&
|
||||
this.substr(this.length - suffix.length, suffix.length).equals(&suffix);
|
||||
}
|
||||
|
||||
#! Finds where a run of text first begins within the text.
|
||||
#! @param this The text to search.
|
||||
#! @param text The run to look for.
|
||||
#! @returns Maybe the index where it first begins; otherwise, `nothing`. Empty
|
||||
#! text is found at 0.
|
||||
#! @remarks Its first and last bytes are looked for at once, at eight places a
|
||||
#! step, and the bytes between compared only where both are found, as
|
||||
#! Wojciech Muła's search does with vectors.
|
||||
pub def strview::find(&this, text: strview): maybe<u64> {
|
||||
val length := text.length;
|
||||
return 0 if length == 0;
|
||||
return nothing {} if length > this._len;
|
||||
return this.index_of(&text[0]) if length == 1;
|
||||
|
||||
# The places it may begin are 0 to `last`, eight at a time, then the last few
|
||||
# read without passing the end.
|
||||
val data := this._dat;
|
||||
val last := this._len - length;
|
||||
val first_byte := swar::repeat(text[0]);
|
||||
val last_byte := swar::repeat(text[length - 1]);
|
||||
var at: u64 = 0;
|
||||
while at <= last {
|
||||
val places := last + 1 - at;
|
||||
var firsts: swar::bytes8;
|
||||
var lasts: swar::bytes8;
|
||||
if places >= 8 {
|
||||
firsts = swar::load(data + at) ^ first_byte;
|
||||
lasts = swar::load(data + at + length - 1) ^ last_byte;
|
||||
} else {
|
||||
firsts = swar::load_last(data + at, places) ^ first_byte;
|
||||
lasts = swar::load_last(data + at + length - 1, places) ^ last_byte;
|
||||
}
|
||||
var found: swar::mask8 = firsts.zero() & lasts.zero() & swar::mask8::leading(places);
|
||||
while found.any() {
|
||||
val begins := at + found.first();
|
||||
return begins if memcmp(data + begins + 1, text._dat + 1, length - 2) == 0;
|
||||
found = found.rest();
|
||||
}
|
||||
at += 8;
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
#! Finds where the text first holds any of some bytes.
|
||||
#! @param this The text to search.
|
||||
#! @param bytes The bytes to look for.
|
||||
#! @returns Maybe the index of the first; otherwise, `nothing`, as for no bytes.
|
||||
#! @remarks Up to four bytes are looked for eight places a step, and more a byte
|
||||
#! at a time, through a set of the 256.
|
||||
pub def strview::find_any(&this, bytes: span<u8>): maybe<u64> {
|
||||
val count := bytes.length;
|
||||
return nothing {} if count == 0;
|
||||
val data := this._dat;
|
||||
if count <= 4 {
|
||||
# Each byte repeated, where the first stands for any not given.
|
||||
val a := swar::repeat(bytes[0]);
|
||||
val b := swar::repeat(bytes[count > 1 ? 1 : 0]);
|
||||
val c := swar::repeat(bytes[count > 2 ? 2 : 0]);
|
||||
val d := swar::repeat(bytes[count > 3 ? 3 : 0]);
|
||||
var at: u64 = 0;
|
||||
while at < this._len {
|
||||
val left := this._len - at;
|
||||
val word := left >= 8 ? swar::load(data + at) : swar::load_last(data + at, left);
|
||||
val wa: swar::bytes8 = word ^ a;
|
||||
val wb: swar::bytes8 = word ^ b;
|
||||
val wc: swar::bytes8 = word ^ c;
|
||||
val wd: swar::bytes8 = word ^ d;
|
||||
val found: swar::mask8 = (wa.zero() | wb.zero() | wc.zero() | wd.zero()) & swar::mask8::leading(left);
|
||||
return at + found.first() if found.any();
|
||||
at += 8;
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
var set: u64[4] = [0, 0, 0, 0];
|
||||
for byte in bytes {
|
||||
set[(byte >> 6) as u64] |= 1u << (byte & 63) as u64;
|
||||
}
|
||||
for at in 0..this._len {
|
||||
val byte := data[at];
|
||||
return at if ((set[(byte >> 6) as u64] >> (byte & 63) as u64) & 1) != 0;
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
#! Tells whether the text is all ASCII, every byte below 128.
|
||||
#! @param this The text to check.
|
||||
#! @returns Whether it is, which empty text is.
|
||||
#! @remarks Thirty-two bytes are read a step, as four words whose high bits are
|
||||
#! gathered, then eight, then the last few.
|
||||
pub def strview::is_ascii(&this): bool {
|
||||
val data := this._dat;
|
||||
var at: u64 = 0;
|
||||
while at + 32 <= this._len {
|
||||
val high := (swar::load(data + at) | swar::load(data + at + 8) | swar::load(data + at + 16) | swar::load(data + at + 24))
|
||||
& 0x8080808080808080;
|
||||
return false if high != 0;
|
||||
at += 32;
|
||||
}
|
||||
while at + 8 <= this._len {
|
||||
return false if (swar::load(data + at) & 0x8080808080808080) != 0;
|
||||
at += 8;
|
||||
}
|
||||
return (swar::load_last(data + at, this._len - at) & 0x8080808080808080) == 0;
|
||||
}
|
||||
|
||||
#! Views the text without the whitespace at its beginning.
|
||||
#! @param this The text to read.
|
||||
#! @returns A view of what is left, which views into the same text.
|
||||
pub def strview::ltrim(&this): strview {
|
||||
var at := 0u;
|
||||
while at < this.length && this[at].is_space {
|
||||
at++;
|
||||
}
|
||||
|
||||
return this.substr(at, this.length - at);
|
||||
}
|
||||
|
||||
#! Views the text without the whitespace at its end.
|
||||
#! @param this The text to read.
|
||||
#! @returns A view of what is left, which views into the same text.
|
||||
pub def strview::rtrim(&this): strview {
|
||||
var count := this.length;
|
||||
while count > 0 && this[count - 1].is_space {
|
||||
count--;
|
||||
}
|
||||
|
||||
return this.substr(0, count);
|
||||
}
|
||||
|
||||
#! Views the text without the whitespace at either end.
|
||||
#! @param this The text to read.
|
||||
#! @returns A view of what is left, which views into the same text.
|
||||
pub def strview::trim(&this): strview {
|
||||
return this.rtrim().ltrim();
|
||||
}
|
||||
|
||||
#! Compares text with other text, by their bytes and then by their length.
|
||||
#! @param this The text to compare against.
|
||||
#! @param other The text to compare with.
|
||||
#! @retval -1 Where this text orders before the other.
|
||||
#! @retval 0 Where they hold the same bytes.
|
||||
#! @retval 1 Where this text orders after the other.
|
||||
pub def strview::compare(&this, other: strview): i64 {
|
||||
val shorter := this.length < other.length ? this.length : other.length;
|
||||
if shorter != 0 {
|
||||
val order := memcmp(this._dat, other._dat, shorter);
|
||||
return order if order != 0;
|
||||
}
|
||||
|
||||
if this.length == other.length {
|
||||
return 0;
|
||||
}
|
||||
return this.length < other.length ? -1 : 1;
|
||||
}
|
||||
|
||||
#! Makes a copy of the text with every ASCII letter in upper case.
|
||||
#! @param this The text to read.
|
||||
#! @returns A string holding the converted text.
|
||||
pub def strview::upper(&this): string {
|
||||
var made := string::from(this, nothing {});
|
||||
change_case(made._dat, made._len, true);
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Makes a copy of the text with every ASCII letter in lower case.
|
||||
#! @param this The text to read.
|
||||
#! @returns A string holding the converted text.
|
||||
pub def strview::lower(&this): string {
|
||||
var made := string::from(this, nothing {});
|
||||
change_case(made._dat, made._len, false);
|
||||
return made;
|
||||
}
|
||||
|
||||
# Changes the case of the ASCII letters of bytes where they are, eight at a time,
|
||||
# the last word ending where the bytes do, over letters the words before changed,
|
||||
# which changing again leaves as they are. Fewer than eight a byte at a time.
|
||||
def change_case(data: *mut u8, length: u64, upper: bool): void {
|
||||
if length < 8 {
|
||||
for at in 0..length {
|
||||
data[at] = upper ? data[at].to_upper : data[at].to_lower;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
var at: u64 = 0;
|
||||
while true {
|
||||
val word := swar::load(data + at);
|
||||
*((data + at) as *mut u64) = upper ? word.upper() : word.lower();
|
||||
break if at + 8 == length;
|
||||
at = at + 16 <= length ? at + 8 : length - 8;
|
||||
}
|
||||
}
|
||||
|
||||
#! Makes text of a width, with this text at its end and a byte repeated before
|
||||
#! it.
|
||||
#! @param this The text to read.
|
||||
#! @param width How many bytes the text should hold.
|
||||
#! @param fill The byte to put before it.
|
||||
#! @returns A string holding the padded text, which is a copy of this text where
|
||||
#! it is already that wide.
|
||||
pub def strview::lpad(&this, width: u64, fill: u8): string {
|
||||
var made := string::new(nothing {});
|
||||
made.reserve(width > this.length ? width : this.length);
|
||||
var at := this.length;
|
||||
while at < width {
|
||||
made.push(fill);
|
||||
at++;
|
||||
}
|
||||
|
||||
made.append(this);
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Makes text of a width, with this text at its beginning and a byte repeated
|
||||
#! after it.
|
||||
#! @param this The text to read.
|
||||
#! @param width How many bytes the text should hold.
|
||||
#! @param fill The byte to put after it.
|
||||
#! @returns A string holding the padded text, which is a copy of this text where
|
||||
#! it is already that wide.
|
||||
pub def strview::rpad(&this, width: u64, fill: u8): string {
|
||||
var made := string::new(nothing {});
|
||||
made.reserve(width > this.length ? width : this.length);
|
||||
made.append(this);
|
||||
var at := this.length;
|
||||
while at < width {
|
||||
made.push(fill);
|
||||
at++;
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
|
||||
# NOTE: Extremely fragile! Should be moved somewhere else or dropped because
|
||||
# it is just storing a string. Replace with a standard error when it is
|
||||
# finally added.
|
||||
#! Explains why text could not be read as a number.
|
||||
pub struct failure {
|
||||
pub var message: string; #! What was wrong with the text.
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
#! @file tuple.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains `tuple`, values of any number of types kept together, each reached
|
||||
#! by its index, as `pair` keeps two, each reached by its name.
|
||||
|
||||
module core;
|
||||
|
||||
|
||||
#! Values of any number of types kept together in order, each reached by its
|
||||
#! index, known while compiling, as `t.items[0]` or `t.at::<0>()`. What closes
|
||||
#! among them is closed where the tuple is, the last first, as fields are.
|
||||
#! @tparam Ts The types of the values.
|
||||
pub struct tuple<Ts...> {
|
||||
pub var items: Ts...; #! The values, one of each type, as a pack is used.
|
||||
}
|
||||
|
||||
#! Makes a tuple of the values given, moving those that close into it.
|
||||
#! @param items The values.
|
||||
#! @returns The tuple.
|
||||
pub def tuple<Ts...>::of(items: Ts...): tuple<Ts...> {
|
||||
return tuple { items = items };
|
||||
}
|
||||
|
||||
#! Reads the value at an index.
|
||||
#! @tparam index The index, known while compiling.
|
||||
#! @param this The tuple to read.
|
||||
#! @returns The value.
|
||||
pub def tuple<Ts...>::at<const index: u64>(&this): &Ts...[index] {
|
||||
return &this.items[index];
|
||||
}
|
||||
|
||||
#! Reaches the value at an index to write it.
|
||||
#! @tparam index The index, known while compiling.
|
||||
#! @param this The tuple to write.
|
||||
#! @returns The value.
|
||||
pub def tuple<Ts...>::at<const index: u64>(&mut this): &mut Ts...[index] {
|
||||
return &mut this.items[index];
|
||||
}
|
||||
@@ -0,0 +1,705 @@
|
||||
#! @file vector.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Defines an implementation for a `vector` container. Vectors are a structured
|
||||
#! container that acts like a regular array, but storing its data dynamically.
|
||||
module core;
|
||||
|
||||
|
||||
#! A growable array, holding its elements one after another in one block of
|
||||
#! memory, which it owns.
|
||||
#!
|
||||
#! It is the general container wherever an array that grows is wanted: adding at
|
||||
#! its end, indexing, and walking are cheap, and adding or removing anywhere
|
||||
#! else moves the elements after it.
|
||||
#! @tparam T The type of its elements.
|
||||
pub struct vector<T>
|
||||
: owning
|
||||
, copyable
|
||||
, moveable
|
||||
, equatable
|
||||
, hashable
|
||||
, indexable<u64, T>
|
||||
, assignable<u64, T>
|
||||
, sliceable<T>
|
||||
, spannable<T>
|
||||
, iterable<T>
|
||||
, extendable<T>
|
||||
{
|
||||
var _alc: maybe<&mut allocator>;
|
||||
var _dat: *mut T;
|
||||
var _len: u64;
|
||||
var _cap: u64;
|
||||
}
|
||||
|
||||
#! Provides read only access to the length of a vector.
|
||||
#! @param this The vector to get the length of.
|
||||
#! @returns The length of the vector.
|
||||
pub val vector<T>::length: u64 {
|
||||
get => this._len;
|
||||
}
|
||||
|
||||
#! Provides read only access to the capacity of a vector.
|
||||
#! @param this The vector to get the capacity of.
|
||||
#! @returns The capacity of the vector.
|
||||
pub val vector<T>::capacity: u64 {
|
||||
get => this._cap;
|
||||
}
|
||||
|
||||
#! Indicates if a vector is empty.
|
||||
#! @param this The vector to check.
|
||||
#! @returns Whether the vector is empty.
|
||||
pub val vector<T>::empty: bool {
|
||||
get => this._len == 0;
|
||||
}
|
||||
|
||||
#! Indicates if a vector is full, so the next element added makes it grow.
|
||||
#! @param this The vector to check.
|
||||
#! @returns Whether the vector is full.
|
||||
pub val vector<T>::full: bool {
|
||||
get => this._len == this._cap;
|
||||
}
|
||||
|
||||
#! Makes an empty vector, which takes memory from the program's heap once
|
||||
#! something is added.
|
||||
#! @returns The vector, which holds no memory yet.
|
||||
pub def vector<T>::new(): vector<T> {
|
||||
return vector::<T>::new(nothing{});
|
||||
}
|
||||
|
||||
#! Makes an empty vector, which takes memory once something is added.
|
||||
#! @param alloc The optional allocator to take memory from, or `nothing` for
|
||||
#! the program's heap.
|
||||
#! @returns The vector, which holds no memory yet.
|
||||
pub def vector<T>::new(alloc: maybe<&mut allocator>): vector<T> {
|
||||
return vector::<T> { _alc = alloc, _dat = null, _len = 0, _cap = 0 };
|
||||
}
|
||||
|
||||
#! Makes an empty vector with room for a number of elements.
|
||||
#! @param size How many elements it has room for before it grows.
|
||||
#! @param alloc The optional allocator to take memory from, or `nothing` for
|
||||
#! the program's heap.
|
||||
#! @returns The vector, which holds no elements yet.
|
||||
#! @remarks An allocator that gives no memory stops the program.
|
||||
pub def vector<T>::new(size: u64, alloc: maybe<&mut allocator>): vector<T> {
|
||||
var made := vector::<T>::new(alloc);
|
||||
made.reserve(size);
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Builds a vector holding copies of another's elements, with its allocator.
|
||||
#! @param other The vector to copy elements from.
|
||||
#! @returns The copy, which owns its own memory and its own elements.
|
||||
#! @remarks Each element is copied by `copyable` where it provides one, so an
|
||||
#! element owning memory is copied rather than shared. Elements are
|
||||
#! filled where they stand, so nothing is built only to be written
|
||||
#! over.
|
||||
pub def vector<T>::copy_of(other: &vector<T>): vector<T> {
|
||||
var made := vector::<T>::new(other._len, other._alc);
|
||||
for index in 0..other._len {
|
||||
made.append(copy::<T>(&other._dat[index]));
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Creates a new vector by moving the data of another vector into it.
|
||||
#! @param other The vector to move elements from, which holds none after.
|
||||
#! @returns The moved vector, which takes the memory and the allocator with it.
|
||||
#! @remarks Nothing is copied and no element is touched: the memory moves as it
|
||||
#! stands, and the other is left empty rather than dead, so closing it
|
||||
#! afterwards is harmless.
|
||||
pub def vector<T>::move_of(other: &mut vector<T>): vector<T> {
|
||||
var made: vector<T> = vector {
|
||||
_alc = other._alc,
|
||||
_dat = other._dat,
|
||||
_len = other._len,
|
||||
_cap = other._cap
|
||||
};
|
||||
|
||||
other._dat = null;
|
||||
other._len = 0;
|
||||
other._cap = 0;
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Lends a writable view of some of the elements of a vector, without copying
|
||||
#! them, as `sliceable` asks, which `v[start:count]` calls where `v` can be
|
||||
#! written.
|
||||
#! @param other The vector to view, which can be written.
|
||||
#! @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 view of those elements, which writes them where they are, and is
|
||||
#! only good until the vector grows, shrinks, or closes.
|
||||
#! @remarks A start or count past the last element stops a checked program.
|
||||
pub def vector<T>::slice_of(other: &mut vector<T>, start: u64 = 0, count: u64 = ~0u): slice<T> {
|
||||
# A vector holding any element is never over null, so the slice is made as it
|
||||
# is rather than through `over`, which would ask.
|
||||
val whole := slice::<T> { _dat = other._dat, _len = other._len };
|
||||
return count == ~0u ? whole[start:] : whole[start:count];
|
||||
}
|
||||
|
||||
#! Lends a view of some of the elements of a vector, without copying them, as
|
||||
#! `spannable` asks, which `v[start:count]` calls.
|
||||
#! @param other The vector to view.
|
||||
#! @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 view of those elements, which is only good until the vector grows,
|
||||
#! shrinks, or closes.
|
||||
#! @remarks A start or count past the last element stops a checked program.
|
||||
pub def vector<T>::span_of(other: &vector<T>, start: u64 = 0, count: u64 = ~0u): span<T> {
|
||||
# Made as it is, as the slice is.
|
||||
val whole := span::<T> { _dat = other._dat, _len = other._len };
|
||||
return count == ~0u ? whole[start:] : whole[start:count];
|
||||
}
|
||||
|
||||
#! A view of the elements, which copies nothing.
|
||||
#! @param this The vector to view.
|
||||
#! @returns A view of its elements, which is only good until the vector grows,
|
||||
#! shrinks, or closes.
|
||||
@inline
|
||||
pub val vector<T>::span: span<T> {
|
||||
get => span::<T> { _dat = this._dat, _len = this._len };
|
||||
}
|
||||
|
||||
#! A writable view of the elements, read of a vector that can be written.
|
||||
#! @param this The vector to view.
|
||||
#! @returns A view of its elements, which writes them where they are, and is
|
||||
#! only good until the vector grows, shrinks, or closes.
|
||||
@inline
|
||||
pub val vector<T>::slice: slice<T> {
|
||||
get mut => slice::<T> { _dat = this._dat, _len = this._len };
|
||||
}
|
||||
|
||||
#! Reads the element at an index, which `v[index]` calls.
|
||||
#! @param this The vector to read.
|
||||
#! @param index The index of the element, below `length`, past which a checked
|
||||
#! program stops.
|
||||
#! @returns The element, which is only good until the vector grows, shrinks, or
|
||||
#! closes.
|
||||
pub def vector<T>::at(&this, index: u64): &T {
|
||||
# Indexed as a view of the elements, it is checked as a view's index is.
|
||||
return &span::<T> { _dat = this._dat, _len = this._len }[index];
|
||||
}
|
||||
|
||||
#! Reaches the element at an index to write it, which `v[index] = x` calls.
|
||||
#! @param this The vector to write.
|
||||
#! @param index The index of the element, below `length`, past which a checked
|
||||
#! program stops.
|
||||
#! @returns The element, which is only good until the vector grows, shrinks, or
|
||||
#! closes.
|
||||
pub def vector<T>::at(&mut this, index: u64): &mut T {
|
||||
return &mut slice::<T> { _dat = this._dat, _len = this._len }[index];
|
||||
}
|
||||
|
||||
#! Gets the element at an index, if the vector holds one there, for an index
|
||||
#! that may be past the end in a program that is right.
|
||||
#! @param this The vector to read.
|
||||
#! @param index The index of the element.
|
||||
#! @returns Maybe the element, which is only good until the vector grows,
|
||||
#! shrinks, or closes; otherwise, `nothing`.
|
||||
pub def vector<T>::get(&this, index: u64): maybe<&T> {
|
||||
# Reached as a view's element, whose check the test answers.
|
||||
return index < this._len
|
||||
? &span::<T> { _dat = this._dat, _len = this._len }[index]
|
||||
: nothing {};
|
||||
}
|
||||
|
||||
#! Gets the element at an index to write it, if the vector holds one there.
|
||||
#! @param this The vector to write.
|
||||
#! @param index The index of the element.
|
||||
#! @returns Maybe the element, which is only good until the vector grows,
|
||||
#! shrinks, or closes; otherwise, `nothing`.
|
||||
pub def vector<T>::get(&mut this, index: u64): maybe<&mut T> {
|
||||
return index < this._len
|
||||
? &mut slice::<T> { _dat = this._dat, _len = this._len }[index]
|
||||
: nothing {};
|
||||
}
|
||||
|
||||
#! The first element, which a vector that can be written writes through.
|
||||
#! @param this The vector to read, which holds an element, or else a checked
|
||||
#! program stops.
|
||||
#! @returns The first element, which is only good until the vector grows,
|
||||
#! shrinks, or closes.
|
||||
#! @remarks A view's `first` gives `none` where it is empty instead, as one that
|
||||
#! is asked whether it holds anything.
|
||||
pub val vector<T>::first: &T {
|
||||
get {
|
||||
expects(!this.empty, "The vector is empty.");
|
||||
# Reached as a view's element, whose check the test above answers.
|
||||
return &span::<T> { _dat = this._dat, _len = this._len }[0];
|
||||
}
|
||||
get mut {
|
||||
expects(!this.empty, "The vector is empty.");
|
||||
return &mut slice::<T> { _dat = this._dat, _len = this._len }[0];
|
||||
}
|
||||
}
|
||||
|
||||
#! The last element, which `v[v.length - 1]` reads, and which a vector that can
|
||||
#! be written writes through.
|
||||
#! @param this The vector to read, which holds an element, or else a checked
|
||||
#! program stops.
|
||||
#! @returns The last element, which is only good until the vector grows,
|
||||
#! shrinks, or closes.
|
||||
#! @remarks A view's `last` gives `none` where it is empty instead, as one that
|
||||
#! is asked whether it holds anything.
|
||||
pub val vector<T>::last: &T {
|
||||
get {
|
||||
expects(!this.empty, "The vector is empty.");
|
||||
return &this._dat[this._len - 1];
|
||||
}
|
||||
get mut {
|
||||
expects(!this.empty, "The vector is empty.");
|
||||
return &mut this._dat[this._len - 1];
|
||||
}
|
||||
}
|
||||
|
||||
#! Replaces the element at an index.
|
||||
#! @param this The vector to write.
|
||||
#! @param index The index of the element, below `length`, past which a checked
|
||||
#! program stops.
|
||||
#! @param elem The element to store there.
|
||||
pub def vector<T>::set(&mut this, index: u64, elem: T): void {
|
||||
slice::<T> { _dat = this._dat, _len = this._len }[index] = move(elem);
|
||||
}
|
||||
|
||||
#! Adds an element after the last.
|
||||
#! @param this The vector to append to.
|
||||
#! @param elem The element to append.
|
||||
#! @remarks An allocator that gives no more memory stops the program.
|
||||
pub def vector<T>::append(&mut this, elem: T): void {
|
||||
if this.full {
|
||||
this.grow();
|
||||
}
|
||||
this._dat[this._len++] = move(elem);
|
||||
}
|
||||
|
||||
#! Adds copies of elements after the last, in order, as appending each would.
|
||||
#! @param this The vector to add to.
|
||||
#! @param items The elements to add, which are not the vector's own, since
|
||||
#! growing would move them.
|
||||
#! @remarks Elements are copied by `copyable` where they provide one, and byte
|
||||
#! for byte where nothing they hold closes. The vector grows once for
|
||||
#! all of them. An allocator that gives no more memory stops the
|
||||
#! program.
|
||||
pub def vector<T>::extend(&mut this, items: span<T>): void {
|
||||
val count := items.length;
|
||||
return if count == 0;
|
||||
|
||||
val wanted := this._len + count;
|
||||
if wanted > this._cap {
|
||||
this.grow(wanted > this._cap * 2 ? wanted : this._cap * 2);
|
||||
}
|
||||
|
||||
if const T is copyable || T is closeable {
|
||||
for index in 0..count {
|
||||
this._dat[this._len + index] = copy::<T>(&items.data[index]);
|
||||
}
|
||||
} else {
|
||||
memcpy((this._dat + this._len) as *mut u8, items.data as *u8, bytes_of::<T>(count));
|
||||
}
|
||||
this._len = wanted;
|
||||
}
|
||||
|
||||
#! Removes the first element equal to the one given, where any is, moving the
|
||||
#! elements after it down one.
|
||||
#! @param this The vector to remove from.
|
||||
#! @param elem The element to look for, compared by `equals`.
|
||||
#! @remarks The element removed is closed where it closes, or holds what does.
|
||||
pub def vector<T>::remove(&mut this, elem: &T): void {
|
||||
for index in 0..this._len {
|
||||
if equals::<T>(&this._dat[index], elem) {
|
||||
this.remove_at(index);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#! Adds an element at an index, moving the elements from it up one.
|
||||
#! @param this The vector to insert into.
|
||||
#! @param index Where the element goes, from `0` to `length`, where `length`
|
||||
#! appends. An index past that stops a checked program.
|
||||
#! @param elem The element to insert.
|
||||
pub def vector<T>::insert_at(&mut this, index: u64, elem: T): void {
|
||||
expects(index <= this._len);
|
||||
if index == this._len {
|
||||
this.append(move(elem));
|
||||
return;
|
||||
}
|
||||
|
||||
if this.full {
|
||||
this.grow();
|
||||
}
|
||||
|
||||
val orig := this._dat;
|
||||
val src := orig + index;
|
||||
val dst := src + 1;
|
||||
memcpy(
|
||||
dst as *mut u8,
|
||||
src as *mut u8,
|
||||
bytes_of::<T>(this._len - index)
|
||||
);
|
||||
|
||||
# The slot still holds the bytes of the element moved up from it, which writing
|
||||
# over would close, so it is emptied first.
|
||||
if const T is closeable {
|
||||
memset(src as *mut u8, 0, bytes_of::<T>(1));
|
||||
}
|
||||
this._dat[index] = move(elem);
|
||||
this._len++;
|
||||
}
|
||||
|
||||
#! Removes the element at an index, moving the elements after it down one.
|
||||
#! @param this The vector to remove from.
|
||||
#! @param index The index of the element, below `length`, past which a checked
|
||||
#! program stops.
|
||||
#! @remarks The element is closed where it closes, or holds what does.
|
||||
pub def vector<T>::remove_at(&mut this, index: u64): void {
|
||||
close(&mut slice::<T> { _dat = this._dat, _len = this._len }[index]);
|
||||
|
||||
# Removing the last element moves nothing, as a stack popping does.
|
||||
val orig := this._dat;
|
||||
val after := this._len - index - 1;
|
||||
if after != 0 {
|
||||
val dst := orig + index;
|
||||
memcpy(dst as *mut u8, (dst + 1) as *mut u8, bytes_of::<T>(after));
|
||||
}
|
||||
|
||||
# The last slot still holds the bytes of the element moved down from it, which
|
||||
# appending would close.
|
||||
this._len--;
|
||||
if const T is closeable {
|
||||
memset((orig + this._len) as *mut u8, 0, bytes_of::<T>(1));
|
||||
}
|
||||
}
|
||||
|
||||
#! Removes the last element, as `remove_at(length - 1)` does, without moving
|
||||
#! any other.
|
||||
#! @param this The vector to remove from, which holds an element, or else a
|
||||
#! checked program stops.
|
||||
#! @remarks The element is closed where it closes, or holds what does, and not
|
||||
#! given back, so read it with `last` first. It is named apart from the
|
||||
#! `pop` of the stack, queue and heap, which a vector is not.
|
||||
pub def vector<T>::truncate(&mut this): void {
|
||||
expects(!this.empty, "The vector is empty.");
|
||||
|
||||
this._len--;
|
||||
if const T is closeable {
|
||||
close(&mut this._dat[this._len]);
|
||||
|
||||
# The slot past the length is kept empty where appending would close it.
|
||||
memset((this._dat + this._len) as *mut u8, 0, bytes_of::<T>(1));
|
||||
}
|
||||
}
|
||||
|
||||
#! Makes room for at least a number of elements, keeping those it holds.
|
||||
#! @param this The vector to make room in.
|
||||
#! @param count How many elements it should have room for.
|
||||
#! @remarks A vector with room for that many already is left as it is, so this
|
||||
#! never shrinks it. An allocator that gives no more memory stops the
|
||||
#! program.
|
||||
pub def vector<T>::reserve(&mut this, count: u64): void {
|
||||
this.grow(count);
|
||||
}
|
||||
|
||||
#! Where the elements begin, `length` of them, for what reads or writes them
|
||||
#! through a pointer, as a container built on a vector does.
|
||||
#! @param this The vector.
|
||||
#! @returns The pointer, which is only good until the vector grows, shrinks, or
|
||||
#! closes. Nothing checks what is reached through it.
|
||||
pub val vector<T>::data: *mut T {
|
||||
get => this._dat;
|
||||
}
|
||||
|
||||
#! Makes the vector hold a number of elements, adding zeroed ones or removing
|
||||
#! those past it.
|
||||
#! @param this The vector to resize.
|
||||
#! @param size How many elements it holds after.
|
||||
#! @remarks Elements removed are closed where they close, or hold what does. An
|
||||
#! allocator that gives no more memory stops the program.
|
||||
pub def vector<T>::resize(&mut this, size: u64): void {
|
||||
# A vector holding no memory yet takes it zeroed, which memory the system
|
||||
# gives fresh already is, so it is not cleared again.
|
||||
if this._dat == null && size != 0 {
|
||||
this._dat = acquire_zeroed::<T>(this._alc, size);
|
||||
this._cap = size;
|
||||
this._len = size;
|
||||
return;
|
||||
}
|
||||
|
||||
# Growing doubles at least, so resizing a little at a time copies little.
|
||||
if size > this._cap {
|
||||
this.grow(size > this._cap * 2 ? size : this._cap * 2);
|
||||
}
|
||||
|
||||
if size < this._len {
|
||||
if const T is closeable {
|
||||
for index in size..this._len {
|
||||
close(&mut this._dat[index]);
|
||||
}
|
||||
}
|
||||
|
||||
# The slots past the length are kept empty where appending would close them.
|
||||
if const T is closeable {
|
||||
memset((this._dat + size) as *mut u8, 0, bytes_of::<T>(this._len - size));
|
||||
}
|
||||
}
|
||||
else {
|
||||
memset((this._dat + this._len) as *mut u8, 0, bytes_of::<T>(size - this._len));
|
||||
}
|
||||
this._len = size;
|
||||
}
|
||||
|
||||
#! Makes the vector hold a number more elements, which hold what their memory
|
||||
#! happened to, for elements the program writes before it reads them, as a
|
||||
#! table it fills whole: what `resize` adds, without zeroing it.
|
||||
#! @param this The vector to grow.
|
||||
#! @param count How many elements it gains.
|
||||
#! @remarks Only for what closes nothing, since closing an element would close
|
||||
#! what its memory held. Reading an element before writing it is the
|
||||
#! program's mistake, which `run --checks` stops at. An allocator that
|
||||
#! gives no more memory stops the program.
|
||||
pub def vector<T>::extend_undefined(&mut this, count: u64): void {
|
||||
if const T is closeable {
|
||||
compile_error("Elements left undefined would be closed holding what their memory happened to, so a vector of what closes is grown by `resize`.");
|
||||
}
|
||||
val size := this._len + count;
|
||||
if size > this._cap {
|
||||
this.grow(size > this._cap * 2 ? size : this._cap * 2);
|
||||
}
|
||||
this._len = size;
|
||||
}
|
||||
|
||||
#! Makes the vector hold a number of elements, adding copies of one or removing
|
||||
#! those past it.
|
||||
#! @param this The vector to resize.
|
||||
#! @param size How many elements it holds after.
|
||||
#! @param fill The element each one added is a copy of.
|
||||
#! @remarks Elements added are copied by `copyable` where they provide one, and
|
||||
#! elements removed are closed where they close, or hold what does. An
|
||||
#! allocator that gives no more memory stops the program.
|
||||
pub def vector<T>::resize(&mut this, size: u64, fill: T): void {
|
||||
if size <= this._len {
|
||||
this.resize(size);
|
||||
return;
|
||||
}
|
||||
|
||||
if size > this._cap {
|
||||
this.grow(size > this._cap * 2 ? size : this._cap * 2);
|
||||
}
|
||||
for index in this._len..size {
|
||||
this._dat[index] = copy::<T>(&fill);
|
||||
}
|
||||
this._len = size;
|
||||
}
|
||||
|
||||
#! Removes every element, keeping the memory the vector holds them in.
|
||||
#! @param this The vector to empty.
|
||||
#! @remarks Elements are closed where they close, or hold what does.
|
||||
pub def vector<T>::clear(&mut this): void {
|
||||
if const T is closeable {
|
||||
for index in 0..this._len {
|
||||
close(&mut this._dat[index]);
|
||||
}
|
||||
}
|
||||
|
||||
if const T is closeable {
|
||||
memset(this._dat as *mut u8, 0, bytes_of::<T>(this._len));
|
||||
}
|
||||
this._len = 0;
|
||||
}
|
||||
|
||||
#! Removes every element and gives back the memory the vector holds.
|
||||
#! @param this The vector to close.
|
||||
#! @remarks Elements are closed where they close, or hold what does. The vector
|
||||
#! is left empty, so closing it again does nothing.
|
||||
pub def vector<T>::close(&mut this): void {
|
||||
if const T is closeable {
|
||||
for index in 0..this._len {
|
||||
close(&mut this._dat[index]);
|
||||
}
|
||||
}
|
||||
|
||||
dispose::<T>(this._alc, this._dat, this._cap);
|
||||
this._dat = null;
|
||||
this._len = 0;
|
||||
this._cap = 0;
|
||||
}
|
||||
|
||||
#! Compares one vector to another for equality.
|
||||
#! @param this The vector to compare against.
|
||||
#! @param other The vector to compare with.
|
||||
#! @returns Whether both hold the same elements, in the same order.
|
||||
pub def vector<T>::equals(&this, other: &vector<T>): bool {
|
||||
if this._len != other._len {
|
||||
return false;
|
||||
}
|
||||
|
||||
for index in 0..this._len {
|
||||
return false if !equals::<T>(
|
||||
&this._dat[index],
|
||||
&other._dat[index]
|
||||
);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#! What `iter(&this)` lends, which reads the elements where the vector holds
|
||||
#! them.
|
||||
pub type vector<T>::iterator = span<T>::iterator;
|
||||
|
||||
#! What `iter(&mut this)` lends, which writes the elements where the vector
|
||||
#! holds them.
|
||||
pub type vector<T>::mutator = slice<T>::mutator;
|
||||
|
||||
#! Lends an iterator that reads the elements, in order, which `for x in v`
|
||||
#! walks.
|
||||
#! @param this The vector to walk.
|
||||
#! @returns The iterator, which is only good until the vector grows, shrinks, or
|
||||
#! closes.
|
||||
pub def vector<T>::iter(&this): span<T>::iterator {
|
||||
return this.span.iter();
|
||||
}
|
||||
|
||||
#! Lends a mutator that writes the elements where they are, which `for &mut x
|
||||
#! in v` walks.
|
||||
#! @param this The vector to walk.
|
||||
#! @returns The mutator, which is only good until the vector grows, shrinks, or
|
||||
#! closes.
|
||||
pub def vector<T>::iter(&mut this): slice<T>::mutator {
|
||||
var whole := this.slice;
|
||||
return whole.iter();
|
||||
}
|
||||
|
||||
#! An iterator that reads the elements, in order, as `iter` lends.
|
||||
#! @param this The vector to walk.
|
||||
#! @returns The iterator, which is only good until the vector grows, shrinks, or
|
||||
#! closes.
|
||||
@inline
|
||||
pub val vector<T>::iterator: span<T>::iterator {
|
||||
get => this.span.iter();
|
||||
}
|
||||
|
||||
#! A mutator that writes the elements where they are, read of a vector that can
|
||||
#! be written.
|
||||
#! @param this The vector to walk.
|
||||
#! @returns The mutator, which is only good until the vector grows, shrinks, or
|
||||
#! closes.
|
||||
@inline
|
||||
pub val vector<T>::mutator: slice<T>::mutator {
|
||||
get mut => this.iter();
|
||||
}
|
||||
|
||||
#! Makes a vector of the elements an iterator gives, as its `to_vector` does.
|
||||
#! @tparam I The iterator.
|
||||
#! @param from The iterator, walked to its end.
|
||||
#! @returns The vector, given room for them all at once where the iterator knows
|
||||
#! how many it has left.
|
||||
pub def vector<T>::from<I>(from: I): vector<T> where I: iter<T> {
|
||||
return from.to_vector();
|
||||
}
|
||||
|
||||
#! Moves the elements a range of indexes covers out of the vector, one at a
|
||||
#! time, as what it gives is walked. Those not walked to are closed where it
|
||||
#! closes, and the elements after the range then move down into the gap, so
|
||||
#! the vector holds only those outside the range.
|
||||
#! @param this The vector to take from.
|
||||
#! @param which The indexes, from `0` up to `length`, past which a checked
|
||||
#! program stops.
|
||||
#! @returns The elements, from the front or the far end, which the vector is not
|
||||
#! to be changed by anything else while they are walked.
|
||||
pub def vector<T>::drain(&mut this, which: range<u64>): vector<T>::drained {
|
||||
val start := which.start;
|
||||
val stop := start + which.remaining();
|
||||
expects(stop <= this._len && start <= stop);
|
||||
return vector::<T>::drained { _of = this, _start = start, _at = start, _end = stop, _stop = stop };
|
||||
}
|
||||
|
||||
#! What `drain` gives: the elements of a range of indexes, moved out of the
|
||||
#! vector as they are given. Closing it closes those it did not give and moves
|
||||
#! the elements after the range down into the gap.
|
||||
pub struct vector<T>::drained : owning, sized_iterator<T>, backward_iterator<T> {
|
||||
var _of: &mut vector<T>; #! The vector drained.
|
||||
var _start: u64; #! Where the range begins, which the gap begins at.
|
||||
var _at: u64; #! The next element to give from the front.
|
||||
var _end: u64; #! Just past the next to give from the far end.
|
||||
var _stop: u64; #! Where the range ends, after which the rest stay.
|
||||
}
|
||||
|
||||
#! Moves out the next element of the range.
|
||||
#! @param this The drain.
|
||||
#! @returns Maybe the element; otherwise, `nothing` once the ends have met.
|
||||
pub def vector<T>::drained::next(&mut this): maybe<T> {
|
||||
return nothing {} if this._at == this._end;
|
||||
val at := this._at;
|
||||
this._at++;
|
||||
return move(&mut this._of._dat[at]);
|
||||
}
|
||||
|
||||
#! Moves out the element of the range at its far end.
|
||||
#! @param this The drain.
|
||||
#! @returns Maybe the element; otherwise, `nothing` once the ends have met.
|
||||
pub def vector<T>::drained::next_back(&mut this): maybe<T> {
|
||||
return nothing {} if this._at == this._end;
|
||||
this._end--;
|
||||
return move(&mut this._of._dat[this._end]);
|
||||
}
|
||||
|
||||
#! How many elements of the range it has left to give.
|
||||
#! @param this The drain.
|
||||
#! @returns How many.
|
||||
pub def vector<T>::drained::remaining(&this): u64 {
|
||||
return this._end - this._at;
|
||||
}
|
||||
|
||||
#! Closes the elements of the range it did not give, and moves the elements
|
||||
#! after the range down into the gap.
|
||||
#! @param this The drain.
|
||||
pub def vector<T>::drained::close(&mut this): void {
|
||||
if const T is closeable {
|
||||
for index in this._at..this._end {
|
||||
close(&mut this._of._dat[index]);
|
||||
}
|
||||
}
|
||||
val after := this._of._len - this._stop;
|
||||
if after != 0 && this._stop != this._start {
|
||||
val base := this._of._dat;
|
||||
memcpy((base + this._start) as *mut u8, (base + this._stop) as *mut u8, bytes_of::<T>(after));
|
||||
}
|
||||
this._of._len = this._start + after;
|
||||
this._at = this._end;
|
||||
this._stop = this._start;
|
||||
}
|
||||
|
||||
#! Hashes the elements, in order.
|
||||
#! @param this The vector to hash.
|
||||
#! @returns The hash code, which vectors holding equal elements share.
|
||||
#! @remarks Elements are hashed by their `hash`, so they are `hashable`.
|
||||
pub def vector<T>::hash(&this): u64 {
|
||||
var code := this._len;
|
||||
for index in 0..this._len {
|
||||
code = mix(code, hash(this._dat[index]));
|
||||
}
|
||||
return code;
|
||||
}
|
||||
|
||||
# Gives the vector room for more elements, doubling it.
|
||||
def vector<T>::grow(&mut this): void {
|
||||
this.grow(this._cap == 0 ? 4 : this._cap * 2);
|
||||
}
|
||||
|
||||
# Gives the vector room for a number of elements, where it has less. An allocator
|
||||
# that gives no memory stops the program, as `acquire` and `resize` do.
|
||||
def vector<T>::grow(&mut this, size: u64): void {
|
||||
if size <= this._cap {
|
||||
return;
|
||||
}
|
||||
|
||||
this._dat = this._dat == null
|
||||
? acquire::<T>(this._alc, size)
|
||||
: resize::<T>(this._alc, this._dat, this._cap, size);
|
||||
this._cap = size;
|
||||
}
|
||||
Reference in new issue
Block a user