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

mlange-42 / ark / 13605379170

01 Mar 2025 01:41PM CUT coverage: 97.11%. Remained the same
13605379170

push

github

web-flow
Update README for ark-serde feature (#84)

3831 of 3945 relevant lines covered (97.11%)

46743.07 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 {
261✔
24
        componentsMap := make([]int16, MaskTotalBits)
261✔
25
        for i := range MaskTotalBits {
67,077✔
26
                componentsMap[i] = -1
66,816✔
27
        }
66,816✔
28
        for i, id := range components {
744✔
29
                componentsMap[id.id] = int16(i)
483✔
30
        }
483✔
31

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

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

58
func (a *archetype) GetTable(storage *storage, relations []RelationID) (*table, bool) {
495,936✔
59
        if len(a.tables) == 0 {
496,078✔
60
                return nil, false
142✔
61
        }
142✔
62
        if !a.HasRelations() {
991,182✔
63
                return &storage.tables[a.tables[0]], true
495,388✔
64
        }
495,388✔
65

66
        index := a.componentsMap[relations[0].component.id]
406✔
67
        tables, ok := a.relationTables[index][relations[0].target.id]
406✔
68
        if !ok {
441✔
69
                return nil, false
35✔
70
        }
35✔
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 {
25✔
81
        if !a.HasRelations() {
25✔
82
                return a.tables
×
83
        }
×
84
        if len(relations) == 0 {
37✔
85
                return a.tables
12✔
86
        }
12✔
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) {
179✔
95
        if len(a.freeTables) == 0 {
357✔
96
                return 0, false
178✔
97
        }
178✔
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) {
179✔
117
        a.tables = append(a.tables, table.id)
179✔
118
        if !a.HasRelations() {
294✔
119
                return
115✔
120
        }
115✔
121

122
        for i := range table.ids {
294✔
123
                column := &table.columns[i]
230✔
124
                if !column.isRelation {
388✔
125
                        continue
158✔
126
                }
127
                target := column.target
72✔
128
                relations := a.relationTables[i]
72✔
129

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