/
b64set.go
279 lines (244 loc) · 6.19 KB
/
b64set.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
// Copyright (C) 2017 ScyllaDB
// Use of this source code is governed by a ALv2-style
// license that can be found at https://github.com/scylladb/go-set/LICENSE.
package b64set
import (
"fmt"
"math"
"strings"
)
var (
// helpful to not write everywhere struct{}{}
keyExists = struct{}{}
nonExistent [64]byte
)
// Set is the main set structure that holds all the data
// and methods used to working with the set.
type Set struct {
m map[[64]byte]struct{}
}
// New creates and initializes a new Set.
func New(ts ...[64]byte) *Set {
s := NewWithSize(len(ts))
s.Add(ts...)
return s
}
// NewWithSize creates a new Set and gives make map a size hint.
func NewWithSize(size int) *Set {
return &Set{make(map[[64]byte]struct{}, size)}
}
// Add includes the specified items (one or more) to the Set. The underlying
// Set s is modified. If passed nothing it silently returns.
func (s *Set) Add(items ...[64]byte) {
for _, item := range items {
s.m[item] = keyExists
}
}
// Remove deletes the specified items from the Set. The underlying Set s is
// modified. If passed nothing it silently returns.
func (s *Set) Remove(items ...[64]byte) {
for _, item := range items {
delete(s.m, item)
}
}
// Pop deletes and returns an item from the Set. The underlying Set s is
// modified. If Set is empty, the zero value is returned.
func (s *Set) Pop() [64]byte {
for item := range s.m {
delete(s.m, item)
return item
}
return nonExistent
}
// Pop2 tries to delete and return an item from the Set. The underlying Set s
// is modified. The second value is a bool that is true if the item existed in
// the set, and false if not. If Set is empty, the zero value and false are
// returned.
func (s *Set) Pop2() ([64]byte, bool) {
for item := range s.m {
delete(s.m, item)
return item, true
}
return nonExistent, false
}
// Has looks for the existence of items passed. It returns false if nothing is
// passed. For multiple items it returns true only if all of the items exist.
func (s *Set) Has(items ...[64]byte) bool {
has := false
for _, item := range items {
if _, has = s.m[item]; !has {
break
}
}
return has
}
// HasAny looks for the existence of any of the items passed.
// It returns false if nothing is passed.
// For multiple items it returns true if any of the items exist.
func (s *Set) HasAny(items ...[64]byte) bool {
has := false
for _, item := range items {
if _, has = s.m[item]; has {
break
}
}
return has
}
// Size returns the number of items in a Set.
func (s *Set) Size() int {
return len(s.m)
}
// Clear removes all items from the Set.
func (s *Set) Clear() {
s.m = make(map[[64]byte]struct{})
}
// IsEmpty reports whether the Set is empty.
func (s *Set) IsEmpty() bool {
return s.Size() == 0
}
// IsEqual test whether s and t are the same in size and have the same items.
func (s *Set) IsEqual(t *Set) bool {
// return false if they are no the same size
if s.Size() != t.Size() {
return false
}
equal := true
t.Each(func(item [64]byte) bool {
_, equal = s.m[item]
return equal // if false, Each() will end
})
return equal
}
// IsSubset tests whether t is a subset of s.
func (s *Set) IsSubset(t *Set) bool {
if s.Size() < t.Size() {
return false
}
subset := true
t.Each(func(item [64]byte) bool {
_, subset = s.m[item]
return subset
})
return subset
}
// IsSuperset tests whether t is a superset of s.
func (s *Set) IsSuperset(t *Set) bool {
return t.IsSubset(s)
}
// Each traverses the items in the Set, calling the provided function for each
// Set member. Traversal will continue until all items in the Set have been
// visited, or if the closure returns false.
func (s *Set) Each(f func(item [64]byte) bool) {
for item := range s.m {
if !f(item) {
break
}
}
}
// Copy returns a new Set with a copy of s.
func (s *Set) Copy() *Set {
u := NewWithSize(s.Size())
for item := range s.m {
u.m[item] = keyExists
}
return u
}
// String returns a string representation of s
func (s *Set) String() string {
v := make([]string, 0, s.Size())
for item := range s.m {
v = append(v, fmt.Sprintf("%v", item))
}
return fmt.Sprintf("[%s]", strings.Join(v, ", "))
}
// List returns a slice of all items. There is also StringSlice() and
// IntSlice() methods for returning slices of type string or int.
func (s *Set) List() [][64]byte {
v := make([][64]byte, 0, s.Size())
for item := range s.m {
v = append(v, item)
}
return v
}
// Merge is like Union, however it modifies the current Set it's applied on
// with the given t Set.
func (s *Set) Merge(t *Set) {
for item := range t.m {
s.m[item] = keyExists
}
}
// Separate removes the Set items containing in t from Set s. Please aware that
// it's not the opposite of Merge.
func (s *Set) Separate(t *Set) {
for item := range t.m {
delete(s.m, item)
}
}
// Union is the merger of multiple sets. It returns a new set with all the
// elements present in all the sets that are passed.
func Union(sets ...*Set) *Set {
maxPos := -1
maxSize := 0
for i, set := range sets {
if l := set.Size(); l > maxSize {
maxSize = l
maxPos = i
}
}
if maxSize == 0 {
return New()
}
u := sets[maxPos].Copy()
for i, set := range sets {
if i == maxPos {
continue
}
for item := range set.m {
u.m[item] = keyExists
}
}
return u
}
// Difference returns a new set which contains items which are in in the first
// set but not in the others.
func Difference(set1 *Set, sets ...*Set) *Set {
s := set1.Copy()
for _, set := range sets {
s.Separate(set)
}
return s
}
// Intersection returns a new set which contains items that only exist in all
// given sets.
func Intersection(sets ...*Set) *Set {
minPos := -1
minSize := math.MaxInt64
for i, set := range sets {
if l := set.Size(); l < minSize {
minSize = l
minPos = i
}
}
if minSize == math.MaxInt64 || minSize == 0 {
return New()
}
t := sets[minPos].Copy()
for i, set := range sets {
if i == minPos {
continue
}
for item := range t.m {
if _, has := set.m[item]; !has {
delete(t.m, item)
}
}
}
return t
}
// SymmetricDifference returns a new set which s is the difference of items
// which are in one of either, but not in both.
func SymmetricDifference(s *Set, t *Set) *Set {
u := Difference(s, t)
v := Difference(t, s)
return Union(u, v)
}