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

mlange-42 / ark / 13617144606

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

push

github

web-flow
Simplify component/resource registration functions (#100)

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

3893 of 3996 relevant lines covered (97.42%)

46242.85 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 {
267✔
24
        componentsMap := make([]int16, MaskTotalBits)
267✔
25
        for i := range MaskTotalBits {
68,619✔
26
                componentsMap[i] = -1
68,352✔
27
        }
68,352✔
28
        for i, id := range components {
756✔
29
                componentsMap[id.id] = int16(i)
489✔
30
        }
489✔
31

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

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

58
func (a *archetype) GetTable(storage *storage, relations []RelationID) (*table, bool) {
496,508✔
59
        if len(a.tables) == 0 {
496,654✔
60
                return nil, false
146✔
61
        }
146✔
62
        if !a.HasRelations() {
992,316✔
63
                return &storage.tables[a.tables[0]], true
495,954✔
64
        }
495,954✔
65

66
        index := a.componentsMap[relations[0].component.id]
408✔
67
        tables, ok := a.relationTables[index][relations[0].target.id]
408✔
68
        if !ok {
445✔
69
                return nil, false
37✔
70
        }
37✔
71
        for _, t := range tables.tables {
761✔
72
                table := &storage.tables[t]
390✔
73
                if table.MatchesExact(relations) {
759✔
74
                        return table, true
369✔
75
                }
369✔
76
        }
77
        return nil, false
2✔
78
}
79

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

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

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

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

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

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

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

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

122
        for i := range table.ids {
303✔
123
                column := &table.columns[i]
236✔
124
                if !column.isRelation {
397✔
125
                        continue
161✔
126
                }
127
                target := column.target
75✔
128
                relations := a.relationTables[i]
75✔
129

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

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

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

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

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

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