# ============================================================================= # LIST (FRAMEWORK FILE) VERSION 2026-08-06 # ============================================================================= # This file provides list classes that automatically resize as items are added. # If your program doesn't need them, you could delete this file. # ----------------------------------------------------------------------------- class list view var _items: t[] view var count: int # --------------------------------------------------------------------------- constructor() ._items <- new t[](4) end constructor # --------------------------------------------------------------------------- func _grow_if_needed(new_size: int) var size: int size <- ._items.size if new_size > size then # round up new_size <- math::bit_and(new_size + 7, math::bit_not(7)) var new_items: t[] new_items <- new t[](new_size) new_items.copy_from(0, ._items, 0, .count) ._items <- new_items end if end func # --------------------------------------------------------------------------- func clear() if .count = 0 then return end if var items: t[] items <- ._items if items.size > 4 then ._items <- new t[](4) else items[3] <- null unsafe(items)[2] <- null unsafe(items)[1] <- null unsafe(items)[0] <- null end if .count <- 0 end func # --------------------------------------------------------------------------- func get[](i: int): t if i < 0 or i >= .count do kernel::fail_code(19) # array index out of bounds return unsafe(._items)[i] end func # --------------------------------------------------------------------------- func set[](i: int, value: t) if i < 0 or i >= .count do kernel::fail_code(19) # array index out of bounds unsafe(._items)[i] <- value end func # --------------------------------------------------------------------------- func append(item: t) var count: int count <- .count var new_count: int new_count <- count + 1 ._grow_if_needed(new_count) unsafe(._items)[count] <- item .count <- new_count end func # --------------------------------------------------------------------------- func to_array(): t[] var count: int count <- .count var result: t[] result <- new t[](count) result.copy_from(0, ._items, 0, count) return result end func end class # ----------------------------------------------------------------------------- class list_int view var _items: int[] view var count: int # --------------------------------------------------------------------------- constructor() ._items <- new int[](4) end constructor # --------------------------------------------------------------------------- func _grow_if_needed(new_size: int) var size: int size <- ._items.size if new_size > size then # round up new_size <- math::bit_and(new_size + 7, math::bit_not(7)) var new_items: int[] new_items <- new int[](new_size) new_items.copy_from(0, ._items, 0, .count) ._items <- new_items end if end func # --------------------------------------------------------------------------- func clear() if ._items.size > 4 then ._items <- new int[](4) end if .count <- 0 end func # --------------------------------------------------------------------------- func get[](i: int): int if i < 0 or i >= .count do kernel::fail_code(19) # array index out of bounds return unsafe(._items)[i] end func # --------------------------------------------------------------------------- func set[](i: int, value: int) if i < 0 or i >= .count do kernel::fail_code(19) # array index out of bounds unsafe(._items)[i] <- value end func # --------------------------------------------------------------------------- func append(item: int) var count: int count <- .count var new_count: int new_count <- count + 1 ._grow_if_needed(new_count) unsafe(._items)[count] <- item .count <- new_count end func # --------------------------------------------------------------------------- func to_array(): int[] var count: int count <- .count var result: int[] result <- new int[](count) result.copy_from(0, ._items, 0, count) return result end func end class