Skip to content

Commit 46762cf

Browse files
author
Han Kang
committed
make this implementation of sets compatible with the one in apimachinery
1 parent d8088cf commit 46762cf

File tree

4 files changed

+245
-71
lines changed

4 files changed

+245
-71
lines changed

sets/OWNERS renamed to set/OWNERS

File renamed without changes.

set/ordered.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
Copyright 2022 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package set
18+
19+
// Ordered is a constraint that permits any ordered type: any type
20+
// that supports the operators < <= >= >.
21+
// If future releases of Go add new ordered types,
22+
// this constraint will be modified to include them.
23+
type Ordered interface {
24+
integer | float | ~string
25+
}
26+
27+
// integer is a constraint that permits any integer type.
28+
// If future releases of Go add new predeclared integer types,
29+
// this constraint will be modified to include them.
30+
type integer interface {
31+
signed | unsigned
32+
}
33+
34+
// float is a constraint that permits any floating-point type.
35+
// If future releases of Go add new predeclared floating-point types,
36+
// this constraint will be modified to include them.
37+
type float interface {
38+
~float32 | ~float64
39+
}
40+
41+
// signed is a constraint that permits any signed integer type.
42+
// If future releases of Go add new predeclared signed integer types,
43+
// this constraint will be modified to include them.
44+
type signed interface {
45+
~int | ~int8 | ~int16 | ~int32 | ~int64
46+
}
47+
48+
// unsigned is a constraint that permits any unsigned integer type.
49+
// If future releases of Go add new predeclared unsigned integer types,
50+
// this constraint will be modified to include them.
51+
type unsigned interface {
52+
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
53+
}

sets/set.go renamed to set/set.go

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,28 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package sets
17+
package set
1818

19-
import "sort"
20-
21-
type Ordered interface {
22-
int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | uintptr | float32 | float64 | string
23-
}
19+
import (
20+
"sort"
21+
)
2422

2523
// Empty is public since it is used by some internal API objects for conversions between external
2624
// string arrays and internal sets, and conversion logic requires public types today.
2725
type Empty struct{}
2826

27+
// Set is a set of the same type elements, implemented via map[comparable]struct{} for minimal memory consumption.
2928
type Set[E Ordered] map[E]Empty
3029

31-
// NewSet creates a new set.
32-
func NewSet[E Ordered](items ...E) Set[E] {
30+
// New creates a new set.
31+
func New[E Ordered](items ...E) Set[E] {
3332
ss := Set[E]{}
3433
ss.Insert(items...)
3534
return ss
3635
}
3736

38-
// NewSetFromMapKeys creates a Set[E] from a keys of a map[E](? extends interface{}).
39-
func NewSetFromMapKeys[E Ordered, A any](theMap map[E]A) Set[E] {
37+
// KeySet creates a Set[E] from a keys of a map[E](? extends interface{}).
38+
func KeySet[E Ordered, A any](theMap map[E]A) Set[E] {
4039
ret := Set[E]{}
4140
for key := range theMap {
4241
ret.Insert(key)
@@ -86,10 +85,16 @@ func (s Set[E]) HasAny(items ...E) bool {
8685
return false
8786
}
8887

88+
// Union returns a new set which includes items in either s1 or s2.
89+
// For example:
90+
// s1 = {a1, a2}
91+
// s2 = {a3, a4}
92+
// s1.Union(s2) = {a1, a2, a3, a4}
93+
// s2.Union(s1) = {a1, a2, a3, a4}
8994
func (s Set[E]) Union(s2 Set[E]) Set[E] {
9095
result := Set[E]{}
91-
result.Insert(s.List()...)
92-
result.Insert(s2.List()...)
96+
result.Insert(s.UnsortedList()...)
97+
result.Insert(s2.UnsortedList()...)
9398
return result
9499
}
95100

@@ -98,6 +103,11 @@ func (s Set[E]) Len() int {
98103
return len(s)
99104
}
100105

106+
// Intersection returns a new set which includes the item in BOTH s1 and s2
107+
// For example:
108+
// s1 = {a1, a2}
109+
// s2 = {a2, a3}
110+
// s1.Intersection(s2) = {a2}
101111
func (s Set[E]) Intersection(s2 Set[E]) Set[E] {
102112
var walk, other Set[E]
103113
result := Set[E]{}
@@ -144,7 +154,6 @@ func (s Set[E]) Difference(s2 Set[E]) Set[E] {
144154

145155
// Equal returns true if and only if s1 is equal (as a set) to s2.
146156
// Two sets are equal if their membership is identical.
147-
// (In practice, this means same elements, order doesn't matter)
148157
func (s Set[E]) Equal(s2 Set[E]) bool {
149158
return s.Len() == s.Len() && s.IsSuperset(s2)
150159
}
@@ -157,8 +166,8 @@ func (s sortableSlice[E]) Len() int {
157166
func (s sortableSlice[E]) Less(i, j int) bool { return s[i] < s[j] }
158167
func (s sortableSlice[E]) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
159168

160-
// List returns the contents as a sorted int slice.
161-
func (s Set[E]) List() []E {
169+
// SortedList returns the contents as a sorted slice.
170+
func (s Set[E]) SortedList() []E {
162171
res := make(sortableSlice[E], 0, s.Len())
163172
for key := range s {
164173
res = append(res, key)
@@ -169,7 +178,7 @@ func (s Set[E]) List() []E {
169178

170179
// UnsortedList returns the slice with contents in random order.
171180
func (s Set[E]) UnsortedList() []E {
172-
res := make(sortableSlice[E], 0, len(s))
181+
res := make([]E, 0, len(s))
173182
for key := range s {
174183
res = append(res, key)
175184
}
@@ -185,3 +194,36 @@ func (s Set[E]) PopAny() (E, bool) {
185194
var zeroValue E
186195
return zeroValue, false
187196
}
197+
198+
// Clone returns a new set which is a copy of the current set.
199+
func (s Set[T]) Clone() Set[T] {
200+
result := make(Set[T], len(s))
201+
for key := range s {
202+
result.Insert(key)
203+
}
204+
return result
205+
}
206+
207+
// SymmetricDifference returns a set of elements which are in either of the sets, but not in their intersection.
208+
// For example:
209+
// s1 = {a1, a2, a3}
210+
// s2 = {a1, a2, a4, a5}
211+
// s1.SymmetricDifference(s2) = {a3, a4, a5}
212+
// s2.SymmetricDifference(s1) = {a3, a4, a5}
213+
func (s1 Set[T]) SymmetricDifference(s2 Set[T]) Set[T] {
214+
return s1.Difference(s2).Union(s2.Difference(s1))
215+
}
216+
217+
// Clear empties the set.
218+
// It is preferable to replace the set with a newly constructed set,
219+
// but not all callers can do that (when there are other references to the map).
220+
// In some cases the set *won't* be fully cleared, e.g. a Set[float32] containing NaN
221+
// can't be cleared because NaN can't be removed.
222+
// For sets containing items of a type that is reflexive for ==,
223+
// this is optimized to a single call to runtime.mapclear().
224+
func (s Set[T]) Clear() Set[T] {
225+
for key := range s {
226+
delete(s, key)
227+
}
228+
return s
229+
}

0 commit comments

Comments
 (0)