Skip to content

Commit

Permalink
Added count method to queries
Browse files Browse the repository at this point in the history
  • Loading branch information
unitoftime committed Nov 20, 2023
1 parent 23fe989 commit ce8e32f
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 0 deletions.
4 changes: 4 additions & 0 deletions arch.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ type lookupList struct {
mask archetypeMask
}

func (l *lookupList) Len() int {
return l.index.Len()
}

// Adds ourselves to the last available hole, else appends
// Returns the index
func (l *lookupList) addToEasiestHole(id Id) int {
Expand Down
4 changes: 4 additions & 0 deletions hashmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ func newMap[K,V intkey](size int) *internalMap[K,V] {
intmap.New[K, V](0),
}
}
func (m *internalMap[K,V]) Len() int {
return m.inner.Len()
}

func (m *internalMap[K,V]) Get(k K) (V, bool) {
// v,ok := m.inner[k]
// return v, ok
Expand Down
14 changes: 14 additions & 0 deletions internal/gen/view.tgo
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,20 @@ func (v *View{{len $element}}[{{join $element ","}}]) Read(id Id) (*{{join $elem
return {{retlist $element}}
}

// Counts the number of entities that match this query
func (v *View{{len $element}}[{{join $element ","}}]) Count() int {
v.filter.regenerate(v.world)

total := 0
for _, archId := range v.filter.archIds {
lookup := v.world.engine.lookup[archId]
if lookup == nil { panic("LookupList is missing!") }

total += lookup.Len()
}
return total
}

// Maps the lambda function across every entity which matched the specified filters.
func (v *View{{len $element}}[{{join $element ","}}]) MapId(lambda func(id Id, {{lambdaArgs $element}})) {
v.filter.regenerate(v.world)
Expand Down
168 changes: 168 additions & 0 deletions view_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,20 @@ func (v *View1[A]) Read(id Id) (*A) {
return retA
}

// Counts the number of entities that match this query
func (v *View1[A]) Count() int {
v.filter.regenerate(v.world)

total := 0
for _, archId := range v.filter.archIds {
lookup := v.world.engine.lookup[archId]
if lookup == nil { panic("LookupList is missing!") }

total += lookup.Len()
}
return total
}

// Maps the lambda function across every entity which matched the specified filters.
func (v *View1[A]) MapId(lambda func(id Id, a *A)) {
v.filter.regenerate(v.world)
Expand Down Expand Up @@ -300,6 +314,20 @@ func (v *View2[A,B]) Read(id Id) (*A,*B) {
return retA, retB
}

// Counts the number of entities that match this query
func (v *View2[A,B]) Count() int {
v.filter.regenerate(v.world)

total := 0
for _, archId := range v.filter.archIds {
lookup := v.world.engine.lookup[archId]
if lookup == nil { panic("LookupList is missing!") }

total += lookup.Len()
}
return total
}

// Maps the lambda function across every entity which matched the specified filters.
func (v *View2[A,B]) MapId(lambda func(id Id, a *A, b *B)) {
v.filter.regenerate(v.world)
Expand Down Expand Up @@ -539,6 +567,20 @@ func (v *View3[A,B,C]) Read(id Id) (*A,*B,*C) {
return retA, retB, retC
}

// Counts the number of entities that match this query
func (v *View3[A,B,C]) Count() int {
v.filter.regenerate(v.world)

total := 0
for _, archId := range v.filter.archIds {
lookup := v.world.engine.lookup[archId]
if lookup == nil { panic("LookupList is missing!") }

total += lookup.Len()
}
return total
}

// Maps the lambda function across every entity which matched the specified filters.
func (v *View3[A,B,C]) MapId(lambda func(id Id, a *A, b *B, c *C)) {
v.filter.regenerate(v.world)
Expand Down Expand Up @@ -803,6 +845,20 @@ func (v *View4[A,B,C,D]) Read(id Id) (*A,*B,*C,*D) {
return retA, retB, retC, retD
}

// Counts the number of entities that match this query
func (v *View4[A,B,C,D]) Count() int {
v.filter.regenerate(v.world)

total := 0
for _, archId := range v.filter.archIds {
lookup := v.world.engine.lookup[archId]
if lookup == nil { panic("LookupList is missing!") }

total += lookup.Len()
}
return total
}

// Maps the lambda function across every entity which matched the specified filters.
func (v *View4[A,B,C,D]) MapId(lambda func(id Id, a *A, b *B, c *C, d *D)) {
v.filter.regenerate(v.world)
Expand Down Expand Up @@ -1092,6 +1148,20 @@ func (v *View5[A,B,C,D,E]) Read(id Id) (*A,*B,*C,*D,*E) {
return retA, retB, retC, retD, retE
}

// Counts the number of entities that match this query
func (v *View5[A,B,C,D,E]) Count() int {
v.filter.regenerate(v.world)

total := 0
for _, archId := range v.filter.archIds {
lookup := v.world.engine.lookup[archId]
if lookup == nil { panic("LookupList is missing!") }

total += lookup.Len()
}
return total
}

// Maps the lambda function across every entity which matched the specified filters.
func (v *View5[A,B,C,D,E]) MapId(lambda func(id Id, a *A, b *B, c *C, d *D, e *E)) {
v.filter.regenerate(v.world)
Expand Down Expand Up @@ -1406,6 +1476,20 @@ func (v *View6[A,B,C,D,E,F]) Read(id Id) (*A,*B,*C,*D,*E,*F) {
return retA, retB, retC, retD, retE, retF
}

// Counts the number of entities that match this query
func (v *View6[A,B,C,D,E,F]) Count() int {
v.filter.regenerate(v.world)

total := 0
for _, archId := range v.filter.archIds {
lookup := v.world.engine.lookup[archId]
if lookup == nil { panic("LookupList is missing!") }

total += lookup.Len()
}
return total
}

// Maps the lambda function across every entity which matched the specified filters.
func (v *View6[A,B,C,D,E,F]) MapId(lambda func(id Id, a *A, b *B, c *C, d *D, e *E, f *F)) {
v.filter.regenerate(v.world)
Expand Down Expand Up @@ -1745,6 +1829,20 @@ func (v *View7[A,B,C,D,E,F,G]) Read(id Id) (*A,*B,*C,*D,*E,*F,*G) {
return retA, retB, retC, retD, retE, retF, retG
}

// Counts the number of entities that match this query
func (v *View7[A,B,C,D,E,F,G]) Count() int {
v.filter.regenerate(v.world)

total := 0
for _, archId := range v.filter.archIds {
lookup := v.world.engine.lookup[archId]
if lookup == nil { panic("LookupList is missing!") }

total += lookup.Len()
}
return total
}

// Maps the lambda function across every entity which matched the specified filters.
func (v *View7[A,B,C,D,E,F,G]) MapId(lambda func(id Id, a *A, b *B, c *C, d *D, e *E, f *F, g *G)) {
v.filter.regenerate(v.world)
Expand Down Expand Up @@ -2109,6 +2207,20 @@ func (v *View8[A,B,C,D,E,F,G,H]) Read(id Id) (*A,*B,*C,*D,*E,*F,*G,*H) {
return retA, retB, retC, retD, retE, retF, retG, retH
}

// Counts the number of entities that match this query
func (v *View8[A,B,C,D,E,F,G,H]) Count() int {
v.filter.regenerate(v.world)

total := 0
for _, archId := range v.filter.archIds {
lookup := v.world.engine.lookup[archId]
if lookup == nil { panic("LookupList is missing!") }

total += lookup.Len()
}
return total
}

// Maps the lambda function across every entity which matched the specified filters.
func (v *View8[A,B,C,D,E,F,G,H]) MapId(lambda func(id Id, a *A, b *B, c *C, d *D, e *E, f *F, g *G, h *H)) {
v.filter.regenerate(v.world)
Expand Down Expand Up @@ -2498,6 +2610,20 @@ func (v *View9[A,B,C,D,E,F,G,H,I]) Read(id Id) (*A,*B,*C,*D,*E,*F,*G,*H,*I) {
return retA, retB, retC, retD, retE, retF, retG, retH, retI
}

// Counts the number of entities that match this query
func (v *View9[A,B,C,D,E,F,G,H,I]) Count() int {
v.filter.regenerate(v.world)

total := 0
for _, archId := range v.filter.archIds {
lookup := v.world.engine.lookup[archId]
if lookup == nil { panic("LookupList is missing!") }

total += lookup.Len()
}
return total
}

// Maps the lambda function across every entity which matched the specified filters.
func (v *View9[A,B,C,D,E,F,G,H,I]) MapId(lambda func(id Id, a *A, b *B, c *C, d *D, e *E, f *F, g *G, h *H, i *I)) {
v.filter.regenerate(v.world)
Expand Down Expand Up @@ -2912,6 +3038,20 @@ func (v *View10[A,B,C,D,E,F,G,H,I,J]) Read(id Id) (*A,*B,*C,*D,*E,*F,*G,*H,*I,*J
return retA, retB, retC, retD, retE, retF, retG, retH, retI, retJ
}

// Counts the number of entities that match this query
func (v *View10[A,B,C,D,E,F,G,H,I,J]) Count() int {
v.filter.regenerate(v.world)

total := 0
for _, archId := range v.filter.archIds {
lookup := v.world.engine.lookup[archId]
if lookup == nil { panic("LookupList is missing!") }

total += lookup.Len()
}
return total
}

// Maps the lambda function across every entity which matched the specified filters.
func (v *View10[A,B,C,D,E,F,G,H,I,J]) MapId(lambda func(id Id, a *A, b *B, c *C, d *D, e *E, f *F, g *G, h *H, i *I, j *J)) {
v.filter.regenerate(v.world)
Expand Down Expand Up @@ -3351,6 +3491,20 @@ func (v *View11[A,B,C,D,E,F,G,H,I,J,K]) Read(id Id) (*A,*B,*C,*D,*E,*F,*G,*H,*I,
return retA, retB, retC, retD, retE, retF, retG, retH, retI, retJ, retK
}

// Counts the number of entities that match this query
func (v *View11[A,B,C,D,E,F,G,H,I,J,K]) Count() int {
v.filter.regenerate(v.world)

total := 0
for _, archId := range v.filter.archIds {
lookup := v.world.engine.lookup[archId]
if lookup == nil { panic("LookupList is missing!") }

total += lookup.Len()
}
return total
}

// Maps the lambda function across every entity which matched the specified filters.
func (v *View11[A,B,C,D,E,F,G,H,I,J,K]) MapId(lambda func(id Id, a *A, b *B, c *C, d *D, e *E, f *F, g *G, h *H, i *I, j *J, k *K)) {
v.filter.regenerate(v.world)
Expand Down Expand Up @@ -3815,6 +3969,20 @@ func (v *View12[A,B,C,D,E,F,G,H,I,J,K,L]) Read(id Id) (*A,*B,*C,*D,*E,*F,*G,*H,*
return retA, retB, retC, retD, retE, retF, retG, retH, retI, retJ, retK, retL
}

// Counts the number of entities that match this query
func (v *View12[A,B,C,D,E,F,G,H,I,J,K,L]) Count() int {
v.filter.regenerate(v.world)

total := 0
for _, archId := range v.filter.archIds {
lookup := v.world.engine.lookup[archId]
if lookup == nil { panic("LookupList is missing!") }

total += lookup.Len()
}
return total
}

// Maps the lambda function across every entity which matched the specified filters.
func (v *View12[A,B,C,D,E,F,G,H,I,J,K,L]) MapId(lambda func(id Id, a *A, b *B, c *C, d *D, e *E, f *F, g *G, h *H, i *I, j *J, k *K, l *L)) {
v.filter.regenerate(v.world)
Expand Down

0 comments on commit ce8e32f

Please sign in to comment.