-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.go
286 lines (247 loc) · 7.36 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
package g
import "fmt"
// NewSet creates a new Set of the specified size or an empty Set if no size is provided.
func NewSet[T comparable](size ...Int) Set[T] {
if len(size) == 0 {
return make(Set[T], 0)
}
return make(Set[T], size[0])
}
// SetOf creates a new generic set containing the provided elements.
func SetOf[T comparable](values ...T) Set[T] {
set := NewSet[T](Int(len(values)))
for _, v := range values {
set.Add(v)
}
return set
}
// SetMap applies the given function to each element of a Set and returns a new Set
// containing the transformed values.
//
// Parameters:
//
// - s: The input Set.
// - fn: The function to apply to each element of the input Set.
//
// Returns:
//
// A new Set containing the results of applying the function to each element of the input Set.
func SetMap[T, U comparable](s Set[T], fn func(T) U) Set[U] { return mapSet(s.Iter(), fn).Collect() }
// Iter returns an iterator (SeqSet[T]) for the Set, allowing for sequential iteration
// over its elements. It is commonly used in combination with higher-order functions,
// such as 'ForEach' or 'SetMap', to perform operations on each element of the Set.
//
// Returns:
//
// A SeqSet[T], which can be used for sequential iteration over the elements of the Set.
//
// Example usage:
//
// iter := g.SetOf(1, 2, 3).Iter()
// iter.ForEach(func(val T) {
// fmt.Println(val) // Replace this with the function logic you need.
// })
//
// The 'Iter' method provides a convenient way to traverse the elements of a Set
// in a functional style, enabling operations like mapping or filtering.
func (s Set[T]) Iter() SeqSet[T] { return ToSeqSet(s) }
// Add adds the provided elements to the set and returns the modified set.
func (s Set[T]) Add(values ...T) Set[T] {
for _, v := range values {
s[v] = struct{}{}
}
return s
}
// Remove removes the specified values from the Set.
func (s Set[T]) Remove(values ...T) Set[T] {
for _, v := range values {
delete(s, v)
}
return s
}
// Len returns the number of values in the Set.
func (s Set[T]) Len() Int { return Int(len(s)) }
// Contains checks if the Set contains the specified value.
func (s Set[T]) Contains(v T) bool {
_, ok := s[v]
return ok
}
// ContainsAny checks if the Set contains any element from another Set.
func (s Set[T]) ContainsAny(other Set[T]) bool {
for v := range other {
if s.Contains(v) {
return true
}
}
return false
}
// ContainsAll checks if the Set contains all elements from another Set.
func (s Set[T]) ContainsAll(other Set[T]) bool {
if len(s) < len(other) {
return false
}
for v := range other {
if !s.Contains(v) {
return false
}
}
return true
}
// Clone creates a new Set that is a copy of the original Set.
func (s Set[T]) Clone() Set[T] { return s.Iter().Collect() }
// ToSlice returns a new Slice with the same elements as the Set[T].
func (s Set[T]) ToSlice() Slice[T] {
sl := NewSlice[T](0, s.Len())
s.Iter().ForEach(func(v T) { sl = append(sl, v) })
return sl
}
// Intersection returns the intersection of the current set and another set, i.e., elements
// present in both sets.
//
// Parameters:
//
// - other Set[T]: The other set to calculate the intersection with.
//
// Returns:
//
// - Set[T]: A new Set containing the intersection of the two sets.
//
// Example usage:
//
// s1 := g.SetOf(1, 2, 3, 4, 5)
// s2 := g.SetOf(4, 5, 6, 7, 8)
// intersection := s1.Intersection(s2)
//
// The resulting intersection will be: [4, 5].
func (s Set[T]) Intersection(other Set[T]) SeqSet[T] {
if len(s) <= len(other) {
return intersection(s.Iter(), other)
}
return intersection(other.Iter(), s)
}
// Difference returns the difference between the current set and another set,
// i.e., elements present in the current set but not in the other set.
//
// Parameters:
//
// - other Set[T]: The other set to calculate the difference with.
//
// Returns:
//
// - Set[T]: A new Set containing the difference between the two sets.
//
// Example usage:
//
// s1 := g.SetOf(1, 2, 3, 4, 5)
// s2 := g.SetOf(4, 5, 6, 7, 8)
// diff := s1.Difference(s2)
//
// The resulting diff will be: [1, 2, 3].
func (s Set[T]) Difference(other Set[T]) SeqSet[T] { return difference(s.Iter(), other) }
// Union returns a new set containing the unique elements of the current set and the provided
// other set.
//
// Parameters:
//
// - other Set[T]: The other set to create the union with.
//
// Returns:
//
// - Set[T]: A new Set containing the unique elements of the current set and the provided
// other set.
//
// Example usage:
//
// s1 := g.SetOf(1, 2, 3)
// s2 := g.SetOf(3, 4, 5)
// union := s1.Union(s2)
//
// The resulting union set will be: [1, 2, 3, 4, 5].
func (s Set[T]) Union(other Set[T]) SeqSet[T] {
if len(s) > len(other) {
return s.Iter().Chain(other.Difference(s))
}
return other.Iter().Chain(s.Difference(other))
}
// SymmetricDifference returns the symmetric difference between the current set and another
// set, i.e., elements present in either the current set or the other set but not in both.
//
// Parameters:
//
// - other Set[T]: The other set to calculate the symmetric difference with.
//
// Returns:
//
// - Set[T]: A new Set containing the symmetric difference between the two sets.
//
// Example usage:
//
// s1 := g.SetOf(1, 2, 3, 4, 5)
// s2 := g.SetOf(4, 5, 6, 7, 8)
// symDiff := s1.SymmetricDifference(s2)
//
// The resulting symDiff will be: [1, 2, 3, 6, 7, 8].
func (s Set[T]) SymmetricDifference(other Set[T]) SeqSet[T] {
return s.Difference(other).Chain(other.Difference(s))
}
// Subset checks if the current set 's' is a subset of the provided 'other' set.
// A set 's' is a subset of 'other' if all elements of 's' are also elements of 'other'.
//
// Parameters:
//
// - other Set[T]: The other set to compare with.
//
// Returns:
//
// - bool: true if 's' is a subset of 'other', false otherwise.
//
// Example usage:
//
// s1 := g.SetOf(1, 2, 3)
// s2 := g.SetOf(1, 2, 3, 4, 5)
// isSubset := s1.Subset(s2) // Returns true
func (s Set[T]) Subset(other Set[T]) bool { return other.ContainsAll(s) }
// Superset checks if the current set 's' is a superset of the provided 'other' set.
// A set 's' is a superset of 'other' if all elements of 'other' are also elements of 's'.
//
// Parameters:
//
// - other Set[T]: The other set to compare with.
//
// Returns:
//
// - bool: true if 's' is a superset of 'other', false otherwise.
//
// Example usage:
//
// s1 := g.SetOf(1, 2, 3, 4, 5)
// s2 := g.SetOf(1, 2, 3)
// isSuperset := s1.Superset(s2) // Returns true
func (s Set[T]) Superset(other Set[T]) bool { return s.ContainsAll(other) }
// Eq checks if two Sets are equal.
func (s Set[T]) Eq(other Set[T]) bool {
if len(s) != len(other) {
return false
}
for v := range other {
if !s.Contains(v) {
return false
}
}
return true
}
// Ne checks if two Sets are not equal.
func (s Set[T]) Ne(other Set[T]) bool { return !s.Eq(other) }
// Clear removes all values from the Set.
func (s Set[T]) Clear() Set[T] { return s.Remove(s.ToSlice()...) }
// Empty checks if the Set is empty.
func (s Set[T]) Empty() bool { return len(s) == 0 }
// String returns a string representation of the Set.
func (s Set[T]) String() string {
builder := NewBuilder()
s.Iter().ForEach(func(v T) { builder.Write(Sprintf("%v, ", v)) })
return builder.String().TrimRight(", ").Format("Set{%s}").Std()
}
// Print prints the elements of the Set to the standard output (console)
// and returns the Set unchanged.
func (s Set[T]) Print() Set[T] { fmt.Println(s); return s }