Skip to content

Commit 1658d57

Browse files
committed
Removing all temp *Generic based names by swapping with the original names to minimize changes
1 parent cd0ae61 commit 1658d57

13 files changed

+572
-1569
lines changed

bench_test.go

+120-120
Large diffs are not rendered by default.

iterator.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ package mapset
2727

2828
// Iterator defines an iterator over a Set, its C channel can be used to range over the Set's
2929
// elements.
30-
type Iterator struct {
31-
C <-chan interface{}
30+
type Iterator[T comparable] struct {
31+
C <-chan T
3232
stop chan struct{}
3333
}
3434

3535
// Stop stops the Iterator, no further elements will be received on C, C will be closed.
36-
func (i *Iterator) Stop() {
36+
func (i *Iterator[T]) Stop() {
3737
// Allows for Stop() to be called multiple times
3838
// (close() panics when called on already closed channel)
3939
defer func() {
@@ -48,10 +48,10 @@ func (i *Iterator) Stop() {
4848
}
4949

5050
// newIterator returns a new Iterator instance together with its item and stop channels.
51-
func newIterator() (*Iterator, chan<- interface{}, <-chan struct{}) {
52-
itemChan := make(chan interface{})
51+
func newIterator[T comparable]() (*Iterator[T], chan<- T, <-chan struct{}) {
52+
itemChan := make(chan T)
5353
stopChan := make(chan struct{})
54-
return &Iterator{
54+
return &Iterator[T]{
5555
C: itemChan,
5656
stop: stopChan,
5757
}, itemChan, stopChan

iterator_example_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type yourType struct {
3535

3636
func Test_ExampleIterator(t *testing.T) {
3737

38-
s := NewSetFromSliceGeneric[*yourType]([]*yourType{
38+
s := NewSetFromSlice[*yourType]([]*yourType{
3939
&yourType{name: "Alise"},
4040
&yourType{name: "Bob"},
4141
&yourType{name: "John"},

iterator_generic.go

-58
This file was deleted.

set.go

+48-41
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2323
SOFTWARE.
2424
*/
2525

26-
// Package mapset implements a simple and generic set collection.
26+
// Package mapset implements a simple and set collection.
2727
// Items stored within it are unordered and unique. It supports
2828
// typical set operations: membership testing, intersection, union,
2929
// difference, symmetric difference and cloning.
@@ -38,10 +38,10 @@ package mapset
3838
// Set is the primary interface provided by the mapset package. It
3939
// represents an unordered set of data and a large number of
4040
// operations that can be applied to that set.
41-
type Set interface {
41+
type Set[T comparable] interface {
4242
// Adds an element to the set. Returns whether
4343
// the item was added.
44-
Add(i interface{}) bool
44+
Add(val T) bool
4545

4646
// Returns the number of elements in the set.
4747
Cardinality() int
@@ -52,11 +52,11 @@ type Set interface {
5252

5353
// Returns a clone of the set using the same
5454
// implementation, duplicating all keys.
55-
Clone() Set
55+
Clone() Set[T]
5656

5757
// Returns whether the given items
5858
// are all in the set.
59-
Contains(i ...interface{}) bool
59+
Contains(val ...T) bool
6060

6161
// Returns the difference between this set
6262
// and other. The returned set will contain
@@ -67,7 +67,7 @@ type Set interface {
6767
// must be of the same type as the receiver
6868
// of the method. Otherwise, Difference will
6969
// panic.
70-
Difference(other Set) Set
70+
Difference(other Set[T]) Set[T]
7171

7272
// Determines if two sets are equal to each
7373
// other. If they have the same cardinality
@@ -78,7 +78,7 @@ type Set interface {
7878
// Note that the argument to Equal must be
7979
// of the same type as the receiver of the
8080
// method. Otherwise, Equal will panic.
81-
Equal(other Set) bool
81+
Equal(other Set[T]) bool
8282

8383
// Returns a new set containing only the elements
8484
// that exist only in both sets.
@@ -87,7 +87,7 @@ type Set interface {
8787
// must be of the same type as the receiver
8888
// of the method. Otherwise, Intersect will
8989
// panic.
90-
Intersect(other Set) Set
90+
Intersect(other Set[T]) Set[T]
9191

9292
// Determines if every element in this set is in
9393
// the other set but the two sets are not equal.
@@ -96,7 +96,7 @@ type Set interface {
9696
// must be of the same type as the receiver
9797
// of the method. Otherwise, IsProperSubset
9898
// will panic.
99-
IsProperSubset(other Set) bool
99+
IsProperSubset(other Set[T]) bool
100100

101101
// Determines if every element in the other set
102102
// is in this set but the two sets are not
@@ -106,7 +106,7 @@ type Set interface {
106106
// must be of the same type as the receiver
107107
// of the method. Otherwise, IsSuperset will
108108
// panic.
109-
IsProperSuperset(other Set) bool
109+
IsProperSuperset(other Set[T]) bool
110110

111111
// Determines if every element in this set is in
112112
// the other set.
@@ -115,7 +115,7 @@ type Set interface {
115115
// must be of the same type as the receiver
116116
// of the method. Otherwise, IsSubset will
117117
// panic.
118-
IsSubset(other Set) bool
118+
IsSubset(other Set[T]) bool
119119

120120
// Determines if every element in the other set
121121
// is in this set.
@@ -124,22 +124,22 @@ type Set interface {
124124
// must be of the same type as the receiver
125125
// of the method. Otherwise, IsSuperset will
126126
// panic.
127-
IsSuperset(other Set) bool
127+
IsSuperset(other Set[T]) bool
128128

129129
// Iterates over elements and executes the passed func against each element.
130130
// If passed func returns true, stop iteration at the time.
131-
Each(func(interface{}) bool)
131+
Each(func(T) bool)
132132

133133
// Returns a channel of elements that you can
134134
// range over.
135-
Iter() <-chan interface{}
135+
Iter() <-chan T
136136

137137
// Returns an Iterator object that you can
138138
// use to range over the set.
139-
Iterator() *Iterator
139+
Iterator() *Iterator[T]
140140

141141
// Remove a single element from the set.
142-
Remove(i interface{})
142+
Remove(i T)
143143

144144
// Provides a convenient string representation
145145
// of the current state of the set.
@@ -152,66 +152,73 @@ type Set interface {
152152
// must be of the same type as the receiver
153153
// of the method. Otherwise, SymmetricDifference
154154
// will panic.
155-
SymmetricDifference(other Set) Set
155+
SymmetricDifference(other Set[T]) Set[T]
156156

157157
// Returns a new set with all elements in both sets.
158158
//
159159
// Note that the argument to Union must be of the
160-
161160
// same type as the receiver of the method.
162161
// Otherwise, IsSuperset will panic.
163-
Union(other Set) Set
162+
Union(other Set[T]) Set[T]
164163

165164
// Pop removes and returns an arbitrary item from the set.
166-
Pop() interface{}
165+
Pop() any
167166

168167
// 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]
170170

171171
// Returns the Cartesian Product of two sets.
172-
CartesianProduct(other Set) Set
172+
CartesianProduct(other Set[T]) Set[any]
173173

174174
// 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
176183
}
177184

178185
// NewSet creates and returns a reference to an empty set. Operations
179186
// 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)
184191
}
185-
return &set
192+
return &s
186193
}
187194

188195
// NewSetWith creates and returns a new set with the given elements.
189196
// 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)
192199
}
193200

194201
// NewSetFromSlice creates and returns a reference to a set from an
195202
// 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
199206
}
200207

201208
// NewThreadUnsafeSet creates and returns a reference to an empty set.
202209
// 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
206213
}
207214

208215
// NewThreadUnsafeSetFromSlice creates and returns a reference to a
209216
// set from an existing slice. Operations on the resulting set are
210217
// 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)
215222
}
216-
return a
223+
return s
217224
}

0 commit comments

Comments
 (0)