2019-09-24 20:10:22 +02:00
|
|
|
package uint64set
|
|
|
|
|
|
|
|
import (
|
2019-09-24 23:34:09 +02:00
|
|
|
"math/bits"
|
2019-09-24 20:10:22 +02:00
|
|
|
"sort"
|
2019-11-13 12:11:02 +01:00
|
|
|
"unsafe"
|
2019-09-24 20:10:22 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Set is a fast set for uint64.
|
|
|
|
//
|
|
|
|
// It should work faster than map[uint64]struct{} for semi-sparse uint64 values
|
|
|
|
// such as MetricIDs generated by lib/storage.
|
|
|
|
//
|
|
|
|
// It is unsafe calling Set methods from concurrent goroutines.
|
|
|
|
type Set struct {
|
2020-01-03 17:16:37 +01:00
|
|
|
itemsCount int
|
|
|
|
buckets bucket32Sorter
|
2019-09-24 20:10:22 +02:00
|
|
|
}
|
|
|
|
|
2020-01-03 17:16:37 +01:00
|
|
|
type bucket32Sorter []bucket32
|
2019-09-24 20:10:22 +02:00
|
|
|
|
|
|
|
func (s *bucket32Sorter) Len() int { return len(*s) }
|
|
|
|
func (s *bucket32Sorter) Less(i, j int) bool {
|
|
|
|
a := *s
|
|
|
|
return a[i].hi < a[j].hi
|
|
|
|
}
|
|
|
|
func (s *bucket32Sorter) Swap(i, j int) {
|
|
|
|
a := *s
|
|
|
|
a[i], a[j] = a[j], a[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clone returns an independent copy of s.
|
|
|
|
func (s *Set) Clone() *Set {
|
|
|
|
if s == nil {
|
lib/uint64set: return an emptry set instead of nil set from `Set.Clone`, since the caller may add data to the cloned set
This fixes the following panic in v1.28.1:
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0x783a7e]
goroutine 1155 [running]:
github.com/VictoriaMetrics/VictoriaMetrics/lib/uint64set.(*Set).Add(0x0, 0x15b3bfb41e8b71ec)
github.com/VictoriaMetrics/VictoriaMetrics@/lib/uint64set/uint64set.go:57 +0x2e
github.com/VictoriaMetrics/VictoriaMetrics/lib/storage.(*indexSearch).getMetricIDsForRecentHours(0xc5bdc0dd40, 0x16e273f6b50, 0x16e2745d3f0, 0x5b8d95, 0x10, 0x4a2f51, 0xaa01000000000000)
github.com/VictoriaMetrics/VictoriaMetrics@/lib/storage/index_db.go:1951 +0x260
github.com/VictoriaMetrics/VictoriaMetrics/lib/storage.(*indexSearch).getMetricIDsForTimeRange(0xc5bdc0dd40, 0x16e273f6b50, 0x16e2745d3f0, 0x5b8d95, 0x10, 0xb296c0, 0xc00009cd80, 0x9bc640)
2019-11-01 15:11:15 +01:00
|
|
|
// Return an empty set, so data could be added into it later.
|
|
|
|
return &Set{}
|
2019-09-24 20:10:22 +02:00
|
|
|
}
|
|
|
|
var dst Set
|
|
|
|
dst.itemsCount = s.itemsCount
|
2020-01-03 17:16:37 +01:00
|
|
|
if len(s.buckets) > 0 {
|
|
|
|
dst.buckets = make([]bucket32, len(s.buckets))
|
|
|
|
for i := range s.buckets {
|
|
|
|
s.buckets[i].copyTo(&dst.buckets[i])
|
|
|
|
}
|
2019-09-24 20:10:22 +02:00
|
|
|
}
|
|
|
|
return &dst
|
|
|
|
}
|
|
|
|
|
2019-11-13 12:11:02 +01:00
|
|
|
// SizeBytes returns an estimate size of s in RAM.
|
|
|
|
func (s *Set) SizeBytes() uint64 {
|
|
|
|
if s == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
n := uint64(unsafe.Sizeof(*s))
|
2020-01-03 17:16:37 +01:00
|
|
|
for i := range s.buckets {
|
|
|
|
b32 := &s.buckets[i]
|
|
|
|
n += uint64(unsafe.Sizeof(b32))
|
|
|
|
n += b32.sizeBytes()
|
2019-11-13 12:11:02 +01:00
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2019-09-24 20:10:22 +02:00
|
|
|
// Len returns the number of distinct uint64 values in s.
|
|
|
|
func (s *Set) Len() int {
|
|
|
|
if s == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return s.itemsCount
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add adds x to s.
|
|
|
|
func (s *Set) Add(x uint64) {
|
|
|
|
hi := uint32(x >> 32)
|
|
|
|
lo := uint32(x)
|
2020-01-03 17:16:37 +01:00
|
|
|
for i := range s.buckets {
|
|
|
|
b32 := &s.buckets[i]
|
2019-09-24 20:10:22 +02:00
|
|
|
if b32.hi == hi {
|
|
|
|
if b32.add(lo) {
|
|
|
|
s.itemsCount++
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.addAlloc(hi, lo)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Set) addAlloc(hi, lo uint32) {
|
2020-01-03 17:16:37 +01:00
|
|
|
b32 := s.addBucket32()
|
2019-09-24 20:10:22 +02:00
|
|
|
b32.hi = hi
|
|
|
|
_ = b32.add(lo)
|
|
|
|
s.itemsCount++
|
2020-01-03 17:16:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Set) addBucket32() *bucket32 {
|
|
|
|
s.buckets = append(s.buckets, bucket32{})
|
|
|
|
return &s.buckets[len(s.buckets)-1]
|
2019-09-24 20:10:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Has verifies whether x exists in s.
|
|
|
|
func (s *Set) Has(x uint64) bool {
|
|
|
|
if s == nil {
|
|
|
|
return false
|
|
|
|
}
|
2019-11-08 12:16:40 +01:00
|
|
|
hi := uint32(x >> 32)
|
|
|
|
lo := uint32(x)
|
2020-01-03 17:16:37 +01:00
|
|
|
for i := range s.buckets {
|
|
|
|
b32 := &s.buckets[i]
|
2019-09-24 20:10:22 +02:00
|
|
|
if b32.hi == hi {
|
|
|
|
return b32.has(lo)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Del deletes x from s.
|
|
|
|
func (s *Set) Del(x uint64) {
|
|
|
|
hi := uint32(x >> 32)
|
|
|
|
lo := uint32(x)
|
2020-01-03 17:16:37 +01:00
|
|
|
for i := range s.buckets {
|
|
|
|
b32 := &s.buckets[i]
|
2019-09-24 20:10:22 +02:00
|
|
|
if b32.hi == hi {
|
|
|
|
if b32.del(lo) {
|
|
|
|
s.itemsCount--
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AppendTo appends all the items from the set to dst and returns the result.
|
|
|
|
//
|
|
|
|
// The returned items are sorted.
|
2019-11-08 12:16:40 +01:00
|
|
|
//
|
|
|
|
// AppendTo can mutate s.
|
2019-09-24 20:10:22 +02:00
|
|
|
func (s *Set) AppendTo(dst []uint64) []uint64 {
|
|
|
|
if s == nil {
|
|
|
|
return dst
|
|
|
|
}
|
2019-11-08 12:16:40 +01:00
|
|
|
|
2019-09-24 20:10:22 +02:00
|
|
|
// pre-allocate memory for dst
|
|
|
|
dstLen := len(dst)
|
|
|
|
if n := s.Len() - cap(dst) + dstLen; n > 0 {
|
|
|
|
dst = append(dst[:cap(dst)], make([]uint64, n)...)
|
|
|
|
dst = dst[:dstLen]
|
|
|
|
}
|
|
|
|
// sort s.buckets if it isn't sorted yet
|
|
|
|
if !sort.IsSorted(&s.buckets) {
|
|
|
|
sort.Sort(&s.buckets)
|
|
|
|
}
|
2020-01-03 17:16:37 +01:00
|
|
|
for i := range s.buckets {
|
|
|
|
dst = s.buckets[i].appendTo(dst)
|
2019-09-24 20:10:22 +02:00
|
|
|
}
|
|
|
|
return dst
|
|
|
|
}
|
|
|
|
|
2019-11-03 23:34:24 +01:00
|
|
|
// Union adds all the items from a to s.
|
|
|
|
func (s *Set) Union(a *Set) {
|
2019-11-08 12:16:40 +01:00
|
|
|
// Clone a, since AppendTo may mutate it below.
|
|
|
|
aCopy := a.Clone()
|
|
|
|
if s.Len() == 0 {
|
|
|
|
// Fast path if the initial set is empty.
|
|
|
|
*s = *aCopy
|
|
|
|
return
|
|
|
|
}
|
2019-11-03 23:34:24 +01:00
|
|
|
// TODO: optimize it
|
2019-11-08 12:16:40 +01:00
|
|
|
for _, x := range aCopy.AppendTo(nil) {
|
2019-11-03 23:34:24 +01:00
|
|
|
s.Add(x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-08 12:16:40 +01:00
|
|
|
// Intersect removes all the items missing in a from s.
|
|
|
|
func (s *Set) Intersect(a *Set) {
|
|
|
|
if a.Len() == 0 {
|
|
|
|
// Fast path
|
|
|
|
*s = Set{}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// TODO: optimize it
|
|
|
|
for _, x := range s.AppendTo(nil) {
|
|
|
|
if !a.Has(x) {
|
|
|
|
s.Del(x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Subtract removes from s all the shared items between s and a.
|
|
|
|
func (s *Set) Subtract(a *Set) {
|
|
|
|
if s.Len() == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Copy a because AppendTo below can mutate a.
|
|
|
|
aCopy := a.Clone()
|
|
|
|
// TODO: optimize it
|
|
|
|
for _, x := range aCopy.AppendTo(nil) {
|
2019-11-09 10:50:30 +01:00
|
|
|
s.Del(x)
|
2019-11-08 12:16:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Equal returns true if s contains the same items as a.
|
|
|
|
func (s *Set) Equal(a *Set) bool {
|
|
|
|
if s.Len() != a.Len() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// Copy a because AppendTo below can mutate a
|
|
|
|
aCopy := a.Clone()
|
|
|
|
// TODO: optimize it
|
|
|
|
for _, x := range aCopy.AppendTo(nil) {
|
|
|
|
if !s.Has(x) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-09-24 20:10:22 +02:00
|
|
|
type bucket32 struct {
|
2020-01-03 17:16:37 +01:00
|
|
|
hi uint32
|
|
|
|
b16his []uint16
|
|
|
|
buckets []bucket16
|
2019-09-24 20:10:22 +02:00
|
|
|
}
|
|
|
|
|
2019-11-13 12:11:02 +01:00
|
|
|
func (b *bucket32) sizeBytes() uint64 {
|
|
|
|
n := uint64(unsafe.Sizeof(*b))
|
|
|
|
n += 2 * uint64(len(b.b16his))
|
2020-01-03 17:16:37 +01:00
|
|
|
for i := range b.buckets {
|
|
|
|
b16 := &b.buckets[i]
|
|
|
|
n += uint64(unsafe.Sizeof(b16))
|
|
|
|
n += b16.sizeBytes()
|
2019-11-13 12:11:02 +01:00
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2020-01-03 17:16:37 +01:00
|
|
|
func (b *bucket32) copyTo(dst *bucket32) {
|
2019-09-24 20:10:22 +02:00
|
|
|
dst.hi = b.hi
|
|
|
|
dst.b16his = append(dst.b16his[:0], b.b16his...)
|
2020-01-03 17:16:37 +01:00
|
|
|
// Do not reuse dst.buckets, since it may be used in other places.
|
|
|
|
dst.buckets = nil
|
|
|
|
if len(b.buckets) > 0 {
|
|
|
|
dst.buckets = make([]bucket16, len(b.buckets))
|
|
|
|
for i := range b.buckets {
|
|
|
|
b.buckets[i].copyTo(&dst.buckets[i])
|
|
|
|
}
|
2019-09-24 20:10:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is for sort.Interface
|
|
|
|
func (b *bucket32) Len() int { return len(b.b16his) }
|
|
|
|
func (b *bucket32) Less(i, j int) bool { return b.b16his[i] < b.b16his[j] }
|
|
|
|
func (b *bucket32) Swap(i, j int) {
|
|
|
|
his := b.b16his
|
|
|
|
buckets := b.buckets
|
|
|
|
his[i], his[j] = his[j], his[i]
|
|
|
|
buckets[i], buckets[j] = buckets[j], buckets[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
const maxUnsortedBuckets = 32
|
|
|
|
|
|
|
|
func (b *bucket32) add(x uint32) bool {
|
|
|
|
hi := uint16(x >> 16)
|
|
|
|
lo := uint16(x)
|
|
|
|
if len(b.buckets) > maxUnsortedBuckets {
|
|
|
|
return b.addSlow(hi, lo)
|
|
|
|
}
|
|
|
|
for i, hi16 := range b.b16his {
|
|
|
|
if hi16 == hi {
|
|
|
|
return i < len(b.buckets) && b.buckets[i].add(lo)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b.addAllocSmall(hi, lo)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *bucket32) addAllocSmall(hi, lo uint16) {
|
|
|
|
b.b16his = append(b.b16his, hi)
|
2020-01-03 17:16:37 +01:00
|
|
|
b16 := b.addBucket16()
|
|
|
|
_ = b16.add(lo)
|
2019-09-24 20:10:22 +02:00
|
|
|
if len(b.buckets) > maxUnsortedBuckets {
|
|
|
|
sort.Sort(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-03 17:16:37 +01:00
|
|
|
func (b *bucket32) addBucket16() *bucket16 {
|
|
|
|
b.buckets = append(b.buckets, bucket16{})
|
|
|
|
return &b.buckets[len(b.buckets)-1]
|
|
|
|
}
|
|
|
|
|
2019-09-24 20:10:22 +02:00
|
|
|
func (b *bucket32) addSlow(hi, lo uint16) bool {
|
|
|
|
n := binarySearch16(b.b16his, hi)
|
|
|
|
if n < 0 || n >= len(b.b16his) || b.b16his[n] != hi {
|
|
|
|
b.addAllocBig(hi, lo, n)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return n < len(b.buckets) && b.buckets[n].add(lo)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *bucket32) addAllocBig(hi, lo uint16, n int) {
|
|
|
|
if n < 0 {
|
2019-11-08 12:16:40 +01:00
|
|
|
// This is a hint to Go compiler to remove automatic bounds checks below.
|
2019-09-24 20:10:22 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if n >= len(b.b16his) {
|
|
|
|
b.b16his = append(b.b16his, hi)
|
2020-01-03 17:16:37 +01:00
|
|
|
b16 := b.addBucket16()
|
|
|
|
_ = b16.add(lo)
|
2019-09-24 20:10:22 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
b.b16his = append(b.b16his[:n+1], b.b16his[n:]...)
|
|
|
|
b.b16his[n] = hi
|
|
|
|
b.buckets = append(b.buckets[:n+1], b.buckets[n:]...)
|
2020-01-03 17:16:37 +01:00
|
|
|
b16 := &b.buckets[n]
|
|
|
|
*b16 = bucket16{}
|
|
|
|
_ = b16.add(lo)
|
2019-09-24 20:10:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *bucket32) has(x uint32) bool {
|
|
|
|
hi := uint16(x >> 16)
|
|
|
|
lo := uint16(x)
|
|
|
|
if len(b.buckets) > maxUnsortedBuckets {
|
|
|
|
return b.hasSlow(hi, lo)
|
|
|
|
}
|
|
|
|
for i, hi16 := range b.b16his {
|
|
|
|
if hi16 == hi {
|
|
|
|
return i < len(b.buckets) && b.buckets[i].has(lo)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *bucket32) hasSlow(hi, lo uint16) bool {
|
|
|
|
n := binarySearch16(b.b16his, hi)
|
|
|
|
if n < 0 || n >= len(b.b16his) || b.b16his[n] != hi {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return n < len(b.buckets) && b.buckets[n].has(lo)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *bucket32) del(x uint32) bool {
|
|
|
|
hi := uint16(x >> 16)
|
|
|
|
lo := uint16(x)
|
|
|
|
if len(b.buckets) > maxUnsortedBuckets {
|
|
|
|
return b.delSlow(hi, lo)
|
|
|
|
}
|
|
|
|
for i, hi16 := range b.b16his {
|
|
|
|
if hi16 == hi {
|
|
|
|
return i < len(b.buckets) && b.buckets[i].del(lo)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *bucket32) delSlow(hi, lo uint16) bool {
|
|
|
|
n := binarySearch16(b.b16his, hi)
|
|
|
|
if n < 0 || n >= len(b.b16his) || b.b16his[n] != hi {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return n < len(b.buckets) && b.buckets[n].del(lo)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *bucket32) appendTo(dst []uint64) []uint64 {
|
|
|
|
if len(b.buckets) <= maxUnsortedBuckets && !sort.IsSorted(b) {
|
|
|
|
sort.Sort(b)
|
|
|
|
}
|
2020-01-03 17:16:37 +01:00
|
|
|
for i := range b.buckets {
|
2019-09-24 20:10:22 +02:00
|
|
|
hi16 := b.b16his[i]
|
2020-01-03 17:16:37 +01:00
|
|
|
dst = b.buckets[i].appendTo(dst, b.hi, hi16)
|
2019-09-24 20:10:22 +02:00
|
|
|
}
|
|
|
|
return dst
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
bitsPerBucket = 1 << 16
|
|
|
|
wordsPerBucket = bitsPerBucket / 64
|
|
|
|
)
|
|
|
|
|
|
|
|
type bucket16 struct {
|
2020-01-03 17:16:37 +01:00
|
|
|
bits *[wordsPerBucket]uint64
|
|
|
|
smallPoolLen int
|
|
|
|
smallPool [56]uint16
|
2019-09-24 20:10:22 +02:00
|
|
|
}
|
|
|
|
|
2019-11-13 12:11:02 +01:00
|
|
|
func (b *bucket16) sizeBytes() uint64 {
|
2020-01-03 17:16:37 +01:00
|
|
|
return uint64(unsafe.Sizeof(*b)) + uint64(unsafe.Sizeof(*b.bits))
|
2019-11-13 12:11:02 +01:00
|
|
|
}
|
|
|
|
|
2020-01-03 17:16:37 +01:00
|
|
|
func (b *bucket16) copyTo(dst *bucket16) {
|
|
|
|
// Do not reuse dst.bits, since it may be used in other places.
|
|
|
|
dst.bits = nil
|
|
|
|
if b.bits != nil {
|
|
|
|
bits := *b.bits
|
|
|
|
dst.bits = &bits
|
|
|
|
}
|
|
|
|
dst.smallPoolLen = b.smallPoolLen
|
|
|
|
dst.smallPool = b.smallPool
|
2019-09-24 20:10:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *bucket16) add(x uint16) bool {
|
2020-01-03 17:16:37 +01:00
|
|
|
if b.bits == nil {
|
|
|
|
return b.addToSmallPool(x)
|
|
|
|
}
|
2019-09-24 20:10:22 +02:00
|
|
|
wordNum, bitMask := getWordNumBitMask(x)
|
|
|
|
word := &b.bits[wordNum]
|
|
|
|
ok := *word&bitMask == 0
|
|
|
|
*word |= bitMask
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2020-01-03 17:16:37 +01:00
|
|
|
func (b *bucket16) addToSmallPool(x uint16) bool {
|
|
|
|
if b.hasInSmallPool(x) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if b.smallPoolLen < len(b.smallPool) {
|
|
|
|
b.smallPool[b.smallPoolLen] = x
|
|
|
|
b.smallPoolLen++
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
b.smallPoolLen = 0
|
|
|
|
var bits [wordsPerBucket]uint64
|
|
|
|
b.bits = &bits
|
|
|
|
for _, v := range b.smallPool[:] {
|
|
|
|
b.add(v)
|
|
|
|
}
|
|
|
|
b.add(x)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-09-24 20:10:22 +02:00
|
|
|
func (b *bucket16) has(x uint16) bool {
|
2020-01-03 17:16:37 +01:00
|
|
|
if b.bits == nil {
|
|
|
|
return b.hasInSmallPool(x)
|
|
|
|
}
|
2019-09-24 20:10:22 +02:00
|
|
|
wordNum, bitMask := getWordNumBitMask(x)
|
|
|
|
return b.bits[wordNum]&bitMask != 0
|
|
|
|
}
|
|
|
|
|
2020-01-03 17:16:37 +01:00
|
|
|
func (b *bucket16) hasInSmallPool(x uint16) bool {
|
|
|
|
for _, v := range b.smallPool[:b.smallPoolLen] {
|
|
|
|
if v == x {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-09-24 20:10:22 +02:00
|
|
|
func (b *bucket16) del(x uint16) bool {
|
2020-01-03 17:16:37 +01:00
|
|
|
if b.bits == nil {
|
|
|
|
return b.delFromSmallPool(x)
|
|
|
|
}
|
2019-09-24 20:10:22 +02:00
|
|
|
wordNum, bitMask := getWordNumBitMask(x)
|
|
|
|
word := &b.bits[wordNum]
|
|
|
|
ok := *word&bitMask != 0
|
|
|
|
*word &^= bitMask
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2020-01-03 17:16:37 +01:00
|
|
|
func (b *bucket16) delFromSmallPool(x uint16) bool {
|
|
|
|
for i, v := range b.smallPool[:b.smallPoolLen] {
|
|
|
|
if v == x {
|
|
|
|
copy(b.smallPool[i:], b.smallPool[i+1:])
|
|
|
|
b.smallPoolLen--
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-09-24 20:10:22 +02:00
|
|
|
func (b *bucket16) appendTo(dst []uint64, hi uint32, hi16 uint16) []uint64 {
|
|
|
|
hi64 := uint64(hi)<<32 | uint64(hi16)<<16
|
2020-01-03 17:16:37 +01:00
|
|
|
if b.bits == nil {
|
|
|
|
a := b.smallPool[:b.smallPoolLen]
|
|
|
|
if len(a) > 1 {
|
|
|
|
sort.Slice(a, func(i, j int) bool { return a[i] < a[j] })
|
|
|
|
}
|
|
|
|
for _, v := range a {
|
|
|
|
x := hi64 | uint64(v)
|
|
|
|
dst = append(dst, x)
|
|
|
|
}
|
|
|
|
return dst
|
|
|
|
}
|
2019-09-24 20:10:22 +02:00
|
|
|
var wordNum uint64
|
|
|
|
for _, word := range b.bits {
|
2019-09-24 23:34:09 +02:00
|
|
|
if word == 0 {
|
|
|
|
wordNum++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
x64 := hi64 | (wordNum * 64)
|
|
|
|
for {
|
|
|
|
tzn := uint64(bits.TrailingZeros64(word))
|
|
|
|
if tzn >= 64 {
|
|
|
|
break
|
2019-09-24 20:10:22 +02:00
|
|
|
}
|
2019-09-24 23:34:09 +02:00
|
|
|
word &^= uint64(1) << tzn
|
|
|
|
x := x64 | tzn
|
|
|
|
dst = append(dst, x)
|
2019-09-24 20:10:22 +02:00
|
|
|
}
|
|
|
|
wordNum++
|
|
|
|
}
|
|
|
|
return dst
|
|
|
|
}
|
|
|
|
|
|
|
|
func getWordNumBitMask(x uint16) (uint16, uint64) {
|
|
|
|
wordNum := x / 64
|
|
|
|
bitMask := uint64(1) << (x & 63)
|
|
|
|
return wordNum, bitMask
|
|
|
|
}
|
|
|
|
|
|
|
|
func binarySearch16(u16 []uint16, x uint16) int {
|
|
|
|
// The code has been adapted from sort.Search.
|
|
|
|
n := len(u16)
|
|
|
|
i, j := 0, n
|
|
|
|
for i < j {
|
|
|
|
h := int(uint(i+j) >> 1)
|
|
|
|
if h >= 0 && h < len(u16) && u16[h] < x {
|
|
|
|
i = h + 1
|
|
|
|
} else {
|
|
|
|
j = h
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|