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"
|
2020-01-15 11:12:46 +01:00
|
|
|
"sync"
|
2020-03-25 14:14:10 +01:00
|
|
|
"sync/atomic"
|
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
|
2020-03-25 14:14:10 +01:00
|
|
|
|
|
|
|
// Most likely the buckets contains only a single item, so put it here for performance reasons
|
|
|
|
// in order to improve memory locality.
|
|
|
|
scratchBuckets [1]bucket32
|
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 {
|
2020-01-17 15:11:46 +01:00
|
|
|
if s == nil || s.itemsCount == 0 {
|
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-03-25 14:14:10 +01:00
|
|
|
if len(s.buckets) == 1 {
|
|
|
|
dst.buckets = dst.scratchBuckets[:]
|
|
|
|
} else {
|
|
|
|
dst.buckets = make([]bucket32, len(s.buckets))
|
|
|
|
}
|
2020-01-17 15:11:46 +01:00
|
|
|
for i := range s.buckets {
|
|
|
|
s.buckets[i].copyTo(&dst.buckets[i])
|
2019-09-24 20:10:22 +02:00
|
|
|
}
|
|
|
|
return &dst
|
|
|
|
}
|
|
|
|
|
2020-01-18 12:43:01 +01:00
|
|
|
func (s *Set) fixItemsCount() {
|
|
|
|
n := 0
|
|
|
|
for i := range s.buckets {
|
|
|
|
n += s.buckets[i].getLen()
|
|
|
|
}
|
|
|
|
s.itemsCount = n
|
|
|
|
}
|
|
|
|
|
2020-01-17 15:11:46 +01:00
|
|
|
func (s *Set) cloneShallow() *Set {
|
|
|
|
var dst Set
|
|
|
|
dst.itemsCount = s.itemsCount
|
2020-03-25 14:14:10 +01:00
|
|
|
if len(s.buckets) == 1 {
|
|
|
|
dst.buckets = dst.scratchBuckets[:]
|
|
|
|
}
|
2020-01-17 15:11:46 +01:00
|
|
|
dst.buckets = append(dst.buckets[:0], s.buckets...)
|
|
|
|
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 {
|
2021-05-25 13:13:36 +02:00
|
|
|
n += s.buckets[i].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) {
|
2020-03-25 14:14:10 +01:00
|
|
|
hi32 := uint32(x >> 32)
|
|
|
|
lo32 := uint32(x)
|
|
|
|
bs := s.buckets
|
|
|
|
if len(bs) > 0 && bs[0].hi == hi32 {
|
|
|
|
// Manually inline bucket32.add for performance reasons.
|
|
|
|
hi16 := uint16(lo32 >> 16)
|
|
|
|
lo16 := uint16(lo32)
|
|
|
|
b32 := &bs[0]
|
|
|
|
his := b32.b16his
|
|
|
|
if n := b32.getHint(); n < uint32(len(his)) && his[n] == hi16 {
|
|
|
|
bs := b32.buckets
|
|
|
|
if n < uint32(len(bs)) && bs[n].add(lo16) {
|
|
|
|
s.itemsCount++
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if b32.addSlow(hi16, lo16) {
|
|
|
|
s.itemsCount++
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for i := range bs {
|
|
|
|
b32 := &bs[i]
|
|
|
|
if b32.hi == hi32 {
|
|
|
|
if b32.add(lo32) {
|
2019-09-24 20:10:22 +02:00
|
|
|
s.itemsCount++
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-01-03 17:16:37 +01:00
|
|
|
b32 := s.addBucket32()
|
2020-07-21 19:56:49 +02:00
|
|
|
b32.hi = hi32
|
|
|
|
_ = b32.add(lo32)
|
2019-09-24 20:10:22 +02:00
|
|
|
s.itemsCount++
|
2020-01-03 17:16:37 +01:00
|
|
|
}
|
|
|
|
|
2020-07-21 19:56:49 +02:00
|
|
|
// AddMulti adds all the items from a to s.
|
|
|
|
//
|
|
|
|
// It is usually faster than calling s.Add() for each item in a.
|
|
|
|
//
|
|
|
|
// The caller is responsible for splitting a into items with clustered values.
|
|
|
|
func (s *Set) AddMulti(a []uint64) {
|
|
|
|
if len(a) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2021-03-15 16:10:40 +01:00
|
|
|
hiPrev := uint32(a[0] >> 32)
|
|
|
|
i := 0
|
|
|
|
for j, x := range a {
|
|
|
|
hi := uint32(x >> 32)
|
|
|
|
if hi == hiPrev {
|
|
|
|
continue
|
2020-07-21 19:56:49 +02:00
|
|
|
}
|
2021-03-15 16:10:40 +01:00
|
|
|
b32 := s.getOrCreateBucket32(hiPrev)
|
|
|
|
s.itemsCount += b32.addMulti(a[i:j])
|
|
|
|
hiPrev = hi
|
|
|
|
i = j
|
2020-07-21 19:56:49 +02:00
|
|
|
}
|
2021-03-15 16:10:40 +01:00
|
|
|
b32 := s.getOrCreateBucket32(hiPrev)
|
|
|
|
s.itemsCount += b32.addMulti(a[i:])
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Set) getOrCreateBucket32(hi uint32) *bucket32 {
|
2020-07-21 19:56:49 +02:00
|
|
|
bs := s.buckets
|
|
|
|
for i := range bs {
|
|
|
|
if bs[i].hi == hi {
|
2021-03-15 16:10:40 +01:00
|
|
|
return &bs[i]
|
2020-07-21 19:56:49 +02:00
|
|
|
}
|
|
|
|
}
|
2021-03-15 16:10:40 +01:00
|
|
|
b32 := s.addBucket32()
|
|
|
|
b32.hi = hi
|
|
|
|
return b32
|
2020-07-21 19:56:49 +02:00
|
|
|
}
|
|
|
|
|
2020-01-03 17:16:37 +01:00
|
|
|
func (s *Set) addBucket32() *bucket32 {
|
2020-03-25 14:14:10 +01:00
|
|
|
if len(s.buckets) == 0 {
|
|
|
|
s.buckets = s.scratchBuckets[:]
|
|
|
|
} else {
|
|
|
|
s.buckets = append(s.buckets, bucket32{})
|
|
|
|
}
|
2020-01-03 17:16:37 +01:00
|
|
|
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
|
|
|
|
}
|
2020-03-25 14:14:10 +01:00
|
|
|
hi32 := uint32(x >> 32)
|
|
|
|
lo32 := uint32(x)
|
|
|
|
bs := s.buckets
|
|
|
|
for i := range bs {
|
|
|
|
b32 := &bs[i]
|
|
|
|
if b32.hi == hi32 {
|
|
|
|
return b32.has(lo32)
|
2019-09-24 20:10:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Del deletes x from s.
|
|
|
|
func (s *Set) Del(x uint64) {
|
|
|
|
hi := uint32(x >> 32)
|
|
|
|
lo := uint32(x)
|
2020-03-25 14:14:10 +01:00
|
|
|
bs := s.buckets
|
|
|
|
if len(bs) > 0 && bs[0].hi == hi {
|
|
|
|
if bs[0].del(lo) {
|
|
|
|
s.itemsCount--
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for i := range bs {
|
|
|
|
b32 := &bs[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]
|
|
|
|
}
|
2020-01-17 15:11:46 +01:00
|
|
|
s.sort()
|
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
|
|
|
|
}
|
|
|
|
|
2020-01-17 15:11:46 +01:00
|
|
|
func (s *Set) sort() {
|
|
|
|
// sort s.buckets if it isn't sorted yet
|
|
|
|
if !sort.IsSorted(&s.buckets) {
|
|
|
|
sort.Sort(&s.buckets)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-03 23:34:24 +01:00
|
|
|
// Union adds all the items from a to s.
|
|
|
|
func (s *Set) Union(a *Set) {
|
2021-07-06 17:21:35 +02:00
|
|
|
s.union(a, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnionMayOwn adds all the items from a to s.
|
|
|
|
//
|
|
|
|
// It may own a if s is empty. This means that `a` cannot be used
|
|
|
|
// after the call to UnionMayOwn.
|
|
|
|
func (s *Set) UnionMayOwn(a *Set) {
|
|
|
|
s.union(a, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Set) union(a *Set, mayOwn bool) {
|
2020-01-17 21:10:36 +01:00
|
|
|
if a.Len() == 0 {
|
|
|
|
// Fast path - nothing to union.
|
|
|
|
return
|
|
|
|
}
|
2019-11-08 12:16:40 +01:00
|
|
|
if s.Len() == 0 {
|
2020-01-18 12:43:01 +01:00
|
|
|
// Fast path - copy `a` to `s`.
|
2021-07-06 17:21:35 +02:00
|
|
|
if !mayOwn {
|
|
|
|
a = a.Clone()
|
|
|
|
}
|
2020-01-18 12:43:01 +01:00
|
|
|
*s = *a
|
2019-11-08 12:16:40 +01:00
|
|
|
return
|
|
|
|
}
|
2020-01-18 12:43:01 +01:00
|
|
|
// Make shallow copy of `a`, since it can be modified by a.sort().
|
2021-07-06 17:21:35 +02:00
|
|
|
if !mayOwn {
|
|
|
|
a = a.cloneShallow()
|
|
|
|
}
|
2020-01-18 12:43:01 +01:00
|
|
|
a.sort()
|
|
|
|
s.sort()
|
|
|
|
i := 0
|
|
|
|
j := 0
|
2020-03-27 00:15:51 +01:00
|
|
|
sBucketsLen := len(s.buckets)
|
2020-01-18 12:43:01 +01:00
|
|
|
for {
|
2020-03-27 00:15:51 +01:00
|
|
|
for i < sBucketsLen && j < len(a.buckets) && s.buckets[i].hi < a.buckets[j].hi {
|
2020-01-18 12:43:01 +01:00
|
|
|
i++
|
2020-01-15 11:12:46 +01:00
|
|
|
}
|
2020-03-27 00:15:51 +01:00
|
|
|
if i >= sBucketsLen {
|
2020-01-18 12:43:01 +01:00
|
|
|
for j < len(a.buckets) {
|
|
|
|
b32 := s.addBucket32()
|
|
|
|
a.buckets[j].copyTo(b32)
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2020-03-25 14:14:10 +01:00
|
|
|
for j < len(a.buckets) && a.buckets[j].hi < s.buckets[i].hi {
|
2020-01-18 12:43:01 +01:00
|
|
|
b32 := s.addBucket32()
|
|
|
|
a.buckets[j].copyTo(b32)
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
if j >= len(a.buckets) {
|
|
|
|
break
|
|
|
|
}
|
2020-03-25 14:14:10 +01:00
|
|
|
if s.buckets[i].hi == a.buckets[j].hi {
|
2021-07-06 17:21:35 +02:00
|
|
|
s.buckets[i].union(&a.buckets[j], mayOwn)
|
2020-01-18 12:43:01 +01:00
|
|
|
i++
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.fixItemsCount()
|
2019-11-03 23:34:24 +01:00
|
|
|
}
|
|
|
|
|
2019-11-08 12:16:40 +01:00
|
|
|
// Intersect removes all the items missing in a from s.
|
|
|
|
func (s *Set) Intersect(a *Set) {
|
2020-01-15 11:12:46 +01:00
|
|
|
if s.Len() == 0 || a.Len() == 0 {
|
|
|
|
// Fast path - the result is empty.
|
2019-11-08 12:16:40 +01:00
|
|
|
*s = Set{}
|
|
|
|
return
|
|
|
|
}
|
2020-01-18 12:43:01 +01:00
|
|
|
// Make shallow copy of `a`, since it can be modified by a.sort().
|
2020-01-17 15:11:46 +01:00
|
|
|
a = a.cloneShallow()
|
|
|
|
a.sort()
|
|
|
|
s.sort()
|
|
|
|
i := 0
|
|
|
|
j := 0
|
|
|
|
for {
|
2020-01-17 17:10:58 +01:00
|
|
|
for i < len(s.buckets) && j < len(a.buckets) && s.buckets[i].hi < a.buckets[j].hi {
|
2020-01-17 15:11:46 +01:00
|
|
|
s.buckets[i] = bucket32{}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
if i >= len(s.buckets) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
for j < len(a.buckets) && a.buckets[j].hi < s.buckets[i].hi {
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
if j >= len(a.buckets) {
|
|
|
|
for i < len(s.buckets) {
|
|
|
|
s.buckets[i] = bucket32{}
|
|
|
|
i++
|
2020-01-15 11:12:46 +01:00
|
|
|
}
|
2020-01-17 15:11:46 +01:00
|
|
|
break
|
2019-11-08 12:16:40 +01:00
|
|
|
}
|
2020-01-17 15:11:46 +01:00
|
|
|
if s.buckets[i].hi == a.buckets[j].hi {
|
2020-01-18 12:43:01 +01:00
|
|
|
s.buckets[i].intersect(&a.buckets[j])
|
2020-01-17 15:11:46 +01:00
|
|
|
i++
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
}
|
2020-01-18 12:43:01 +01:00
|
|
|
s.fixItemsCount()
|
2019-11-08 12:16:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Subtract removes from s all the shared items between s and a.
|
|
|
|
func (s *Set) Subtract(a *Set) {
|
2020-01-15 11:12:46 +01:00
|
|
|
if s.Len() == 0 || a.Len() == 0 {
|
|
|
|
// Fast path - nothing to subtract.
|
2019-11-08 12:16:40 +01:00
|
|
|
return
|
|
|
|
}
|
2020-01-15 11:12:46 +01:00
|
|
|
a.ForEach(func(part []uint64) bool {
|
|
|
|
for _, x := range part {
|
|
|
|
s.Del(x)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
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
|
|
|
|
}
|
2020-01-15 11:12:46 +01:00
|
|
|
equal := true
|
|
|
|
a.ForEach(func(part []uint64) bool {
|
|
|
|
for _, x := range part {
|
|
|
|
if !s.Has(x) {
|
|
|
|
equal = false
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
return equal
|
|
|
|
}
|
|
|
|
|
|
|
|
// ForEach calls f for all the items stored in s.
|
|
|
|
//
|
|
|
|
// Each call to f contains part with arbitrary part of items stored in the set.
|
|
|
|
// The iteration is stopped if f returns false.
|
|
|
|
func (s *Set) ForEach(f func(part []uint64) bool) {
|
|
|
|
if s == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for i := range s.buckets {
|
|
|
|
if !s.buckets[i].forEach(f) {
|
|
|
|
return
|
2019-11-08 12:16:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-24 20:10:22 +02:00
|
|
|
type bucket32 struct {
|
2020-03-25 14:14:10 +01:00
|
|
|
hi uint32
|
2020-01-17 15:11:46 +01:00
|
|
|
|
2020-03-25 14:14:10 +01:00
|
|
|
// hint may contain bucket index for the last successful operation.
|
2020-01-17 15:11:46 +01:00
|
|
|
// This allows saving CPU time on subsequent calls to the same bucket.
|
2020-03-25 14:14:10 +01:00
|
|
|
hint uint32
|
2020-01-17 15:11:46 +01:00
|
|
|
|
2020-03-25 14:14:10 +01:00
|
|
|
// b16his contains high 16 bits for each bucket in buckets.
|
|
|
|
//
|
|
|
|
// It is always sorted.
|
|
|
|
b16his []uint16
|
|
|
|
|
|
|
|
// buckets are sorted by b16his
|
2021-05-25 13:13:36 +02:00
|
|
|
buckets []*bucket16
|
2020-01-17 15:11:46 +01:00
|
|
|
}
|
|
|
|
|
2020-01-18 12:43:01 +01:00
|
|
|
func (b *bucket32) getLen() int {
|
|
|
|
n := 0
|
|
|
|
for i := range b.buckets {
|
|
|
|
n += b.buckets[i].getLen()
|
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2021-07-06 17:21:35 +02:00
|
|
|
func (b *bucket32) union(a *bucket32, mayOwn bool) {
|
2020-01-18 12:43:01 +01:00
|
|
|
i := 0
|
|
|
|
j := 0
|
2020-03-27 00:15:51 +01:00
|
|
|
bBucketsLen := len(b.buckets)
|
2020-01-18 12:43:01 +01:00
|
|
|
for {
|
2020-03-27 00:15:51 +01:00
|
|
|
for i < bBucketsLen && j < len(a.b16his) && b.b16his[i] < a.b16his[j] {
|
2020-01-18 12:43:01 +01:00
|
|
|
i++
|
|
|
|
}
|
2020-03-27 00:15:51 +01:00
|
|
|
if i >= bBucketsLen {
|
2020-01-18 12:43:01 +01:00
|
|
|
for j < len(a.b16his) {
|
2020-03-25 14:14:10 +01:00
|
|
|
b16 := b.addBucket16(a.b16his[j])
|
2021-07-06 17:21:35 +02:00
|
|
|
if mayOwn {
|
|
|
|
*b16 = *a.buckets[j]
|
|
|
|
} else {
|
|
|
|
a.buckets[j].copyTo(b16)
|
|
|
|
}
|
2020-01-18 12:43:01 +01:00
|
|
|
j++
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2020-03-25 14:14:10 +01:00
|
|
|
for j < len(a.b16his) && a.b16his[j] < b.b16his[i] {
|
|
|
|
b16 := b.addBucket16(a.b16his[j])
|
2021-07-06 17:21:35 +02:00
|
|
|
if mayOwn {
|
|
|
|
*b16 = *a.buckets[j]
|
|
|
|
} else {
|
|
|
|
a.buckets[j].copyTo(b16)
|
|
|
|
}
|
2020-01-18 12:43:01 +01:00
|
|
|
j++
|
|
|
|
}
|
|
|
|
if j >= len(a.b16his) {
|
|
|
|
break
|
|
|
|
}
|
2020-03-25 14:14:10 +01:00
|
|
|
if b.b16his[i] == a.b16his[j] {
|
2021-05-25 13:13:36 +02:00
|
|
|
b.buckets[i].union(a.buckets[j])
|
2020-01-18 12:43:01 +01:00
|
|
|
i++
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
}
|
2020-03-25 14:14:10 +01:00
|
|
|
// Restore buckets order, which could be violated during the merge above.
|
|
|
|
if !sort.IsSorted(b) {
|
|
|
|
sort.Sort(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is for sort.Interface used in bucket32.union
|
|
|
|
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]
|
2020-01-18 12:43:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *bucket32) intersect(a *bucket32) {
|
2020-01-17 15:11:46 +01:00
|
|
|
i := 0
|
|
|
|
j := 0
|
|
|
|
for {
|
|
|
|
for i < len(b.b16his) && j < len(a.b16his) && b.b16his[i] < a.b16his[j] {
|
2021-05-25 13:13:36 +02:00
|
|
|
*b.buckets[i] = bucket16{}
|
2020-01-17 15:11:46 +01:00
|
|
|
i++
|
|
|
|
}
|
|
|
|
if i >= len(b.b16his) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
for j < len(a.b16his) && a.b16his[j] < b.b16his[i] {
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
if j >= len(a.b16his) {
|
|
|
|
for i < len(b.b16his) {
|
2021-05-25 13:13:36 +02:00
|
|
|
*b.buckets[i] = bucket16{}
|
2020-01-17 15:11:46 +01:00
|
|
|
i++
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if b.b16his[i] == a.b16his[j] {
|
2021-05-25 13:13:36 +02:00
|
|
|
b.buckets[i].intersect(a.buckets[j])
|
2020-01-17 15:11:46 +01:00
|
|
|
i++
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
}
|
2020-03-27 00:15:51 +01:00
|
|
|
// Remove zero buckets
|
|
|
|
b16his := b.b16his[:0]
|
|
|
|
bs := b.buckets[:0]
|
2021-05-25 13:13:36 +02:00
|
|
|
for i, b16 := range b.buckets {
|
|
|
|
if b16.isZero() {
|
2020-03-27 00:15:51 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
b16his = append(b16his, b.b16his[i])
|
2021-05-25 13:13:36 +02:00
|
|
|
bs = append(bs, b16)
|
2020-03-27 00:15:51 +01:00
|
|
|
}
|
|
|
|
for i := len(bs); i < len(b.buckets); i++ {
|
2021-05-25 13:13:36 +02:00
|
|
|
b.buckets[i] = nil
|
2020-03-27 00:15:51 +01:00
|
|
|
}
|
|
|
|
b.hint = 0
|
|
|
|
b.b16his = b16his
|
|
|
|
b.buckets = bs
|
2019-09-24 20:10:22 +02:00
|
|
|
}
|
|
|
|
|
2020-01-15 11:12:46 +01:00
|
|
|
func (b *bucket32) forEach(f func(part []uint64) bool) bool {
|
|
|
|
xbuf := partBufPool.Get().(*[]uint64)
|
|
|
|
buf := *xbuf
|
2021-05-25 13:13:36 +02:00
|
|
|
for i, b16 := range b.buckets {
|
2020-01-15 11:12:46 +01:00
|
|
|
hi16 := b.b16his[i]
|
2021-05-25 13:13:36 +02:00
|
|
|
buf = b16.appendTo(buf[:0], b.hi, hi16)
|
2020-01-15 11:12:46 +01:00
|
|
|
if !f(buf) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*xbuf = buf
|
|
|
|
partBufPool.Put(xbuf)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
var partBufPool = &sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
buf := make([]uint64, 0, bitsPerBucket)
|
|
|
|
return &buf
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-11-13 12:11:02 +01:00
|
|
|
func (b *bucket32) sizeBytes() uint64 {
|
|
|
|
n := uint64(unsafe.Sizeof(*b))
|
|
|
|
n += 2 * uint64(len(b.b16his))
|
2021-05-25 13:13:36 +02:00
|
|
|
for _, b16 := range b.buckets {
|
2020-01-03 17:16:37 +01:00
|
|
|
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 {
|
2021-05-25 13:13:36 +02:00
|
|
|
dst.buckets = make([]*bucket16, len(b.buckets))
|
|
|
|
for i, b16 := range b.buckets {
|
2021-07-06 17:21:35 +02:00
|
|
|
b16Dst := &bucket16{}
|
2021-05-25 13:13:36 +02:00
|
|
|
b16.copyTo(b16Dst)
|
|
|
|
dst.buckets[i] = b16Dst
|
2020-01-03 17:16:37 +01:00
|
|
|
}
|
2019-09-24 20:10:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-25 14:14:10 +01:00
|
|
|
func (b *bucket32) getHint() uint32 {
|
|
|
|
return atomic.LoadUint32(&b.hint)
|
2019-09-24 20:10:22 +02:00
|
|
|
}
|
|
|
|
|
2020-03-25 14:14:10 +01:00
|
|
|
func (b *bucket32) setHint(n int) {
|
|
|
|
atomic.StoreUint32(&b.hint, uint32(n))
|
|
|
|
}
|
2019-09-24 20:10:22 +02:00
|
|
|
|
|
|
|
func (b *bucket32) add(x uint32) bool {
|
|
|
|
hi := uint16(x >> 16)
|
|
|
|
lo := uint16(x)
|
2020-03-25 14:14:10 +01:00
|
|
|
his := b.b16his
|
|
|
|
if n := b.getHint(); n < uint32(len(his)) && his[n] == hi {
|
2020-01-17 15:11:46 +01:00
|
|
|
// Fast path - add to the previously used bucket.
|
2020-03-25 14:14:10 +01:00
|
|
|
bs := b.buckets
|
|
|
|
return n < uint32(len(bs)) && bs[n].add(lo)
|
2020-01-17 15:11:46 +01:00
|
|
|
}
|
|
|
|
return b.addSlow(hi, lo)
|
|
|
|
}
|
|
|
|
|
2020-07-21 19:56:49 +02:00
|
|
|
func (b *bucket32) addMulti(a []uint64) int {
|
|
|
|
if len(a) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
2021-03-15 16:10:40 +01:00
|
|
|
count := 0
|
|
|
|
hiPrev := uint16(a[0] >> 16)
|
|
|
|
i := 0
|
|
|
|
for j, x := range a {
|
|
|
|
hi := uint16(x >> 16)
|
|
|
|
if hi == hiPrev {
|
|
|
|
continue
|
2020-07-21 19:56:49 +02:00
|
|
|
}
|
2021-03-15 16:10:40 +01:00
|
|
|
b16 := b.getOrCreateBucket16(hiPrev)
|
|
|
|
count += b16.addMulti(a[i:j])
|
|
|
|
hiPrev = hi
|
|
|
|
i = j
|
2020-07-21 19:56:49 +02:00
|
|
|
}
|
2021-03-15 16:10:40 +01:00
|
|
|
b16 := b.getOrCreateBucket16(hiPrev)
|
|
|
|
count += b16.addMulti(a[i:])
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *bucket32) getOrCreateBucket16(hi uint16) *bucket16 {
|
2020-07-21 19:56:49 +02:00
|
|
|
his := b.b16his
|
|
|
|
bs := b.buckets
|
2021-03-15 16:10:40 +01:00
|
|
|
n := binarySearch16(his, hi)
|
|
|
|
if n < 0 || n >= len(his) || his[n] != hi {
|
|
|
|
return b.addBucketAtPos(hi, n)
|
2020-07-21 19:56:49 +02:00
|
|
|
}
|
2021-05-25 13:13:36 +02:00
|
|
|
return bs[n]
|
2020-07-21 19:56:49 +02:00
|
|
|
}
|
|
|
|
|
2020-01-17 15:11:46 +01:00
|
|
|
func (b *bucket32) addSlow(hi, lo uint16) bool {
|
2020-03-25 14:14:10 +01:00
|
|
|
his := b.b16his
|
|
|
|
n := binarySearch16(his, hi)
|
|
|
|
if n < 0 || n >= len(his) || his[n] != hi {
|
2020-07-21 19:56:49 +02:00
|
|
|
b16 := b.addBucketAtPos(hi, n)
|
|
|
|
b16.add(lo)
|
2020-03-25 14:14:10 +01:00
|
|
|
return true
|
2019-09-24 20:10:22 +02:00
|
|
|
}
|
2020-03-25 14:14:10 +01:00
|
|
|
b.setHint(n)
|
|
|
|
bs := b.buckets
|
|
|
|
return n < len(bs) && bs[n].add(lo)
|
2019-09-24 20:10:22 +02:00
|
|
|
}
|
|
|
|
|
2020-03-25 14:14:10 +01:00
|
|
|
func (b *bucket32) addBucket16(hi uint16) *bucket16 {
|
2019-09-24 20:10:22 +02:00
|
|
|
b.b16his = append(b.b16his, hi)
|
2021-07-06 17:21:35 +02:00
|
|
|
b.buckets = append(b.buckets, &bucket16{})
|
2021-05-25 13:13:36 +02:00
|
|
|
return b.buckets[len(b.buckets)-1]
|
2020-01-03 17:16:37 +01:00
|
|
|
}
|
|
|
|
|
2020-07-21 19:56:49 +02:00
|
|
|
func (b *bucket32) addBucketAtPos(hi uint16, pos int) *bucket16 {
|
|
|
|
if pos < 0 {
|
2019-11-08 12:16:40 +01:00
|
|
|
// This is a hint to Go compiler to remove automatic bounds checks below.
|
2020-07-21 19:56:49 +02:00
|
|
|
return nil
|
2019-09-24 20:10:22 +02:00
|
|
|
}
|
2020-07-21 19:56:49 +02:00
|
|
|
if pos >= len(b.b16his) {
|
|
|
|
return b.addBucket16(hi)
|
2019-09-24 20:10:22 +02:00
|
|
|
}
|
2020-07-21 19:56:49 +02:00
|
|
|
b.b16his = append(b.b16his[:pos+1], b.b16his[pos:]...)
|
|
|
|
b.b16his[pos] = hi
|
|
|
|
b.buckets = append(b.buckets[:pos+1], b.buckets[pos:]...)
|
2021-07-06 17:21:35 +02:00
|
|
|
b16 := &bucket16{}
|
2021-05-25 13:13:36 +02:00
|
|
|
b.buckets[pos] = b16
|
2020-07-21 19:56:49 +02:00
|
|
|
return b16
|
2019-09-24 20:10:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *bucket32) has(x uint32) bool {
|
|
|
|
hi := uint16(x >> 16)
|
|
|
|
lo := uint16(x)
|
2020-03-25 14:14:10 +01:00
|
|
|
his := b.b16his
|
|
|
|
n := binarySearch16(his, hi)
|
|
|
|
if n < 0 || n >= len(his) || his[n] != hi {
|
2019-09-24 20:10:22 +02:00
|
|
|
return false
|
|
|
|
}
|
2021-03-29 11:32:27 +02:00
|
|
|
// Do not call b.setHint(n) here, since this may trash performance
|
|
|
|
// when many concurrent goroutines call b.has() method from many CPU cores.
|
2020-03-25 14:14:10 +01:00
|
|
|
bs := b.buckets
|
|
|
|
return n < len(bs) && bs[n].has(lo)
|
2019-09-24 20:10:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *bucket32) del(x uint32) bool {
|
|
|
|
hi := uint16(x >> 16)
|
|
|
|
lo := uint16(x)
|
2020-03-25 14:14:10 +01:00
|
|
|
his := b.b16his
|
|
|
|
if n := b.getHint(); n < uint32(len(his)) && his[n] == hi {
|
2020-01-17 15:11:46 +01:00
|
|
|
// Fast path - use the bucket from the previous operation.
|
2020-03-25 14:14:10 +01:00
|
|
|
bs := b.buckets
|
|
|
|
return n < uint32(len(bs)) && bs[n].del(lo)
|
2020-01-17 15:11:46 +01:00
|
|
|
}
|
|
|
|
return b.delSlow(hi, lo)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *bucket32) delSlow(hi, lo uint16) bool {
|
2020-03-25 14:14:10 +01:00
|
|
|
his := b.b16his
|
|
|
|
n := binarySearch16(his, hi)
|
|
|
|
if n < 0 || n >= len(his) || his[n] != hi {
|
|
|
|
return false
|
2019-09-24 20:10:22 +02:00
|
|
|
}
|
2020-03-25 14:14:10 +01:00
|
|
|
b.setHint(n)
|
|
|
|
bs := b.buckets
|
|
|
|
return n < len(bs) && bs[n].del(lo)
|
2019-09-24 20:10:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *bucket32) appendTo(dst []uint64) []uint64 {
|
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
|
2021-04-14 11:27:40 +02:00
|
|
|
smallPool [smallPoolSize]uint16
|
2020-01-03 17:16:37 +01:00
|
|
|
smallPoolLen int
|
2019-09-24 20:10:22 +02:00
|
|
|
}
|
|
|
|
|
2021-02-16 14:31:38 +01:00
|
|
|
const smallPoolSize = 56
|
|
|
|
|
2020-03-27 00:15:51 +01:00
|
|
|
func (b *bucket16) isZero() bool {
|
|
|
|
return b.bits == nil && b.smallPoolLen == 0
|
|
|
|
}
|
|
|
|
|
2020-01-18 12:43:01 +01:00
|
|
|
func (b *bucket16) getLen() int {
|
|
|
|
if b.bits == nil {
|
|
|
|
return b.smallPoolLen
|
|
|
|
}
|
|
|
|
n := 0
|
|
|
|
for _, x := range b.bits {
|
|
|
|
if x > 0 {
|
|
|
|
n += bits.OnesCount64(x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *bucket16) union(a *bucket16) {
|
|
|
|
if a.bits != nil && b.bits != nil {
|
|
|
|
// Fast path - use bitwise ops.
|
2020-03-25 14:14:10 +01:00
|
|
|
ab := a.bits
|
|
|
|
bb := b.bits
|
|
|
|
_ = bb[len(ab)-1]
|
|
|
|
for i, ax := range ab {
|
|
|
|
bx := bb[i]
|
2020-01-18 12:43:01 +01:00
|
|
|
bx |= ax
|
2020-03-25 14:14:10 +01:00
|
|
|
bb[i] = bx
|
2020-01-18 12:43:01 +01:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Slow path
|
|
|
|
xbuf := partBufPool.Get().(*[]uint64)
|
|
|
|
buf := *xbuf
|
|
|
|
buf = a.appendTo(buf[:0], 0, 0)
|
|
|
|
for _, x := range buf {
|
|
|
|
x16 := uint16(x)
|
|
|
|
b.add(x16)
|
|
|
|
}
|
|
|
|
*xbuf = buf
|
|
|
|
partBufPool.Put(xbuf)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *bucket16) intersect(a *bucket16) {
|
2020-01-17 15:11:46 +01:00
|
|
|
if a.bits != nil && b.bits != nil {
|
|
|
|
// Fast path - use bitwise ops
|
2020-03-25 14:14:10 +01:00
|
|
|
ab := a.bits
|
|
|
|
bb := b.bits
|
|
|
|
_ = bb[len(ab)-1]
|
|
|
|
for i, ax := range ab {
|
|
|
|
bx := bb[i]
|
2020-01-17 15:11:46 +01:00
|
|
|
bx &= ax
|
2020-03-25 14:14:10 +01:00
|
|
|
bb[i] = bx
|
2020-01-17 15:11:46 +01:00
|
|
|
}
|
2020-01-18 12:43:01 +01:00
|
|
|
return
|
2020-01-17 15:11:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Slow path
|
|
|
|
xbuf := partBufPool.Get().(*[]uint64)
|
|
|
|
buf := *xbuf
|
|
|
|
buf = b.appendTo(buf[:0], 0, 0)
|
|
|
|
for _, x := range buf {
|
|
|
|
x16 := uint16(x)
|
|
|
|
if !a.has(x16) {
|
|
|
|
b.del(x16)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*xbuf = buf
|
|
|
|
partBufPool.Put(xbuf)
|
|
|
|
}
|
|
|
|
|
2019-11-13 12:11:02 +01:00
|
|
|
func (b *bucket16) sizeBytes() uint64 {
|
2021-03-15 16:23:29 +01:00
|
|
|
n := unsafe.Sizeof(*b)
|
|
|
|
if b.bits != nil {
|
|
|
|
n += unsafe.Sizeof(*b.bits)
|
|
|
|
}
|
|
|
|
return uint64(n)
|
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
|
|
|
|
}
|
2021-04-14 11:27:40 +02:00
|
|
|
dst.smallPool = b.smallPool
|
2020-01-03 17:16:37 +01:00
|
|
|
dst.smallPoolLen = b.smallPoolLen
|
2019-09-24 20:10:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *bucket16) add(x uint16) bool {
|
2021-03-15 15:57:51 +01:00
|
|
|
bits := b.bits
|
|
|
|
if bits == nil {
|
2020-01-03 17:16:37 +01:00
|
|
|
return b.addToSmallPool(x)
|
|
|
|
}
|
2019-09-24 20:10:22 +02:00
|
|
|
wordNum, bitMask := getWordNumBitMask(x)
|
2021-03-15 15:57:51 +01:00
|
|
|
ok := bits[wordNum]&bitMask == 0
|
|
|
|
if ok {
|
|
|
|
bits[wordNum] |= bitMask
|
|
|
|
}
|
2019-09-24 20:10:22 +02:00
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2020-07-21 19:56:49 +02:00
|
|
|
func (b *bucket16) addMulti(a []uint64) int {
|
2021-03-16 20:08:19 +01:00
|
|
|
if b.bits == nil {
|
2024-02-15 13:30:08 +01:00
|
|
|
if b.smallPoolLen + len(a) > len(b.smallPool) {
|
|
|
|
b.switchSmallPoolToBits()
|
|
|
|
goto fastPath
|
|
|
|
}
|
2020-07-21 19:56:49 +02:00
|
|
|
// Slow path
|
2024-02-15 13:30:08 +01:00
|
|
|
count := 0
|
|
|
|
for _, x := range a {
|
|
|
|
if b.addToSmallPool(uint16(x)) {
|
2020-07-21 19:56:49 +02:00
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
2021-03-16 20:08:19 +01:00
|
|
|
return count
|
|
|
|
}
|
2024-02-15 13:30:08 +01:00
|
|
|
|
2021-03-16 20:08:19 +01:00
|
|
|
fastPath:
|
2024-02-15 13:30:08 +01:00
|
|
|
count := 0
|
2021-03-16 20:08:19 +01:00
|
|
|
bits := b.bits
|
|
|
|
for _, x := range a {
|
|
|
|
wordNum, bitMask := getWordNumBitMask(uint16(x))
|
|
|
|
if bits[wordNum]&bitMask == 0 {
|
|
|
|
bits[wordNum] |= bitMask
|
|
|
|
count++
|
|
|
|
}
|
2020-07-21 19:56:49 +02:00
|
|
|
}
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
2020-01-03 17:16:37 +01:00
|
|
|
func (b *bucket16) addToSmallPool(x uint16) bool {
|
|
|
|
if b.hasInSmallPool(x) {
|
|
|
|
return false
|
|
|
|
}
|
2021-04-14 11:27:40 +02:00
|
|
|
sp := b.smallPool[:]
|
2021-03-15 16:23:29 +01:00
|
|
|
if b.smallPoolLen < len(sp) {
|
|
|
|
sp[b.smallPoolLen] = x
|
2020-01-03 17:16:37 +01:00
|
|
|
b.smallPoolLen++
|
|
|
|
return true
|
|
|
|
}
|
2024-02-15 13:30:08 +01:00
|
|
|
b.switchSmallPoolToBits()
|
|
|
|
b.add(x)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *bucket16) switchSmallPoolToBits() {
|
|
|
|
smallPoolLen := b.smallPoolLen
|
2020-01-03 17:16:37 +01:00
|
|
|
b.smallPoolLen = 0
|
|
|
|
var bits [wordsPerBucket]uint64
|
|
|
|
b.bits = &bits
|
2024-02-15 13:30:08 +01:00
|
|
|
for _, v := range b.smallPool[:smallPoolLen] {
|
2020-01-03 17:16:37 +01:00
|
|
|
b.add(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2021-04-14 11:27:40 +02:00
|
|
|
for _, v := range b.smallPool[:b.smallPoolLen] {
|
2020-01-03 17:16:37 +01:00
|
|
|
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 {
|
2021-04-14 11:27:40 +02:00
|
|
|
sp := b.smallPool[:]
|
2021-03-15 16:23:29 +01:00
|
|
|
for i, v := range sp[:b.smallPoolLen] {
|
2020-01-03 17:16:37 +01:00
|
|
|
if v == x {
|
2021-03-15 16:23:29 +01:00
|
|
|
copy(sp[i:], sp[i+1:])
|
2020-01-03 17:16:37 +01:00
|
|
|
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 {
|
2021-02-16 14:31:38 +01:00
|
|
|
// Use smallPoolSorter instead of sort.Slice here in order to reduce memory allocations.
|
|
|
|
sps := smallPoolSorterPool.Get().(*smallPoolSorter)
|
2021-04-14 11:27:40 +02:00
|
|
|
// Sort a copy of b.smallPool, since b must be readonly in order to prevent from data races
|
2020-11-07 11:44:15 +01:00
|
|
|
// when b.appendTo is called from concurrent goroutines.
|
2021-04-14 11:27:40 +02:00
|
|
|
sps.smallPool = b.smallPool
|
2021-02-16 14:31:38 +01:00
|
|
|
sps.a = sps.smallPool[:b.smallPoolLen]
|
|
|
|
if len(sps.a) > 1 && !sort.IsSorted(sps) {
|
|
|
|
sort.Sort(sps)
|
2020-01-03 17:16:37 +01:00
|
|
|
}
|
2021-02-16 14:31:38 +01:00
|
|
|
for _, v := range sps.a {
|
2020-01-03 17:16:37 +01:00
|
|
|
x := hi64 | uint64(v)
|
|
|
|
dst = append(dst, x)
|
|
|
|
}
|
2021-02-16 14:31:38 +01:00
|
|
|
smallPoolSorterPool.Put(sps)
|
2020-01-03 17:16:37 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-02-16 14:31:38 +01:00
|
|
|
var smallPoolSorterPool = &sync.Pool{
|
2020-01-17 21:32:30 +01:00
|
|
|
New: func() interface{} {
|
2021-02-16 14:31:38 +01:00
|
|
|
return &smallPoolSorter{}
|
2020-01-17 21:32:30 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-02-16 14:31:38 +01:00
|
|
|
type smallPoolSorter struct {
|
|
|
|
smallPool [smallPoolSize]uint16
|
|
|
|
a []uint16
|
|
|
|
}
|
2020-01-17 21:32:30 +01:00
|
|
|
|
2021-02-16 14:31:38 +01:00
|
|
|
func (sps *smallPoolSorter) Len() int { return len(sps.a) }
|
|
|
|
func (sps *smallPoolSorter) Less(i, j int) bool {
|
|
|
|
a := sps.a
|
|
|
|
return a[i] < a[j]
|
2020-01-17 21:32:30 +01:00
|
|
|
}
|
2021-02-16 14:31:38 +01:00
|
|
|
func (sps *smallPoolSorter) Swap(i, j int) {
|
|
|
|
a := sps.a
|
|
|
|
a[i], a[j] = a[j], a[i]
|
2020-01-17 21:32:30 +01:00
|
|
|
}
|
|
|
|
|
2019-09-24 20:10:22 +02:00
|
|
|
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)
|
2021-03-15 16:23:29 +01:00
|
|
|
if n > 0 && u16[n-1] < x {
|
|
|
|
// Fast path for values scanned in ascending order.
|
|
|
|
return n
|
|
|
|
}
|
2019-09-24 20:10:22 +02:00
|
|
|
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
|
|
|
|
}
|