package ecs
import "math"
type cacheID uint32
const maxCacheID = math .MaxUint32
type cacheEntry struct {
filter *filter
tables tableIDs
relations []relationID
id cacheID
}
type cache struct {
indices map [cacheID ]int
filters []cacheEntry
intPool intPool [cacheID ]
}
func newCache() cache {
return cache {
intPool : newIntPool [cacheID ](128 ),
indices : map [cacheID ]int {},
filters : []cacheEntry {},
}
}
func (c *cache ) getEntry (id cacheID ) *cacheEntry {
return &c .filters [c .indices [id ]]
}
func (c *cache ) register (storage *storage , filter *filter , relations []relationID ) {
id := c .intPool .Get ()
filter .cache = id
index := len (c .filters )
tables := newTableIDs (storage .getCacheTables (filter , relations )...)
c .filters = append (c .filters ,
cacheEntry {
id : id ,
filter : filter ,
relations : relations ,
tables : tables ,
})
c .indices [id ] = index
}
func (c *cache ) unregister (filter *filter ) {
idx , ok := c .indices [filter .cache ]
if !ok {
panic ("no filter for id found to unregister" )
}
delete (c .indices , filter .cache )
filter .cache = maxCacheID
last := len (c .filters ) - 1
if idx != last {
c .filters [idx ], c .filters [last ] = c .filters [last ], c .filters [idx ]
c .indices [c .filters [idx ].id ] = idx
}
c .filters [last ] = cacheEntry {}
c .filters = c .filters [:last ]
}
func (c *cache ) addTable (storage *storage , table *table ) {
arch := &storage .archetypes [table .archetype ]
if !table .HasRelations () {
for i := range c .filters {
e := &c .filters [i ]
if !e .filter .matches (&arch .mask ) {
continue
}
e .tables .Append (table .id )
}
return
}
for i := range c .filters {
e := &c .filters [i ]
if !e .filter .matches (&arch .mask ) {
continue
}
if !table .Matches (e .relations ) {
continue
}
e .tables .Append (table .id )
}
}
func (c *cache ) removeTable (table *table ) {
for i := range c .filters {
e := &c .filters [i ]
e .tables .Remove (table .id )
}
}
func (c *cache ) Reset () {
if len (c .indices ) == 0 {
return
}
for i := range c .filters {
f := &c .filters [i ]
f .filter .cache = maxCacheID
}
c .indices = map [cacheID ]int {}
c .filters = c .filters [:0 ]
c .intPool .Reset ()
}
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 .