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

mlange-42 / ark / 13683680318

05 Mar 2025 07:11PM CUT coverage: 99.318% (+0.002%) from 99.316%
13683680318

Pull #136

github

web-flow
Merge 0b4eff22d into c73c750d7
Pull Request #136: Add World.NewEntities

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

5824 of 5864 relevant lines covered (99.32%)

35696.89 hits per line

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

97.52
/ecs/archetype.go
1
package ecs
2

3
import (
4
        "math"
5
        "slices"
6
)
7

8
type archetypeID uint32
9

10
// maxArchetypeID is used as unassigned archetype ID.
11
const maxArchetypeID = math.MaxUint32
12

13
type archetype struct {
14
        id             archetypeID
15
        node           nodeID
16
        mask           Mask
17
        components     []ID                     // components IDs of the archetype in arbitrary order
18
        componentsMap  []int16                  // mapping from component IDs to column indices; -1 indicates none
19
        isRelation     []bool                   // whether columns are relations components, indexed by column index
20
        relationTables []map[entityID]*tableIDs // lookup for relation targets of tables, indexed by column index
21
        tables         []tableID                // all active tables
22
        freeTables     []tableID                // all inactive/free tables
23
        numRelations   uint8                    // number of relation components
24
}
25

26
type tableIDs struct {
27
        tables []tableID
28
}
29

30
func newArchetype(id archetypeID, node nodeID, mask *Mask, components []ID, tables []tableID, reg *componentRegistry) archetype {
589✔
31
        componentsMap := make([]int16, MaskTotalBits)
589✔
32
        for i := range MaskTotalBits {
151,373✔
33
                componentsMap[i] = -1
150,784✔
34
        }
150,784✔
35
        for i, id := range components {
2,010✔
36
                componentsMap[id.id] = int16(i)
1,421✔
37
        }
1,421✔
38

39
        numRelations := uint8(0)
589✔
40
        isRelation := make([]bool, len(components))
589✔
41
        relationTables := make([]map[entityID]*tableIDs, len(components))
589✔
42
        for i, id := range components {
2,010✔
43
                if reg.IsRelation[id.id] {
1,473✔
44
                        isRelation[i] = true
52✔
45
                        relationTables[i] = map[entityID]*tableIDs{}
52✔
46
                        numRelations++
52✔
47
                }
52✔
48
        }
49
        return archetype{
589✔
50
                id:             id,
589✔
51
                node:           node,
589✔
52
                mask:           *mask,
589✔
53
                components:     components,
589✔
54
                componentsMap:  componentsMap,
589✔
55
                isRelation:     isRelation,
589✔
56
                tables:         tables,
589✔
57
                numRelations:   numRelations,
589✔
58
                relationTables: relationTables,
589✔
59
        }
589✔
60
}
61

62
func (a *archetype) HasRelations() bool {
523,161✔
63
        return a.numRelations > 0
523,161✔
64
}
523,161✔
65

66
func (a *archetype) GetTable(storage *storage, relations []RelationID) (*table, bool) {
520,611✔
67
        if len(a.tables) == 0 {
521,004✔
68
                return nil, false
393✔
69
        }
393✔
70
        if !a.HasRelations() {
1,039,772✔
71
                return &storage.tables[a.tables[0]], true
519,554✔
72
        }
519,554✔
73

74
        index := a.componentsMap[relations[0].component.id]
664✔
75
        tables, ok := a.relationTables[index][relations[0].target.id]
664✔
76
        if !ok {
740✔
77
                return nil, false
76✔
78
        }
76✔
79
        for _, t := range tables.tables {
1,195✔
80
                table := &storage.tables[t]
607✔
81
                if table.MatchesExact(relations) {
1,193✔
82
                        return table, true
586✔
83
                }
586✔
84
        }
85
        return nil, false
2✔
86
}
87

88
func (a *archetype) GetTables(relations []RelationID) []tableID {
91✔
89
        if !a.HasRelations() {
91✔
90
                return a.tables
×
91
        }
×
92
        if len(relations) == 0 {
125✔
93
                return a.tables
34✔
94
        }
34✔
95
        index := a.componentsMap[relations[0].component.id]
57✔
96
        if tables, ok := a.relationTables[index][relations[0].target.id]; ok {
114✔
97
                return tables.tables
57✔
98
        }
57✔
99
        return nil
×
100
}
101

102
func (a *archetype) GetFreeTable() (tableID, bool) {
471✔
103
        if len(a.freeTables) == 0 {
940✔
104
                return 0, false
469✔
105
        }
469✔
106
        last := len(a.freeTables) - 1
2✔
107
        table := a.freeTables[last]
2✔
108

2✔
109
        a.freeTables = a.freeTables[:last]
2✔
110

2✔
111
        return table, true
2✔
112
}
113

114
func (a *archetype) FreeTable(table tableID) {
10✔
115
        // TODO: can we speed this up for large numbers of relation targets?
10✔
116
        index := slices.Index(a.tables, table)
10✔
117
        last := len(a.tables) - 1
10✔
118

10✔
119
        if index != last {
14✔
120
                a.tables[index], a.tables[last] = a.tables[last], a.tables[index]
4✔
121
        }
4✔
122
        a.tables = a.tables[:last]
10✔
123

10✔
124
        a.freeTables = append(a.freeTables, table)
10✔
125
}
126

127
func (a *archetype) AddTable(table *table) {
471✔
128
        a.tables = append(a.tables, table.id)
471✔
129
        if !a.HasRelations() {
815✔
130
                return
344✔
131
        }
344✔
132

133
        for i := range table.ids {
599✔
134
                column := &table.columns[i]
472✔
135
                if !column.isRelation {
809✔
136
                        continue
337✔
137
                }
138
                target := column.target
135✔
139
                relations := a.relationTables[i]
135✔
140

135✔
141
                if tables, ok := relations[target.id]; ok {
140✔
142
                        tables.tables = append(tables.tables, table.id)
5✔
143
                } else {
135✔
144
                        relations[target.id] = &tableIDs{tables: []tableID{table.id}}
130✔
145
                }
130✔
146
        }
147
}
148

149
func (a *archetype) RemoveTarget(entity Entity) {
13✔
150
        for i := range a.relationTables {
31✔
151
                if !a.isRelation[i] {
23✔
152
                        continue
5✔
153
                }
154
                delete(a.relationTables[i], entity.id)
13✔
155
        }
156
}
157

158
func (a *archetype) Reset(storage *storage) {
4✔
159
        if !a.HasRelations() {
7✔
160
                storage.tables[a.tables[0]].Reset()
3✔
161
                return
3✔
162
        }
3✔
163

164
        for _, tab := range a.tables {
2✔
165
                table := &storage.tables[tab]
1✔
166
                table.Reset()
1✔
167
        }
1✔
168

169
        for i := len(a.tables) - 1; i >= 0; i-- {
2✔
170
                storage.cache.removeTable(storage, &storage.tables[a.tables[i]])
1✔
171
                a.FreeTable(a.tables[i])
1✔
172
        }
1✔
173

174
        for _, m := range a.relationTables {
3✔
175
                for key := range m {
3✔
176
                        delete(m, key)
1✔
177
                }
1✔
178
        }
179
}
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