Table

View source

Stores component values for one exact ECS component layout.

A Table organizes each configured component TypeId in its own column and addresses the values by row. Create it with the component TypeIds, then use its row methods to add, read, update, move, or remove values. Rows stay dense, so removing or moving a row can change another row’s index.

Use Table#insert to append a complete row. Use Table#reserve with Table#insertUnchecked when you need to allocate and populate a row in separate steps.

Examples

import { Table } from '@wimaengine/ecs'
import { typeid } from '@wimaengine/type'

class Position {}
class Velocity {}

const positionId = typeid(Position)
const velocityId = typeid(Velocity)
const table = new Table([positionId, velocityId])

const row = table.insert(
  [positionId, velocityId],
  [new Position(), new Velocity()]
)

table.set(positionId, row, new Position())
const velocity = table.get(velocityId, row)

Constructor


constructor(typeids: TypeId[])

View source

Creates an empty table with one column for each unique type ID.

The type IDs define this table’s exact component layout. Rows are added later with Table#insert or Table#reserve.

Parameters

NameDescriptionDefault
typeids

Type IDs for the columns this table stores.

—

Properties


readonlycolumns: ReadonlyMap<TypeId, unknown[]>

View source

Column arrays keyed by component type ID. Every column uses the same row indexes, so values at one row belong to the same entity.

The map reference is readonly in the generated type. Table operations still write to the arrays when rows are inserted, updated, moved, or removed.

Default: ...

Methods


set(typeid: TypeId, row: TableRow, value: unknown): void

View source

Writes a value into a configured column.

If row is greater than or equal to Table#size, this method returns without changing the table. It also does nothing when typeid is not one of the table’s columns. The upper bound is the only row check; negative rows are not rejected, so callers should pass a non-negative row returned by Table#insert or Table#reserve.

Parameters

NameDescriptionDefault
typeid——
row——
value——

get(typeid: TypeId, row: TableRow): unknown

View source

Reads a value from a configured column at a row.

Returns undefined when the type ID is not configured. For a configured column, this is a direct lookup; the method does not explicitly compare row with Table#size. Callers are responsible for passing a valid allocated row, and an unpopulated array entry normally reads as undefined.

Parameters

NameDescriptionDefault
typeid——
row——

reserve(): TableRow

View source

Reserves the next row index without populating its columns.

Returns the table’s current size, then increments the logical row count. The column arrays are not initialized by this method. Fill the returned row immediately with Table#set or Table#insertUnchecked before another operation relies on the table being dense.

insert(ids: TypeId[], components: unknown[]): TableRow

View source

Appends a row and returns its new row index.

ids and components are parallel arrays. This method first checks that they have the same length, then reserves a row and writes each pair. Every supplied ID must already be a column in this table or insertion throws an Error. To keep the row complete, supply every column ID; this method does not verify that the set is complete and cannot roll back a row after a later insertion error.

This is the safer insertion path for appending a row because it owns row reservation and validates the parallel array lengths. Use Table#insertUnchecked only when those checks are already guaranteed by the caller.

Parameters

NameDescriptionDefault
ids

Column IDs corresponding to components.

—
components

Values to store at the new row.

—

Throws

If the arrays have different lengths or an ID is unknown.

Returns

The newly reserved row index.

insertUnchecked(index: TableRow, ids: TypeId[], components: unknown[]): void

View source

Writes component values into a chosen row without reserving it.

This low-level path does not change Table#size, check the row bounds, compare the lengths of ids and components, or ensure that every table column is filled. It writes one value for each item in components; ids[i] must name a configured column or the method throws after any prior pairs have already been written.

Use it only for an existing row, or for a row returned by Table#reserve, when the caller can guarantee the IDs and complete the row itself. Use Table#insert for normal row insertion.

Parameters

NameDescriptionDefault
index

Existing or reserved row to populate.

—
ids

Column IDs corresponding to components.

—
components

Values to write into the chosen row.

—

Throws

If a supplied ID is not a column in this table.

remove(row: TableRow): void

View source

Removes a row using swap removal and keeps the columns dense.

When the removed row is not the last row, the last value in each column may take its place, so row order is not preserved and another row’s index can change. The logical table size is then decremented. If other records store row indexes, the caller must update any index affected by the swap.

Pass a valid row index. This method does not validate row bounds.

Parameters

NameDescriptionDefault
row

Row to remove.

—

extract(row: TableRow): [TypeId[], unknown[]]

View source

Reads every column value at a row without removing it.

Returns parallel arrays in the table’s column order: the first array contains the type IDs and the second contains the value from each matching column. The table is unchanged. The row must be valid; this method does not perform a bounds check.

Parameters

NameDescriptionDefault
row

Row whose complete component set should be read.

—

Returns

Column IDs and their values at row.

hasOnly(ids: TypeId[]): boolean

View source

Tests whether a list describes exactly this table’s column set.

The order of ids does not matter. The list must have the same length as the table’s columns, and every supplied ID must be present. Pass one occurrence of each ID; this method does not reject duplicates. Row contents are not inspected.

Parameters

NameDescriptionDefault
ids

Type IDs to compare with this table’s columns.

—

Returns

true when the column sets are exactly equal.

moveTo(newTable: Table, row: TableRow): TableRow

View source

Copies a row into another table, then removes it from this table.

The destination reserves a new row first. Values for this table’s columns are copied with Table#set; a source column missing from the destination is therefore ignored. Columns that exist only in the destination remain unpopulated, so the caller must fill them afterward. The source row is then removed with swap removal, which can change another row’s index. The source row index is not preserved.

Use this when moving a row between tables with different component layouts. Pass a valid source row and update any other row indexes affected by the source table’s swap.

Examples

const destinationRow = source.moveTo(destination, sourceRow)
destination.insertUnchecked(destinationRow, [healthId], [new Health()])

Parameters

NameDescriptionDefault
newTable

Table receiving the row.

—
row

Source row to move.

—

Returns

The newly reserved row in newTable.