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

mlange-42 / arche / 4725747591

17 Apr 2023 08:58PM CUT coverage: 100.0%. Remained the same
4725747591

push

github

GitHub
Add parallel simulations example (#223)

2729 of 2729 relevant lines covered (100.0%)

4272.62 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 {
465✔
25
        var mask Mask
465✔
26
        for _, id := range ids {
1,265✔
27
                mask.Set(id, true)
800✔
28
        }
800✔
29
        return mask
465✔
30
}
31

32
// Matches matches a filter against a bitmask.
33
func (b Mask) Matches(bits Mask) bool {
1,589✔
34
        return bits.Contains(b)
1,589✔
35
}
1,589✔
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 {
8✔
49
        return MaskFilter{
8✔
50
                Include: b,
8✔
51
                Exclude: b.Not(),
8✔
52
        }
8✔
53
}
8✔
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 {
89,205✔
59
        if bit < wordSize {
178,278✔
60
                mask := uint64(1 << bit)
89,073✔
61
                return b.Lo&mask == mask
89,073✔
62
        }
89,073✔
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) {
29,586✔
71
        if bit < wordSize {
58,969✔
72
                if value {
58,468✔
73
                        b.Lo |= uint64(1 << bit)
29,085✔
74
                } else {
29,383✔
75
                        b.Lo &= uint64(^(1 << bit))
298✔
76
                }
298✔
77
        }
78
        if value {
58,873✔
79
                b.Hi |= uint64(1 << (bit - wordSize))
29,287✔
80
        } else {
29,586✔
81
                b.Hi &= uint64(^(1 << (bit - wordSize)))
299✔
82
        }
299✔
83
}
84

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

93
// IsZero returns whether no bits are set in the bitmask.
94
func (b *Mask) IsZero() bool {
6,057✔
95
        return b.Lo == 0 && b.Hi == 0
6,057✔
96
}
6,057✔
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 {
1,648✔
105
        return b.Lo&other.Lo == other.Lo && b.Hi&other.Hi == other.Hi
1,648✔
106
}
1,648✔
107

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

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