This repository has been archived by the owner on Nov 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset.go
208 lines (168 loc) · 3.67 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
// Package set provides a Set data structure.
package set
import (
"fmt"
"strings"
)
type valType struct{}
var val valType
type Set[T comparable] struct {
c map[T]valType
}
func New[T comparable](size int) *Set[T] {
return &Set[T]{c: make(map[T]valType, size)}
}
func From[T comparable](sl ...T) *Set[T] {
return New[T](len(sl)).Add(sl...)
}
func FromSlice[T comparable](sl []T) *Set[T] {
return New[T](len(sl)).Add(sl...)
}
// Add adds items to set.
func (s *Set[T]) Add(items ...T) *Set[T] {
for i := range items {
s.c[items[i]] = val
}
return s
}
// Remove deletes items from set.
func (s *Set[T]) Remove(items ...T) *Set[T] {
for i := range items {
delete(s.c, items[i])
}
return s
}
// Has returns true if item is in set.
func (s *Set[T]) Has(item T) bool {
_, ok := s.c[item]
return ok
}
// Clone creates a shallow copy of the set.
func (s *Set[T]) Clone() *Set[T] {
out := New[T](len(s.c))
for k := range s.c {
out.c[k] = val
}
return out
}
// Merge merges all sets into current set.
func (s *Set[T]) Merge(others ...*Set[T]) *Set[T] {
for _, other := range others {
for k := range other.c {
s.c[k] = val
}
}
return s
}
// IsDisjoint returns true if set has no elements in common with other.
func (s *Set[T]) IsDisjoint(other *Set[T]) bool {
smaller, bigger := s.c, other.c
if len(s.c) > len(other.c) {
smaller, bigger = bigger, smaller
}
for k := range smaller {
if _, ok := bigger[k]; ok {
return false
}
}
return true
}
// IsSubset returns true if every element in the set is in other.
func (s *Set[T]) IsSubset(other *Set[T]) bool {
for k := range s.c {
if _, ok := other.c[k]; !ok {
return false
}
}
return true
}
// Len returns the number of elements in set.
func (s *Set[T]) Len() int {
return len(s.c)
}
// Intersection return a new set with elements common to the set and all others.
func (s *Set[T]) Intersection(others ...*Set[T]) *Set[T] {
others = append(others, s)
smallest, _ := findSmallestAndBigestIndex(others)
needle := others[smallest]
copy(others[smallest:], others[smallest+1:])
others = others[:len(others)-1]
out := New[T](len(needle.c))
for k := range needle.c {
shouldAdd := true
for _, set := range others {
if _, ok := set.c[k]; !ok {
shouldAdd = false
break
}
}
if shouldAdd {
out.c[k] = val
}
}
return out
}
// Diff return a new set with elements in the set that are not in the others.
func (s *Set[T]) Diff(others ...*Set[T]) *Set[T] {
out := New[T](len(s.c))
for k := range s.c {
shouldAdd := true
for _, other := range others {
if _, ok := other.c[k]; ok {
shouldAdd = false
break
}
}
if shouldAdd {
out.c[k] = val
}
}
return out
}
// Pop remove and return an arbitrary element from the set.
func (s *Set[T]) Pop() *T {
for k := range s.c {
delete(s.c, k)
return &k
}
return nil
}
// ToList returns a list of items in set.
func (s *Set[T]) ToList() []T {
out := make([]T, 0, len(s.c))
for k := range s.c {
out = append(out, k)
}
return out
}
// ForEach calls fn for each item in set.
func (s *Set[T]) ForEach(fn func(item T)) {
for k := range s.c {
fn(k)
}
}
// Equal returns true if set is equal to other.
func (s *Set[T]) Equal(other *Set[T]) bool {
if len(s.c) != len(other.c) {
return false
}
for k := range s.c {
if _, ok := other.c[k]; !ok {
return false
}
}
return true
}
// String returns a string representation of set elements.
func (s *Set[T]) String() string {
sb := strings.Builder{}
sb.WriteString(fmt.Sprintf("[%d]{", len(s.c)))
loopDelimiter := ""
for i := range s.c {
sb.WriteString(loopDelimiter)
loopDelimiter = ","
sb.WriteString(fmt.Sprint(i))
}
sb.WriteString("}")
return sb.String()
}