storage

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 2 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains

func Contains(set []CompID, id CompID) bool

Contains reports whether the set contains id. Cost is O(log n).

func IsSet

func IsSet(ids []CompID) bool

IsSet reports whether ids is strictly ascending, i.e. sorted and free of duplicates. Component sets are always kept in this form.

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

func (a *Archetype) AppendRow(e Entity) int

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

func (a *Archetype) Column(i int) Column

Column returns the i-th column, in Comps order. It panics if i is out of range.

func (*Archetype) ColumnIndex

func (a *Archetype) ColumnIndex(id CompID) int

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

func (a *Archetype) Comps() []CompID

Comps returns the sorted component set. The slice is shared and must not be modified.

func (*Archetype) Entities

func (a *Archetype) Entities() []Entity

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

func (a *Archetype) Entity(row int) Entity

Entity returns the entity stored in row. It panics if row is out of range.

func (*Archetype) Has

func (a *Archetype) Has(id CompID) bool

Has reports whether the archetype stores component id. It is O(1).

func (*Archetype) Len

func (a *Archetype) Len() int

Len returns the number of rows (entities).

func (*Archetype) RemoveRow

func (a *Archetype) RemoveRow(row int) (moved Entity, ok bool)

RemoveRow swap-removes row: the last row moves into it. When another entity was moved, ok is true and moved is that entity, which now lives in row; the caller must update its record. ok is false when row was the last 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.

func Diff

func Diff(dst, a, b []CompID) []CompID

Diff appends the elements of a that are not in b to dst[:0] and returns it. It doesn't allocate when dst has enough capacity. dst must not alias a or b.

func Union

func Union(dst, a, b []CompID) []CompID

Union appends the union of the sets a and b to dst[:0] and returns it. It doesn't allocate when dst has enough capacity. dst must not alias a or b.

type Entity

type Entity struct {
	ID         uint32
	Generation uint32
}

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

func MoveRow(src *Archetype, row int, dst *Archetype) (dstRow int, moved Entity, ok bool)

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

func NewIndex(newColumn func(CompID) Column) *Index

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

func (ix *Index) Archetypes() []*Archetype

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) Empty

func (ix *Index) Empty() *Archetype

Empty returns the archetype with no components. It is always Archetypes()[0].

func (*Index) Get

func (ix *Index) Get(ids []CompID) *Archetype

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)).

func (*Index) WithAdded

func (ix *Index) WithAdded(a *Archetype, id CompID) *Archetype

WithAdded returns the archetype for a's components plus id, or a itself when it already has id. The result is cached on a, so repeated calls are a map lookup without allocation.

func (*Index) WithRemoved

func (ix *Index) WithRemoved(a *Archetype, id CompID) *Archetype

WithRemoved returns the archetype for a's components minus id, or a itself when it doesn't have id. The result is cached on a like WithAdded.

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]) Append

func (c *TypedColumn[T]) Append(v T)

Append appends v.

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]) Len

func (c *TypedColumn[T]) Len() int

Len returns the number of rows.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL