forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
bits_64.go
57 lines (45 loc) · 1.17 KB
/
bits_64.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package set
import (
"fmt"
"math/bits"
)
// Bits64 is a set that can contain uints in the range [0, 64). All functions
// are O(1). The zero value is the empty set.
type Bits64 uint64
// Add [i] to the set of ints
func (b *Bits64) Add(i uint) {
*b |= 1 << i
}
// Union adds all the elements in [s] to this set
func (b *Bits64) Union(s Bits64) {
*b |= s
}
// Intersection takes the intersection of [s] with this set
func (b *Bits64) Intersection(s Bits64) {
*b &= s
}
// Difference removes all the elements in [s] from this set
func (b *Bits64) Difference(s Bits64) {
*b &^= s
}
// Remove [i] from the set of ints
func (b *Bits64) Remove(i uint) {
*b &^= 1 << i
}
// Clear removes all elements from this set
func (b *Bits64) Clear() {
*b = 0
}
// Contains returns true if [i] was previously added to this set
func (b Bits64) Contains(i uint) bool {
return b&(1<<i) != 0
}
// Len returns the number of elements in this set
func (b Bits64) Len() int {
return bits.OnesCount64(uint64(b))
}
func (b Bits64) String() string {
return fmt.Sprintf("%016x", uint64(b))
}