@@ -23,7 +23,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
23
SOFTWARE.
24
24
*/
25
25
26
- // Package mapset implements a simple and generic set collection.
26
+ // Package mapset implements a simple and set collection.
27
27
// Items stored within it are unordered and unique. It supports
28
28
// typical set operations: membership testing, intersection, union,
29
29
// difference, symmetric difference and cloning.
@@ -38,10 +38,10 @@ package mapset
38
38
// Set is the primary interface provided by the mapset package. It
39
39
// represents an unordered set of data and a large number of
40
40
// operations that can be applied to that set.
41
- type Set interface {
41
+ type Set [ T comparable ] interface {
42
42
// Adds an element to the set. Returns whether
43
43
// the item was added.
44
- Add (i interface {} ) bool
44
+ Add (val T ) bool
45
45
46
46
// Returns the number of elements in the set.
47
47
Cardinality () int
@@ -52,11 +52,11 @@ type Set interface {
52
52
53
53
// Returns a clone of the set using the same
54
54
// implementation, duplicating all keys.
55
- Clone () Set
55
+ Clone () Set [ T ]
56
56
57
57
// Returns whether the given items
58
58
// are all in the set.
59
- Contains (i ... interface {} ) bool
59
+ Contains (val ... T ) bool
60
60
61
61
// Returns the difference between this set
62
62
// and other. The returned set will contain
@@ -67,7 +67,7 @@ type Set interface {
67
67
// must be of the same type as the receiver
68
68
// of the method. Otherwise, Difference will
69
69
// panic.
70
- Difference (other Set ) Set
70
+ Difference (other Set [ T ] ) Set [ T ]
71
71
72
72
// Determines if two sets are equal to each
73
73
// other. If they have the same cardinality
@@ -78,7 +78,7 @@ type Set interface {
78
78
// Note that the argument to Equal must be
79
79
// of the same type as the receiver of the
80
80
// method. Otherwise, Equal will panic.
81
- Equal (other Set ) bool
81
+ Equal (other Set [ T ] ) bool
82
82
83
83
// Returns a new set containing only the elements
84
84
// that exist only in both sets.
@@ -87,7 +87,7 @@ type Set interface {
87
87
// must be of the same type as the receiver
88
88
// of the method. Otherwise, Intersect will
89
89
// panic.
90
- Intersect (other Set ) Set
90
+ Intersect (other Set [ T ] ) Set [ T ]
91
91
92
92
// Determines if every element in this set is in
93
93
// the other set but the two sets are not equal.
@@ -96,7 +96,7 @@ type Set interface {
96
96
// must be of the same type as the receiver
97
97
// of the method. Otherwise, IsProperSubset
98
98
// will panic.
99
- IsProperSubset (other Set ) bool
99
+ IsProperSubset (other Set [ T ] ) bool
100
100
101
101
// Determines if every element in the other set
102
102
// is in this set but the two sets are not
@@ -106,7 +106,7 @@ type Set interface {
106
106
// must be of the same type as the receiver
107
107
// of the method. Otherwise, IsSuperset will
108
108
// panic.
109
- IsProperSuperset (other Set ) bool
109
+ IsProperSuperset (other Set [ T ] ) bool
110
110
111
111
// Determines if every element in this set is in
112
112
// the other set.
@@ -115,7 +115,7 @@ type Set interface {
115
115
// must be of the same type as the receiver
116
116
// of the method. Otherwise, IsSubset will
117
117
// panic.
118
- IsSubset (other Set ) bool
118
+ IsSubset (other Set [ T ] ) bool
119
119
120
120
// Determines if every element in the other set
121
121
// is in this set.
@@ -124,22 +124,22 @@ type Set interface {
124
124
// must be of the same type as the receiver
125
125
// of the method. Otherwise, IsSuperset will
126
126
// panic.
127
- IsSuperset (other Set ) bool
127
+ IsSuperset (other Set [ T ] ) bool
128
128
129
129
// Iterates over elements and executes the passed func against each element.
130
130
// If passed func returns true, stop iteration at the time.
131
- Each (func (interface {} ) bool )
131
+ Each (func (T ) bool )
132
132
133
133
// Returns a channel of elements that you can
134
134
// range over.
135
- Iter () <- chan interface {}
135
+ Iter () <- chan T
136
136
137
137
// Returns an Iterator object that you can
138
138
// use to range over the set.
139
- Iterator () * Iterator
139
+ Iterator () * Iterator [ T ]
140
140
141
141
// Remove a single element from the set.
142
- Remove (i interface {} )
142
+ Remove (i T )
143
143
144
144
// Provides a convenient string representation
145
145
// of the current state of the set.
@@ -152,66 +152,73 @@ type Set interface {
152
152
// must be of the same type as the receiver
153
153
// of the method. Otherwise, SymmetricDifference
154
154
// will panic.
155
- SymmetricDifference (other Set ) Set
155
+ SymmetricDifference (other Set [ T ] ) Set [ T ]
156
156
157
157
// Returns a new set with all elements in both sets.
158
158
//
159
159
// Note that the argument to Union must be of the
160
-
161
160
// same type as the receiver of the method.
162
161
// Otherwise, IsSuperset will panic.
163
- Union (other Set ) Set
162
+ Union (other Set [ T ] ) Set [ T ]
164
163
165
164
// Pop removes and returns an arbitrary item from the set.
166
- Pop () interface {}
165
+ Pop () any
167
166
168
167
// Returns all subsets of a given set (Power Set).
169
- PowerSet () Set
168
+ // PowerSet({x, y, z}) -> {{}, {x}, {y}, {z}, {x, y}, {x, z}, {y, z}, {x, y, z}}
169
+ PowerSet () Set [any ]
170
170
171
171
// Returns the Cartesian Product of two sets.
172
- CartesianProduct (other Set ) Set
172
+ CartesianProduct (other Set [ T ] ) Set [ any ]
173
173
174
174
// Returns the members of the set as a slice.
175
- ToSlice () []interface {}
175
+ ToSlice () []T
176
+
177
+ // MarshalJSON will marshal the set into a JSON-based representation.
178
+ MarshalJSON () ([]byte , error )
179
+
180
+ // UnmarshalJSON will unmarshal a JSON-based byte slice into a full Set datastructure.
181
+ // For this to work, set subtypes must implemented the Marshal/Unmarshal interface.
182
+ UnmarshalJSON (b []byte ) error
176
183
}
177
184
178
185
// NewSet creates and returns a reference to an empty set. Operations
179
186
// on the resulting set are thread-safe.
180
- func NewSet ( s ... interface {} ) Set {
181
- set := newThreadSafeSet ()
182
- for _ , item := range s {
183
- set .Add (item )
187
+ func NewSet [ T comparable ]( vals ... T ) Set [ T ] {
188
+ s := newThreadSafeSet [ T ] ()
189
+ for _ , item := range vals {
190
+ s .Add (item )
184
191
}
185
- return & set
192
+ return & s
186
193
}
187
194
188
195
// NewSetWith creates and returns a new set with the given elements.
189
196
// Operations on the resulting set are thread-safe.
190
- func NewSetWith ( elts ... interface {} ) Set {
191
- return NewSetFromSlice ( elts )
197
+ func NewSetWith [ T comparable ]( vals ... T ) Set [ T ] {
198
+ return NewSetFromSlice [ T ]( vals )
192
199
}
193
200
194
201
// NewSetFromSlice creates and returns a reference to a set from an
195
202
// existing slice. Operations on the resulting set are thread-safe.
196
- func NewSetFromSlice ( s []interface {} ) Set {
197
- a := NewSet (s ... )
198
- return a
203
+ func NewSetFromSlice [ T comparable ]( v []T ) Set [ T ] {
204
+ s := NewSet (v ... )
205
+ return s
199
206
}
200
207
201
208
// NewThreadUnsafeSet creates and returns a reference to an empty set.
202
209
// Operations on the resulting set are not thread-safe.
203
- func NewThreadUnsafeSet () Set {
204
- set := newThreadUnsafeSet ()
205
- return & set
210
+ func NewThreadUnsafeSet [ T comparable ] () Set [ T ] {
211
+ s := newThreadUnsafeSet [ T ] ()
212
+ return & s
206
213
}
207
214
208
215
// NewThreadUnsafeSetFromSlice creates and returns a reference to a
209
216
// set from an existing slice. Operations on the resulting set are
210
217
// not thread-safe.
211
- func NewThreadUnsafeSetFromSlice ( s []interface {} ) Set {
212
- a := NewThreadUnsafeSet ()
213
- for _ , item := range s {
214
- a .Add (item )
218
+ func NewThreadUnsafeSetFromSlice [ T comparable ]( v []T ) Set [ T ] {
219
+ s := NewThreadUnsafeSet [ T ] ()
220
+ for _ , item := range v {
221
+ s .Add (item )
215
222
}
216
- return a
223
+ return s
217
224
}
0 commit comments