-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathset.go
171 lines (145 loc) · 3.13 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
package datastructures
// Set implements an unordered set based on Go's map type
// The semantics for insert/lookup/remove are amortized O(1)
// which is a rough approximation (see https://medium.com/@ConnorPeet/go-maps-are-not-o-1-91c1e61110bf#.sp9v4ldi3)
type Set struct {
data map[string]struct{}
}
// NewSet constructs a set
// Time O(n)
// Space O(n)
func NewSet(items ...string) *Set {
s := &Set{data: make(map[string]struct{})}
for _, item := range items {
s.Add(item)
}
return s
}
// Each iterates thru each element of the set
// Time O(n)
func (s *Set) Each(f func(string)) {
for k := range s.data {
f(k)
}
}
// Add adds the element to the set
// Time O(1)
// Space O(1)
func (s *Set) Add(e string) {
s.data[e] = struct{}{}
}
// Remove removes the element from the set if it exists
// Time O(1)
// Space O(1)
func (s *Set) Remove(e string) {
delete(s.data, e)
}
// Contains returns true if the string is in set s
// Time O(1)
// Space O(1)
func (s *Set) Contains(e string) bool {
_, ok := s.data[e]
return ok
}
// IsEmpty is true if this is the empty set ∅
// Time O(1)
// Space O(1)
func (s *Set) IsEmpty() bool {
return s.Cardinality() == 0
}
// Cardinality gives the cardinality/length of this set
// Time O(1)
// Space O(1)
func (s *Set) Cardinality() int {
return s.Len()
}
// Len gives the cardinality/length of this set
// Time O(1)
// Space O(1)
func (s *Set) Len() int {
return len(s.data)
}
// Union returns the union: s ∪ other
// Time O(n)
// Space O(n)
func (s *Set) Union(other *Set) *Set {
out := NewSet()
s.Each(func(str string) {
out.Add(str)
})
other.Each(func(str string) {
out.Add(str)
})
return out
}
// Intersection returns the intersection: s ∩ other
// Time O(n)
// Space O(n)
func (s *Set) Intersection(other *Set) *Set {
out := NewSet()
// iterate thru smaller set for performance
if s.Cardinality() <= other.Cardinality() {
s.Each(func(str string) {
if other.Contains(str) {
out.Add(str)
}
})
} else {
other.Each(func(str string) {
if s.Contains(str) {
out.Add(str)
}
})
}
return out
}
// Difference is the set relative complement: other ∖ s
// Time O(n)
// Space O(n)
func (s *Set) Difference(other *Set) *Set {
out := NewSet()
s.Each(func(str string) {
if !other.Contains(str) {
out.Add(str)
}
})
return out
}
// SymmetricDifference is the set of all elements in s and other which
// are not also in their intersection. More simply: any element in s
// not found in other and vice versa. Also understood as the union
// without the intersection.
// Time O(n)
// Space O(n)
func (s *Set) SymmetricDifference(other *Set) *Set {
out := NewSet()
s.Each(func(str string) {
if !other.Contains(str) {
out.Add(str)
}
})
other.Each(func(str string) {
if !s.Contains(str) {
out.Add(str)
}
})
return out
}
// IsSubsetOf tests whether every element of this set is an element
// of other
// Time O(n)
// Space O(n)
func (s *Set) IsSubsetOf(other *Set) bool {
if s.IsEmpty() {
return true
}
if s.Cardinality() > other.Cardinality() {
return false
}
for str := range s.data {
if !other.Contains(str) {
return false
}
}
return true
}