-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset.go
More file actions
101 lines (84 loc) · 1.7 KB
/
set.go
File metadata and controls
101 lines (84 loc) · 1.7 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
package main
import "fmt"
// copied from https://gist.github.com/bgadrian/cb8b9344d9c66571ef331a14eb7a2e80
type Set[T comparable] struct {
list map[T]struct{} //empty structs occupy 0 memory
}
func (s *Set[T]) String() string {
var result = "["
for k := range s.list {
result = result + fmt.Sprint(k) + ","
}
return result + "]"
}
func (s *Set[T]) Has(v T) bool {
_, ok := s.list[v]
return ok
}
func (s *Set[T]) Add(v T) {
s.list[v] = struct{}{}
}
func (s *Set[T]) Remove(v T) {
delete(s.list, v)
}
func (s *Set[T]) Clear() {
s.list = make(map[T]struct{})
}
func (s *Set[T]) Size() int {
return len(s.list)
}
func NewSet[T comparable]() *Set[T] {
s := &Set[T]{}
s.list = make(map[T]struct{})
return s
}
// AddMulti Add multiple values in the set
func (s *Set[T]) AddMulti(list ...T) {
for _, v := range list {
s.Add(v)
}
}
type FilterFunc[T comparable] func(v T) bool
// Filter returns a subset, that contains only the values that satisfies the given predicate P
func (s *Set[T]) Filter(P FilterFunc[T]) *Set[T] {
res := &Set[T]{}
res.list = make(map[T]struct{})
for v := range s.list {
if !P(v) {
continue
}
res.Add(v)
}
return res
}
func (s *Set[T]) Union(s2 *Set[T]) *Set[T] {
res := NewSet[T]()
for v := range s.list {
res.Add(v)
}
for v := range s2.list {
res.Add(v)
}
return res
}
func (s *Set[T]) Intersect(s2 *Set[T]) *Set[T] {
res := NewSet[T]()
for v := range s.list {
if !s2.Has(v) {
continue
}
res.Add(v)
}
return res
}
// Difference returns the subset from s, that doesn't exists in s2 (param)
func (s *Set[T]) Difference(s2 *Set[T]) *Set[T] {
res := NewSet[T]()
for v := range s.list {
if s2.Has(v) {
continue
}
res.Add(v)
}
return res
}