• 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/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 (see [All] and [Mask.Without]).
13
type Mask struct {
14
        Lo uint64 // First 64 bits of the mask
15
        Hi uint64 // Second 64 bits of the mask
16
}
17

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

32
// Matches matches a filter against a bitmask.
33
func (b Mask) Matches(bits Mask, relation *Entity) bool {
3,110✔
34
        return bits.Contains(b)
3,110✔
35
}
3,110✔
36

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

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

55
// Get reports if bit index defined by [ID] is true or false.
56
//
57
// The return will be always false for bit >= [MaskTotalBits].
58
func (b *Mask) Get(bit ID) bool {
104,540✔
59
        if bit < wordSize {
208,948✔
60
                mask := uint64(1 << bit)
104,408✔
61
                return b.Lo&mask == mask
104,408✔
62
        }
104,408✔
63
        mask := uint64(1 << (bit - wordSize))
132✔
64
        return b.Hi&mask == mask
132✔
65
}
66

67
// Set sets the state of bit index to true or false.
68
//
69
// This function has no effect for bit >= [MaskTotalBits].
70
func (b *Mask) Set(bit ID, value bool) {
32,370✔
71
        if bit < wordSize {
64,537✔
72
                if value {
64,007✔
73
                        b.Lo |= uint64(1 << bit)
31,840✔
74
                } else {
32,167✔
75
                        b.Lo &= uint64(^(1 << bit))
327✔
76
                }
327✔
77
        }
78
        if value {
64,412✔
79
                b.Hi |= uint64(1 << (bit - wordSize))
32,042✔
80
        } else {
32,370✔
81
                b.Hi &= uint64(^(1 << (bit - wordSize)))
328✔
82
        }
328✔
83
}
84

85
// Not returns the inversion of this mask.
86
func (b *Mask) Not() Mask {
3✔
87
        return Mask{
3✔
88
                Lo: ^b.Lo,
3✔
89
                Hi: ^b.Hi,
3✔
90
        }
3✔
91
}
3✔
92

93
// IsZero returns whether no bits are set in the bitmask.
94
func (b *Mask) IsZero() bool {
11,149✔
95
        return b.Lo == 0 && b.Hi == 0
11,149✔
96
}
11,149✔
97

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

103
// Contains reports if other mask is a subset of this mask.
104
func (b *Mask) Contains(other Mask) bool {
3,146✔
105
        return b.Lo&other.Lo == other.Lo && b.Hi&other.Hi == other.Hi
3,146✔
106
}
3,146✔
107

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

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