-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathfast_int_set.go
471 lines (422 loc) · 11.3 KB
/
fast_int_set.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
// Copyright 2017 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
//go:build !fast_int_set_small && !fast_int_set_large
// +build !fast_int_set_small,!fast_int_set_large
package util
import (
"bytes"
"encoding/binary"
"io"
"math/bits"
"github.com/cockroachdb/errors"
"golang.org/x/tools/container/intsets"
)
// FastIntSet keeps track of a set of integers. It does not perform any
// allocations when the values are small. It is not thread-safe.
type FastIntSet struct {
small bitmap
large *intsets.Sparse
}
// We maintain a bitmap for small element values (specifically 0 to
// smallCutoff-1). When this bitmap is sufficient, we avoid allocating the
// `Sparse` set. Even when we have to allocate the `Sparse` set, we still
// maintain the bitmap as it can provide a fast path for certain operations.
// Note: this can be set to a smaller value, e.g. for testing.
const smallCutoff = 128
// bitmap implements a bitmap of size smallCutoff.
type bitmap struct {
// We don't use an array because that makes Go always keep the struct on the
// stack (see https://github.com/golang/go/issues/24416).
lo, hi uint64
}
// MakeFastIntSet returns a set initialized with the given values.
func MakeFastIntSet(vals ...int) FastIntSet {
var res FastIntSet
for _, v := range vals {
res.Add(v)
}
return res
}
func (s *FastIntSet) toLarge() *intsets.Sparse {
if s.large != nil {
return s.large
}
large := new(intsets.Sparse)
for i, ok := s.Next(0); ok; i, ok = s.Next(i + 1) {
large.Insert(i)
}
return large
}
// fitsInSmall returns whether all elements in this set are between 0 and
// smallCutoff.
func (s *FastIntSet) fitsInSmall() bool {
if s.large == nil {
return true
}
// It is possible that we have a large set allocated but all elements still
// fit the cutoff. This can happen if the set used to contain other elements.
return s.large.Min() >= 0 && s.large.Max() < smallCutoff
}
// Add adds a value to the set. No-op if the value is already in the set. If the
// large set is not nil and the value is within the range [0, 63], the value is
// added to both the large and small sets.
func (s *FastIntSet) Add(i int) {
withinSmallBounds := i >= 0 && i < smallCutoff
if withinSmallBounds {
s.small.Set(i)
}
if !withinSmallBounds && s.large == nil {
s.large = s.toLarge()
}
if s.large != nil {
s.large.Insert(i)
}
}
// AddRange adds values 'from' up to 'to' (inclusively) to the set.
// E.g. AddRange(1,5) adds the values 1, 2, 3, 4, 5 to the set.
// 'to' must be >= 'from'.
// AddRange is always more efficient than individual Adds.
func (s *FastIntSet) AddRange(from, to int) {
if to < from {
panic("invalid range when adding range to FastIntSet")
}
if s.large == nil && from >= 0 && to < smallCutoff {
s.small.SetRange(from, to)
} else {
for i := from; i <= to; i++ {
s.Add(i)
}
}
}
// Remove removes a value from the set. No-op if the value is not in the set.
func (s *FastIntSet) Remove(i int) {
if i >= 0 && i < smallCutoff {
s.small.Unset(i)
}
if s.large != nil {
s.large.Remove(i)
}
}
// Contains returns true if the set contains the value.
func (s FastIntSet) Contains(i int) bool {
if i >= 0 && i < smallCutoff {
return s.small.IsSet(i)
}
if s.large != nil {
return s.large.Has(i)
}
return false
}
// Empty returns true if the set is empty.
func (s FastIntSet) Empty() bool {
return s.small == bitmap{} && (s.large == nil || s.large.IsEmpty())
}
// Len returns the number of the elements in the set.
func (s FastIntSet) Len() int {
if s.large == nil {
return s.small.OnesCount()
}
return s.large.Len()
}
// Next returns the first value in the set which is >= startVal. If there is no
// value, the second return value is false.
func (s FastIntSet) Next(startVal int) (int, bool) {
if startVal < 0 && s.large == nil {
startVal = 0
}
if startVal >= 0 && startVal < smallCutoff {
if nextVal, ok := s.small.Next(startVal); ok {
return nextVal, true
}
}
if s.large != nil {
res := s.large.LowerBound(startVal)
return res, res != intsets.MaxInt
}
return intsets.MaxInt, false
}
// ForEach calls a function for each value in the set (in increasing order).
func (s FastIntSet) ForEach(f func(i int)) {
if !s.fitsInSmall() {
for x := s.large.Min(); x != intsets.MaxInt; x = s.large.LowerBound(x + 1) {
f(x)
}
return
}
for v := s.small.lo; v != 0; {
i := bits.TrailingZeros64(v)
f(i)
v &^= 1 << uint(i)
}
for v := s.small.hi; v != 0; {
i := bits.TrailingZeros64(v)
f(64 + i)
v &^= 1 << uint(i)
}
}
// Ordered returns a slice with all the integers in the set, in increasing order.
func (s FastIntSet) Ordered() []int {
if s.Empty() {
return nil
}
if s.large != nil {
return s.large.AppendTo([]int(nil))
}
result := make([]int, 0, s.Len())
s.ForEach(func(i int) {
result = append(result, i)
})
return result
}
// Copy returns a copy of s which can be modified independently.
func (s FastIntSet) Copy() FastIntSet {
var c FastIntSet
c.small = s.small
if s.large != nil {
c.large = new(intsets.Sparse)
c.large.Copy(s.large)
}
return c
}
// CopyFrom sets the receiver to a copy of other, which can then be modified
// independently.
func (s *FastIntSet) CopyFrom(other FastIntSet) {
s.small = other.small
if other.large != nil {
if s.large == nil {
s.large = new(intsets.Sparse)
}
s.large.Copy(other.large)
} else {
if s.large != nil {
s.large.Clear()
}
}
}
// UnionWith adds all the elements from rhs to this set.
func (s *FastIntSet) UnionWith(rhs FastIntSet) {
s.small.UnionWith(rhs.small)
if s.large == nil && rhs.large == nil {
// Fast path.
return
}
if s.large == nil {
s.large = s.toLarge()
}
if rhs.large == nil {
for i, ok := rhs.Next(0); ok; i, ok = rhs.Next(i + 1) {
s.large.Insert(i)
}
} else {
s.large.UnionWith(rhs.large)
}
}
// Union returns the union of s and rhs as a new set.
func (s FastIntSet) Union(rhs FastIntSet) FastIntSet {
r := s.Copy()
r.UnionWith(rhs)
return r
}
// IntersectionWith removes any elements not in rhs from this set.
func (s *FastIntSet) IntersectionWith(rhs FastIntSet) {
s.small.IntersectionWith(rhs.small)
if rhs.large == nil {
s.large = nil
}
if s.large == nil {
// Fast path.
return
}
s.large.IntersectionWith(rhs.toLarge())
}
// Intersection returns the intersection of s and rhs as a new set.
func (s FastIntSet) Intersection(rhs FastIntSet) FastIntSet {
r := s.Copy()
r.IntersectionWith(rhs)
return r
}
// Intersects returns true if s has any elements in common with rhs.
func (s FastIntSet) Intersects(rhs FastIntSet) bool {
if s.small.Intersects(rhs.small) {
return true
}
if s.large == nil || rhs.large == nil {
return false
}
return s.large.Intersects(rhs.toLarge())
}
// DifferenceWith removes any elements in rhs from this set.
func (s *FastIntSet) DifferenceWith(rhs FastIntSet) {
s.small.DifferenceWith(rhs.small)
if s.large == nil {
// Fast path
return
}
s.large.DifferenceWith(rhs.toLarge())
}
// Difference returns the elements of s that are not in rhs as a new set.
func (s FastIntSet) Difference(rhs FastIntSet) FastIntSet {
r := s.Copy()
r.DifferenceWith(rhs)
return r
}
// Equals returns true if the two sets are identical.
func (s FastIntSet) Equals(rhs FastIntSet) bool {
if s.small != rhs.small {
return false
}
if s.fitsInSmall() {
// We already know that the `small` fields are equal. We just have to make
// sure that the other set also has no large elements.
return rhs.fitsInSmall()
}
// We know that s has large elements.
return rhs.large != nil && s.large.Equals(rhs.large)
}
// SubsetOf returns true if rhs contains all the elements in s.
func (s FastIntSet) SubsetOf(rhs FastIntSet) bool {
if s.fitsInSmall() {
return s.small.SubsetOf(rhs.small)
}
if rhs.fitsInSmall() {
// s doesn't fit in small and rhs does.
return false
}
return s.large.SubsetOf(rhs.large)
}
// Encode the set and write it to a bytes.Buffer using binary.varint byte
// encoding.
//
// This method cannot be used if the set contains negative elements.
//
// If the set has only elements in the range [0, 63], we encode a 0 followed by
// a 64-bit bitmap. Otherwise, we encode a length followed by each element.
//
// WARNING: this is used by plan gists, so if this encoding changes,
// explain.gistVersion needs to be bumped.
func (s *FastIntSet) Encode(buf *bytes.Buffer) error {
if s.large != nil && s.large.Min() < 0 {
return errors.AssertionFailedf("Encode used with negative elements")
}
// This slice should stay on stack. We only need enough bytes to encode a 0
// and then an arbitrary 64-bit integer.
//gcassert:noescape
tmp := make([]byte, binary.MaxVarintLen64+1)
if s.small.hi == 0 && s.fitsInSmall() {
n := binary.PutUvarint(tmp, 0)
n += binary.PutUvarint(tmp[n:], s.small.lo)
buf.Write(tmp[:n])
} else {
n := binary.PutUvarint(tmp, uint64(s.Len()))
buf.Write(tmp[:n])
for i, ok := s.Next(0); ok; i, ok = s.Next(i + 1) {
n := binary.PutUvarint(tmp, uint64(i))
buf.Write(tmp[:n])
}
}
return nil
}
// Decode does the opposite of Encode. The contents of the receiver are
// overwritten.
func (s *FastIntSet) Decode(br io.ByteReader) error {
length, err := binary.ReadUvarint(br)
if err != nil {
return err
}
*s = FastIntSet{}
if length == 0 {
// Special case: a 64-bit bitmap is encoded directly.
val, err := binary.ReadUvarint(br)
if err != nil {
return err
}
s.small.lo = val
} else {
for i := 0; i < int(length); i++ {
elem, err := binary.ReadUvarint(br)
if err != nil {
*s = FastIntSet{}
return err
}
s.Add(int(elem))
}
}
return nil
}
func (v bitmap) IsSet(i int) bool {
w := v.lo
if i >= 64 {
w = v.hi
}
return w&(1<<uint64(i&63)) != 0
}
func (v *bitmap) Set(i int) {
if i < 64 {
v.lo |= (1 << uint64(i))
} else {
v.hi |= (1 << uint64(i&63))
}
}
func (v *bitmap) Unset(i int) {
if i < 64 {
v.lo &= ^(1 << uint64(i))
} else {
v.hi &= ^(1 << uint64(i&63))
}
}
func (v *bitmap) SetRange(from, to int) {
mask := func(from, to int) uint64 {
return (1<<(to-from+1) - 1) << from
}
switch {
case to < 64:
v.lo |= mask(from, to)
case from >= 64:
v.hi |= mask(from&63, to&63)
default:
v.lo |= mask(from, 63)
v.hi |= mask(0, to&63)
}
}
func (v *bitmap) UnionWith(other bitmap) {
v.lo |= other.lo
v.hi |= other.hi
}
func (v *bitmap) IntersectionWith(other bitmap) {
v.lo &= other.lo
v.hi &= other.hi
}
func (v bitmap) Intersects(other bitmap) bool {
return ((v.lo & other.lo) | (v.hi & other.hi)) != 0
}
func (v *bitmap) DifferenceWith(other bitmap) {
v.lo &^= other.lo
v.hi &^= other.hi
}
func (v bitmap) SubsetOf(other bitmap) bool {
return (v.lo&other.lo == v.lo) && (v.hi&other.hi == v.hi)
}
func (v bitmap) OnesCount() int {
return bits.OnesCount64(v.lo) + bits.OnesCount64(v.hi)
}
func (v bitmap) Next(startVal int) (nextVal int, ok bool) {
if startVal < 64 {
if ntz := bits.TrailingZeros64(v.lo >> uint64(startVal)); ntz < 64 {
// Found next element in the low word.
return startVal + ntz, true
}
startVal = 64
}
// Check high word.
if ntz := bits.TrailingZeros64(v.hi >> uint64(startVal&63)); ntz < 64 {
return startVal + ntz, true
}
return -1, false
}