-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathquery.go
200 lines (175 loc) · 5.13 KB
/
query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package donburi
import (
"cmp"
"iter"
"slices"
"sync"
"github.com/yohamta/donburi/filter"
"github.com/yohamta/donburi/internal/storage"
)
type cache struct {
archetypes []storage.ArchetypeIndex
seen int
}
type IOrderable interface {
Order() int
}
// Query represents a query for entities.
// It is used to filter entities based on their components.
// It receives arbitrary filters that are used to filter entities.
// It contains a cache that is used to avoid re-evaluating the query.
// So it is not recommended to create a new query every time you want
// to filter entities with the same query.
type Query struct {
layoutMatches map[WorldId]*cache
filter filter.LayoutFilter
}
// NewQuery creates a new query.
// It receives arbitrary filters that are used to filter entities.
func NewQuery(filter filter.LayoutFilter) *Query {
return &Query{
layoutMatches: make(map[WorldId]*cache),
filter: filter,
}
}
// OrderedQuery is a special extension of Query which has a type parameter used
// when running ordered queries using `EachOrdered`.
type OrderedQuery[T IOrderable] struct {
Query
entries []*Entry
lock sync.Mutex
}
// NewOrderedQuery creates a new ordered query.
// It takes a filter parameter that is used when evaluating the query.
// Use `OrderedQuery.EachOrdered` to run a Each query in ordered mode.
func NewOrderedQuery[T IOrderable](filter filter.LayoutFilter) *OrderedQuery[T] {
return &OrderedQuery[T]{
//orderedBy: orderedBy,
Query: Query{
layoutMatches: make(map[WorldId]*cache),
filter: filter,
},
}
}
// IterOrdered returns an iterator over all entities within the query filter,
// ordered by the specified component.
func (q *OrderedQuery[T]) IterOrdered(w World, orderBy *ComponentType[T]) iter.Seq[*Entry] {
return func(yield func(*Entry) bool) {
q.lock.Lock()
defer q.lock.Unlock()
accessor := w.StorageAccessor()
iter := storage.NewEntityIterator(0, accessor.Archetypes, q.evaluateQuery(w, &accessor))
// Clear the slice while keeping the underlying array
q.entries = q.entries[:0]
for iter.HasNext() {
archetype := iter.Next()
archetype.Lock()
ents := archetype.Entities()
for _, entity := range ents {
entry := w.Entry(entity)
if entry.entity.IsReady() {
q.entries = append(q.entries, entry)
}
}
archetype.Unlock()
}
// Sort all entries using a stable sort
slices.SortStableFunc(q.entries, func(a, b *Entry) int {
return cmp.Compare(
orderBy.GetValue(a).Order(),
orderBy.GetValue(b).Order(),
)
})
// Yield sorted entries
for _, entry := range q.entries {
if !yield(entry) {
return
}
}
}
}
// EachOrdered iterates over all entities within the query filter, and uses the `orderBy` parameter to
// figure out which property to order using.
// `T` must implement `IOrderable`
func (q *OrderedQuery[T]) EachOrdered(w World, orderBy *ComponentType[T], callback func(*Entry)) {
for entry := range q.IterOrdered(w, orderBy) {
callback(entry)
}
}
func (q *Query) Iter(w World) iter.Seq[*Entry] {
return func(yield func(*Entry) bool) {
accessor := w.StorageAccessor()
iter := storage.NewEntityIterator(0, accessor.Archetypes, q.evaluateQuery(w, &accessor))
for iter.HasNext() {
archetype := iter.Next()
archetype.Lock()
for _, entity := range archetype.Entities() {
entry := w.Entry(entity)
if entry.entity.IsReady() {
archetype.Unlock()
if !yield(entry) {
return
}
archetype.Lock()
}
}
archetype.Unlock()
}
}
}
// Each iterates over all entities that match the query.
func (q *Query) Each(w World, callback func(*Entry)) {
for entry := range q.Iter(w) {
callback(entry)
}
}
// deprecated: use Each instead
func (q *Query) EachEntity(w World, callback func(*Entry)) {
q.Each(w, callback)
}
// Count returns the number of entities that match the query.
func (q *Query) Count(w World) int {
accessor := w.StorageAccessor()
iter := storage.NewEntityIterator(0, accessor.Archetypes, q.evaluateQuery(w, &accessor))
ret := 0
for iter.HasNext() {
archetype := iter.Next()
ret += len(archetype.Entities())
}
return ret
}
// First returns the first entity that matches the query.
func (q *Query) First(w World) (entry *Entry, ok bool) {
accessor := w.StorageAccessor()
iter := storage.NewEntityIterator(0, accessor.Archetypes, q.evaluateQuery(w, &accessor))
if !iter.HasNext() {
return nil, false
}
for iter.HasNext() {
archetype := iter.Next()
entities := archetype.Entities()
if len(entities) > 0 {
return w.Entry(entities[0]), true
}
}
return nil, false
}
// deprecated: use First instead
func (q *Query) FirstEntity(w World) (entry *Entry, ok bool) {
return q.First(w)
}
func (q *Query) evaluateQuery(world World, accessor *StorageAccessor) []storage.ArchetypeIndex {
w := world.Id()
if _, ok := q.layoutMatches[w]; !ok {
q.layoutMatches[w] = &cache{
archetypes: make([]storage.ArchetypeIndex, 0),
seen: 0,
}
}
cache := q.layoutMatches[w]
for it := accessor.Index.SearchFrom(q.filter, cache.seen); it.HasNext(); {
cache.archetypes = append(cache.archetypes, it.Next())
}
cache.seen = len(accessor.Archetypes)
return cache.archetypes
}