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

mlange-42 / ark / 13593874277

28 Feb 2025 06:06PM CUT coverage: 96.872% (+2.1%) from 94.795%
13593874277

Pull #79

github

web-flow
Merge 94da96c6d into 10315654e
Pull Request #79: Relation getters for queries and maps

118 of 118 new or added lines in 6 files covered. (100.0%)

8 existing lines in 1 file now uncovered.

3562 of 3677 relevant lines covered (96.87%)

50530.93 hits per line

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

97.03
/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 {
231✔
24
        componentsMap := make([]int16, MaskTotalBits)
231✔
25
        for i := range MaskTotalBits {
59,367✔
26
                componentsMap[i] = -1
59,136✔
27
        }
59,136✔
28
        for i, id := range components {
690✔
29
                componentsMap[id.id] = int16(i)
459✔
30
        }
459✔
31

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

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

58
func (a *archetype) GetTable(storage *storage, relations []relationID) (*table, bool) {
498,003✔
59
        if len(a.tables) == 0 {
498,133✔
60
                return nil, false
130✔
61
        }
130✔
62
        if !a.HasRelations() {
995,370✔
63
                return &storage.tables[a.tables[0]], true
497,497✔
64
        }
497,497✔
65

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

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

94
func (a *archetype) GetFreeTable() (tableID, bool) {
164✔
95
        if len(a.freeTables) == 0 {
327✔
96
                return 0, false
163✔
97
        }
163✔
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) {
1✔
107
        index := slices.Index(a.tables, table)
1✔
108
        last := len(a.tables) - 1
1✔
109

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

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

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

122
        for i := range table.ids {
269✔
123
                column := &table.columns[i]
212✔
124
                if !column.isRelation {
361✔
125
                        continue
149✔
126
                }
127
                target := column.target
63✔
128
                relations := a.relationTables[i]
63✔
129

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

138
func (a *archetype) RemoveTarget(entity Entity) {
1✔
139
        for i := range a.relationTables {
3✔
140
                if !a.isRelation[i] {
3✔
141
                        continue
1✔
142
                }
143
                delete(a.relationTables[i], entity.id)
1✔
144
        }
145
}
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