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

mlange-42 / ark / 13620752776

03 Mar 2025 12:17AM CUT coverage: 97.759%. Remained the same
13620752776

push

github

web-flow
Set up user guide (#106)

5192 of 5311 relevant lines covered (97.76%)

38008.39 hits per line

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

97.48
/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 {
557✔
31
        componentsMap := make([]int16, MaskTotalBits)
557✔
32
        for i := range MaskTotalBits {
143,149✔
33
                componentsMap[i] = -1
142,592✔
34
        }
142,592✔
35
        for i, id := range components {
1,931✔
36
                componentsMap[id.id] = int16(i)
1,374✔
37
        }
1,374✔
38

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

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

66
func (a *archetype) GetTable(storage *storage, relations []RelationID) (*table, bool) {
504,720✔
67
        if len(a.tables) == 0 {
505,098✔
68
                return nil, false
378✔
69
        }
378✔
70
        if !a.HasRelations() {
1,008,258✔
71
                return &storage.tables[a.tables[0]], true
503,916✔
72
        }
503,916✔
73

74
        index := a.componentsMap[relations[0].component.id]
426✔
75
        tables, ok := a.relationTables[index][relations[0].target.id]
426✔
76
        if !ok {
481✔
77
                return nil, false
55✔
78
        }
55✔
79
        for _, t := range tables.tables {
761✔
80
                table := &storage.tables[t]
390✔
81
                if table.MatchesExact(relations) {
759✔
82
                        return table, true
369✔
83
                }
369✔
84
        }
85
        return nil, false
2✔
86
}
87

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

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

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

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

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

4✔
119
        a.tables[index], a.tables[last] = a.tables[last], a.tables[index]
4✔
120
        a.tables = a.tables[:last]
4✔
121

4✔
122
        a.freeTables = append(a.freeTables, table)
4✔
123
}
4✔
124

125
func (a *archetype) AddTable(table *table) {
435✔
126
        a.tables = append(a.tables, table.id)
435✔
127
        if !a.HasRelations() {
776✔
128
                return
341✔
129
        }
341✔
130

131
        for i := range table.ids {
447✔
132
                column := &table.columns[i]
353✔
133
                if !column.isRelation {
604✔
134
                        continue
251✔
135
                }
136
                target := column.target
102✔
137
                relations := a.relationTables[i]
102✔
138

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

147
func (a *archetype) RemoveTarget(entity Entity) {
2✔
148
        for i := range a.relationTables {
6✔
149
                if !a.isRelation[i] {
6✔
150
                        continue
2✔
151
                }
152
                delete(a.relationTables[i], entity.id)
2✔
153
        }
154
}
155

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

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

167
        for i := len(a.tables) - 1; i >= 0; i-- {
3✔
168
                a.FreeTable(a.tables[i])
2✔
169
        }
2✔
170

171
        for _, m := range a.relationTables {
3✔
172
                for key := range m {
4✔
173
                        delete(m, key)
2✔
174
                }
2✔
175
        }
176
}
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