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

mlange-42 / ark / 13605167657

01 Mar 2025 01:14PM CUT coverage: 97.11% (+0.09%) from 97.025%
13605167657

push

github

web-flow
Add functionality required for (de)-serialization (#83)

112 of 113 new or added lines in 4 files covered. (99.12%)

3831 of 3945 relevant lines covered (97.11%)

48492.89 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
type Query struct {
7
        world     *World
8
        filter    Filter
9
        relations []RelationID
10
        lock      uint8
11
        cursor    cursor
12
        tables    []tableID
13
        table     *table
14
}
15

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

31
// Next advances the query's cursor to the next entity.
32
func (q *Query) Next() bool {
84✔
33
        q.world.checkQueryNext(&q.cursor)
84✔
34
        if int64(q.cursor.index) < q.cursor.maxIndex {
149✔
35
                q.cursor.index++
65✔
36
                return true
65✔
37
        }
65✔
38
        return q.nextTableOrArchetype()
19✔
39
}
40

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

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

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

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

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

72
                if !archetype.HasRelations() {
14✔
73
                        table := &q.world.storage.tables[archetype.tables[0]]
6✔
74
                        count += table.Len()
6✔
75
                        continue
6✔
76
                }
77

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

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

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

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

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

127
                if !archetype.HasRelations() {
16✔
128
                        table := &q.world.storage.tables[archetype.tables[0]]
7✔
129
                        if table.Len() > 0 {
13✔
130
                                q.setTable(0, table)
6✔
131
                                return true
6✔
132
                        }
6✔
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()
9✔
143
        return false
9✔
144
}
145

146
func (q *Query) nextTable() bool {
11✔
147
        maxTableIndex := len(q.tables) - 1
11✔
148
        for q.cursor.table < maxTableIndex {
15✔
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
8✔
161
}
162

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