class list<t>
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
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
if ._items.size > 4 then
._items <- new t[](4)
else
._items[0] <- null
._items[1] <- null
._items[2] <- null
._items[3] <- null
end if
0 -> .count
end func
func get[](i: int): t
if i < 0 or i >= .count then
kernel::fail("out of bounds")
end if
return unsafe(._items)[i]
end func
func set[](i: int, value: t)
if i < 0 or i >= .count then
kernel::fail("out of bounds")
end if
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
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
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
0 -> .count
end func
func get[](i: int): int
if i < 0 or i >= .count then
kernel::fail("out of bounds")
end if
return unsafe(._items)[i]
end func
func set[](i: int, value: int)
if i < 0 or i >= .count then
kernel::fail("out of bounds")
end if
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
end class