Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-10-28 22:13:49 -05:00
commit 05756094ac
706 files changed
+135430

No files matched your search

+165
View File
@@ -0,0 +1,165 @@
#! @file range.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the methods of `range`, the integers from a start up to an end, or
#! the members of an enum between two, which `a..b` and `a..=b` make wherever
#! they are written as a value.
#!
#! `range` is among the types the syntax makes, which the compiler declares
#! itself. It is an iterator, walked forward or from its far end, that knows how
#! many it has left, so `(0..n).rev()` and `(0..n).step(2)` count as a loop
#! would. A `for` loop over `a..b` written in place counts without making one.
#!
#! A range of an enum's members gives them in the order of their values, each
#! value once, passing over those no member has, as `color::red..=color::blue`
#! gives `red`, `green`, and `blue` of `enum color { red, green = 4, blue }`.
module core;
#! The place of a member of an enum among the values of its members, from 0 for
#! the least, each value counted once.
#! @tparam T The enum.
#! @param member The member.
#! @returns Its place.
@intrinsic
def rank_of<T>(member: T): u64;
#! The member of an enum at a place among the values of its members, as
#! `rank_of` gives it.
#! @tparam T The enum.
#! @param rank The place, before the count of the members' values.
#! @returns The member.
@intrinsic
def member_at<T>(rank: u64): T;
#! The value a number of places past another, as a range counts: the integer
#! that far past it, or the member of an enum that many members past it.
#! @tparam T The integer type, or the enum.
#! @param from The value to count from.
#! @param n How many places past it.
#! @returns The value.
def range<T>::past(from: T, n: u64): T {
if const T is integer {
return ((from as u64) + n) as T;
}
return member_at::<T>(rank_of(from) + n);
}
#! How many places a range counts from one value up to another, which does not
#! come before it.
#! @tparam T The integer type, or the enum.
#! @param from The first value.
#! @param to The value it counts up to.
#! @returns How many.
def range<T>::between(from: T, to: T): u64 {
if const T is integer {
return (to as u64) - (from as u64);
}
return rank_of(to) - rank_of(from);
}
#! Gives the next value, counting up from the start.
#! @param this The range.
#! @returns Maybe the value; otherwise, `nothing` once it has given them all.
pub def range<T>::next(&mut this): maybe<T> {
if this.start < this.end {
val at := this.start;
if const T is integer {
this.start++;
} else {
this.start = range::<T>::past(at, 1);
}
return at;
}
if this._last && this.start == this.end {
this._last = false;
return this.end;
}
return nothing {};
}
#! Gives the value at the far end, counting down from the end.
#! @param this The range.
#! @returns Maybe the value; otherwise, `nothing` once the ends have met.
pub def range<T>::next_back(&mut this): maybe<T> {
if this._last && !(this.end < this.start) {
this._last = false;
return this.end;
}
if this.start < this.end {
if const T is integer {
this.end--;
} else {
this.end = member_at::<T>(rank_of(this.end) - 1);
}
return this.end;
}
return nothing {};
}
#! How many values it has left to give.
#! @param this The range.
#! @returns How many.
pub def range<T>::remaining(&this): u64 {
var made: u64 = this.start < this.end ? range::<T>::between(this.start, this.end) : 0;
if this._last && !(this.end < this.start) {
made++;
}
return made;
}
#! How many values it has left to give, as `remaining` says.
#! @param this The range.
#! @returns How many.
pub val range<T>::length: u64 {
get => this.remaining();
}
#! Whether it has given every value, or had none to give.
#! @param this The range.
#! @returns Whether it has none left.
pub def range<T>::is_empty(&this): bool {
return !(this.start < this.end) && !(this._last && this.start == this.end);
}
#! Whether a value is among those it has left to give.
#! @param this The range.
#! @param x The value.
#! @returns Whether it is.
pub def range<T>::contains(&this, x: T): bool {
return !(x < this.start) && (x < this.end || (this._last && x == this.end));
}
#! The value `n` along, counting from 0, passing over those before it at once.
#! @param this The range.
#! @param n How many to pass over first.
#! @returns Maybe the value; otherwise, `nothing` where it has fewer left.
pub def range<T>::nth(&mut this, n: u64): maybe<T> {
val held: u64 = this.start < this.end ? range::<T>::between(this.start, this.end) : 0;
if n < held {
this.start = range::<T>::past(this.start, n);
return this.next();
}
if this.start < this.end {
this.start = this.end;
}
return this.next() if n == held;
this._last = false;
return nothing {};
}
#! Gives every value it has left at once, counting them.
#! @param this The range.
#! @returns How many it had left.
pub def range<T>::count(&mut this): u64 {
val made := this.remaining();
if this.start < this.end {
this.start = this.end;
}
this._last = false;
return made;
}