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

mlange-42 / arche / 6783344875

07 Nov 2023 10:42AM CUT coverage: 100.0%. Remained the same
6783344875

push

github

web-flow
upgrade to Go 1.21 (#308)

3837 of 3837 relevant lines covered (100.0%)

83117.77 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 all 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 greater than or equal to [MaskTotalBits], it will not be added to the mask.
27
func All(ids ...ID) Mask {
25,466✔
28
        var mask Mask
25,466✔
29
        for _, id := range ids {
51,179✔
30
                mask.Set(id, true)
25,713✔
31
        }
25,713✔
32
        return mask
25,466✔
33
}
34

35
// Matches the mask as filter against another mask.
36
func (b Mask) Matches(bits Mask) bool {
76,796✔
37
        return bits.Contains(b)
76,796✔
38
}
76,796✔
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 whether the bit at the given index [ID] is set.
59
//
60
// Returns false for bit >= [MaskTotalBits].
61
func (b *Mask) Get(bit ID) bool {
251,253✔
62
        if bit < wordSize {
502,374✔
63
                mask := uint64(1 << bit)
251,121✔
64
                return b.Lo&mask == mask
251,121✔
65
        }
251,121✔
66
        mask := uint64(1 << (bit - wordSize))
132✔
67
        return b.Hi&mask == mask
132✔
68
}
69

70
// Set sets the state of bit at the given index.
71
//
72
// Has no effect for bit >= [MaskTotalBits].
73
func (b *Mask) Set(bit ID, value bool) {
156,258✔
74
        if bit < wordSize {
312,313✔
75
                if value {
285,972✔
76
                        b.Lo |= uint64(1 << bit)
129,917✔
77
                } else {
156,055✔
78
                        b.Lo &= uint64(^(1 << bit))
26,138✔
79
                }
26,138✔
80
        }
81
        if value {
286,377✔
82
                b.Hi |= uint64(1 << (bit - wordSize))
130,119✔
83
        } else {
156,258✔
84
                b.Hi &= uint64(^(1 << (bit - wordSize)))
26,139✔
85
        }
26,139✔
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 mask.
97
func (b *Mask) IsZero() bool {
164,487✔
98
        return b.Lo == 0 && b.Hi == 0
164,487✔
99
}
164,487✔
100

101
// Reset the mask setting 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 the other mask is a subset of this mask.
107
func (b *Mask) Contains(other Mask) bool {
76,843✔
108
        return b.Lo&other.Lo == other.Lo && b.Hi&other.Hi == other.Hi
76,843✔
109
}
76,843✔
110

111
// ContainsAny reports if any bit of the 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,577✔
118
        return bits.OnesCount64(b.Hi) + bits.OnesCount64(b.Lo)
1,577✔
119
}
1,577✔
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