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

mlange-42 / arche / 7528343499

15 Jan 2024 11:43AM CUT coverage: 100.0%. Remained the same
7528343499

push

github

web-flow
Remove entity ID and generation getters (#332)

4930 of 4930 relevant lines covered (100.0%)

64817.27 hits per line

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

100.0
/ecs/bitmask.go
1
package ecs
2

3
import "math/bits"
4

5
// MaskTotalBits is the size of Mask in bits.
6
//
7
// It is the maximum number of component types that may exist in any [World].
8
const MaskTotalBits = 256
9
const wordSize = 64
10

11
// Mask is a 256 bit bitmask.
12
// It is also a [Filter] for including certain components.
13
//
14
// Use [All] to create a mask for a list of component IDs.
15
// A mask can be further specified using [Mask.Without] or [Mask.Exclusive].
16
type Mask struct {
17
        bits [4]uint64 // 4x 64 bits of the mask
18
}
19

20
// All creates a new Mask from a list of IDs.
21
// Matches all entities that have the respective components, and potentially further components.
22
//
23
// See also [Mask.Without] and [Mask.Exclusive]
24
//
25
// If any [ID] is greater than or equal to [MaskTotalBits], it will not be added to the mask.
26
func All(ids ...ID) Mask {
25,731✔
27
        var mask Mask
25,731✔
28
        for _, id := range ids {
51,834✔
29
                mask.Set(id, true)
26,103✔
30
        }
26,103✔
31
        return mask
25,731✔
32
}
33

34
// Matches the mask as filter against another mask.
35
func (b Mask) Matches(bits *Mask) bool {
77,064✔
36
        return bits.Contains(&b)
77,064✔
37
}
77,064✔
38

39
// Without creates a [MaskFilter] which filters for including the mask's components,
40
// and excludes the components given as arguments.
41
func (b Mask) Without(comps ...ID) MaskFilter {
7✔
42
        return MaskFilter{
7✔
43
                Include: b,
7✔
44
                Exclude: All(comps...),
7✔
45
        }
7✔
46
}
7✔
47

48
// Exclusive creates a [MaskFilter] which filters for exactly the mask's components.
49
// Matches only entities that have exactly the given components, and no other.
50
func (b Mask) Exclusive() MaskFilter {
6✔
51
        return MaskFilter{
6✔
52
                Include: b,
6✔
53
                Exclude: b.Not(),
6✔
54
        }
6✔
55
}
6✔
56

57
// Get reports whether the bit at the given index [ID] is set.
58
//
59
// Returns false for bit >= [MaskTotalBits].
60
func (b *Mask) Get(bit ID) bool {
251,928✔
61
        idx := bit.id / 64
251,928✔
62
        offset := bit.id - (64 * idx)
251,928✔
63
        mask := uint64(1 << offset)
251,928✔
64
        return b.bits[idx]&mask == mask
251,928✔
65
}
251,928✔
66

67
// Set sets the state of the bit at the given index.
68
//
69
// Has no effect for bit >= [MaskTotalBits].
70
func (b *Mask) Set(bit ID, value bool) {
156,988✔
71
        idx := bit.id / 64
156,988✔
72
        offset := bit.id - (64 * idx)
156,988✔
73
        if value {
287,826✔
74
                b.bits[idx] |= (1 << offset)
130,838✔
75
        } else {
156,988✔
76
                b.bits[idx] &= ^(1 << offset)
26,150✔
77
        }
26,150✔
78
}
79

80
// Not returns the inversion of this mask.
81
func (b *Mask) Not() Mask {
6✔
82
        return Mask{
6✔
83
                bits: [4]uint64{^b.bits[0], ^b.bits[1], ^b.bits[2], ^b.bits[3]},
6✔
84
        }
6✔
85
}
6✔
86

87
// IsZero returns whether no bits are set in the mask.
88
func (b *Mask) IsZero() bool {
164,719✔
89
        return b.bits[0] == 0 && b.bits[1] == 0 && b.bits[2] == 0 && b.bits[3] == 0
164,719✔
90
}
164,719✔
91

92
// Reset the mask setting all bits to false.
93
func (b *Mask) Reset() {
1✔
94
        b.bits = [4]uint64{0, 0, 0, 0}
1✔
95
}
1✔
96

97
// Contains reports if the other mask is a subset of this mask.
98
func (b *Mask) Contains(other *Mask) bool {
77,111✔
99
        return b.bits[0]&other.bits[0] == other.bits[0] &&
77,111✔
100
                b.bits[1]&other.bits[1] == other.bits[1] &&
77,111✔
101
                b.bits[2]&other.bits[2] == other.bits[2] &&
77,111✔
102
                b.bits[3]&other.bits[3] == other.bits[3]
77,111✔
103
}
77,111✔
104

105
// ContainsAny reports if any bit of the other mask is in this mask.
106
func (b *Mask) ContainsAny(other *Mask) bool {
26✔
107
        return b.bits[0]&other.bits[0] != 0 ||
26✔
108
                b.bits[1]&other.bits[1] != 0 ||
26✔
109
                b.bits[2]&other.bits[2] != 0 ||
26✔
110
                b.bits[3]&other.bits[3] != 0
26✔
111
}
26✔
112

113
// TotalBitsSet returns how many bits are set in this mask.
114
func (b *Mask) TotalBitsSet() int {
1,850✔
115
        return bits.OnesCount64(b.bits[0]) + bits.OnesCount64(b.bits[1]) + bits.OnesCount64(b.bits[2]) + bits.OnesCount64(b.bits[3])
1,850✔
116
}
1,850✔
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