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

mlange-42 / ark / 13619591564

02 Mar 2025 09:42PM CUT coverage: 97.72%. Remained the same
13619591564

push

github

web-flow
Update README (#103)

5101 of 5220 relevant lines covered (97.72%)

36056.23 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 {
557✔
24
        componentsMap := make([]int16, MaskTotalBits)
557✔
25
        for i := range MaskTotalBits {
143,149✔
26
                componentsMap[i] = -1
142,592✔
27
        }
142,592✔
28
        for i, id := range components {
1,931✔
29
                componentsMap[id.id] = int16(i)
1,374✔
30
        }
1,374✔
31

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

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

58
func (a *archetype) GetTable(storage *storage, relations []RelationID) (*table, bool) {
502,016✔
59
        if len(a.tables) == 0 {
502,394✔
60
                return nil, false
378✔
61
        }
378✔
62
        if !a.HasRelations() {
1,002,850✔
63
                return &storage.tables[a.tables[0]], true
501,212✔
64
        }
501,212✔
65

66
        index := a.componentsMap[relations[0].component.id]
426✔
67
        tables, ok := a.relationTables[index][relations[0].target.id]
426✔
68
        if !ok {
481✔
69
                return nil, false
55✔
70
        }
55✔
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 {
54✔
81
        if !a.HasRelations() {
54✔
82
                return a.tables
×
83
        }
×
84
        if len(relations) == 0 {
68✔
85
                return a.tables
14✔
86
        }
14✔
87
        index := a.componentsMap[relations[0].component.id]
40✔
88
        if tables, ok := a.relationTables[index][relations[0].target.id]; ok {
80✔
89
                return tables.tables
40✔
90
        }
40✔
91
        return nil
×
92
}
93

94
func (a *archetype) GetFreeTable() (tableID, bool) {
435✔
95
        if len(a.freeTables) == 0 {
869✔
96
                return 0, false
434✔
97
        }
434✔
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) {
435✔
117
        a.tables = append(a.tables, table.id)
435✔
118
        if !a.HasRelations() {
776✔
119
                return
341✔
120
        }
341✔
121

122
        for i := range table.ids {
447✔
123
                column := &table.columns[i]
353✔
124
                if !column.isRelation {
604✔
125
                        continue
251✔
126
                }
127
                target := column.target
102✔
128
                relations := a.relationTables[i]
102✔
129

102✔
130
                if tables, ok := relations[target.id]; ok {
107✔
131
                        tables.tables = append(tables.tables, table.id)
5✔
132
                } else {
102✔
133
                        relations[target.id] = &tableIDs{tables: []tableID{table.id}}
97✔
134
                }
97✔
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