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

mlange-42 / ark / 13620752776

03 Mar 2025 12:17AM CUT coverage: 97.759%. Remained the same
13620752776

push

github

web-flow
Set up user guide (#106)

5192 of 5311 relevant lines covered (97.76%)

38008.39 hits per line

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

94.83
/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      // mapping from component IDs to column indices
18
        entities    column       // column for entities
19
        ids         []ID         // components IDs in the same order as in the archetype
20
        columns     []column     // columns in dense order
21
        relationIDs []RelationID // all relation IDs and targets of the table
22

23
        zeroValue   []byte         // zero value with the size of the largest item type, for fast zeroing
24
        zeroPointer unsafe.Pointer // pointer to the zero value, for fast zeroing
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 {
614✔
29

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

614✔
33
        var maxSize uintptr = entitySize
614✔
34
        for i, id := range ids {
2,207✔
35
                columns[i] = newColumn(reg.Types[id.id], isRelation[i], targets[i], capacity)
1,593✔
36
                if columns[i].itemSize > maxSize {
2,011✔
37
                        maxSize = columns[i].itemSize
418✔
38
                }
418✔
39
        }
40
        var zeroValue []byte
614✔
41
        var zeroPointer unsafe.Pointer
614✔
42
        if maxSize > 0 {
1,228✔
43
                zeroValue = make([]byte, maxSize)
614✔
44
                zeroPointer = unsafe.Pointer(&zeroValue[0])
614✔
45
        }
614✔
46

47
        return table{
614✔
48
                id:          id,
614✔
49
                archetype:   archetype,
614✔
50
                components:  componentsMap,
614✔
51
                entities:    entities,
614✔
52
                ids:         ids,
614✔
53
                columns:     columns,
614✔
54
                zeroValue:   zeroValue,
614✔
55
                zeroPointer: zeroPointer,
614✔
56
                relationIDs: relationIDs,
614✔
57
        }
614✔
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 {
504,778✔
68
        _, idx := t.entities.Add(unsafe.Pointer(&entity))
504,778✔
69

504,778✔
70
        for i := range t.columns {
1,013,153✔
71
                t.columns[i].Alloc(1)
508,375✔
72
        }
508,375✔
73
        return idx
504,778✔
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,498,189✔
85
        return *(*Entity)(t.entities.Get(index))
2,498,189✔
86
}
2,498,189✔
87

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

92
func (t *table) GetColumn(component ID) *column {
2,377✔
93
        return &t.columns[t.components[component.id]]
2,377✔
94
}
2,377✔
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) {
513,472✔
101
        t.columns[t.components[component.id]].Set(index, comp)
513,472✔
102
}
513,472✔
103

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

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

116
func (t *table) Remove(index uint32) bool {
503,529✔
117
        swapped := t.entities.Remove(index, nil)
503,529✔
118
        for i := range t.columns {
1,008,572✔
119
                t.columns[i].Remove(index, t.zeroPointer)
505,043✔
120
        }
505,043✔
121
        return swapped
503,529✔
122
}
123

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

131
func (t *table) AddAll(other *table, count uint32) {
11✔
132
        t.entities.AddAll(&other.entities, count)
11✔
133
        for c := range t.columns {
54✔
134
                t.columns[c].AddAll(&other.columns[c], count)
43✔
135
        }
43✔
136
}
137

138
func (t *table) AddAllEntities(other *table, count uint32, allocColumns bool) {
160✔
139
        t.entities.AddAll(&other.entities, count)
160✔
140
        if allocColumns {
320✔
141
                for c := range t.columns {
912✔
142
                        t.columns[c].Alloc(uint32(count))
752✔
143
                }
752✔
144
        }
145
}
146

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

166
func (t *table) Matches(relations []RelationID) bool {
61✔
167
        if len(relations) == 0 {
89✔
168
                return true
28✔
169
        }
28✔
170
        for _, rel := range relations {
68✔
171
                if rel.target == wildcard {
35✔
172
                        continue
×
173
                }
174
                if rel.target != t.columns[t.components[rel.component.id]].target {
36✔
175
                        return false
1✔
176
                }
1✔
177
        }
178
        return true
32✔
179
}
180

181
func (t *table) Len() int {
2,122✔
182
        return t.entities.Len()
2,122✔
183
}
2,122✔
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