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

mlange-42 / arche / 7502900423

12 Jan 2024 01:35PM CUT coverage: 100.0%. Remained the same
7502900423

push

github

web-flow
Component ID getters in world and query copy ID slice (#325)

Both return a copy of the archetype's component IDs slice, for safety.
This means that the result can be manipulated safely,
but also that calling the method may incur some significant cost.

4814 of 4814 relevant lines covered (100.0%)

66181.46 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,728✔
27
        var mask Mask
25,728✔
28
        for _, id := range ids {
51,826✔
29
                mask.Set(id, true)
26,098✔
30
        }
26,098✔
31
        return mask
25,728✔
32
}
33

34
// Matches the mask as filter against another mask.
35
func (b Mask) Matches(bits *Mask) bool {
77,060✔
36
        return bits.Contains(&b)
77,060✔
37
}
77,060✔
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,858✔
61
        idx := bit / 64
251,858✔
62
        offset := bit - (64 * idx)
251,858✔
63
        mask := uint64(1 << offset)
251,858✔
64
        return b.bits[idx]&mask == mask
251,858✔
65
}
251,858✔
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,943✔
71
        idx := bit / 64
156,943✔
72
        offset := bit - (64 * idx)
156,943✔
73
        if value {
287,742✔
74
                b.bits[idx] |= (1 << offset)
130,799✔
75
        } else {
156,943✔
76
                b.bits[idx] &= ^(1 << offset)
26,144✔
77
        }
26,144✔
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,497✔
89
        return b.bits[0] == 0 && b.bits[1] == 0 && b.bits[2] == 0 && b.bits[3] == 0
164,497✔
90
}
164,497✔
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,107✔
99
        return b.bits[0]&other.bits[0] == other.bits[0] &&
77,107✔
100
                b.bits[1]&other.bits[1] == other.bits[1] &&
77,107✔
101
                b.bits[2]&other.bits[2] == other.bits[2] &&
77,107✔
102
                b.bits[3]&other.bits[3] == other.bits[3]
77,107✔
103
}
77,107✔
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,847✔
115
        return bits.OnesCount64(b.bits[0]) + bits.OnesCount64(b.bits[1]) + bits.OnesCount64(b.bits[2]) + bits.OnesCount64(b.bits[3])
1,847✔
116
}
1,847✔
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