Documentation
¶
Overview ¶
Package storage holds the archetype tables behind package ecs: typed component columns, archetypes (rows of entities that share one component set) and an append-only index of archetypes with cached add and remove edges.
The package knows nothing about the component registry or the World. Callers inject a column factory into NewIndex and keep their own entity records, using the moved-entity results of row operations to fix them up.
Nothing here is safe for concurrent use; an Index belongs to one World.
Index ¶
- func Contains(set []CompID, id CompID) bool
- func IsSet(ids []CompID) bool
- type Archetype
- func (a *Archetype) AppendRow(e Entity) int
- func (a *Archetype) Column(i int) Column
- func (a *Archetype) ColumnIndex(id CompID) int
- func (a *Archetype) Comps() []CompID
- func (a *Archetype) Entities() []Entity
- func (a *Archetype) Entity(row int) Entity
- func (a *Archetype) Has(id CompID) bool
- func (a *Archetype) Len() int
- func (a *Archetype) RemoveRow(row int) (moved Entity, ok bool)
- type Column
- type CompID
- type Entity
- type Index
- type TypedColumn
- func (c *TypedColumn[T]) Append(v T)
- func (c *TypedColumn[T]) AppendAny(v any)
- func (c *TypedColumn[T]) AppendZero()
- func (c *TypedColumn[T]) Len() int
- func (c *TypedColumn[T]) MoveRowTo(dst Column, row int)
- func (c *TypedColumn[T]) Ptr(row int) *T
- func (c *TypedColumn[T]) PtrAny(row int) any
- func (c *TypedColumn[T]) Set(row int, v T)
- func (c *TypedColumn[T]) SetAny(row int, v any)
- func (c *TypedColumn[T]) SwapRemove(row int)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Archetype ¶
type Archetype struct {
// contains filtered or unexported fields
}
Archetype is a table of all entities that have exactly one set of components. Each component has a column; row i of every column and entities[i] describe the same entity, so all columns always have the same length as the entity list.
An archetype never gains or loses components, which is what makes its dense colIdx lookup safe. Archetypes are created by an Index and live as long as it does.
func (*Archetype) AppendRow ¶
AppendRow adds a row for e with zero values in every column and returns its index. The caller writes the real values next.
func (*Archetype) Column ¶
Column returns the i-th column, in Comps order. It panics if i is out of range.
func (*Archetype) ColumnIndex ¶
ColumnIndex returns the index of id's column (the position of id in Comps), or -1 when the archetype doesn't store id. It is O(1).
func (*Archetype) Comps ¶
Comps returns the sorted component set. The slice is shared and must not be modified.
func (*Archetype) Entities ¶
Entities returns the row -> entity table. The slice is shared: it is valid until the next AppendRow, RemoveRow or MoveRow involving this archetype, and must not be modified.
func (*Archetype) Entity ¶
Entity returns the entity stored in row. It panics if row is out of range.
type Column ¶
type Column interface {
// Len returns the number of rows.
Len() int
// AppendZero appends a zero value row.
AppendZero()
// AppendAny appends v, which must be a T.
AppendAny(v any)
// SetAny overwrites row with v, which must be a T. row must be in range.
SetAny(row int, v any)
// PtrAny returns &data[row] as a *T boxed in any. It doesn't allocate:
// a pointer fits in the interface word. The pointer is valid until the
// next append or swap-remove on this column.
PtrAny(row int) any
// SwapRemove removes row by moving the last row into it and shrinking the
// column. row must be in range.
SwapRemove(row int)
// MoveRowTo appends a copy of row to dst, which must be a column of the
// same T. It doesn't remove row; call SwapRemove for that.
MoveRowTo(dst Column, row int)
}
Column is the untyped view of a TypedColumn[T]. Archetype code works through it; generic fast paths downcast with col.(*TypedColumn[T]).
Values passed as any are T values, not *T. A value of any other type panics with the runtime's type-assertion error; callers validate types beforehand.
type CompID ¶
type CompID uint16
CompID identifies a registered component type. The registry hands out dense IDs starting at 0, so an ID can index a slice.
type Entity ¶
Entity is the handle stored in an archetype's row-to-entity table. It has the same layout as ecs.Entity; package ecs converts field by field.
func MoveRow ¶
MoveRow moves row of src into a new row of dst and returns that row. Components stored by both archetypes keep their values, components only dst stores are zero. The row is then swap-removed from src: ok is true and moved is the entity that now occupies row in src, whose record the caller must fix (ok is false when row was the last row). The moved entity's own record (archetype and dstRow) is also the caller's to update.
Preconditions, not checked: src != dst and row is in range. Pointers into both archetypes are invalidated. Cost is O(components) with no allocation once the destination columns have capacity.
type Index ¶
type Index struct {
// contains filtered or unexported fields
}
Index owns every archetype of one World and finds them by component set.
The index is append-only: archetypes are never removed, reordered or replaced, and empty ones stay. Archetype pointers therefore stay valid and the position in Archetypes is stable, so a query cache can remember how many archetypes it has scanned and look only at the new ones next time.
func NewIndex ¶
NewIndex returns an index whose archetypes create their columns with newColumn, called once per component of each new archetype. The archetype for the empty set is created immediately and is available as Empty.
func (*Index) Archetypes ¶
Archetypes returns every archetype in creation order. The slice only grows and existing elements never change, so callers may remember its length and scan [length:] later. It is shared and must not be modified.
func (*Index) Get ¶
Get returns the archetype for the component set ids, creating it when it doesn't exist yet. ids must be strictly ascending (sorted, no duplicates); Get panics with "ecs: internal error: ..." otherwise, which public API validation is expected to make unreachable. ids isn't retained.
A hit doesn't allocate and costs O(len(ids)).
type TypedColumn ¶
type TypedColumn[T any] struct { // contains filtered or unexported fields }
TypedColumn stores the values of one component type, one per archetype row.
Storage is a plain []T rather than raw bytes: components may hold pointers, strings and slices, and only a typed slice lets the GC see them.
func NewTypedColumn ¶
func NewTypedColumn[T any]() *TypedColumn[T]
NewTypedColumn returns an empty column of T. It is the natural body of a column factory passed to NewIndex.
func (*TypedColumn[T]) AppendAny ¶
func (c *TypedColumn[T]) AppendAny(v any)
AppendAny appends v, which must be a T.
func (*TypedColumn[T]) AppendZero ¶
func (c *TypedColumn[T]) AppendZero()
AppendZero appends a zero value row.
func (*TypedColumn[T]) MoveRowTo ¶
func (c *TypedColumn[T]) MoveRowTo(dst Column, row int)
MoveRowTo appends a copy of row to dst, which must be a *TypedColumn[T].
func (*TypedColumn[T]) Ptr ¶
func (c *TypedColumn[T]) Ptr(row int) *T
Ptr returns a pointer to row. It stays valid until the next append or swap-remove on this column. It panics if row is out of range.
func (*TypedColumn[T]) PtrAny ¶
func (c *TypedColumn[T]) PtrAny(row int) any
PtrAny returns Ptr(row) boxed in any, without allocating.
func (*TypedColumn[T]) Set ¶
func (c *TypedColumn[T]) Set(row int, v T)
Set overwrites row with v. It panics if row is out of range.
func (*TypedColumn[T]) SetAny ¶
func (c *TypedColumn[T]) SetAny(row int, v any)
SetAny overwrites row with v, which must be a T.
func (*TypedColumn[T]) SwapRemove ¶
func (c *TypedColumn[T]) SwapRemove(row int)
SwapRemove removes row by moving the last row into it.