forked from hashicorp/go-set
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset.go
346 lines (308 loc) · 8.48 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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
// Package set provides a basic generic set implementation.
//
// https://en.wikipedia.org/wiki/Set_(mathematics)
package set
import (
"fmt"
"sort"
)
type nothing struct{}
var sentinel = nothing{}
// New creates a new Set with initial underlying capacity of size.
//
// A Set will automatically grow or shrink its capacity as items are added or
// removed.
//
// T may be any comparable type. Keep in mind that pointer types or structs
// containing pointer fields will be compared using shallow equality. For deep
// equality use HashSet instead.
func New[T comparable](size int) *Set[T] {
return &Set[T]{
items: make(map[T]nothing, max(0, size)),
}
}
// From creates a new Set containing each item in items.
//
// T may be any comparable type. Keep in mind that pointer types or structs
// containing pointer fields will be compared using shallow equality. For deep
// equality use HashSet instead.
func From[T comparable](items []T) *Set[T] {
s := New[T](len(items))
s.InsertSlice(items)
return s
}
// FromFunc creates a new Set containing a conversion of each item in items.
//
// T may be any comparable type. Keep in mind that pointer types or structs
// containing pointer fields will be compared using shallow equality. For deep
// equality use HashSet instead.
func FromFunc[A any, T comparable](items []A, conversion func(A) T) *Set[T] {
s := New[T](len(items))
for _, item := range items {
s.Insert(conversion(item))
}
return s
}
// Set is a simple, generic implementation of the set mathematical data structure.
// It is optimized for correctness and convenience, as a replacement for the use
// of map[interface{}]struct{}.
type Set[T comparable] struct {
items map[T]nothing
}
// Insert item into s.
//
// Return true if s was modified (item was not already in s), false otherwise.
func (s *Set[T]) Insert(item T) bool {
if _, exists := s.items[item]; exists {
return false
}
s.items[item] = sentinel
return true
}
// InsertAll will insert each item in items into s.
//
// Return true if s was modified (at least one item was not already in s), false otherwise.
//
// Deprecated: use InsertSlice instead.
func (s *Set[T]) InsertAll(items []T) bool {
return s.InsertSlice(items)
}
// InsertSlice will insert each item in items into s.
//
// Return true if s was modified (at least one item was not already in s), false otherwise.
func (s *Set[T]) InsertSlice(items []T) bool {
modified := false
for _, item := range items {
if s.Insert(item) {
modified = true
}
}
return modified
}
// InsertSet will insert each element of o into s.
//
// Return true if s was modified (at least one item of o was not already in s), false otherwise.
func (s *Set[T]) InsertSet(o *Set[T]) bool {
modified := false
for item := range o.items {
if s.Insert(item) {
modified = true
}
}
return modified
}
// Remove will remove item from s.
//
// Return true if s was modified (item was present), false otherwise.
func (s *Set[T]) Remove(item T) bool {
if _, exists := s.items[item]; !exists {
return false
}
delete(s.items, item)
return true
}
// RemoveAll will remove each item in items from s.
//
// Return true if s was modified (any item was present), false otherwise.
//
// Deprecated: use RemoveSlice instead.
func (s *Set[T]) RemoveAll(items []T) bool {
return s.RemoveSlice(items)
}
// RemoveSlice will remove each item in items from s.
//
// Return true if s was modified (any item was present), false otherwise.
func (s *Set[T]) RemoveSlice(items []T) bool {
modified := false
for _, item := range items {
if s.Remove(item) {
modified = true
}
}
return modified
}
// RemoveSet will remove each element of o from s.
//
// Return true if s was modified (any item of o was present in s), false otherwise.
func (s *Set[T]) RemoveSet(o *Set[T]) bool {
modified := false
for item := range o.items {
if s.Remove(item) {
modified = true
}
}
return modified
}
// RemoveFunc will remove each element from s that satisfies condition f.
//
// Return true if s was modified, false otherwise.
func (s *Set[T]) RemoveFunc(f func(T) bool) bool {
modified := false
for item := range s.items {
if applies := f(item); applies {
s.Remove(item)
modified = true
}
}
return modified
}
// Contains returns whether item is present in s.
func (s *Set[T]) Contains(item T) bool {
_, exists := s.items[item]
return exists
}
// ContainsAll returns whether s contains at least every item in items.
func (s *Set[T]) ContainsAll(items []T) bool {
if len(s.items) < len(items) {
return false
}
for _, item := range items {
if !s.Contains(item) {
return false
}
}
return true
}
// ContainsSlice returns whether s contains the same set of of elements
// that are in items. The elements of items may contain duplicates.
//
// If the slice is known to be set-like (no duplicates), EqualSlice provides
// a more efficient implementation.
func (s *Set[T]) ContainsSlice(items []T) bool {
return s.Equal(From(items))
}
// Subset returns whether o is a subset of s.
func (s *Set[T]) Subset(o *Set[T]) bool {
if len(s.items) < len(o.items) {
return false
}
for item := range o.items {
if !s.Contains(item) {
return false
}
}
return true
}
// Size returns the cardinality of s.
func (s *Set[T]) Size() int {
return len(s.items)
}
// Empty returns true if s contains no elements, false otherwise.
func (s *Set[T]) Empty() bool {
return s.Size() == 0
}
// Union returns a set that contains all elements of s and o combined.
func (s *Set[T]) Union(o *Set[T]) *Set[T] {
result := New[T](s.Size())
for item := range s.items {
result.items[item] = sentinel
}
for item := range o.items {
result.items[item] = sentinel
}
return result
}
// Difference returns a set that contains elements of s that are not in o.
func (s *Set[T]) Difference(o *Set[T]) *Set[T] {
result := New[T](max(0, s.Size()-o.Size()))
for item := range s.items {
if !o.Contains(item) {
result.items[item] = sentinel
}
}
return result
}
// Intersect returns a set that contains elements that are present in both s and o.
func (s *Set[T]) Intersect(o *Set[T]) *Set[T] {
result := New[T](0)
big, small := s, o
if s.Size() < o.Size() {
big, small = o, s
}
for item := range small.items {
if big.Contains(item) {
result.Insert(item)
}
}
return result
}
// Copy creates a copy of s.
func (s *Set[T]) Copy() *Set[T] {
result := New[T](s.Size())
for item := range s.items {
result.items[item] = sentinel
}
return result
}
// Slice creates a copy of s as a slice. Elements are in no particular order.
func (s *Set[T]) Slice() []T {
result := make([]T, 0, s.Size())
for item := range s.items {
result = append(result, item)
}
return result
}
// List creates a copy of s as a slice.
//
// Deprecated: use Slice() instead.
func (s *Set[T]) List() []T {
return s.Slice()
}
// String creates a string representation of s, using "%v" printf formating to transform
// each element into a string. The result contains elements sorted by their lexical
// string order.
func (s *Set[T]) String() string {
return s.StringFunc(func(element T) string {
return fmt.Sprintf("%v", element)
})
}
// StringFunc creates a string representation of s, using f to transform each element
// into a string. The result contains elements sorted by their lexical string order.
func (s *Set[T]) StringFunc(f func(element T) string) string {
l := make([]string, 0, s.Size())
for item := range s.items {
l = append(l, f(item))
}
sort.Strings(l)
return fmt.Sprintf("%s", l)
}
// Equal returns whether s and o contain the same elements.
func (s *Set[T]) Equal(o *Set[T]) bool {
if len(s.items) != len(o.items) {
return false
}
for item := range s.items {
if !o.Contains(item) {
return false
}
}
return true
}
// EqualSlice returns whether s and items contain the same elements.
//
// If items contains duplicates EqualSlice will return false; it is
// assumed that items is itself set-like. For comparing equality with
// a slice that may contain duplicates, use ContainsSlice.
func (s *Set[T]) EqualSlice(items []T) bool {
if len(s.items) != len(items) {
return false
}
return s.ContainsAll(items)
}
// MarshalJSON implements the json.Marshaler interface.
func (s *Set[T]) MarshalJSON() ([]byte, error) {
return marshalJSON[T](s)
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (s *Set[T]) UnmarshalJSON(data []byte) error {
return unmarshalJSON[T](s, data)
}
func (s *Set[T]) ForEach(visit func(T) bool) {
for item := range s.items {
if !visit(item) {
return
}
}
}