forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
bit_set.go
39 lines (27 loc) · 1.17 KB
/
bit_set.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
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package ids
import (
"fmt"
"math/bits"
)
// BitSet 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 BitSet uint64
// Add [i] to the set of ints
func (bs *BitSet) Add(i uint) { *bs |= 1 << i }
// Union adds all the elements in [s] to this set
func (bs *BitSet) Union(s BitSet) { *bs |= s }
// Intersection takes the intersection of [s] with this set
func (bs *BitSet) Intersection(s BitSet) { *bs &= s }
// Difference removes all the elements in [s] from this set
func (bs *BitSet) Difference(s BitSet) { *bs &^= s }
// Remove [i] from the set of ints
func (bs *BitSet) Remove(i uint) { *bs &^= 1 << i }
// Clear removes all elements from this set
func (bs *BitSet) Clear() { *bs = 0 }
// Contains returns true if [i] was previously added to this set
func (bs BitSet) Contains(i uint) bool { return bs&(1<<i) != 0 }
// Len returns the number of elements in this set
func (bs BitSet) Len() int { return bits.OnesCount64(uint64(bs)) }
func (bs BitSet) String() string { return fmt.Sprintf("%016x", uint64(bs)) }