ludic/packages/ludic.base/ecs_table_reserve.ludic

43 lines
1.5 KiB
Text

# ludic.base/ecs_table_reserve.ludic - room made at the start: a table sized for its busiest moment
# never grows in play, and nothing that grows is ever given back. What does grow past it is here
@alloc_ok("at a world's start: the rows it will need")
export function tb_reserve<T>(tb: Table<T>, rows: int) -> void {
let k = rows - tb.n
if k <= 0 { return }
for j in 0 .. k { tb_push_row(tb, null, 0) }
for j in 0 .. k { tb_pop(tb) }
let s0 = len(tb.slot_gen)
for j in s0 .. rows {
push(tb.slot_gen, 0)
push(tb.slot_row, -1)
push(tb.free, 0)
}
for j in s0 .. rows {
List.pop(tb.slot_gen)
List.pop(tb.slot_row)
List.pop(tb.free)
}
while len(tb.blk) <= (rows >> 6) { push(tb.blk, 0) }
for g in 0 .. len(tb.grids) { grid_reserve(tb.grids[g], rows) }
}
# a row on every column, grid and index; past tb_reserve's rows each is a kept list's growth
function tb_push_row<T>(tb: Table<T>, r: T, h: int) -> void {
kept_push(tb.rec, r)
kept_push(tb.ent, h)
kept_push(tb.added, tb.tick)
kept_push(tb.changed, tb.tick)
for c in 0 .. len(tb.f) { kept_push(tb.f[c], 0.0) }
for c in 0 .. len(tb.i) { kept_push(tb.i[c], 0) }
tb.n += 1
for g in 0 .. len(tb.grids) { grid_grow(tb.grids[g]) }
for k in 0 .. len(tb.idx) { ix_grow(tb.idx[k]) }
}
# a slot is made once and reused through the free list
function tb_new_slot<T>(tb: Table<T>) -> int {
let s = len(tb.slot_gen)
kept_push(tb.slot_gen, 0)
kept_push(tb.slot_row, -1)
return s
}