dsa.arr
Growable arrays with checked indexing, optional access, and iteration.
All modules · Writing docstrings
Imports and exports
Section titled “Imports and exports”import core.{Opt, GetItem, SetItem, ToBin}import mem.{Ptr, Buf, heap}kind Arr[T]A growable sequence of values of type T. len counts initialized elements.
Use get for optional access or brackets for access that stops on an invalid index.
let values = Arr[Int].new()values.push(10)values.push(20)print(values.len)print(values.pop().unwrap())print(values.get(8).unwrap_or(-1))220-1Arr.buf
Section titled “Arr.buf”buf: Buf[T]Backing buffer. Changing it directly can violate the array’s invariants.
Arr.len
Section titled “Arr.len”len: IntNumber of live elements, from zero through the buffer capacity.
Arr[@T]
Section titled “Arr[@T]”impl Arr[@T]Arr[@T].new
Section titled “Arr[@T].new”func new(): Arr[T]Create an empty array with an initial capacity of four.
Arr[@T].push
Section titled “Arr[@T].push”func push(self, val: T)Append val, growing the backing allocation when full. Mutates the array.
Arr[@T].pop
Section titled “Arr[@T].pop”func pop(self): Opt[T]Remove and return the last value, or Non if empty.
Arr[@T].get
Section titled “Arr[@T].get”func get(self, index: Int): Opt[T]Return the indexed value, or Non for a negative or out-of-range index.
Arr[@T].set
Section titled “Arr[@T].set”func set(self, index: Int, val: T): BoolReplace an existing element. Return false without writing if the index is invalid.
let values = [10, 20]print(values.set(1, 30))print(values[1])print(values.set(9, 50))true30falseArrIter
Section titled “ArrIter”kind ArrIter[T]An array iterator. Avoid changing the source array while iterating.
ArrIter.arr
Section titled “ArrIter.arr”arr: Arr[T]Array value used by this iterator.
ArrIter.idx
Section titled “ArrIter.idx”idx: IntIndex of the next element to visit.
ArrIter[@T] as Iter[T]
Section titled “ArrIter[@T] as Iter[T]”impl ArrIter[@T] as Iter[T]ArrIter[@T] as Iter[T].next
Section titled “ArrIter[@T] as Iter[T].next”func next(self): Opt[T]Yield the next element and advance, or return Non after the last element.
Arr[@T] as GetItem[Int, T]
Section titled “Arr[@T] as GetItem[Int, T]”impl Arr[@T] as GetItem[Int, T]Arr[@T] as GetItem[Int, T].get_item
Section titled “Arr[@T] as GetItem[Int, T].get_item”func get_item(self, key: Int): TImplement array[key]. Stop with an error when key is outside 0..len.
Arr[@T] as SetItem[Int, T]
Section titled “Arr[@T] as SetItem[Int, T]”impl Arr[@T] as SetItem[Int, T]Arr[@T] as SetItem[Int, T].set_item
Section titled “Arr[@T] as SetItem[Int, T].set_item”func set_item(self, key: Int, val: T)Implement array[key] = val. Stop with an error for an invalid index.
Arr[@T] as ToIter[T]
Section titled “Arr[@T] as ToIter[T]”impl Arr[@T] as ToIter[T]Arr[@T] as ToIter[T].to_iter
Section titled “Arr[@T] as ToIter[T].to_iter”func to_iter(self): ArrIter[T]Create an iterator starting at the first element.
Arr[Byte] as ToBin
Section titled “Arr[Byte] as ToBin”impl Arr[Byte] as ToBinArr[Byte] as ToBin.to_bin
Section titled “Arr[Byte] as ToBin.to_bin”func to_bin(self): BinCopy the live bytes into an immutable byte string. Available on Arr[Byte].