package ecs
import (
"fmt"
"math"
"reflect"
"unsafe"
"github.com/mlange-42/ark/ecs/stats"
)
type tableID uint32
const maxTableID = math .MaxUint32
type table struct {
entities entityColumn
components []*column
ids []ID
columns []column
relationIDs []relationID
id tableID
archetype archetypeID
len uint32
cap uint32
isFree bool
}
func newTable(id tableID , archetype *archetype , capacity uint32 , reg *componentRegistry , targets []Entity , relationIDs []relationID ) table {
entities := newEntityColumn (capacity )
columns := make ([]column , len (archetype .components ))
components := make ([]*column , maskTotalBits )
for i , id := range archetype .components {
itemSize := uintptr (archetype .itemSizes [i ])
columns [i ] = newColumn (uint32 (i ), reg .Types [id .id ], itemSize , archetype .isRelation [i ], reg .IsTrivial [id .id ], targets [i ], capacity )
components [id .id ] = &columns [i ]
}
return table {
id : id ,
archetype : archetype .id ,
components : components ,
entities : entities ,
ids : archetype .components ,
columns : columns ,
relationIDs : relationIDs ,
cap : capacity ,
}
}
func (t *table ) Recycle (targets []Entity , relationIDs []relationID ) {
t .relationIDs = relationIDs
for i := range t .columns {
t .columns [i ].target = targets [i ]
}
t .isFree = false
}
func (t *table ) HasRelations () bool {
return len (t .relationIDs ) > 0
}
func (t *table ) Add (entity Entity ) uint32 {
idx := t .len
t .Alloc (1 )
t .entities .Set (idx , unsafe .Pointer (&entity ))
return idx
}
func (t *table ) Get (component ID , index uintptr ) unsafe .Pointer {
return t .components [component .id ].Get (index )
}
func (t *table ) Has (component ID ) bool {
return t .components [component .id ] != nil
}
func (t *table ) GetEntity (index uintptr ) Entity {
return *(*Entity )(t .entities .Get (index ))
}
func (t *table ) GetRelation (component ID ) Entity {
return t .components [component .id ].target
}
func (t *table ) Column (component ID ) *column {
return t .components [component .id ]
}
func (t *table ) Set (component ID , index uint32 , src *column , srcIndex uint32 ) {
t .components [component .id ].Set (index , src , srcIndex )
}
func (t *table ) CopyAll (table *table , index uint32 , srcIndex uint32 ) {
for i := range t .columns {
t .columns [i ].Set (index , &table .columns [i ], srcIndex )
}
}
func (t *table ) SetEntity (index uint32 , entity Entity ) {
t .entities .Set (index , unsafe .Pointer (&entity ))
}
func (t *table ) Alloc (n uint32 ) {
t .Extend (n )
t .len += n
}
func (t *table ) Extend (by uint32 ) {
required := t .len + by
if t .cap >= required {
return
}
t .adjustCapacity (capPow2 (required ))
}
func (t *table ) CanShrink (minCapacity uint32 ) bool {
target := max (capPow2 (t .len ), minCapacity )
return t .cap > target
}
func (t *table ) Shrink (minCapacity uint32 ) bool {
target := max (capPow2 (t .len ), minCapacity )
if t .cap <= target {
return false
}
t .adjustCapacity (target )
return true
}
func (t *table ) adjustCapacity (cap uint32 ) {
t .cap = cap
t .entities .data = reflect .MakeSlice (reflect .SliceOf (entityType ), int (t .cap ), int (t .cap ))
newPtr := t .entities .data .Index (0 ).Addr ().UnsafePointer ()
if t .len > 0 {
copyPtr (t .entities .pointer , newPtr , uintptr (t .len )*entitySize )
}
t .entities .pointer = newPtr
for i := range t .columns {
column := &t .columns [i ]
old := column .data
column .data = reflect .MakeSlice (reflect .SliceOf (column .elemType ), int (t .cap ), int (t .cap ))
if column .isTrivial {
newPtr := column .data .Index (0 ).Addr ().UnsafePointer ()
if t .len > 0 {
copyPtr (column .pointer , newPtr , uintptr (t .len )*column .itemSize )
}
column .pointer = newPtr
} else {
column .pointer = column .data .Index (0 ).Addr ().UnsafePointer ()
if t .len > 0 {
reflect .Copy (column .data , old )
}
}
}
}
func (t *table ) Remove (index uint32 ) bool {
lastIndex := uintptr (t .len - 1 )
swapped := index != uint32 (lastIndex )
if swapped {
t .swapEntity (index , lastIndex )
t .removeColumnsSwapped (index , lastIndex )
} else {
t .zeroAllColumns (lastIndex )
}
t .len --
return swapped
}
func (t *table ) swapEntity (index uint32 , lastIndex uintptr ) {
size := entitySize
src := unsafe .Add (t .entities .pointer , lastIndex *size )
dst := unsafe .Add (t .entities .pointer , uintptr (index )*size )
copyPtr (src , dst , size )
}
func (t *table ) removeColumnsSwapped (index uint32 , lastIndex uintptr ) {
for i := range t .columns {
column := &t .columns [i ]
if column .isTrivial {
size := column .itemSize
src := unsafe .Add (column .pointer , lastIndex *size )
dst := unsafe .Add (column .pointer , uintptr (index )*size )
copyPtr (src , dst , size )
column .Zero (lastIndex )
continue
}
copyValue (column , column , lastIndex , uintptr (index ))
column .Zero (lastIndex )
}
}
func (t *table ) zeroAllColumns (lastIndex uintptr ) {
for i := range t .columns {
t .columns [i ].Zero (lastIndex )
}
}
func (t *table ) Reset () {
for c := range t .columns {
t .columns [c ].Reset (t .len )
}
t .len = 0
}
func (t *table ) AddAll (from *table , count uint32 ) {
t .Alloc (count )
t .entities .CopyToEnd (&from .entities , t .len , count )
for c := range t .columns {
t .columns [c ].CopyToEnd (&from .columns [c ], t .len , count )
}
}
func (t *table ) AddAllEntities (from *table , count uint32 ) {
t .Alloc (count )
t .entities .CopyToEnd (&from .entities , t .len , count )
}
func (t *table ) MatchesExact (relations []relationID ) bool {
if len (relations ) < len (t .relationIDs ) {
panic ("relation targets must be fully specified" )
}
for _ , rel := range relations {
column := t .components [rel .component .id ]
if column == nil {
continue
}
if !column .isRelation {
panic (fmt .Sprintf ("component %d is not a relation component" , rel .component .id ))
}
if rel .target != column .target {
return false
}
}
return true
}
func (t *table ) Matches (relations []relationID ) bool {
if len (relations ) == 0 || !t .HasRelations () {
return true
}
return t .matchesRelations (relations )
}
func (t *table ) matchesRelations (relations []relationID ) bool {
for _ , rel := range relations {
if rel .target != t .components [rel .component .id ].target {
return false
}
}
return true
}
func (t *table ) Len () int {
return int (t .len )
}
func (t *table ) Stats (memPerEntity int ) stats .Table {
cap := int (t .cap )
return stats .Table {
Size : t .Len (),
Capacity : cap ,
Memory : cap * memPerEntity ,
MemoryUsed : t .Len () * memPerEntity ,
}
}
func (t *table ) UpdateStats (memPerEntity int , stats *stats .Table ) {
cap := int (t .cap )
stats .Size = t .Len ()
stats .Capacity = cap
stats .Memory = cap * memPerEntity
stats .MemoryUsed = t .Len () * memPerEntity
}
The pages are generated with Golds v0.8.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .