From e1d05f02ab7c865d752d38d0611b95583876bd8b Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 8 Mar 2025 15:22:47 +0000 Subject: Partial implementation of array and lists --- projects/std/src/Std/Lists.shed | 58 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 projects/std/src/Std/Lists.shed (limited to 'projects/std/src/Std') diff --git a/projects/std/src/Std/Lists.shed b/projects/std/src/Std/Lists.shed new file mode 100644 index 00000000..8771eb74 --- /dev/null +++ b/projects/std/src/Std/Lists.shed @@ -0,0 +1,58 @@ +struct List[T](array: Array[T]) { + // TODO: generic effect + fun forEach(this, func: fn (T) ! Io -> Unit) ! Io -> Unit { + var index = 0; + val size = this.array.size(); + + while (index < size) { + func!(this.array.unsafeGet(index)); + assign index = index + 1; + } + } +} + +struct ListBuilder[T](arrayBuilder: ArrayBuilder[T]) { + fun add(this, element: T) -> ListBuilder[T] { + if (this.arrayBuilder.size() < this.arrayBuilder.capacity()) { + ListBuilder(this.arrayBuilder.unsafeAdd(element)) + } else { + val newCapacity = this.arrayBuilder.capacity() * 2; + val newArrayBuilder = this.arrayBuilder + .unsafeEnlarge(.capacity = newCapacity) + .unsafeAdd(element); + ListBuilder(newArrayBuilder) + } + } + + fun build(this) -> List[T] { + List(this.arrayBuilder.build()) + } +} + +pub fun listBuilder[T]() -> ListBuilder[T] { + ListBuilder[T](arrayBuilder[T](.capacity = 16)) +} + +native type Array[T] { + // Precondition: the index is in the range [0, size). + native fun unsafeGet(This, Int) -> T; + + native fun size(This) -> Int; +} + +native type ArrayBuilder[T] { + native fun capacity(This) -> Int; + + native fun size(This) -> Int; + + // Precondition: size < capacity. + native fun unsafeAdd(This, T) -> ArrayBuilder[T]; + + // Precondition: the new capacity is greater than or equal to the current + // capacity. + native fun unsafeEnlarge(This, .capacity: Int) -> ArrayBuilder[T]; + + native fun build(This) -> Array[T]; +} + +native fun arrayBuilder[T](.capacity: Int) -> ArrayBuilder[T]; -- cgit v1.2.3