Initial Commit
This commit is contained in:
706 files changed
+135430
No files matched your search
@@ -0,0 +1,529 @@
|
||||
#! @file iterator.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the iterator traits: `iterator`, which gives elements one at a time
|
||||
#! and which a `for` loop walks, with the adapters making one iterator of
|
||||
#! another and the consumers walking one to its end as its defaults;
|
||||
#! `sized_iterator`, which knows how many elements it has left; and
|
||||
#! `backward_iterator`, which also walks from its far end.
|
||||
|
||||
module core;
|
||||
|
||||
|
||||
#! Gives elements one at a time, as `some` of each while there are more and
|
||||
#! `none` once there are no more.
|
||||
#!
|
||||
#! `for x in it` walks an iterator where it is, calling `next` until it gives
|
||||
#! `none`, so one that a loop stops early carries on from there in the next. An
|
||||
#! iterator that is `owning` and that a loop is given as a value, rather than as
|
||||
#! a variable, is closed where the loop ends, however it ends.
|
||||
#!
|
||||
#! A type naming the trait has its defaults: the adapters, which make an
|
||||
#! iterator of it, as `v.iter().filter(odd).map(square)`, and the consumers,
|
||||
#! which walk it to its end, or until they know their answer, as `.sum()`. Each
|
||||
#! is compiled for the type, so what it calls of the type is called directly. An
|
||||
#! adapter moves the iterator it is called on into what it makes, which closes
|
||||
#! it where that closes, so `it.map(f)` leaves `it` walked to its end.
|
||||
#! @tparam T The type of what it gives.
|
||||
pub trait iterator<T> {
|
||||
#! Gives the next element, or `none` once there are no more.
|
||||
#! @param this The iterator to advance.
|
||||
#! @returns Maybe the next element; otherwise, `nothing`.
|
||||
def next(&mut this): maybe<T>;
|
||||
|
||||
#! Gives what a function makes of each element.
|
||||
#! @tparam U What the function gives.
|
||||
#! @tparam F The function, a lambda, or anything `invokable`.
|
||||
#! @param this The iterator, moved into what is made.
|
||||
#! @param f The function, called on each element as it is given.
|
||||
#! @returns The iterator of what it makes.
|
||||
def map<U, F>(&mut this, f: F): mapped<T, U, self, F> where F: invokable<def(T): U> {
|
||||
return mapped::<T, U, self, F> { _from = move(this), _f = move(&mut f) };
|
||||
}
|
||||
|
||||
#! Gives each element a test passes.
|
||||
#! @tparam F The test.
|
||||
#! @param this The iterator, moved into what is made.
|
||||
#! @param test The test, given a reference to each element.
|
||||
#! @returns The iterator of those that pass.
|
||||
def filter<F>(&mut this, test: F): filtered<T, self, F> where F: invokable<def(&T): bool> {
|
||||
return filtered::<T, self, F> { _from = move(this), _test = move(&mut test) };
|
||||
}
|
||||
|
||||
#! Gives what a function makes of each element where it makes something, as
|
||||
#! `some`.
|
||||
#! @tparam U What the function may give.
|
||||
#! @tparam F The function.
|
||||
#! @param this The iterator, moved into what is made.
|
||||
#! @param f The function, called on each element.
|
||||
#! @returns The iterator of what it makes.
|
||||
def filter_map<U, F>(&mut this, f: F): filter_mapped<T, U, self, F> where F: invokable<def(T): maybe<U>> {
|
||||
return filter_mapped::<T, U, self, F> { _from = move(this), _f = move(&mut f) };
|
||||
}
|
||||
|
||||
#! Gives each element with its index, counting from 0, as a `pair`.
|
||||
#! @param this The iterator, moved into what is made.
|
||||
#! @returns The iterator of pairs.
|
||||
def enumerate(&mut this): enumerated<T, self> {
|
||||
return enumerated::<T, self> { _from = move(this), _at = 0 };
|
||||
}
|
||||
|
||||
#! Gives the elements of this iterator and another side by side, as a `pair`,
|
||||
#! until either ends.
|
||||
#! @tparam U What the other gives.
|
||||
#! @tparam J The other iterator.
|
||||
#! @param this The iterator, moved into what is made.
|
||||
#! @param other The other iterator, moved into what is made.
|
||||
#! @returns The iterator of pairs.
|
||||
def zip<U, J>(&mut this, other: J): zipped<T, U, self, J> where J: iterator<U> {
|
||||
return zipped::<T, U, self, J> { _from = move(this), _other = move(&mut other) };
|
||||
}
|
||||
|
||||
#! Gives this iterator's elements, then another's.
|
||||
#! @tparam J The other iterator, giving what this one gives.
|
||||
#! @param this The iterator, moved into what is made.
|
||||
#! @param other The other iterator, moved into what is made.
|
||||
#! @returns The iterator of both.
|
||||
def chain<J>(&mut this, other: J): chained<T, self, J> where J: iterator<T> {
|
||||
return chained::<T, self, J> { _first = move(this), _second = move(&mut other), _on_second = false };
|
||||
}
|
||||
|
||||
#! Gives the first elements, up to a count.
|
||||
#! @param this The iterator, moved into what is made.
|
||||
#! @param n How many to give at most.
|
||||
#! @returns The iterator of those.
|
||||
def take(&mut this, n: u64): taken<T, self> {
|
||||
return taken::<T, self> { _from = move(this), _left = n };
|
||||
}
|
||||
|
||||
#! Gives the elements after the first, up to a count, which it passes over.
|
||||
#! @param this The iterator, moved into what is made.
|
||||
#! @param n How many to pass over.
|
||||
#! @returns The iterator of the rest.
|
||||
def skip(&mut this, n: u64): skipped<T, self> {
|
||||
return skipped::<T, self> { _from = move(this), _skip = n };
|
||||
}
|
||||
|
||||
#! Gives the elements while a test passes them, ending at the first it does
|
||||
#! not.
|
||||
#! @tparam F The test.
|
||||
#! @param this The iterator, moved into what is made.
|
||||
#! @param test The test, given a reference to each element.
|
||||
#! @returns The iterator of the run that passes.
|
||||
def take_while<F>(&mut this, test: F): taken_while<T, self, F> where F: invokable<def(&T): bool> {
|
||||
return taken_while::<T, self, F> { _from = move(this), _test = move(&mut test), _done = false };
|
||||
}
|
||||
|
||||
#! Gives the elements from the first a test does not pass on, passing over
|
||||
#! those before it.
|
||||
#! @tparam F The test.
|
||||
#! @param this The iterator, moved into what is made.
|
||||
#! @param test The test, given a reference to each element.
|
||||
#! @returns The iterator of the rest.
|
||||
def skip_while<F>(&mut this, test: F): skipped_while<T, self, F> where F: invokable<def(&T): bool> {
|
||||
return skipped_while::<T, self, F> { _from = move(this), _test = move(&mut test), _past = false };
|
||||
}
|
||||
|
||||
#! Gives every `n`th element, the first included.
|
||||
#! @param this The iterator, moved into what is made.
|
||||
#! @param n How far apart the elements given are, at least 1.
|
||||
#! @returns The iterator of those.
|
||||
def step(&mut this, n: u64): stepped<T, self> {
|
||||
if n == 0 {
|
||||
fault("An iterator steps by 0, which is no step.");
|
||||
}
|
||||
return stepped::<T, self> { _from = move(this), _by = n, _first = true };
|
||||
}
|
||||
|
||||
#! Gives the elements from the far end.
|
||||
#! @param this The iterator, moved into what is made.
|
||||
#! @returns The iterator walking backward.
|
||||
def rev(&mut this): reversed<T, self> where self: backward_iterator<T> {
|
||||
return reversed::<T, self> { _from = move(this) };
|
||||
}
|
||||
|
||||
#! Gives the elements, and looks at the next before it is given, by `peek`.
|
||||
#! @param this The iterator, moved into what is made.
|
||||
#! @returns The iterator that peeks.
|
||||
def peekable(&mut this): peeking<T, self> {
|
||||
return peeking::<T, self> { _from = move(this), _held = nothing {} };
|
||||
}
|
||||
|
||||
#! Gives the elements of each iterator a function makes of each element.
|
||||
#! @tparam U What the iterators it makes give.
|
||||
#! @tparam J The iterators it makes.
|
||||
#! @tparam F The function.
|
||||
#! @param this The iterator, moved into what is made.
|
||||
#! @param f The function, called on each element.
|
||||
#! @returns The iterator of what each gives, in turn.
|
||||
def flat_map<U, J, F>(&mut this, f: F): flat_mapped<T, U, self, J, F>
|
||||
where F: invokable<def(T): J> where J: iterator<U> {
|
||||
return flat_mapped::<T, U, self, J, F> { _from = move(this), _f = move(&mut f), _inner = nothing {} };
|
||||
}
|
||||
|
||||
#! Gives a copy of what each reference it gives refers to, of what closes
|
||||
#! nothing.
|
||||
#! @tparam X What the references refer to.
|
||||
#! @param this The iterator of references, moved into what is made.
|
||||
#! @returns The iterator of copies.
|
||||
def copied<X>(&mut this): copying<X, self> where self: iterator<&X> {
|
||||
return copying::<X, self> { _from = move(this) };
|
||||
}
|
||||
|
||||
#! Gives the elements as they are, after calling a function on each.
|
||||
#! @tparam F The function.
|
||||
#! @param this The iterator, moved into what is made.
|
||||
#! @param f The function, given a reference to each element.
|
||||
#! @returns The iterator.
|
||||
def inspect<F>(&mut this, f: F): inspected<T, self, F> where F: invokable<def(&T): void> {
|
||||
return inspected::<T, self, F> { _from = move(this), _f = move(&mut f) };
|
||||
}
|
||||
|
||||
#! Walks to the end, counting the elements.
|
||||
#! @param this The iterator.
|
||||
#! @returns How many it gave.
|
||||
def count(&mut this): u64 {
|
||||
var n: u64 = 0;
|
||||
while this.next() is some(x) {
|
||||
n++;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
#! Walks to the end, adding the elements up.
|
||||
#! @param this The iterator, of numbers.
|
||||
#! @returns The sum, or 0 where it gives none.
|
||||
def sum(&mut this): T where T: numeric {
|
||||
var total: T = 0;
|
||||
while this.next() is some(x) {
|
||||
total += x;
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
#! Walks to the end, multiplying the elements together.
|
||||
#! @param this The iterator, of numbers.
|
||||
#! @returns The product, or 1 where it gives none.
|
||||
def product(&mut this): T where T: numeric {
|
||||
var total: T = 1;
|
||||
while this.next() is some(x) {
|
||||
total *= x;
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
#! Walks to the end, folding each element into a value: `f(f(start, x0), x1)`
|
||||
#! and on.
|
||||
#! @tparam A The value.
|
||||
#! @tparam F The function folding each in.
|
||||
#! @param this The iterator.
|
||||
#! @param start The value before any element is folded in.
|
||||
#! @param f The function, given the value so far and the element.
|
||||
#! @returns The value once every element is folded in.
|
||||
def fold<A, F>(&mut this, start: A, f: F): A where F: invokable<def(A, T): A> {
|
||||
var made := move(&mut start);
|
||||
while this.next() is some(x) {
|
||||
made = f(move(&mut made), move(&mut x));
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Walks to the end, folding the elements into the first: `f(f(x0, x1), x2)`
|
||||
#! and on.
|
||||
#! @tparam F The function folding each in.
|
||||
#! @param this The iterator.
|
||||
#! @param f The function, given the value so far and the element.
|
||||
#! @returns Maybe the value; otherwise, `nothing` where it gives no elements.
|
||||
def reduce<F>(&mut this, f: F): maybe<T> where F: invokable<def(T, T): T> {
|
||||
var first := this.next();
|
||||
if first is not some(&mut start) {
|
||||
return nothing {};
|
||||
}
|
||||
var made := move(start);
|
||||
while this.next() is some(x) {
|
||||
made = f(move(&mut made), move(&mut x));
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Whether a test passes any element, walking until one does.
|
||||
#! @tparam F The test.
|
||||
#! @param this The iterator.
|
||||
#! @param test The test, given a reference to each element.
|
||||
#! @returns Whether it passes one.
|
||||
def any<F>(&mut this, test: F): bool where F: invokable<def(&T): bool> {
|
||||
while this.next() is some(x) {
|
||||
return true if test(&x);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#! Whether a test passes every element, walking until one fails it.
|
||||
#! @tparam F The test.
|
||||
#! @param this The iterator.
|
||||
#! @param test The test, given a reference to each element.
|
||||
#! @returns Whether it passes all of them, as it does where there are none.
|
||||
def all<F>(&mut this, test: F): bool where F: invokable<def(&T): bool> {
|
||||
while this.next() is some(x) {
|
||||
return false if !test(&x);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#! The first element a test passes, walking until it finds one.
|
||||
#! @tparam F The test.
|
||||
#! @param this The iterator.
|
||||
#! @param test The test, given a reference to each element.
|
||||
#! @returns Maybe the element; otherwise, `nothing`.
|
||||
def find<F>(&mut this, test: F): maybe<T> where F: invokable<def(&T): bool> {
|
||||
while this.next() is some(x) {
|
||||
return move(&mut x) if test(&x);
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
#! The index of the first element a test passes, counting from 0, walking
|
||||
#! until it finds one.
|
||||
#! @tparam F The test.
|
||||
#! @param this The iterator.
|
||||
#! @param test The test, given a reference to each element.
|
||||
#! @returns Maybe the index; otherwise, `nothing`.
|
||||
def position<F>(&mut this, test: F): maybe<u64> where F: invokable<def(&T): bool> {
|
||||
var at: u64 = 0;
|
||||
while this.next() is some(x) {
|
||||
return at if test(&x);
|
||||
at++;
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
#! Walks to the end, keeping the least element, the first of those equally
|
||||
#! least.
|
||||
#! @param this The iterator, of what is ordered.
|
||||
#! @returns Maybe the least; otherwise, `nothing` where it gives none.
|
||||
def min(&mut this): maybe<T> where T: ordered {
|
||||
var first := this.next();
|
||||
if first is not some(&mut start) {
|
||||
return nothing {};
|
||||
}
|
||||
var best := move(start);
|
||||
while this.next() is some(x) {
|
||||
if x < best {
|
||||
best = move(&mut x);
|
||||
}
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
#! Walks to the end, keeping the greatest element, the last of those equally
|
||||
#! greatest.
|
||||
#! @param this The iterator, of what is ordered.
|
||||
#! @returns Maybe the greatest; otherwise, `nothing` where it gives none.
|
||||
def max(&mut this): maybe<T> where T: ordered {
|
||||
var first := this.next();
|
||||
if first is not some(&mut start) {
|
||||
return nothing {};
|
||||
}
|
||||
var best := move(start);
|
||||
while this.next() is some(x) {
|
||||
if !(x < best) {
|
||||
best = move(&mut x);
|
||||
}
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
#! Walks to the end, keeping the least element as an order says, the first of
|
||||
#! those equally least.
|
||||
#! @tparam F The order.
|
||||
#! @param this The iterator.
|
||||
#! @param order Whether one element comes before another.
|
||||
#! @returns Maybe the least; otherwise, `nothing` where it gives none.
|
||||
def min_by<F>(&mut this, order: F): maybe<T> where F: invokable<def(&T, &T): bool> {
|
||||
var first := this.next();
|
||||
if first is not some(&mut start) {
|
||||
return nothing {};
|
||||
}
|
||||
var best := move(start);
|
||||
while this.next() is some(x) {
|
||||
if order(&x, &best) {
|
||||
best = move(&mut x);
|
||||
}
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
#! Walks to the end, keeping the greatest element as an order says, the last
|
||||
#! of those equally greatest.
|
||||
#! @tparam F The order.
|
||||
#! @param this The iterator.
|
||||
#! @param order Whether one element comes before another.
|
||||
#! @returns Maybe the greatest; otherwise, `nothing` where it gives none.
|
||||
def max_by<F>(&mut this, order: F): maybe<T> where F: invokable<def(&T, &T): bool> {
|
||||
var first := this.next();
|
||||
if first is not some(&mut start) {
|
||||
return nothing {};
|
||||
}
|
||||
var best := move(start);
|
||||
while this.next() is some(x) {
|
||||
if !order(&x, &best) {
|
||||
best = move(&mut x);
|
||||
}
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
#! Walks to the end, keeping the element whose key is least, the first of
|
||||
#! those equally least.
|
||||
#! @tparam K The key, which is ordered.
|
||||
#! @tparam F The function giving the key.
|
||||
#! @param this The iterator.
|
||||
#! @param key The function, given a reference to each element.
|
||||
#! @returns Maybe the element; otherwise, `nothing` where it gives none.
|
||||
def min_by_key<K, F>(&mut this, key: F): maybe<T> where F: invokable<def(&T): K> where K: ordered {
|
||||
var first := this.next();
|
||||
if first is not some(&mut start) {
|
||||
return nothing {};
|
||||
}
|
||||
var best := move(start);
|
||||
var least := key(&best);
|
||||
while this.next() is some(x) {
|
||||
var made := key(&x);
|
||||
if made < least {
|
||||
best = move(&mut x);
|
||||
least = move(&mut made);
|
||||
}
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
#! Walks to the end, keeping the element whose key is greatest, the last of
|
||||
#! those equally greatest.
|
||||
#! @tparam K The key, which is ordered.
|
||||
#! @tparam F The function giving the key.
|
||||
#! @param this The iterator.
|
||||
#! @param key The function, given a reference to each element.
|
||||
#! @returns Maybe the element; otherwise, `nothing` where it gives none.
|
||||
def max_by_key<K, F>(&mut this, key: F): maybe<T> where F: invokable<def(&T): K> where K: ordered {
|
||||
var first := this.next();
|
||||
if first is not some(&mut start) {
|
||||
return nothing {};
|
||||
}
|
||||
var best := move(start);
|
||||
var most := key(&best);
|
||||
while this.next() is some(x) {
|
||||
var made := key(&x);
|
||||
if !(made < most) {
|
||||
best = move(&mut x);
|
||||
most = move(&mut made);
|
||||
}
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
#! The last element: from the far end where the iterator walks backward,
|
||||
#! and otherwise by walking to the end.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the last element; otherwise, `nothing` where it gives none.
|
||||
def last(&mut this): maybe<T> {
|
||||
if const self is backward_iterator<T> {
|
||||
return this.next_back();
|
||||
}
|
||||
var kept: maybe<T> = nothing {};
|
||||
while this.next() is some(x) {
|
||||
kept = move(&mut x);
|
||||
}
|
||||
return kept;
|
||||
}
|
||||
|
||||
#! The element `n` along, counting from 0, passing over those before it.
|
||||
#! @param this The iterator.
|
||||
#! @param n How many to pass over first.
|
||||
#! @returns Maybe the element; otherwise, `nothing` where it gives fewer.
|
||||
def nth(&mut this, n: u64): maybe<T> {
|
||||
var left := n;
|
||||
while left != 0 {
|
||||
return nothing {} if this.next() is not some(x);
|
||||
left--;
|
||||
}
|
||||
return this.next();
|
||||
}
|
||||
|
||||
#! Walks to the end, calling a function on each element.
|
||||
#! @tparam F The function.
|
||||
#! @param this The iterator.
|
||||
#! @param f The function, given each element.
|
||||
def for_each<F>(&mut this, f: F): void where F: invokable<def(T): void> {
|
||||
while this.next() is some(x) {
|
||||
f(move(&mut x));
|
||||
}
|
||||
}
|
||||
|
||||
#! Walks to the end, collecting the elements into a vector, which is given
|
||||
#! room for them all at once where the iterator knows how many it has left.
|
||||
#! @param this The iterator.
|
||||
#! @returns The vector.
|
||||
def to_vector(&mut this): vector<T> {
|
||||
var made := vector::<T>::new();
|
||||
if const self is sized_iterator<T> {
|
||||
made.reserve(this.remaining());
|
||||
}
|
||||
while this.next() is some(x) {
|
||||
made.append(move(&mut x));
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Walks to the end, joining the text of the elements, as `$"{x}"` writes
|
||||
#! each, with text between each two.
|
||||
#! @param this The iterator.
|
||||
#! @param between What is written between each two elements.
|
||||
#! @returns The text.
|
||||
def to_string(&mut this, between: strview): string {
|
||||
var made := string::new(nothing {});
|
||||
var first := true;
|
||||
while this.next() is some(x) {
|
||||
if !first {
|
||||
made.append(between);
|
||||
}
|
||||
first = false;
|
||||
val text := $"{x}";
|
||||
made.append(text.view());
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! Walks to the end, appending each element to a container, as a `vector`, a
|
||||
#! `string`, or anything `extendable` is.
|
||||
#! @tparam C The container.
|
||||
#! @param this The iterator.
|
||||
#! @param target The container, which keeps what it held before.
|
||||
def into<C>(&mut this, target: &mut C): void where C: extendable<T> {
|
||||
while this.next() is some(x) {
|
||||
target.append(move(&mut x));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#! An iterator that knows how many elements it has left, as a view, a range,
|
||||
#! and the containers' iterators do, so collecting it makes room for them at
|
||||
#! once.
|
||||
#! @tparam T The type of what it gives.
|
||||
pub trait sized_iterator<T> : iterator<T> {
|
||||
#! How many elements it has left to give.
|
||||
#! @param this The iterator.
|
||||
#! @returns How many.
|
||||
def remaining(&this): u64;
|
||||
}
|
||||
|
||||
#! An iterator that also walks from its far end, until the two ends meet, as a
|
||||
#! view, a range, a `deque`, and a `chain` do, which `rev` and `last` take.
|
||||
#! @tparam T The type of what it gives.
|
||||
pub trait backward_iterator<T> : iterator<T> {
|
||||
#! Gives the element at the far end, or `none` once the ends have met.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the last element left; otherwise, `nothing`.
|
||||
def next_back(&mut this): maybe<T>;
|
||||
}
|
||||
Reference in new issue
Block a user