-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset.go
More file actions
657 lines (596 loc) Β· 13.6 KB
/
Copy pathset.go
File metadata and controls
657 lines (596 loc) Β· 13.6 KB
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
package mapset
import (
"fmt"
"slices"
"strings"
"github.com/forbearing/gst/ds/types"
)
type Set[E comparable] struct {
set map[E]struct{}
mu types.Locker
safe bool
cmp func(E, E) int
sorted bool
}
// New creates a new set without pre-allocates space.
// Options can be provided to customize the set's properties (e.g., thread safety).
func New[E comparable](ops ...Option[E]) (*Set[E], error) {
return NewWithSize(0, ops...)
}
// NewWithSize creates a new set and pre-allocates space for the given size.
// Options can be provided to customize the set's properties (e.g., thread safety).
func NewWithSize[T comparable](size int, ops ...Option[T]) (*Set[T], error) {
s := &Set[T]{
set: make(map[T]struct{}, size),
mu: types.FakeLocker{},
}
for _, op := range ops {
if op == nil {
continue
}
if err := op(s); err != nil {
return nil, err
}
}
return s, nil
}
// NewFromSlice creates a new set from the provided slice.
// If the provided slice is nil or empty, creates an empty set.
// Options can be provided to customize the set's properties (e.g., thread safety).
func NewFromSlice[E comparable](slice []E, ops ...Option[E]) (*Set[E], error) {
if len(slice) == 0 {
return New(ops...)
}
s, err := NewWithSize(len(slice), ops...)
if err != nil {
return nil, err
}
for _, e := range slice {
s.set[e] = struct{}{}
}
return s, nil
}
// NewFromMapKeys creates a new set from the provided map of keys.
// If map "m" is nil or empty, creates an empty set.
// Options can be provided to customize the set's properties (e.g., thread safety).
func NewFromMapKeys[K comparable, V any](m map[K]V, ops ...Option[K]) (*Set[K], error) {
if len(m) == 0 {
return New(ops...)
}
s, err := NewWithSize(len(m), ops...)
if err != nil {
return nil, err
}
for k := range m {
s.set[k] = struct{}{}
}
return s, nil
}
// NewFromMapValues creates a new set from the provided map of values.
// If map "m" is nil or empty, creates an empty set.
// Options can be provided to customize the set's properties (e.g., thread safety).
func NewFromMapValues[K comparable, V comparable](m map[K]V, ops ...Option[V]) (*Set[V], error) {
if len(m) == 0 {
return New(ops...)
}
s, err := NewWithSize(len(m), ops...)
if err != nil {
return nil, err
}
for _, v := range m {
s.set[v] = struct{}{}
}
return s, nil
}
// Add one or more elements into the set.
// Returns the number of elements added.
func (s *Set[E]) Add(el ...E) int {
if s.safe {
s.mu.Lock()
defer s.mu.Unlock()
}
prevLen := len(s.set)
for _, e := range el {
s.set[e] = struct{}{}
}
return len(s.set) - prevLen
}
// Pop removes and returns a single, arbitrary element from the set.
// The order of removal is non-deterministic.
// If the set is empty, it returns zero value of element type and false.
func (s *Set[E]) Pop() (e E, ok bool) {
if s.safe {
s.mu.Lock()
defer s.mu.Unlock()
}
for v := range s.set {
delete(s.set, v)
return v, true
}
return e, false
}
// Remove one or more elements from the set.
func (s *Set[E]) Remove(el ...E) {
if s.safe {
s.mu.Lock()
defer s.mu.Unlock()
}
for _, e := range el {
delete(s.set, e)
}
}
// Clear removes all elements from the set.
func (s *Set[E]) Clear() {
if s.safe {
s.mu.Lock()
defer s.mu.Unlock()
}
for e := range s.set {
delete(s.set, e)
}
}
// Len returns the number of elements in the set.
func (s *Set[E]) Len() int {
if s.safe {
s.mu.RLock()
defer s.mu.RUnlock()
}
return len(s.set)
}
// Clone creates and returns a deep copy of the set.
//
// The property of the cloned set is the same as the original set.
// - If the original set is concurrent safe, the cloned set is concurrent safe.
func (s *Set[E]) Clone() *Set[E] {
if s.safe {
s.mu.RLock()
defer s.mu.RUnlock()
}
return s.clone()
}
func (s *Set[E]) clone() *Set[E] {
var cloned *Set[E]
cloned, _ = NewFromMapKeys(s.set, s.options()...)
return cloned
}
func (s *Set[E]) options() []Option[E] {
ops := []Option[E]{}
if s.safe {
ops = append(ops, WithSafe[E]())
}
if s.sorted {
ops = append(ops, WithSorted(s.cmp))
}
return ops
}
// Contains reports whether the set contains all the given elements.
// It always returns true if the provided slice is nil or empty.
func (s *Set[E]) Contains(el ...E) bool {
if len(el) == 0 {
return true
}
if s.safe {
s.mu.RLock()
defer s.mu.RUnlock()
}
var ok bool
for _, e := range el {
if _, ok = s.set[e]; !ok {
return false
}
}
return true
}
// ContainsOne reports whether the set contains the given element.
func (s *Set[E]) ContainsOne(v E) bool {
if s.safe {
s.mu.RLock()
defer s.mu.RUnlock()
}
_, ok := s.set[v]
return ok
}
// ContainsAny reports whether the set contains any of the given element.
// It returns true if the provided slice is nil or empty.
func (s *Set[E]) ContainsAny(el ...E) bool {
if len(el) == 0 {
return true
}
if s.safe {
s.mu.RLock()
defer s.mu.RUnlock()
}
var ok bool
for _, e := range el {
if _, ok = s.set[e]; ok {
return true
}
}
return false
}
// ContainsAnyElement reports whether the set contains any element of the given set.
// If the given set is nil or empty, it returns false.
func (s *Set[E]) ContainsAnyElement(other *Set[E]) bool {
if other == nil {
return false
}
if s.safe {
s.mu.RLock()
defer s.mu.RUnlock()
}
if other.safe {
other.mu.RLock()
defer other.mu.RUnlock()
}
if len(other.set) == 0 {
return false
}
var ok bool
if len(s.set) < len(other.set) {
for e := range s.set {
if _, ok = other.set[e]; ok {
return true
}
}
} else {
for e := range other.set {
if _, ok = s.set[e]; ok {
return true
}
}
}
return false
}
// Range calls fn for each element in the set.
// If fn returns false, "Range" stops the iteration.
// If fn is nil, "Range" does nothing.
func (s *Set[E]) Range(fn func(e E) bool) {
if fn == nil {
return
}
if s.safe {
s.mu.RLock()
defer s.mu.RUnlock()
}
if s.sorted {
el := s.sortedSlice(s.cmp)
for _, e := range el {
if !fn(e) {
return
}
}
} else {
for e := range s.set {
if !fn(e) {
return
}
}
}
}
// Equal reports whether two sets have the same elements.
func (s *Set[E]) Equal(other *Set[E]) bool {
if other == nil {
return false
}
if s.safe {
s.mu.RLock()
defer s.mu.RUnlock()
}
if other.safe {
other.mu.RLock()
defer other.mu.RUnlock()
}
if len(s.set) != len(other.set) {
return false
}
var ok bool
for e := range s.set {
if _, ok = other.set[e]; !ok {
return false
}
}
return true
}
// IsEmpty reports whether the set is empty.
func (s *Set[E]) IsEmpty() bool {
if s.safe {
s.mu.RLock()
defer s.mu.RUnlock()
}
return len(s.set) == 0
}
// Iter returns a channel of elements that caller can range over.
func (s *Set[E]) Iter() <-chan E {
ch := make(chan E)
go func() {
if s.safe {
s.mu.RLock()
defer s.mu.RUnlock()
}
if s.sorted {
el := s.sortedSlice(s.cmp)
for _, e := range el {
ch <- e
}
} else {
for e := range s.set {
ch <- e
}
}
close(ch)
}()
return ch
}
// IsSubset checks if the current set is a subset of the given set.
// A subset means every element of the current set is also in the given set.
// If the given set is nil, the function always returns false.
func (s *Set[E]) IsSubset(other *Set[E]) bool {
if other == nil {
return false
}
if s.safe {
s.mu.RLock()
defer s.mu.RUnlock()
}
if other.safe {
other.mu.RLock()
defer other.mu.RUnlock()
}
return s.isSubset(other)
}
func (s *Set[E]) isSubset(other *Set[E]) bool {
if len(s.set) > len(other.set) {
return false
}
var ok bool
for e := range s.set {
if _, ok = other.set[e]; !ok {
return false
}
}
return true
}
// IsProperSubset checks if the current set is a proper subset of the given set.
// A proper subset means every element of the current set is in the given set,
// and the given set contains more elements than the current set.
func (s *Set[E]) IsProperSubset(other *Set[E]) bool {
if other == nil {
return false
}
if s.safe {
s.mu.RLock()
defer s.mu.RUnlock()
}
if other.safe {
other.mu.RLock()
defer other.mu.RUnlock()
}
return len(s.set) < len(other.set) && s.isSubset(other)
}
// IsSuperset checks if the current set is a superset of the given set.
// A superset means the current set contains every element of the given set.
// If the given set is nil or empty, the function always returns true.
func (s *Set[E]) IsSuperset(other *Set[E]) bool {
if other == nil {
return true
}
if s.safe {
s.mu.RLock()
defer s.mu.RUnlock()
}
if other.safe {
other.mu.RLock()
defer other.mu.RUnlock()
}
return s.isSuperset(other)
}
func (s *Set[E]) isSuperset(other *Set[E]) bool {
if len(other.set) == 0 {
return true
}
var ok bool
for e := range other.set {
if _, ok = s.set[e]; !ok {
return false
}
}
return true
}
// IsProperSuperset checks if the current set is a proper superset of given set.
// A proper superset means all elements of given set are present int the current set.
// and the current set has additional element not present in the given set.
func (s *Set[E]) IsProperSuperset(other *Set[E]) bool {
if s.safe {
s.mu.RLock()
defer s.mu.RUnlock()
}
if other == nil && len(s.set) > 0 {
return true
}
if other.safe {
other.mu.RLock()
defer other.mu.RUnlock()
}
return len(s.set) > len(other.set) && s.isSuperset(other)
}
// Difference computes the difference between the current set and the given set.
// The resulting set contains element that are present in the current set
// but not in the given set.
//
// The returned set inherits the properties of the current set.
// For example: if the current set is concurrent-safe, the returned set is also
// be concurrent-safe.
func (s *Set[E]) Difference(other *Set[E]) *Set[E] {
if other == nil {
return s.clone()
}
if s.safe {
s.mu.RLock()
defer s.mu.RUnlock()
}
if other.safe {
other.mu.RLock()
defer other.mu.RUnlock()
}
if len(other.set) == 0 || len(s.set) == 0 {
return s.clone()
}
diff, _ := New(s.options()...)
for e := range s.set {
if _, ok := other.set[e]; !ok {
diff.set[e] = struct{}{}
}
}
return diff
}
// SymmetricDifference computes the symmetric difference between the current set
// and the given set.
// The symmetric difference includes elements present in either set but not in both.
//
// The returned set inherits the properties of the current set.
// For example, if the current set is concurrent-safe, the returned set is also
// be concurrent-safe
func (s *Set[E]) SymmetricDifference(other *Set[E]) *Set[E] {
if other == nil {
return s.Clone()
}
if s.safe {
s.mu.RLock()
defer s.mu.RUnlock()
}
if other.safe {
s.mu.RLock()
defer s.mu.RUnlock()
}
if len(other.set) == 0 {
return s.Clone()
}
diff, _ := New(s.options()...)
for e := range s.set {
if _, ok := other.set[e]; !ok {
diff.set[e] = struct{}{}
}
}
for e := range other.set {
if _, ok := s.set[e]; !ok {
diff.set[e] = struct{}{}
}
}
return diff
}
// Union returns computes union of the current set and the given set.
// The resulting is contains all the elements that are present in
// either the current set or the given set.
//
// If the given set is nil or empty, returns the deep clone of the current set.
//
// The returned set inherits the properties of the current set.
// For example, if the current set is concurrent-safe, the returned set is also
// be concurrent-safe
func (s *Set[E]) Union(other *Set[E]) *Set[E] {
if other == nil {
return s.Clone()
}
if s.safe {
s.mu.RLock()
defer s.mu.RUnlock()
}
if other.safe {
other.mu.RLock()
defer other.mu.RUnlock()
}
if len(other.set) == 0 {
return s.Clone()
}
union, _ := New(s.options()...)
for e := range s.set {
union.set[e] = struct{}{}
}
for e := range other.set {
union.set[e] = struct{}{}
}
return union
}
// Intersect computes the intersection of the current set and the given set.
// The resulting set contains elements that are present in both the current set and the given set.
//
// If the given set is nil or empty, returns an empty set.
// The returned set inherits the properties of the current set.
// For example, if the current set is concurrent-safe, the returned set is also
func (s *Set[E]) Intersect(other *Set[E]) *Set[E] {
if s.safe {
s.mu.RLock()
defer s.mu.RUnlock()
}
if other.safe {
other.mu.RLock()
defer other.mu.RUnlock()
}
inter, _ := New(s.options()...)
if other == nil {
return inter
}
if len(other.set) == 0 || len(s.set) == 0 {
return inter
}
if len(s.set) < len(other.set) {
for e := range s.set {
if _, ok := other.set[e]; ok {
inter.set[e] = struct{}{}
}
}
} else {
for e := range other.set {
if _, ok := s.set[e]; ok {
inter.set[e] = struct{}{}
}
}
}
return inter
}
// String returns a string representation of the set.
func (s *Set[E]) String() string {
if s.safe {
s.mu.RLock()
defer s.mu.RUnlock()
}
el := make([]string, 0, len(s.set))
if s.sorted {
elements := s.sortedSlice(s.cmp)
for _, e := range elements {
el = append(el, fmt.Sprintf("%v", e))
}
} else {
for e := range s.set {
el = append(el, fmt.Sprintf("%v", e))
}
}
return fmt.Sprintf("Set{%s}", strings.Join(el, ", "))
}
// Slice returns a slice of the elements in the set.
// The order of the elements is non-deterministic.
func (s *Set[E]) Slice() []E {
if s.safe {
s.mu.RLock()
defer s.mu.RUnlock()
}
if s.sorted {
return s.sortedSlice(s.cmp)
}
return s.unsortedSlice()
}
func (s *Set[E]) sortedSlice(cmp func(E, E) int) []E {
el := make([]E, 0, len(s.set))
for e := range s.set {
el = append(el, e)
}
slices.SortFunc(el, cmp)
return el
}
func (s *Set[E]) unsortedSlice() []E {
el := make([]E, 0, len(s.set))
for e := range s.set {
el = append(el, e)
}
return el
}