-
Notifications
You must be signed in to change notification settings - Fork 3
/
set.go
387 lines (346 loc) · 10.7 KB
/
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
package frozen
import (
"fmt"
"math/bits"
"github.com/arr-ai/hash"
)
// Set holds a set of values. The zero value is the empty Set.
type Set struct {
root *node
count int
}
var _ Key = Set{}
// Iterator provides for iterating over a Set.
type Iterator interface {
Next() bool
Value() interface{}
}
// NewSet creates a new Set with values as elements.
func NewSet(values ...interface{}) Set {
var b SetBuilder
for _, value := range values {
b.Add(value)
}
return b.Finish()
}
// NewSetFromStrings creates a new Set with values as elements.
func NewSetFromStrings(values ...string) Set {
var b SetBuilder
for _, value := range values {
b.Add(value)
}
return b.Finish()
}
// IsEmpty returns true iff the Set has no elements.
func (s Set) IsEmpty() bool {
return s.root == nil
}
// Count returns the number of elements in the Set.
func (s Set) Count() int {
return s.count
}
// Range returns an Iterator over the Set.
func (s Set) Range() Iterator {
return s.root.iterator(s.count)
}
func (s Set) Elements() []interface{} {
result := make([]interface{}, 0, s.Count())
for i := s.Range(); i.Next(); {
result = append(result, i.Value())
}
return result
}
// OrderedElements takes elements in a defined order.
func (s Set) OrderedElements(less Less) []interface{} {
result := make([]interface{}, 0, s.Count())
for i := s.OrderedRange(less); i.Next(); {
result = append(result, i.Value())
}
return result
}
// Any returns an arbitrary element from the Set.
func (s Set) Any() interface{} {
for i := s.Range(); i.Next(); {
return i.Value()
}
panic("Set.Any(): empty set")
}
// AnyN returns a set of N arbitrary elements from the Set.
func (s Set) AnyN(n int) Set {
count := 0
var setBuilder SetBuilder
for i := s.Range(); i.Next() && count < n; count++ {
setBuilder.Add(i.Value())
}
return setBuilder.Finish()
}
// OrderedFirstN returns a list of elements in a defined order.
func (s Set) OrderedFirstN(n int, less Less) []interface{} {
result := make([]interface{}, 0, n)
currentLength := 0
for i := s.root.orderedIterator(less, n); i.Next() && currentLength < n; currentLength++ {
result = append(result, i.Value())
}
return result
}
// First returns the first element in a defined order.
func (s Set) First(less Less) interface{} {
for _, i := range s.OrderedFirstN(1, less) {
return i
}
panic("Set.First(): empty set")
}
// FirstN returns a set of the first n elements in a defined order.
func (s Set) FirstN(n int, less Less) Set {
return NewSet(s.OrderedFirstN(n, less)...)
}
// String returns a string representation of the Set.
func (s Set) String() string {
return fmt.Sprintf("%v", s)
}
// Format writes a string representation of the Set into state.
func (s Set) Format(state fmt.State, _ rune) {
state.Write([]byte("{"))
for i, n := s.Range(), 0; i.Next(); n++ {
if n > 0 {
state.Write([]byte(", "))
}
fmt.Fprintf(state, "%v", i.Value())
}
state.Write([]byte("}"))
}
// OrderedRange returns a SetIterator for the Set that iterates over the elements in
// a specified order.
func (s Set) OrderedRange(less Less) Iterator {
return s.root.orderedIterator(less, s.Count())
}
// Hash computes a hash value for s.
func (s Set) Hash(seed uintptr) uintptr {
h := hash.Uintptr(uintptr(10538386443025343807&uint64(^uintptr(0))), seed)
for i := s.Range(); i.Next(); {
h ^= hash.Interface(i.Value(), seed)
}
return h
}
// Equal implements Equatable.
func (s Set) Equal(t interface{}) bool {
if set, ok := t.(Set); ok {
return s.EqualSet(set)
}
return false
}
// EqualSet returns true iff s and set have all the same elements.
func (s Set) EqualSet(t Set) bool {
if s.root == nil || t.root == nil {
return s.root == nil && t.root == nil
}
c := newCloner(false, s.Count())
equalAsync := c.noneFalse()
equal := s.root.equal(t.root, Equal, 0, c)
return equal && equalAsync()
}
// IsSubsetOf returns true iff no element in s is not in t.
func (s Set) IsSubsetOf(t Set) bool {
c := newCloner(false, s.Count())
isSubsetAsync := c.noneFalse()
c.update(s.root.isSubsetOf(t.root, 0, c))
return isSubsetAsync()
}
// Has returns the value associated with key and true iff the key was found.
func (s Set) Has(val interface{}) bool {
return s.root.get(val) != nil
}
// With returns a new Set retaining all the elements of the Set as well as values.
func (s Set) With(values ...interface{}) Set {
return s.Union(NewSet(values...))
}
// Without returns a new Set with all values retained from Set except values.
func (s Set) Without(values ...interface{}) Set {
return s.Difference(NewSet(values...))
}
// Where returns a Set with all elements that are in s and satisfy pred.
func (s Set) Where(pred func(elem interface{}) bool) Set {
c := newCloner(false, s.Count())
matches := 0
matchesAsync := c.counter()
var root *node
s.root.where(pred, 0, &matches, c, &root)
matches += matchesAsync()
return Set{root: root, count: matches}
}
// Map returns a Set with all the results of applying f to all elements in s.
func (s Set) Map(f func(elem interface{}) interface{}) Set {
sbs := []*SetBuilder{}
var spawn func() *foreacher
spawn = func() *foreacher {
var sb SetBuilder
sbs = append(sbs, &sb)
return &foreacher{
f: func(elem interface{}) { sb.Add(f(elem)) },
spawn: spawn,
}
}
c := newCloner(false, s.Count())
s.root.foreach(spawn(), 0, c)
c.wait()
sets := make([]Set, 0, len(sbs))
for _, sb := range sbs {
sets = append(sets, sb.Finish())
}
return Union(sets...)
}
// Reduce returns the result of applying `reduce` to the elements of `s` or
// `nil` if `s.IsEmpty()`. The result of each call is used as the acc argument
// for the next element.
//
// The `reduce` function must have the following properties:
//
// - commutative: `reduce(a, b, c) == reduce(c, a, b)`
// - associative: `reduce(reduce(a, b), c) == reduce(a, reduce(b, c))`
//
// By implication, `reduce` must accept its own output as input.
//
// 'elems` will never be empty.
func (s Set) Reduce(reduce func(elems ...interface{}) interface{}) interface{} {
if s.Count() == 0 {
return nil
}
pointers := make([]*[]interface{}, 0, bits.Len(uint(s.Count()))>>15)
var spawn func() *forbatcher
spawn = func() *forbatcher {
var value []interface{}
pointers = append(pointers, &value)
return &forbatcher{
f: func(elems ...interface{}) {
value = append(value, reduce(elems...))
},
spawn: spawn,
}
}
c := newCloner(false, s.Count())
s.root.forbatches(spawn(), 0, c)
c.wait()
values := make([]interface{}, 0, len(pointers))
// In case there are no elements above the parallelisation waterline.
if *pointers[0] != nil {
values = append(values, reduce(*pointers[0]...))
}
for _, p := range pointers[1:] {
values = append(values, reduce(*p...))
}
return reduce(values...)
}
// Reduce2 is a convenience wrapper for `Reduce`, allowing the caller to
// implement a simpler, albeit less efficient, binary `reduce` function instead
// of an n-adic one.
func (s Set) Reduce2(reduce func(a, b interface{}) interface{}) interface{} {
return s.Reduce(func(elems ...interface{}) interface{} {
acc := elems[0]
for _, elem := range elems[1:] {
acc = reduce(acc, elem)
}
return acc
})
}
// Intersection returns a Set with all elements that are in both s and t.
func (s Set) Intersection(t Set) Set {
if s.Count() > t.Count() {
s, t = t, s
}
c := newCloner(false, (s.Count()+t.Count())/2)
countAsync := c.counter()
count := 0
var root *node
s.root.intersection(t.root, 0, &count, c, &root)
count += countAsync()
return Set{root: root, count: count}
}
// Union returns a Set with all elements that are in either s or t.
func (s Set) Union(t Set) Set {
c := newCloner(false, s.Count()+t.Count())
matchesAsync := c.counter()
matches := 0
root := s.root.union(t.root, useRHS, 0, &matches, c)
matches += matchesAsync()
return Set{root: root, count: s.Count() + t.Count() - matches}
}
// Difference returns a Set with all elements that are s but not in t.
func (s Set) Difference(t Set) Set {
c := newCloner(false, s.Count())
matchesAsync := c.counter()
matches := 0
var root *node
s.root.difference(t.root, 0, &matches, c, &root)
matches += matchesAsync()
return Set{root: root, count: s.Count() - matches}
}
// SymmetricDifference returns a Set with all elements that are s or t, but not
// both.
func (s Set) SymmetricDifference(t Set) Set {
return s.Difference(t).Union(t.Difference(s))
}
func (s Set) Powerset() Set {
n := s.Count()
if n > 63 {
panic("set too large")
}
elems := s.Elements()
subset := Set{}
result := NewSet(subset)
for i := BitIterator(1); i < 1<<uint(n); i++ {
// Use a special counting order that flips a single bit at at time. The
// bit to flip is the same as the lowest-order 1-bit in the normal
// counting order, denoted by `(1)`. The flipped bit's new value is the
// complement of the bit to the left of the `(1)`, denoted by `^-` and
// `^1`.
//
// ---------- plain ---------- | ------- highlighted -------
// normal 1-bit flips | normal 1-bit flips
// ------------ ----------- | ------------ -----------
// - - - - - - - - | - - - - - - - -
// - - - 1 - - - 1 | - - ^- (1) - - - [1]
// - - 1 - - - 1 1 | - ^- (1) - - - [1] 1
// - - 1 1 - - 1 - | - - ^1 (1) - - 1 [-]
// - 1 - - - 1 1 - | ^- (1) - - - [1] 1 -
// - 1 - 1 - 1 1 1 | - 1 ^- (1) - 1 1 [1]
// - 1 1 - - 1 - 1 | - ^1 (1) - - 1 [-] 1
// - 1 1 1 - 1 - - | - 1 ^1 (1) - 1 - [-]
// 1 - - - 1 1 - - | (1) - - - [1] 1 - -
// 1 - - 1 1 1 - 1 | 1 - ^- (1) 1 1 - [1]
// 1 - 1 - 1 1 1 1 | 1 ^- (1) - 1 1 [1] 1
// 1 - 1 1 1 1 1 - | 1 - ^1 (1) 1 1 1 [-]
// 1 1 - - 1 - 1 - | ^1 (1) - - 1 [-] 1 -
// 1 1 - 1 1 - 1 1 | 1 1 ^- (1) 1 - 1 [1]
// 1 1 1 - 1 - - 1 | 1 ^1 (1) - 1 - [-] 1
// 1 1 1 1 1 - - - | 1 1 ^1 (1) 1 - - [-]
//
if flip := i.Index(); i.Has(flip + 1) {
subset = subset.Without(elems[flip])
} else {
subset = subset.With(elems[flip])
}
result = result.With(subset)
}
return result
}
// GroupBy returns a Map that groups elements in the Set by their key.
func (s Set) GroupBy(key func(el interface{}) interface{}) Map {
var builders MapBuilder
for i := s.Range(); i.Next(); {
v := i.Value()
k := key(v)
var b *SetBuilder
if builder, has := builders.Get(k); has {
b = builder.(*SetBuilder)
} else {
b = &SetBuilder{}
builders.Put(k, b)
}
b.Add(v)
}
var result MapBuilder
for i := builders.Finish().Range(); i.Next(); {
result.Put(i.Key(), i.Value().(*SetBuilder).Finish())
}
return result.Finish()
}