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

mlange-42 / arche / 4963618725

12 May 2023 10:54PM CUT coverage: 100.0%. Remained the same
4963618725

push

github

GitHub
Add ouline to all package docks, tweak docstrings (#295)

3782 of 3782 relevant lines covered (100.0%)

84279.39 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,451✔
28
        var mask Mask
25,451✔
29
        for _, id := range ids {
51,148✔
30
                mask.Set(id, true)
25,697✔
31
        }
25,697✔
32
        return mask
25,451✔
33
}
34

35
// Matches matches a filter against a bitmask.
36
func (b Mask) Matches(bits Mask) bool {
76,759✔
37
        return bits.Contains(b)
76,759✔
38
}
76,759✔
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,532✔
62
        if bit < wordSize {
496,932✔
63
                mask := uint64(1 << bit)
248,400✔
64
                return b.Lo&mask == mask
248,400✔
65
        }
248,400✔
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,066✔
74
        if bit < wordSize {
311,929✔
75
                if value {
285,607✔
76
                        b.Lo |= uint64(1 << bit)
129,744✔
77
                } else {
155,863✔
78
                        b.Lo &= uint64(^(1 << bit))
26,119✔
79
                }
26,119✔
80
        }
81
        if value {
286,012✔
82
                b.Hi |= uint64(1 << (bit - wordSize))
129,946✔
83
        } else {
156,066✔
84
                b.Hi &= uint64(^(1 << (bit - wordSize)))
26,120✔
85
        }
26,120✔
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,454✔
98
        return b.Lo == 0 && b.Hi == 0
164,454✔
99
}
164,454✔
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,806✔
108
        return b.Lo&other.Lo == other.Lo && b.Hi&other.Hi == other.Hi
76,806✔
109
}
76,806✔
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,536✔
118
        return bits.OnesCount64(b.Hi) + bits.OnesCount64(b.Lo)
1,536✔
119
}
1,536✔
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