ludic/packages/ludic.base/ecs_table_remove.ludic
Orkuncakilkaya 52270ab5d1 feat(ecs): observers and block change stamps on a Table
tb_on_add / tb_on_remove take a fn(int) (its states supplied, like a system's) told each handle as
its row is made and before it goes (ecs_hooks.ludic), so what follows a table - a drawing, a
message - does so without a scan. Every row's tick now also stamps its 64-row block, and
tb_changed_since / tb_added_since skip the blocks nobody wrote since: a delta over a large table
costs the blocks that moved. A row moved into a removal's gap keeps its own tick (it was not
written); a removal is told by the hook. ecs_test holds both. The ludic.wildlife table
(5ac3d48, written while builds were held) now builds and passes its 19 tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-27 21:47:06 +03:00

49 lines
1.8 KiB
Text

# ludic.base/ecs_table_remove.ludic - removing a row moves the last one into its place, so the
# rows stay dense; every index is told, and the slot's generation moves on
export function tb_remove<T>(tb: Table<T>, h: int) -> bool {
if tb_row(tb, h) < 0 { return false }
for k in 0 .. len(tb.on_remove) { tb.on_remove[k].run(h) }
let r = tb_row(tb, h)
if r < 0 { return false }
for g in 0 .. len(tb.grids) { grid_unlink(tb.grids[g], r) }
for k in 0 .. len(tb.idx) { ix_unfile(tb.idx[k], r) }
let last = tb.n - 1
if r != last { tb_move(tb, last, r) }
tb_pop(tb)
let s = h & ECS_SLOT
tb.slot_row[s] = -1
tb.slot_gen[s] = (tb.slot_gen[s] + 1) & ECS_GEN
push(tb.free, s)
return true
}
function tb_move<T>(tb: Table<T>, from: int, to: int) -> void {
tb.rec[to] = tb.rec[from]
tb.ent[to] = tb.ent[from]
tb.added[to] = tb.added[from]
tb.changed[to] = tb.changed[from]
for c in 0 .. len(tb.f) { tb.f[c][to] = tb.f[c][from] }
for c in 0 .. len(tb.i) { tb.i[c][to] = tb.i[c][from] }
tb.slot_row[tb.ent[to] & ECS_SLOT] = to
let b = to >> 6
if tb.blk[b] < tb.changed[to] { tb.blk[b] = tb.changed[to] }
for g in 0 .. len(tb.grids) { grid_moved(tb.grids[g], from, to) }
for k in 0 .. len(tb.idx) { ix_moved(tb.idx[k], from, to) }
}
function tb_pop<T>(tb: Table<T>) -> void {
List.pop(tb.rec)
List.pop(tb.ent)
List.pop(tb.added)
List.pop(tb.changed)
for c in 0 .. len(tb.f) { List.pop(tb.f[c]) }
for c in 0 .. len(tb.i) { List.pop(tb.i[c]) }
for g in 0 .. len(tb.grids) { grid_shrink(tb.grids[g]) }
for k in 0 .. len(tb.idx) { ix_shrink(tb.idx[k]) }
tb.n -= 1
}
# every row gone; the slots keep their generations, so an old handle still misses
export function tb_clear<T>(tb: Table<T>) -> void {
while tb.n > 0 { tb_remove(tb, tb.ent[tb.n - 1]) }
}