github.com/MetalBlockchain/metalgo@v1.11.9/utils/set/bits_64.go (about)

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