Skip to content

Commit

Permalink
Improve types for bitset
Browse files Browse the repository at this point in the history
  • Loading branch information
janos committed Feb 15, 2023
1 parent ba2a0d7 commit bea6cda
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
6 changes: 3 additions & 3 deletions bitset.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ package schulze

type bitSet []uint64

func newBitset(size uint) bitSet {
func newBitset(size uint64) bitSet {
return bitSet(make([]uint64, size/64+1))
}

func (s bitSet) set(i uint) {
func (s bitSet) set(i uint64) {
s[i/64] |= 1 << (i % 64)
}

func (s bitSet) isSet(i uint) bool {
func (s bitSet) isSet(i uint64) bool {
return s[i/64]&(1<<(i%64)) != 0
}
12 changes: 6 additions & 6 deletions bitset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func TestBitSet(t *testing.T) {
contains := func(i uint, values []uint) bool {
contains := func(i uint64, values []uint64) bool {
for _, v := range values {
if i == v {
return true
Expand All @@ -23,16 +23,16 @@ func TestBitSet(t *testing.T) {

seed := time.Now().UnixNano()
r := rand.New(rand.NewSource(seed))
size := uint(r.Intn(12345))
size := uint64(r.Intn(12345))
s := newBitset(size)
values := make([]uint, 0)
for i, count := uint(0), uint(r.Intn(100)); i < count; i++ {
values = append(values, uint(r.Intn(int(size))))
values := make([]uint64, 0)
for i, count := uint64(0), uint64(r.Intn(100)); i < count; i++ {
values = append(values, uint64(r.Intn(int(size))))
}
for _, v := range values {
s.set(v)
}
for i := uint(0); i < size; i++ {
for i := uint64(0); i < size; i++ {
if contains(i, values) {
if !s.isSet(i) {
t.Errorf("expected value %v is not set (seed %v)", i, seed)
Expand Down
9 changes: 6 additions & 3 deletions schulze.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func ballotRanks[C comparable](choices []C, b Ballot[C]) (ranks [][]choiceIndex,
ballotRanks := make(map[int][]choiceIndex, ballotLen)
var rankedChoices bitSet
if hasUnrankedChoices {
rankedChoices = newBitset(uint(choicesLen))
rankedChoices = newBitset(uint64(choicesLen))
}

choicesLen = len(choices)
Expand All @@ -163,7 +163,7 @@ func ballotRanks[C comparable](choices []C, b Ballot[C]) (ranks [][]choiceIndex,
ballotRanks[rank] = append(ballotRanks[rank], index)

if hasUnrankedChoices {
rankedChoices.set(uint(index))
rankedChoices.set(uint64(index))
}
}

Expand All @@ -183,7 +183,10 @@ func ballotRanks[C comparable](choices []C, b Ballot[C]) (ranks [][]choiceIndex,

if hasUnrankedChoices {
unranked := make([]choiceIndex, 0, choicesLen-ballotLen)
for i := uint(0); int(i) < choicesLen; i++ {
// rankedChoices.iterateUnset(func(i uint64) {
// unranked = append(unranked, choiceIndex(i))
// })
for i := uint64(0); int(i) < choicesLen; i++ {
if !rankedChoices.isSet(i) {
unranked = append(unranked, choiceIndex(i))
}
Expand Down

0 comments on commit bea6cda

Please sign in to comment.