lib/uint64set: optimize Set.AppendTo

This commit is contained in:
Aliaksandr Valialkin 2019-09-25 00:34:09 +03:00
parent b986516fbe
commit 2cb811b42f
2 changed files with 20 additions and 11 deletions

View file

@ -1,6 +1,7 @@
package uint64set
import (
"math/bits"
"sort"
)
@ -299,11 +300,19 @@ func (b *bucket16) appendTo(dst []uint64, hi uint32, hi16 uint16) []uint64 {
hi64 := uint64(hi)<<32 | uint64(hi16)<<16
var wordNum uint64
for _, word := range b.bits {
for bitNum := uint64(0); bitNum < 64; bitNum++ {
if word&(uint64(1)<<bitNum) != 0 {
x := hi64 | uint64(wordNum)*64 | bitNum
dst = append(dst, x)
if word == 0 {
wordNum++
continue
}
x64 := hi64 | (wordNum * 64)
for {
tzn := uint64(bits.TrailingZeros64(word))
if tzn >= 64 {
break
}
word &^= uint64(1) << tzn
x := x64 | tzn
dst = append(dst, x)
}
wordNum++
}

View file

@ -48,14 +48,14 @@ func testSetBasicOps(t *testing.T, itemsCount int) {
// Verify Has on existing bits
for i := 0; i < itemsCount; i++ {
if !s.Has(uint64(i) + offset) {
t.Fatalf("missing bit %d", i)
t.Fatalf("missing bit %d", uint64(i)+offset)
}
}
// Verify Has on missing bits
for i := itemsCount; i < 2*itemsCount; i++ {
if s.Has(uint64(i) + offset) {
t.Fatalf("unexpected bit found: %d", i)
t.Fatalf("unexpected bit found: %d", uint64(i)+offset)
}
}
@ -66,7 +66,7 @@ func testSetBasicOps(t *testing.T, itemsCount int) {
}
for i := 0; i < itemsCount; i++ {
if !sCopy.Has(uint64(i) + offset) {
t.Fatalf("missing bit %d on sCopy", i)
t.Fatalf("missing bit %d on sCopy", uint64(i)+offset)
}
}
@ -84,7 +84,7 @@ func testSetBasicOps(t *testing.T, itemsCount int) {
}
for i := 0; i < itemsCount; i++ {
if !m[uint64(i)+offset] {
t.Fatalf("missing bit %d in the exported bits; array:\n%d", i, a)
t.Fatalf("missing bit %d in the exported bits; array:\n%d", uint64(i)+offset, a)
}
}
@ -106,11 +106,11 @@ func testSetBasicOps(t *testing.T, itemsCount int) {
for i := 0; i < itemsCount; i++ {
if i >= itemsCount/2 && i < itemsCount-itemsCount/4 {
if m[uint64(i)+offset] {
t.Fatalf("unexpected bit found after deleting: %d", i)
t.Fatalf("unexpected bit found after deleting: %d", uint64(i)+offset)
}
} else {
if !m[uint64(i)+offset] {
t.Fatalf("missing bit %d in the exported bits after deleting", i)
t.Fatalf("missing bit %d in the exported bits after deleting", uint64(i)+offset)
}
}
}
@ -131,7 +131,7 @@ func testSetBasicOps(t *testing.T, itemsCount int) {
}
for i := 0; i < itemsCount; i++ {
if !sCopy.Has(uint64(i) + offset) {
t.Fatalf("missing bit %d on sCopy", i)
t.Fatalf("missing bit %d on sCopy", uint64(i)+offset)
}
}
}