• 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

94.55
/ecs/table.go
1
package ecs
2

3
import (
4
        "fmt"
5
        "math"
6
        "unsafe"
7
)
8

9
type tableID uint32
10

11
// maxTableID is used as table ID for unused entities.
12
const maxTableID = math.MaxUint32
13

14
type table struct {
15
        id          tableID
16
        archetype   archetypeID
17
        components  []int16
18
        entities    column
19
        ids         []ID
20
        columns     []column
21
        relationIDs []RelationID
22

23
        zeroValue   []byte
24
        zeroPointer unsafe.Pointer
25
}
26

27
func newTable(id tableID, archetype archetypeID, capacity uint32, reg *componentRegistry,
28
        ids []ID, componentsMap []int16, isRelation []bool, targets []Entity, relationIDs []RelationID) table {
306✔
29

306✔
30
        entities := newColumn(entityType, false, Entity{}, capacity)
306✔
31
        columns := make([]column, len(ids))
306✔
32

306✔
33
        var maxSize uintptr = entitySize
306✔
34
        for i, id := range ids {
936✔
35
                columns[i] = newColumn(reg.Types[id.id], isRelation[i], targets[i], capacity)
630✔
36
                if columns[i].itemSize > maxSize {
801✔
37
                        maxSize = columns[i].itemSize
171✔
38
                }
171✔
39
        }
40
        var zeroValue []byte
306✔
41
        var zeroPointer unsafe.Pointer
306✔
42
        if maxSize > 0 {
612✔
43
                zeroValue = make([]byte, maxSize)
306✔
44
                zeroPointer = unsafe.Pointer(&zeroValue[0])
306✔
45
        }
306✔
46

47
        return table{
306✔
48
                id:          id,
306✔
49
                archetype:   archetype,
306✔
50
                components:  componentsMap,
306✔
51
                entities:    entities,
306✔
52
                ids:         ids,
306✔
53
                columns:     columns,
306✔
54
                zeroValue:   zeroValue,
306✔
55
                zeroPointer: zeroPointer,
306✔
56
                relationIDs: relationIDs,
306✔
57
        }
306✔
58
}
59

60
func (t *table) recycle(targets []Entity, relationIDs []RelationID) {
1✔
61
        t.relationIDs = relationIDs
1✔
62
        for i := range t.columns {
3✔
63
                t.columns[i].target = targets[i]
2✔
64
        }
2✔
65
}
66

67
func (t *table) Add(entity Entity) uint32 {
496,825✔
68
        _, idx := t.entities.Add(unsafe.Pointer(&entity))
496,825✔
69

496,825✔
70
        for i := range t.columns {
997,274✔
71
                t.columns[i].Alloc(1)
500,449✔
72
        }
500,449✔
73
        return idx
496,825✔
74
}
75

76
func (t *table) Get(component ID, index uintptr) unsafe.Pointer {
895✔
77
        return t.columns[t.components[component.id]].Get(index)
895✔
78
}
895✔
79

80
func (t *table) Has(component ID) bool {
58✔
81
        return t.components[component.id] >= 0
58✔
82
}
58✔
83

84
func (t *table) GetEntity(index uintptr) Entity {
2,463,770✔
85
        return *(*Entity)(t.entities.Get(index))
2,463,770✔
86
}
2,463,770✔
87

88
func (t *table) GetRelation(component ID) Entity {
143✔
89
        return t.columns[t.components[component.id]].target
143✔
90
}
143✔
91

92
func (t *table) GetColumn(component ID) *column {
630✔
93
        return &t.columns[t.components[component.id]]
630✔
94
}
630✔
95

96
func (t *table) GetEntities(component ID) *column {
×
97
        return &t.entities
×
98
}
×
99

100
func (t *table) Set(component ID, index uint32, comp unsafe.Pointer) {
501,082✔
101
        t.columns[t.components[component.id]].Set(index, comp)
501,082✔
102
}
501,082✔
103

104
func (t *table) SetEntity(index uint32, entity Entity) {
384✔
105
        t.entities.Set(index, unsafe.Pointer(&entity))
384✔
106
}
384✔
107

108
// Alloc allocates memory for the given number of entities.
109
func (t *table) Alloc(n uint32) {
16✔
110
        t.entities.Alloc(n)
16✔
111
        for i := range t.columns {
88✔
112
                t.columns[i].Alloc(n)
72✔
113
        }
72✔
114
}
115

116
func (t *table) Remove(index uint32) bool {
495,043✔
117
        swapped := t.entities.Remove(index, nil)
495,043✔
118
        for i := range t.columns {
991,600✔
119
                t.columns[i].Remove(index, t.zeroPointer)
496,557✔
120
        }
496,557✔
121
        return swapped
495,043✔
122
}
123

124
func (t *table) Reset() {
7✔
125
        t.entities.Reset(nil)
7✔
126
        for c := range t.columns {
18✔
127
                t.columns[c].Reset(t.zeroPointer)
11✔
128
        }
11✔
129
}
130

131
func (t *table) AddAll(other *table) {
2✔
132
        t.entities.AddAll(&other.entities)
2✔
133
        for c := range t.columns {
6✔
134
                t.columns[c].AddAll(&other.columns[c])
4✔
135
        }
4✔
136
}
137

138
func (t *table) MatchesExact(relations []RelationID) bool {
390✔
139
        if len(relations) != len(t.relationIDs) {
390✔
140
                panic("relation targets must be fully specified")
×
141
        }
142
        for _, rel := range relations {
838✔
143
                index := t.components[rel.component.id]
448✔
144
                if !t.columns[index].isRelation {
448✔
145
                        panic(fmt.Sprintf("component %d is not a relation component", rel.component.id))
×
146
                }
147
                //if rel.target == wildcard {
148
                //        panic("relation targets must be fully specified, no wildcard allowed")
149
                //}
150
                if rel.target != t.columns[index].target {
469✔
151
                        return false
21✔
152
                }
21✔
153
        }
154
        return true
369✔
155
}
156

157
func (t *table) Matches(relations []RelationID) bool {
43✔
158
        if len(relations) == 0 {
71✔
159
                return true
28✔
160
        }
28✔
161
        for _, rel := range relations {
32✔
162
                if rel.target == wildcard {
17✔
163
                        continue
×
164
                }
165
                if rel.target != t.columns[t.components[rel.component.id]].target {
18✔
166
                        return false
1✔
167
                }
1✔
168
        }
169
        return true
14✔
170
}
171

172
func (t *table) Len() int {
1,177✔
173
        return t.entities.Len()
1,177✔
174
}
1,177✔
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