• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mlange-42 / ark / 13730551907

07 Mar 2025 10:34PM CUT coverage: 99.424% (+0.001%) from 99.423%
13730551907

push

github

web-flow
Component IDs of entities are returned immutable (#156)

13 of 13 new or added lines in 2 files covered. (100.0%)

6387 of 6424 relevant lines covered (99.42%)

27582.11 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.11
/ecs/query.go
1
package ecs
2

3
import "unsafe"
4

5
// Query is an unsafe query.
6
// It is significantly slower than type-safe generic queries like [Query2],
7
// and should only be used when component types are not known at compile time.
8
type Query struct {
9
        world     *World
10
        filter    filter
11
        relations []RelationID
12
        lock      uint8
13
        cursor    cursor
14
        tables    []tableID
15
        table     *table
16
}
17

18
func newQuery(world *World, filter filter, relations []RelationID) Query {
14✔
19
        return Query{
14✔
20
                world:     world,
14✔
21
                filter:    filter,
14✔
22
                relations: relations,
14✔
23
                lock:      world.lock(),
14✔
24
                cursor: cursor{
14✔
25
                        archetype: -1,
14✔
26
                        table:     -1,
14✔
27
                        index:     0,
14✔
28
                        maxIndex:  -1,
14✔
29
                },
14✔
30
        }
14✔
31
}
14✔
32

33
// Next advances the query's cursor to the next entity.
34
func (q *Query) Next() bool {
285✔
35
        q.world.checkQueryNext(&q.cursor)
285✔
36
        if int64(q.cursor.index) < q.cursor.maxIndex {
549✔
37
                q.cursor.index++
264✔
38
                return true
264✔
39
        }
264✔
40
        return q.nextTableOrArchetype()
21✔
41
}
42

43
// Entity returns the current entity.
44
func (q *Query) Entity() Entity {
76✔
45
        q.world.checkQueryGet(&q.cursor)
76✔
46
        return q.table.GetEntity(q.cursor.index)
76✔
47
}
76✔
48

49
// Get returns the queried components of the current entity.
50
func (q *Query) Get(comp ID) unsafe.Pointer {
72✔
51
        q.world.checkQueryGet(&q.cursor)
72✔
52
        return q.table.Get(comp, uintptr(q.cursor.index))
72✔
53
}
72✔
54

55
// Has returns whether the current entity has the given component.
56
func (q *Query) Has(comp ID) bool {
40✔
57
        return q.table.Has(comp)
40✔
58
}
40✔
59

60
// GetRelation returns the entity relation target of the component at the given index.
61
func (q *Query) GetRelation(comp ID) Entity {
10✔
62
        return q.table.GetRelation(comp)
10✔
63
}
10✔
64

65
// Count returns the number of entities matching this query.
66
func (q *Query) Count() int {
10✔
67
        count := 0
10✔
68
        for i := range q.world.storage.archetypes {
39✔
69
                archetype := &q.world.storage.archetypes[i]
29✔
70
                if !q.filter.matches(&archetype.mask) {
41✔
71
                        continue
12✔
72
                }
73

74
                if !archetype.HasRelations() {
30✔
75
                        table := &q.world.storage.tables[archetype.tables[0]]
13✔
76
                        count += table.Len()
13✔
77
                        continue
13✔
78
                }
79

80
                tables := archetype.GetTables(q.relations)
4✔
81
                for _, tab := range tables {
8✔
82
                        table := &q.world.storage.tables[tab]
4✔
83
                        if !table.Matches(q.relations) {
4✔
84
                                continue
×
85
                        }
86
                        count += table.Len()
4✔
87
                }
88
        }
89
        return count
10✔
90
}
91

92
// IDs returns the component IDs for the archetype of the [Entity] at the iterator's current position.
93
//
94
// Returns a copy of the archetype's component IDs slice, for safety.
95
// This means that the result can be manipulated safely,
96
// but also that calling the method may incur some significant cost.
97
func (q *Query) IDs() []ID {
10✔
98
        return append([]ID{}, q.table.ids...)
10✔
99
}
10✔
100

101
// Close closes the Query and unlocks the world.
102
//
103
// Automatically called when iteration finishes.
104
// Needs to be called only if breaking out of the query iteration or not iterating at all.
105
func (q *Query) Close() {
14✔
106
        q.cursor.archetype = -2
14✔
107
        q.cursor.table = -2
14✔
108
        q.tables = nil
14✔
109
        q.table = nil
14✔
110
        q.world.unlock(q.lock)
14✔
111
}
14✔
112

113
func (q *Query) nextTableOrArchetype() bool {
21✔
114
        if q.cursor.archetype >= 0 && q.nextTable() {
22✔
115
                return true
1✔
116
        }
1✔
117
        return q.nextArchetype()
20✔
118
}
119

120
func (q *Query) nextArchetype() bool {
20✔
121
        maxArchIndex := len(q.world.storage.archetypes) - 1
20✔
122
        for q.cursor.archetype < maxArchIndex {
43✔
123
                q.cursor.archetype++
23✔
124
                archetype := &q.world.storage.archetypes[q.cursor.archetype]
23✔
125
                if !q.filter.matches(&archetype.mask) {
35✔
126
                        continue
12✔
127
                }
128

129
                if !archetype.HasRelations() {
18✔
130
                        table := &q.world.storage.tables[archetype.tables[0]]
8✔
131
                        if table.Len() > 0 {
15✔
132
                                q.setTable(0, table)
7✔
133
                                return true
7✔
134
                        }
7✔
135
                        continue
1✔
136
                }
137

138
                q.tables = archetype.GetTables(q.relations)
2✔
139
                q.cursor.table = -1
2✔
140
                if q.nextTable() {
4✔
141
                        return true
2✔
142
                }
2✔
143
        }
144
        q.Close()
10✔
145
        return false
10✔
146
}
147

148
func (q *Query) nextTable() bool {
12✔
149
        maxTableIndex := len(q.tables) - 1
12✔
150
        for q.cursor.table < maxTableIndex {
16✔
151
                q.cursor.table++
4✔
152
                table := &q.world.storage.tables[q.tables[q.cursor.table]]
4✔
153
                if table.Len() == 0 {
5✔
154
                        continue
1✔
155
                }
156
                if !table.Matches(q.relations) {
3✔
157
                        continue
×
158
                }
159
                q.setTable(q.cursor.table, table)
3✔
160
                return true
3✔
161
        }
162
        return false
9✔
163
}
164

165
func (q *Query) setTable(index int, table *table) {
10✔
166
        q.cursor.table = index
10✔
167
        q.table = table
10✔
168
        q.cursor.index = 0
10✔
169
        q.cursor.maxIndex = int64(q.table.Len() - 1)
10✔
170
}
10✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc