package ecs
import (
"fmt"
"math"
"unsafe"
)
type number interface {
int | int8 | uint8 | int16 | uint16 | int32 | uint32 | cacheID | observerID
}
type entityPool struct {
pointer unsafe .Pointer
entities []Entity
next entityID
available uint32
reserved entityID
}
func newEntityPool(initialCapacity uint32 , reserved uint32 ) entityPool {
entities := make ([]Entity , reserved , initialCapacity +reserved )
for i := range reserved {
entities [i ] = Entity {entityID (i ), math .MaxUint32 }
}
return entityPool {
entities : entities ,
next : 0 ,
available : 0 ,
pointer : unsafe .Pointer (&entities [0 ]),
reserved : entityID (reserved ),
}
}
func (p *entityPool ) Get () Entity {
if p .available == 0 {
return p .getNew ()
}
curr := p .next
p .next , p .entities [p .next ].id = p .entities [p .next ].id , p .next
p .available --
return p .entities [curr ]
}
func (p *entityPool ) getNew () Entity {
e := Entity {id : entityID (len (p .entities )), gen : 0 }
p .entities = append (p .entities , e )
p .pointer = unsafe .Pointer (&p .entities [0 ])
return e
}
func (p *entityPool ) Recycle (e Entity ) {
if e .id < p .reserved {
panic ("can't recycle reserved zero or wildcard entity" )
}
p .entities [e .id ].gen ++
p .next , p .entities [e .id ].id = e .id , p .next
p .available ++
}
func (p *entityPool ) Reset () {
p .entities = p .entities [:p .reserved ]
p .next = 0
p .available = 0
}
func (p *entityPool ) Alive (e Entity ) bool {
return e .gen == (*Entity )(unsafe .Add (p .pointer , entitySize *uintptr (e .id ))).gen
}
func (p *entityPool ) Len () int {
return len (p .entities ) - int (p .reserved ) - int (p .available )
}
func (p *entityPool ) Cap () int {
return len (p .entities ) - int (p .reserved )
}
func (p *entityPool ) TotalCap () int {
return cap (p .entities )
}
func (p *entityPool ) Available () int {
return int (p .available )
}
type bitPool struct {
bits []uint8
length uint8
next uint8
available uint8
}
func newBitPool() bitPool {
return bitPool {
bits : make ([]uint8 , mask64TotalBits ),
}
}
func (p *bitPool ) Get () uint8 {
if p .available == 0 {
return p .getNew ()
}
curr := p .next
p .next , p .bits [p .next ] = p .bits [p .next ], p .next
p .available --
return p .bits [curr ]
}
func (p *bitPool ) getNew () uint8 {
if p .length >= mask64TotalBits {
panic (fmt .Sprintf ("run out of the maximum of %d bits. " +
"This is likely caused by unclosed queries that lock the world. " +
"Make sure that all queries finish their iteration or are closed manually" , mask64TotalBits ))
}
b := p .length
p .bits [p .length ] = b
p .length ++
return b
}
func (p *bitPool ) Recycle (b uint8 ) {
p .next , p .bits [b ] = b , p .next
p .available ++
}
func (p *bitPool ) Reset () {
p .next = 0
p .length = 0
p .available = 0
}
type intPool[T number ] struct {
pool []T
next T
available uint32
capacityIncrement uint32
}
func newIntPool[T number ](capacityIncrement uint32 ) intPool [T ] {
return intPool [T ]{
pool : make ([]T , 0 , capacityIncrement ),
next : 0 ,
available : 0 ,
capacityIncrement : capacityIncrement ,
}
}
func (p *intPool [T ]) Get () T {
if p .available == 0 {
return p .getNew ()
}
curr := p .next
p .next , p .pool [p .next ] = p .pool [p .next ], p .next
p .available --
return p .pool [curr ]
}
func (p *intPool [T ]) getNew () T {
e := T (len (p .pool ))
if len (p .pool ) == cap (p .pool ) {
old := p .pool
p .pool = make ([]T , len (p .pool ), len (p .pool )+int (p .capacityIncrement ))
copy (p .pool , old )
}
p .pool = append (p .pool , e )
return e
}
func (p *intPool [T ]) Recycle (e T ) {
p .next , p .pool [e ] = e , p .next
p .available ++
}
func (p *intPool [T ]) Reset () {
p .pool = p .pool [:0 ]
p .next = 0
p .available = 0
}
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 .