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

mlange-42 / ark / 13617072942

02 Mar 2025 04:02PM CUT coverage: 97.422% (-0.002%) from 97.424%
13617072942

Pull #100

github

web-flow
Merge d87f29472 into 8f54a3a49
Pull Request #100: Simplify component/resource registration functions

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

3893 of 3996 relevant lines covered (97.42%)

93522.98 hits per line

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

97.44
/ecs/archetype.go
1
package ecs
2

3
import "slices"
4

5
type archetypeID uint32
6

7
type archetype struct {
8
        id             archetypeID
9
        mask           Mask
10
        components     []ID
11
        componentsMap  []int16
12
        isRelation     []bool
13
        tables         []tableID
14
        freeTables     []tableID
15
        numRelations   uint8
16
        relationTables []map[entityID]*tableIDs
17
}
18

19
type tableIDs struct {
20
        tables []tableID
21
}
22

23
func newArchetype(id archetypeID, mask *Mask, components []ID, tables []tableID, reg *componentRegistry) archetype {
534✔
24
        componentsMap := make([]int16, MaskTotalBits)
534✔
25
        for i := range MaskTotalBits {
137,238✔
26
                componentsMap[i] = -1
136,704✔
27
        }
136,704✔
28
        for i, id := range components {
1,512✔
29
                componentsMap[id.id] = int16(i)
978✔
30
        }
978✔
31

32
        numRelations := uint8(0)
534✔
33
        isRelation := make([]bool, len(components))
534✔
34
        relationTables := make([]map[entityID]*tableIDs, len(components))
534✔
35
        for i, id := range components {
1,512✔
36
                if reg.IsRelation[id.id] {
1,040✔
37
                        isRelation[i] = true
62✔
38
                        relationTables[i] = map[entityID]*tableIDs{}
62✔
39
                        numRelations++
62✔
40
                }
62✔
41
        }
42
        return archetype{
534✔
43
                id:             id,
534✔
44
                mask:           *mask,
534✔
45
                components:     components,
534✔
46
                componentsMap:  componentsMap,
534✔
47
                isRelation:     isRelation,
534✔
48
                tables:         tables,
534✔
49
                numRelations:   numRelations,
534✔
50
                relationTables: relationTables,
534✔
51
        }
534✔
52
}
53

54
func (a *archetype) HasRelations() bool {
1,008,612✔
55
        return a.numRelations > 0
1,008,612✔
56
}
1,008,612✔
57

58
func (a *archetype) GetTable(storage *storage, relations []RelationID) (*table, bool) {
1,005,950✔
59
        if len(a.tables) == 0 {
1,006,242✔
60
                return nil, false
292✔
61
        }
292✔
62
        if !a.HasRelations() {
2,010,500✔
63
                return &storage.tables[a.tables[0]], true
1,004,842✔
64
        }
1,004,842✔
65

66
        index := a.componentsMap[relations[0].component.id]
816✔
67
        tables, ok := a.relationTables[index][relations[0].target.id]
816✔
68
        if !ok {
890✔
69
                return nil, false
74✔
70
        }
74✔
71
        for _, t := range tables.tables {
1,522✔
72
                table := &storage.tables[t]
780✔
73
                if table.MatchesExact(relations) {
1,518✔
74
                        return table, true
738✔
75
                }
738✔
76
        }
77
        return nil, false
4✔
78
}
79

80
func (a *archetype) GetTables(relations []RelationID) []tableID {
54✔
81
        if !a.HasRelations() {
54✔
82
                return a.tables
×
83
        }
×
84
        if len(relations) == 0 {
82✔
85
                return a.tables
28✔
86
        }
28✔
87
        index := a.componentsMap[relations[0].component.id]
26✔
88
        if tables, ok := a.relationTables[index][relations[0].target.id]; ok {
52✔
89
                return tables.tables
26✔
90
        }
26✔
91
        return nil
×
92
}
93

94
func (a *archetype) GetFreeTable() (tableID, bool) {
370✔
95
        if len(a.freeTables) == 0 {
738✔
96
                return 0, false
368✔
97
        }
368✔
98
        last := len(a.freeTables) - 1
2✔
99
        table := a.freeTables[last]
2✔
100

2✔
101
        a.freeTables = a.freeTables[:last]
2✔
102

2✔
103
        return table, true
2✔
104
}
105

106
func (a *archetype) FreeTable(table tableID) {
8✔
107
        index := slices.Index(a.tables, table)
8✔
108
        last := len(a.tables) - 1
8✔
109

8✔
110
        a.tables[index], a.tables[last] = a.tables[last], a.tables[index]
8✔
111
        a.tables = a.tables[:last]
8✔
112

8✔
113
        a.freeTables = append(a.freeTables, table)
8✔
114
}
8✔
115

116
func (a *archetype) AddTable(table *table) {
370✔
117
        a.tables = append(a.tables, table.id)
370✔
118
        if !a.HasRelations() {
606✔
119
                return
236✔
120
        }
236✔
121

122
        for i := range table.ids {
606✔
123
                column := &table.columns[i]
472✔
124
                if !column.isRelation {
794✔
125
                        continue
322✔
126
                }
127
                target := column.target
150✔
128
                relations := a.relationTables[i]
150✔
129

150✔
130
                if tables, ok := relations[target.id]; ok {
160✔
131
                        tables.tables = append(tables.tables, table.id)
10✔
132
                } else {
150✔
133
                        relations[target.id] = &tableIDs{tables: []tableID{table.id}}
140✔
134
                }
140✔
135
        }
136
}
137

138
func (a *archetype) RemoveTarget(entity Entity) {
4✔
139
        for i := range a.relationTables {
12✔
140
                if !a.isRelation[i] {
12✔
141
                        continue
4✔
142
                }
143
                delete(a.relationTables[i], entity.id)
4✔
144
        }
145
}
146

147
func (a *archetype) Reset(storage *storage) {
8✔
148
        if !a.HasRelations() {
14✔
149
                storage.tables[a.tables[0]].Reset()
6✔
150
                return
6✔
151
        }
6✔
152

153
        for _, tab := range a.tables {
6✔
154
                table := &storage.tables[tab]
4✔
155
                table.Reset()
4✔
156
        }
4✔
157

158
        for i := len(a.tables) - 1; i >= 0; i-- {
6✔
159
                a.FreeTable(a.tables[i])
4✔
160
        }
4✔
161

162
        for _, m := range a.relationTables {
6✔
163
                for key := range m {
8✔
164
                        delete(m, key)
4✔
165
                }
4✔
166
        }
167
}
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