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

mlange-42 / ark / 13752743521

09 Mar 2025 09:34PM CUT coverage: 99.43%. Remained the same
13752743521

Pull #169

github

web-flow
Merge 6a3e26b95 into c98324113
Pull Request #169: Useful error on incomplete relations

2 of 2 new or added lines in 1 file covered. (100.0%)

6458 of 6495 relevant lines covered (99.43%)

29592.87 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
//
51
// ⚠️ Do not store the obtained pointer outside of the current context (i.e. the query loop)!
52
func (q *Query) Get(comp ID) unsafe.Pointer {
72✔
53
        q.world.checkQueryGet(&q.cursor)
72✔
54
        return q.table.Get(comp, uintptr(q.cursor.index))
72✔
55
}
72✔
56

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

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

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

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

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

94
// IDs returns the IDs of all component of the current [Entity]n.
95
func (q *Query) IDs() IDs {
10✔
96
        return newIDs(q.table.ids)
10✔
97
}
10✔
98

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

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

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

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

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

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

163
func (q *Query) setTable(index int, table *table) {
10✔
164
        q.cursor.table = index
10✔
165
        q.table = table
10✔
166
        q.cursor.index = 0
10✔
167
        q.cursor.maxIndex = int64(q.table.Len() - 1)
10✔
168
}
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