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

mlange-42 / arche / 4969448132

13 May 2023 11:51PM CUT coverage: 100.0%. Remained the same
4969448132

push

github

GitHub
Simplify names of XxxQuery methods to XxxQ (#298)

16 of 16 new or added lines in 5 files covered. (100.0%)

3834 of 3834 relevant lines covered (100.0%)

83135.94 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 = 128
9
const wordSize = 64
10

11
// Mask is a 128 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
        Lo uint64 // First 64 bits of the mask
18
        Hi uint64 // Second 64 bits of the mask
19
}
20

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

35
// Matches matches a filter against a bitmask.
36
func (b Mask) Matches(bits Mask) bool {
76,769✔
37
        return bits.Contains(b)
76,769✔
38
}
76,769✔
39

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

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

58
// Get reports if bit index defined by [ID] is true or false.
59
//
60
// The return will be always false for bit >= [MaskTotalBits].
61
func (b *Mask) Get(bit ID) bool {
248,709✔
62
        if bit < wordSize {
497,286✔
63
                mask := uint64(1 << bit)
248,577✔
64
                return b.Lo&mask == mask
248,577✔
65
        }
248,577✔
66
        mask := uint64(1 << (bit - wordSize))
132✔
67
        return b.Hi&mask == mask
132✔
68
}
69

70
// Set sets the state of bit index to true or false.
71
//
72
// This function has no effect for bit >= [MaskTotalBits].
73
func (b *Mask) Set(bit ID, value bool) {
156,080✔
74
        if bit < wordSize {
311,957✔
75
                if value {
285,633✔
76
                        b.Lo |= uint64(1 << bit)
129,756✔
77
                } else {
155,877✔
78
                        b.Lo &= uint64(^(1 << bit))
26,121✔
79
                }
26,121✔
80
        }
81
        if value {
286,038✔
82
                b.Hi |= uint64(1 << (bit - wordSize))
129,958✔
83
        } else {
156,080✔
84
                b.Hi &= uint64(^(1 << (bit - wordSize)))
26,122✔
85
        }
26,122✔
86
}
87

88
// Not returns the inversion of this mask.
89
func (b *Mask) Not() Mask {
6✔
90
        return Mask{
6✔
91
                Lo: ^b.Lo,
6✔
92
                Hi: ^b.Hi,
6✔
93
        }
6✔
94
}
6✔
95

96
// IsZero returns whether no bits are set in the bitmask.
97
func (b *Mask) IsZero() bool {
164,450✔
98
        return b.Lo == 0 && b.Hi == 0
164,450✔
99
}
164,450✔
100

101
// Reset changes the state of all bits to false.
102
func (b *Mask) Reset() {
1✔
103
        b.Lo, b.Lo = 0, 0
1✔
104
}
1✔
105

106
// Contains reports if other mask is a subset of this mask.
107
func (b *Mask) Contains(other Mask) bool {
76,816✔
108
        return b.Lo&other.Lo == other.Lo && b.Hi&other.Hi == other.Hi
76,816✔
109
}
76,816✔
110

111
// ContainsAny reports if any bit of other mask is in this mask.
112
func (b *Mask) ContainsAny(other Mask) bool {
26✔
113
        return b.Lo&other.Lo != 0 || b.Hi&other.Hi != 0
26✔
114
}
26✔
115

116
// TotalBitsSet returns how many bits are set in this mask.
117
func (b *Mask) TotalBitsSet() int {
1,537✔
118
        return bits.OnesCount64(b.Hi) + bits.OnesCount64(b.Lo)
1,537✔
119
}
1,537✔
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