Skip to content

Commit

Permalink
Standardize set.Bits method naming with set.Bits64 (ava-labs#2667)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-ogrady authored Feb 28, 2023
1 parent 75cb25e commit b39246a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
11 changes: 7 additions & 4 deletions utils/set/bits.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,16 @@ func (b Bits) Contains(i int) bool {
return b.bits.Bit(i) == 1
}

// Len returns the bit length of this bitset
func (b Bits) Len() int {
// BitLen returns the bit length of this bitset
func (b Bits) BitLen() int {
return b.bits.BitLen()
}

// HammingWeight returns the amount of 1's in the bitset
func (b Bits) HammingWeight() int {
// Len returns the amount of 1's in the bitset
//
// This is typically referred to as the "Hamming Weight"
// of a set of bits.
func (b Bits) Len() int {
result := 0
for _, word := range b.bits.Bits() {
result += bits.OnesCount(uint(word))
Expand Down
14 changes: 7 additions & 7 deletions utils/set/bits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func Test_Bits_New(t *testing.T) {
require.True(b.Contains(bit))
}

require.Equal(test.length, b.Len())
require.Equal(test.length, b.BitLen())
})
}
}
Expand Down Expand Up @@ -124,7 +124,7 @@ func Test_Bits_AddRemove(t *testing.T) {
require.True(b.Contains(element))
}

require.Equal(test.expectedLen, b.Len())
require.Equal(test.expectedLen, b.BitLen())
})
}
}
Expand Down Expand Up @@ -197,7 +197,7 @@ func Test_Bits_Union(t *testing.T) {
require.True(b.Contains(element))
}

require.Equal(test.expectedLen, b.Len())
require.Equal(test.expectedLen, b.BitLen())
})
}
}
Expand Down Expand Up @@ -383,7 +383,7 @@ func Test_Bits_Clear(t *testing.T) {

b.Clear()

require.Zero(b.Len())
require.Zero(b.BitLen())
})
}
}
Expand Down Expand Up @@ -419,7 +419,7 @@ func Test_Bits_String(t *testing.T) {
}
}

func Test_Bits_HammingWeight(t *testing.T) {
func Test_Bits_Len(t *testing.T) {
tests := []struct {
name string
bitset []int
Expand Down Expand Up @@ -465,7 +465,7 @@ func Test_Bits_HammingWeight(t *testing.T) {
b.Add(bit)
}

require.Equal(test.expected, b.HammingWeight())
require.Equal(test.expected, b.Len())
})
}
}
Expand Down Expand Up @@ -499,7 +499,7 @@ func Test_Bits_Bytes(t *testing.T) {
bytes := b.Bytes()
fromBytes := BitsFromBytes(bytes)

require.Equal(len(tt.elts), fromBytes.HammingWeight())
require.Equal(len(tt.elts), fromBytes.Len())
for _, elt := range tt.elts {
require.True(fromBytes.Contains(elt))
}
Expand Down
2 changes: 1 addition & 1 deletion vms/platformvm/warp/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (s *BitSetSignature) NumSigners() (int, error) {
if len(signerIndices.Bytes()) != len(s.Signers) {
return 0, ErrInvalidBitSet
}
return signerIndices.HammingWeight(), nil
return signerIndices.Len(), nil
}

func (s *BitSetSignature) Verify(
Expand Down
4 changes: 2 additions & 2 deletions vms/platformvm/warp/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ func FilterValidators(
vdrs []*Validator,
) ([]*Validator, error) {
// Verify that all alleged signers exist
if indices.Len() > len(vdrs) {
if indices.BitLen() > len(vdrs) {
return nil, fmt.Errorf(
"%w: %d >= %d",
ErrUnknownValidator,
indices.Len()-1, // -1 to convert from length to index
indices.BitLen()-1, // -1 to convert from length to index
len(vdrs),
)
}
Expand Down

0 comments on commit b39246a

Please sign in to comment.