3
3
// (c) 2023 Fortio Authors
4
4
// See LICENSE
5
5
6
- // Sets and Set type and operations in go 1.18+ generics.
7
- // (pending built in support in golang core)
6
+ // Sets and Set[T] type and operations of any comparable type (go 1.18+ generics)
7
+ // [Intersection], [Union], [Subset], difference aka [Minus], [XOR],
8
+ // JSON serialization and deserialization and more.
8
9
package sets // import "fortio.org/sets"
9
10
10
11
import (
@@ -16,6 +17,7 @@ import (
16
17
"golang.org/x/exp/constraints"
17
18
)
18
19
20
+ // Set defines a low memory footprint set of any comparable type. Based on `map[T]struct{}`.
19
21
type Set [T comparable ] map [T ]struct {}
20
22
21
23
// New returns a new set containing the given elements.
@@ -33,6 +35,7 @@ func FromSlice[T comparable](items []T) Set[T] {
33
35
return New (items ... )
34
36
}
35
37
38
+ // Clone returns a copy of the set.
36
39
func (s Set [T ]) Clone () Set [T ] {
37
40
res := make (Set [T ], len (s ))
38
41
for k := range s {
@@ -41,17 +44,20 @@ func (s Set[T]) Clone() Set[T] {
41
44
return res
42
45
}
43
46
47
+ // Add items to the set.
44
48
func (s Set [T ]) Add (item ... T ) {
45
49
for _ , i := range item {
46
50
s [i ] = struct {}{}
47
51
}
48
52
}
49
53
54
+ // Has returns true if the item is present in the set.
50
55
func (s Set [T ]) Has (item T ) bool {
51
56
_ , found := s [item ]
52
57
return found
53
58
}
54
59
60
+ // Remove items from the set.
55
61
func (s Set [T ]) Remove (item ... T ) {
56
62
for _ , i := range item {
57
63
delete (s , i )
@@ -73,6 +79,7 @@ func Union[T comparable](sets ...Set[T]) Set[T] {
73
79
return res
74
80
}
75
81
82
+ // Intersection returns a new set that has the elements common to all the input sets.
76
83
func Intersection [T comparable ](sets ... Set [T ]) Set [T ] {
77
84
if len (sets ) == 0 {
78
85
return New [T ]()
@@ -91,6 +98,7 @@ func Intersection[T comparable](sets ...Set[T]) Set[T] {
91
98
return res
92
99
}
93
100
101
+ // Elements returns a slice of the elements in the set.
94
102
func (s Set [T ]) Elements () []T {
95
103
res := make ([]T , 0 , len (s ))
96
104
for k := range s {
@@ -127,14 +135,17 @@ func (s Set[T]) Plus(others ...Set[T]) Set[T] {
127
135
return s
128
136
}
129
137
138
+ // Equals returns true if the two sets have the same elements.
130
139
func (s Set [T ]) Equals (other Set [T ]) bool {
131
140
return len (s ) == len (other ) && s .Subset (other )
132
141
}
133
142
143
+ // Len returns the number of elements in the set (same as len(s) but as a method).
134
144
func (s Set [T ]) Len () int {
135
145
return len (s )
136
146
}
137
147
148
+ // Clear removes all elements from the set.
138
149
func (s Set [T ]) Clear () {
139
150
for k := range s {
140
151
delete (s , k )
@@ -207,6 +218,8 @@ func (s *Set[T]) UnmarshalJSON(data []byte) error {
207
218
208
219
// -- Additional operations on sets of ordered types
209
220
221
+ // Sort returns a sorted slice of the elements in the set.
222
+ // Only applicable for when the type is sortable.
210
223
func Sort [Q constraints.Ordered ](s Set [Q ]) []Q {
211
224
keys := s .Elements ()
212
225
sort .Slice (keys , func (i , j int ) bool { return keys [i ] < keys [j ] })
0 commit comments