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

mlange-42 / arche / 4846115663

30 Apr 2023 07:56PM CUT coverage: 100.0%. Remained the same
4846115663

push

github

GitHub
Register target entity for later cleanup when spawning with relation (#242)

12 of 12 new or added lines in 1 file covered. (100.0%)

3105 of 3105 relevant lines covered (100.0%)

2577.02 hits per line

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

100.0
/ecs/id_map.go
1
package ecs
2

3
const (
4
        numChunks = 8
5
        chunkSize = 16
6
)
7

8
// idMap maps component IDs to values.
9
//
10
// Is is a data structure meant for fast lookup while being memory-efficient.
11
// Access time is around 2ns, compared to 0.5ns for array access and 20ns for map[int]T.
12
//
13
// The memory footprint is reduced by using chunks, and only allocating chunks if they contain a key.
14
//
15
// The range of keys is limited from 0 to [MaskTotalBits]-1.
16
type idMap[T any] struct {
17
        chunks    [][]T
18
        used      Mask
19
        chunkUsed []uint8
20
        zeroValue T
21
}
22

23
// newIDMap creates a new idMap
24
func newIDMap[T any]() idMap[T] {
3,730✔
25
        return idMap[T]{
3,730✔
26
                chunks:    make([][]T, numChunks),
3,730✔
27
                used:      Mask{},
3,730✔
28
                chunkUsed: make([]uint8, numChunks),
3,730✔
29
        }
3,730✔
30
}
3,730✔
31

32
// Get returns the value at the given key and whether the key is present.
33
func (m *idMap[T]) Get(index uint8) (T, bool) {
10,365✔
34
        if !m.used.Get(index) {
11,506✔
35
                return m.zeroValue, false
1,141✔
36
        }
1,141✔
37
        return m.chunks[index/chunkSize][index%chunkSize], true
9,224✔
38
}
39

40
// Get returns a pointer to the value at the given key and whether the key is present.
41
func (m *idMap[T]) GetPointer(index uint8) (*T, bool) {
5✔
42
        if !m.used.Get(index) {
7✔
43
                return nil, false
2✔
44
        }
2✔
45
        return &m.chunks[index/chunkSize][index%chunkSize], true
3✔
46
}
47

48
// Set sets the value at the given key.
49
func (m *idMap[T]) Set(index uint8, value T) {
7,599✔
50
        chunk := index / chunkSize
7,599✔
51
        if m.chunks[chunk] == nil {
10,492✔
52
                m.chunks[chunk] = make([]T, chunkSize)
2,893✔
53
        }
2,893✔
54
        m.chunks[chunk][index%chunkSize] = value
7,599✔
55
        m.used.Set(index, true)
7,599✔
56
        m.chunkUsed[chunk]++
7,599✔
57
}
58

59
// Remove removes the value at the given key.
60
// It de-allocates empty chunks.
61
func (m *idMap[T]) Remove(index uint8) {
6✔
62
        chunk := index / chunkSize
6✔
63
        m.used.Set(index, false)
6✔
64
        m.chunkUsed[chunk]--
6✔
65
        if m.chunkUsed[chunk] == 0 {
8✔
66
                m.chunks[chunk] = nil
2✔
67
        }
2✔
68
}
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