← alloc
alloc/collections/list.kur
use package as alloc;
use core::cap::{Access, Exclusive!};
use core::mem::Ref;
use alloc::arena::{Allocate, ref_in};
use core::option::{Option, Result};
use core::iter::Iterator;
forall T, 'k.
enum List {
Cell(Ref[Cons[T, 'k], 'k]),
Empty,
}
forall T, 'k.
struct Cons {
head: T,
tail: List[T, 'k],
}
forall T, 'k.
impl List[T, 'k] {
fn empty() -> List[T, 'k] {
List::Empty
}
forall C, R where Copy[T], Allocate[C -> 'k, R].
fn cons(self, &cx: C, head: T) -> List[T, 'k] {
List::Cell.(ref_in(&cx, Cons.{ head, tail: self }))
}
fn is_empty(self) -> bool {
match self {
Empty => true,
Cell(_) => false,
}
}
}
forall T, 'k where Copy[T].
impl List[T, 'k] {
forall C, R where Access[C -> 'k, R].
fn pop(&self, &cx: C) -> Option[T] {
match self {
Empty => Option::None,
Cell(r) => {
let cell = r.copy(&cx);
self = cell.tail;
Option::Some.(cell.head)
},
}
}
forall C, R where Access[C -> 'k, R].
fn len(self, &cx: C) -> usize {
var rest = self;
var count = 0usize;
while !rest.is_empty() {
rest = rest.tail(&cx);
count = count + 1usize;
}
count
}
forall C, R, Q where Access[C -> 'k, R], Allocate[C -> 'k, Q].
fn append(self, &cx: C, other: List[T, 'k]) -> List[T, 'k] {
var out = ListBuilder!::new();
var rest = self;
var going = true;
while going {
match &rest.pop(&cx) {
Some(x) => &out.push(&cx, x),
None => {
going = false;
},
}
}
out.finish(&cx, other)
}
forall C, R, Q where Access[C -> 'k, R], Allocate[C -> 'k, Q].
fn reverse(self, &cx: C, acc: List[T, 'k]) -> List[T, 'k] {
var rest = self;
var out = acc;
var going = true;
while going {
let next = &rest.pop(&cx);
match next {
Some(x) => {
out = out.cons(&cx, x);
},
None => {
going = false;
},
}
}
out
}
forall C, R where Access[C -> 'k, R].
fn nth(self, &cx: C, index: usize) -> Option[T] {
self.skip(&cx, index).first(&cx)
}
forall C, R where Access[C -> 'k, R].
fn first(self, &cx: C) -> Option[T] {
match self {
Empty => Option::None,
Cell(r) => Option::Some.(r.copy(&cx).head),
}
}
forall C, R where Access[C -> 'k, R].
fn tail(self, &cx: C) -> List[T, 'k] {
match self {
Empty => List::Empty,
Cell(r) => r.copy(&cx).tail,
}
}
forall C, R where Access[C -> 'k, R].
fn skip(self, &cx: C, count: usize) -> List[T, 'k] {
var rest = self;
var left = count;
while left > 0usize && !rest.is_empty() {
rest = rest.tail(&cx);
left = left - 1usize;
}
rest
}
forall C, R where Access[C -> 'k, R].
fn last(self, &cx: C) -> Option[T] {
var rest = self;
var found = Option::None;
var going = true;
while going {
let next = &rest.pop(&cx);
match next {
Some(x) => {
found = Option::Some.(x);
},
None => {
going = false;
},
}
}
found
}
forall Env, C, R where Access[C -> 'k, R], Copy[Env].
fn eq_with(self, &cx: C, other: List[T, 'k], eq: closure[Env](&_: C, T, T) -> bool) -> bool {
var a = self;
var b = other;
var same = true;
var going = true;
while same && going {
let x = &a.pop(&cx);
let y = &b.pop(&cx);
match x {
Some(u) => {
match y {
Some(v) => {
same = eq(&cx, u, v);
},
None => {
same = false;
},
}
},
None => {
same = y.is_none();
going = false;
},
}
}
same
}
forall U, Env, C, R where Access[C -> 'k, R], Copy[U], Copy[Env].
fn find_map(self, &cx: C, f: closure[Env](&_: C, T) -> Option[U]) -> Option[U] {
var rest = self;
var found = Option::None;
var going = true;
while going {
let next = &rest.pop(&cx);
match next {
Some(x) => {
found = f(&cx, x);
going = found.is_none();
},
None => {
going = false;
},
}
}
found
}
forall Env, C, R where Access[C -> 'k, R], Copy[Env].
fn find(self, &cx: C, f: closure[Env](&_: C, T) -> bool) -> Option[T] {
self.find_map(&cx, |&c, x| if f(&c, x) { Option::Some.(x) } else { Option::None })
}
forall Env, C, R where Access[C -> 'k, R], Copy[Env].
fn any(self, &cx: C, f: closure[Env](&_: C, T) -> bool) -> bool {
self.find(&cx, f).is_some()
}
forall Env, C, R where Access[C -> 'k, R], Copy[Env].
fn all(self, &cx: C, f: closure[Env](&_: C, T) -> bool) -> bool {
!self.any(&cx, |&c, x| !f(&c, x))
}
forall Env, C, R where Access[C -> 'k, R], Copy[Env].
fn each(self, &cx: C, f: closure[Env](&_: C, T) -> ()) {
var rest = self;
var going = true;
while going {
let next = &rest.pop(&cx);
match next {
Some(x) => f(&cx, x),
None => {
going = false;
},
}
}
}
forall Env, C, R where Access[C -> 'k, R], Copy[Env].
fn count(self, &cx: C, f: closure[Env](&_: C, T) -> bool) -> usize {
self.fold(&cx, 0usize, |&c, n, x| if f(&c, x) { n + 1usize } else { n })
}
forall A, Env, C, R where Access[C -> 'k, R], Copy[Env].
fn fold(self, &cx: C, acc: A, f: closure[Env](&_: C, A, T) -> A) -> A {
var rest = self;
var acc = acc;
var going = true;
while going {
let next = &rest.pop(&cx);
match next {
Some(x) => {
acc = f(&cx, acc, x);
},
None => {
going = false;
},
}
}
acc
}
forall E, Env, C, R where Access[C -> 'k, R], Copy[Env].
fn try_each(self, &cx: C, f: closure[Env](&_: C, T) -> Result[(), E]) -> Result[(), E] {
var rest = self;
while true {
match &rest.pop(&cx) {
Some(x) => {
f(&cx, x)?;
},
None => {
return Result::Ok.(());
},
}
}
Result::Ok.(())
}
forall A, E, Env, C, R where Access[C -> 'k, R], Copy[Env].
fn try_fold(self, &cx: C, acc: A, f: closure[Env](&_: C, A, T) -> Result[A, E]) -> Result[A, E] {
var rest = self;
var acc = acc;
while !rest.is_empty() {
match &rest.pop(&cx) {
Some(x) => {
acc = f(&cx, acc, x)?;
},
None => (),
}
}
Result::Ok.(acc)
}
forall U, Env, C, R, Q where Access[C -> 'k, R], Allocate[C -> 'k, Q], Copy[U], Copy[Env].
fn map(self, &cx: C, f: closure[Env](&_: C, T) -> U) -> List[U, 'k] {
var out = ListBuilder!::new();
var rest = self;
var going = true;
while going {
match &rest.pop(&cx) {
Some(x) => {
let u = f(&cx, x);
&out.push(&cx, u);
},
None => {
going = false;
},
}
}
out.finish(&cx, List::Empty)
}
forall U, Env, C, R, Q where Access[C -> 'k, R], Allocate[C -> 'k, Q], Copy[U], Copy[Env].
fn filter_map(self, &cx: C, f: closure[Env](&_: C, T) -> Option[U]) -> List[U, 'k] {
var out = ListBuilder!::new();
var rest = self;
var going = true;
while going {
match &rest.pop(&cx) {
Some(x) => {
match f(&cx, x) {
Some(u) => &out.push(&cx, u),
None => (),
}
},
None => {
going = false;
},
}
}
out.finish(&cx, List::Empty)
}
forall Env, C, R, Q where Access[C -> 'k, R], Allocate[C -> 'k, Q], Copy[Env].
fn filter(self, &cx: C, f: closure[Env](&_: C, T) -> bool) -> List[T, 'k] {
self.filter_map(&cx, |&c, x| if f(&c, x) { Option::Some.(x) } else { Option::None })
}
forall U, E, Env, C, R, Q where Access[C -> 'k, R], Allocate[C -> 'k, Q], Copy[U], Copy[Env].
fn try_map(self, &cx: C, f: closure[Env](&_: C, T) -> Result[U, E]) -> Result[List[U, 'k], E] {
var out = ListBuilder!::new();
var rest = self;
while !rest.is_empty() {
match &rest.pop(&cx) {
Some(x) => match f(&cx, x) {
Ok(u) => &out.push(&cx, u),
Err(e) => {
let unfinished = out.finish(&cx, List::Empty);
return Result::Err.(e);
},
},
None => (),
}
}
Result::Ok.(out.finish(&cx, List::Empty))
}
}
forall T, 'k.
struct ListBuilder! {
first: List[T, 'k],
last: Option[Ref[Cons[T, 'k], 'k]],
}
forall T, 'k where Copy[T].
impl ListBuilder![T, 'k] {
fn new() -> ListBuilder![T, 'k] {
ListBuilder!.{ first: List::Empty, last: Option::None }
}
forall C, R, Q where Access[C -> 'k, R], Allocate[C -> 'k, Q].
fn push(&self, &cx: C, value: T) {
let { first, last } = self;
let cell = ref_in(&cx, Cons.{ head: value, tail: List::Empty });
let first = match last {
None => List::Cell.(cell),
Some(r) => {
let previous = r.copy(&cx);
r.set(&cx, Cons.{ head: previous.head, tail: List::Cell.(cell) });
first
},
};
self = ListBuilder!.{ first, last: Option::Some.(cell) };
}
forall C, R where Access[C -> 'k, R].
fn finish(self, &cx: C, tail: List[T, 'k]) -> List[T, 'k] {
let { first, last } = self;
match last {
None => tail,
Some(r) => {
let previous = r.copy(&cx);
r.set(&cx, Cons.{ head: previous.head, tail });
first
},
}
}
}
forall T, 'k, C, R.
exposed struct ListIter! {
tag: Exclusive!['k],
held: R,
rest: List[T, 'k],
}
forall T, 'k where Copy[T].
impl List[T, 'k] {
forall C, R where Access[C -> 'k, R].
fn iter(self, cx: C) -> ListIter![T, 'k, C, R] {
let (tag, held) = Access[C -> 'k, R]::open(cx);
ListIter!.{ tag, held, rest: self }
}
}
forall T, 'k, C, R where Access[C -> 'k, R].
impl ListIter![T, 'k, C, R] {
fn finish(self) -> C {
let { tag, held, rest: _rest } = self;
Access[C -> 'k, R]::close(tag, held)
}
}
forall T, 'k, C, R where Copy[T].
instance Iterator[ListIter![T, 'k, C, R] -> T] {
fn next(&it: ListIter![T, 'k, C, R]) -> Option[T] {
var { tag, held, rest } = it;
var rest = rest;
let item = &rest.pop(&tag);
it = ListIter!.{ tag, held, rest };
item
}
}