Merge pull request #112377 from weilaaa/refactor_sets_use_generic

refactor sets use generic
This commit is contained in:
Kubernetes Prow Robot 2022-10-24 21:14:34 -07:00 committed by GitHub
commit 256ade5aa4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 811 additions and 591 deletions

View File

@ -1,5 +1,5 @@
/* /*
Copyright The Kubernetes Authors. Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -14,86 +14,55 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
// Code generated by set-gen. DO NOT EDIT.
package sets package sets
import ( // Byte is a set of bytes, implemented via map[byte]struct{} for minimal memory consumption.
"reflect" //
"sort" // Deprecated: use generic Set instead.
) // new ways:
// s1 := Set[byte]{}
// sets.Byte is a set of bytes, implemented via map[byte]struct{} for minimal memory consumption. // s2 := New[byte]()
type Byte map[byte]Empty type Byte map[byte]Empty
// NewByte creates a Byte from a list of values. // NewByte creates a Byte from a list of values.
func NewByte(items ...byte) Byte { func NewByte(items ...byte) Byte {
ss := make(Byte, len(items)) return Byte(New[byte](items...))
ss.Insert(items...)
return ss
} }
// ByteKeySet creates a Byte from a keys of a map[byte](? extends interface{}). // ByteKeySet creates a Byte from a keys of a map[byte](? extends interface{}).
// If the value passed in is not actually a map, this will panic. // If the value passed in is not actually a map, this will panic.
func ByteKeySet(theMap interface{}) Byte { func ByteKeySet[T any](theMap map[byte]T) Byte {
v := reflect.ValueOf(theMap) return Byte(KeySet(theMap))
ret := Byte{}
for _, keyValue := range v.MapKeys() {
ret.Insert(keyValue.Interface().(byte))
}
return ret
} }
// Insert adds items to the set. // Insert adds items to the set.
func (s Byte) Insert(items ...byte) Byte { func (s Byte) Insert(items ...byte) Byte {
for _, item := range items { return Byte(cast(s).Insert(items...))
s[item] = Empty{}
}
return s
} }
// Delete removes all items from the set. // Delete removes all items from the set.
func (s Byte) Delete(items ...byte) Byte { func (s Byte) Delete(items ...byte) Byte {
for _, item := range items { return Byte(cast(s).Delete(items...))
delete(s, item)
}
return s
} }
// Has returns true if and only if item is contained in the set. // Has returns true if and only if item is contained in the set.
func (s Byte) Has(item byte) bool { func (s Byte) Has(item byte) bool {
_, contained := s[item] return cast(s).Has(item)
return contained
} }
// HasAll returns true if and only if all items are contained in the set. // HasAll returns true if and only if all items are contained in the set.
func (s Byte) HasAll(items ...byte) bool { func (s Byte) HasAll(items ...byte) bool {
for _, item := range items { return cast(s).HasAll(items...)
if !s.Has(item) {
return false
}
}
return true
} }
// HasAny returns true if any items are contained in the set. // HasAny returns true if any items are contained in the set.
func (s Byte) HasAny(items ...byte) bool { func (s Byte) HasAny(items ...byte) bool {
for _, item := range items { return cast(s).HasAny(items...)
if s.Has(item) {
return true
}
}
return false
} }
// Clone returns a new set which is a copy of the current set. // Clone returns a new set which is a copy of the current set.
func (s Byte) Clone() Byte { func (s Byte) Clone() Byte {
result := make(Byte, len(s)) return Byte(cast(s).Clone())
for key := range s {
result.Insert(key)
}
return result
} }
// Difference returns a set of objects that are not in s2. // Difference returns a set of objects that are not in s2.
@ -103,13 +72,7 @@ func (s Byte) Clone() Byte {
// s1.Difference(s2) = {a3} // s1.Difference(s2) = {a3}
// s2.Difference(s1) = {a4, a5} // s2.Difference(s1) = {a4, a5}
func (s1 Byte) Difference(s2 Byte) Byte { func (s1 Byte) Difference(s2 Byte) Byte {
result := NewByte() return Byte(cast(s1).Difference(cast(s2)))
for key := range s1 {
if !s2.Has(key) {
result.Insert(key)
}
}
return result
} }
// SymmetricDifference returns a set of elements which are in either of the sets, but not in their intersection. // SymmetricDifference returns a set of elements which are in either of the sets, but not in their intersection.
@ -119,7 +82,7 @@ func (s1 Byte) Difference(s2 Byte) Byte {
// s1.SymmetricDifference(s2) = {a3, a4, a5} // s1.SymmetricDifference(s2) = {a3, a4, a5}
// s2.SymmetricDifference(s1) = {a3, a4, a5} // s2.SymmetricDifference(s1) = {a3, a4, a5}
func (s1 Byte) SymmetricDifference(s2 Byte) Byte { func (s1 Byte) SymmetricDifference(s2 Byte) Byte {
return s1.Difference(s2).Union(s2.Difference(s1)) return Byte(cast(s1).SymmetricDifference(cast(s2)))
} }
// Union returns a new set which includes items in either s1 or s2. // Union returns a new set which includes items in either s1 or s2.
@ -129,11 +92,7 @@ func (s1 Byte) SymmetricDifference(s2 Byte) Byte {
// s1.Union(s2) = {a1, a2, a3, a4} // s1.Union(s2) = {a1, a2, a3, a4}
// s2.Union(s1) = {a1, a2, a3, a4} // s2.Union(s1) = {a1, a2, a3, a4}
func (s1 Byte) Union(s2 Byte) Byte { func (s1 Byte) Union(s2 Byte) Byte {
result := s1.Clone() return Byte(cast(s1).Union(cast(s2)))
for key := range s2 {
result.Insert(key)
}
return result
} }
// Intersection returns a new set which includes the item in BOTH s1 and s2 // Intersection returns a new set which includes the item in BOTH s1 and s2
@ -142,80 +101,37 @@ func (s1 Byte) Union(s2 Byte) Byte {
// s2 = {a2, a3} // s2 = {a2, a3}
// s1.Intersection(s2) = {a2} // s1.Intersection(s2) = {a2}
func (s1 Byte) Intersection(s2 Byte) Byte { func (s1 Byte) Intersection(s2 Byte) Byte {
var walk, other Byte return Byte(cast(s1).Intersection(cast(s2)))
result := NewByte()
if s1.Len() < s2.Len() {
walk = s1
other = s2
} else {
walk = s2
other = s1
}
for key := range walk {
if other.Has(key) {
result.Insert(key)
}
}
return result
} }
// IsSuperset returns true if and only if s1 is a superset of s2. // IsSuperset returns true if and only if s1 is a superset of s2.
func (s1 Byte) IsSuperset(s2 Byte) bool { func (s1 Byte) IsSuperset(s2 Byte) bool {
for item := range s2 { return cast(s1).IsSuperset(cast(s2))
if !s1.Has(item) {
return false
}
}
return true
} }
// Equal returns true if and only if s1 is equal (as a set) to s2. // Equal returns true if and only if s1 is equal (as a set) to s2.
// Two sets are equal if their membership is identical. // Two sets are equal if their membership is identical.
// (In practice, this means same elements, order doesn't matter) // (In practice, this means same elements, order doesn't matter)
func (s1 Byte) Equal(s2 Byte) bool { func (s1 Byte) Equal(s2 Byte) bool {
return len(s1) == len(s2) && s1.IsSuperset(s2) return cast(s1).Equal(cast(s2))
} }
type sortableSliceOfByte []byte
func (s sortableSliceOfByte) Len() int { return len(s) }
func (s sortableSliceOfByte) Less(i, j int) bool { return lessByte(s[i], s[j]) }
func (s sortableSliceOfByte) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
// List returns the contents as a sorted byte slice. // List returns the contents as a sorted byte slice.
func (s Byte) List() []byte { func (s Byte) List() []byte {
res := make(sortableSliceOfByte, 0, len(s)) return List(cast(s))
for key := range s {
res = append(res, key)
}
sort.Sort(res)
return []byte(res)
} }
// UnsortedList returns the slice with contents in random order. // UnsortedList returns the slice with contents in random order.
func (s Byte) UnsortedList() []byte { func (s Byte) UnsortedList() []byte {
res := make([]byte, 0, len(s)) return cast(s).UnsortedList()
for key := range s {
res = append(res, key)
}
return res
} }
// Returns a single element from the set. // PopAny returns a single element from the set.
func (s Byte) PopAny() (byte, bool) { func (s Byte) PopAny() (byte, bool) {
for key := range s { return cast(s).PopAny()
s.Delete(key)
return key, true
}
var zeroValue byte
return zeroValue, false
} }
// Len returns the size of the set. // Len returns the size of the set.
func (s Byte) Len() int { func (s Byte) Len() int {
return len(s) return len(s)
} }
func lessByte(lhs, rhs byte) bool {
return lhs < rhs
}

View File

@ -1,5 +1,5 @@
/* /*
Copyright The Kubernetes Authors. Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -14,7 +14,6 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
// Code generated by set-gen. DO NOT EDIT. // Package sets has generic set and specified sets. Generic set will
// replace specified ones over time. And specific ones are deprecated.
// Package sets has auto-generated set types.
package sets package sets

View File

@ -1,5 +1,5 @@
/* /*
Copyright The Kubernetes Authors. Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -14,8 +14,6 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
// Code generated by set-gen. DO NOT EDIT.
package sets package sets
// Empty is public since it is used by some internal API objects for conversions between external // Empty is public since it is used by some internal API objects for conversions between external

View File

@ -1,5 +1,5 @@
/* /*
Copyright The Kubernetes Authors. Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -14,86 +14,55 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
// Code generated by set-gen. DO NOT EDIT.
package sets package sets
import ( // Int is a set of ints, implemented via map[int]struct{} for minimal memory consumption.
"reflect" //
"sort" // Deprecated: use generic Set instead.
) // new ways:
// s1 := Set[int]{}
// sets.Int is a set of ints, implemented via map[int]struct{} for minimal memory consumption. // s2 := New[int]()
type Int map[int]Empty type Int map[int]Empty
// NewInt creates a Int from a list of values. // NewInt creates a Int from a list of values.
func NewInt(items ...int) Int { func NewInt(items ...int) Int {
ss := make(Int, len(items)) return Int(New[int](items...))
ss.Insert(items...)
return ss
} }
// IntKeySet creates a Int from a keys of a map[int](? extends interface{}). // IntKeySet creates a Int from a keys of a map[int](? extends interface{}).
// If the value passed in is not actually a map, this will panic. // If the value passed in is not actually a map, this will panic.
func IntKeySet(theMap interface{}) Int { func IntKeySet[T any](theMap map[int]T) Int {
v := reflect.ValueOf(theMap) return Int(KeySet(theMap))
ret := Int{}
for _, keyValue := range v.MapKeys() {
ret.Insert(keyValue.Interface().(int))
}
return ret
} }
// Insert adds items to the set. // Insert adds items to the set.
func (s Int) Insert(items ...int) Int { func (s Int) Insert(items ...int) Int {
for _, item := range items { return Int(cast(s).Insert(items...))
s[item] = Empty{}
}
return s
} }
// Delete removes all items from the set. // Delete removes all items from the set.
func (s Int) Delete(items ...int) Int { func (s Int) Delete(items ...int) Int {
for _, item := range items { return Int(cast(s).Delete(items...))
delete(s, item)
}
return s
} }
// Has returns true if and only if item is contained in the set. // Has returns true if and only if item is contained in the set.
func (s Int) Has(item int) bool { func (s Int) Has(item int) bool {
_, contained := s[item] return cast(s).Has(item)
return contained
} }
// HasAll returns true if and only if all items are contained in the set. // HasAll returns true if and only if all items are contained in the set.
func (s Int) HasAll(items ...int) bool { func (s Int) HasAll(items ...int) bool {
for _, item := range items { return cast(s).HasAll(items...)
if !s.Has(item) {
return false
}
}
return true
} }
// HasAny returns true if any items are contained in the set. // HasAny returns true if any items are contained in the set.
func (s Int) HasAny(items ...int) bool { func (s Int) HasAny(items ...int) bool {
for _, item := range items { return cast(s).HasAny(items...)
if s.Has(item) {
return true
}
}
return false
} }
// Clone returns a new set which is a copy of the current set. // Clone returns a new set which is a copy of the current set.
func (s Int) Clone() Int { func (s Int) Clone() Int {
result := make(Int, len(s)) return Int(cast(s).Clone())
for key := range s {
result.Insert(key)
}
return result
} }
// Difference returns a set of objects that are not in s2. // Difference returns a set of objects that are not in s2.
@ -103,13 +72,7 @@ func (s Int) Clone() Int {
// s1.Difference(s2) = {a3} // s1.Difference(s2) = {a3}
// s2.Difference(s1) = {a4, a5} // s2.Difference(s1) = {a4, a5}
func (s1 Int) Difference(s2 Int) Int { func (s1 Int) Difference(s2 Int) Int {
result := NewInt() return Int(cast(s1).Difference(cast(s2)))
for key := range s1 {
if !s2.Has(key) {
result.Insert(key)
}
}
return result
} }
// SymmetricDifference returns a set of elements which are in either of the sets, but not in their intersection. // SymmetricDifference returns a set of elements which are in either of the sets, but not in their intersection.
@ -119,7 +82,7 @@ func (s1 Int) Difference(s2 Int) Int {
// s1.SymmetricDifference(s2) = {a3, a4, a5} // s1.SymmetricDifference(s2) = {a3, a4, a5}
// s2.SymmetricDifference(s1) = {a3, a4, a5} // s2.SymmetricDifference(s1) = {a3, a4, a5}
func (s1 Int) SymmetricDifference(s2 Int) Int { func (s1 Int) SymmetricDifference(s2 Int) Int {
return s1.Difference(s2).Union(s2.Difference(s1)) return Int(cast(s1).SymmetricDifference(cast(s2)))
} }
// Union returns a new set which includes items in either s1 or s2. // Union returns a new set which includes items in either s1 or s2.
@ -129,11 +92,7 @@ func (s1 Int) SymmetricDifference(s2 Int) Int {
// s1.Union(s2) = {a1, a2, a3, a4} // s1.Union(s2) = {a1, a2, a3, a4}
// s2.Union(s1) = {a1, a2, a3, a4} // s2.Union(s1) = {a1, a2, a3, a4}
func (s1 Int) Union(s2 Int) Int { func (s1 Int) Union(s2 Int) Int {
result := s1.Clone() return Int(cast(s1).Union(cast(s2)))
for key := range s2 {
result.Insert(key)
}
return result
} }
// Intersection returns a new set which includes the item in BOTH s1 and s2 // Intersection returns a new set which includes the item in BOTH s1 and s2
@ -142,80 +101,37 @@ func (s1 Int) Union(s2 Int) Int {
// s2 = {a2, a3} // s2 = {a2, a3}
// s1.Intersection(s2) = {a2} // s1.Intersection(s2) = {a2}
func (s1 Int) Intersection(s2 Int) Int { func (s1 Int) Intersection(s2 Int) Int {
var walk, other Int return Int(cast(s1).Intersection(cast(s2)))
result := NewInt()
if s1.Len() < s2.Len() {
walk = s1
other = s2
} else {
walk = s2
other = s1
}
for key := range walk {
if other.Has(key) {
result.Insert(key)
}
}
return result
} }
// IsSuperset returns true if and only if s1 is a superset of s2. // IsSuperset returns true if and only if s1 is a superset of s2.
func (s1 Int) IsSuperset(s2 Int) bool { func (s1 Int) IsSuperset(s2 Int) bool {
for item := range s2 { return cast(s1).IsSuperset(cast(s2))
if !s1.Has(item) {
return false
}
}
return true
} }
// Equal returns true if and only if s1 is equal (as a set) to s2. // Equal returns true if and only if s1 is equal (as a set) to s2.
// Two sets are equal if their membership is identical. // Two sets are equal if their membership is identical.
// (In practice, this means same elements, order doesn't matter) // (In practice, this means same elements, order doesn't matter)
func (s1 Int) Equal(s2 Int) bool { func (s1 Int) Equal(s2 Int) bool {
return len(s1) == len(s2) && s1.IsSuperset(s2) return cast(s1).Equal(cast(s2))
} }
type sortableSliceOfInt []int
func (s sortableSliceOfInt) Len() int { return len(s) }
func (s sortableSliceOfInt) Less(i, j int) bool { return lessInt(s[i], s[j]) }
func (s sortableSliceOfInt) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
// List returns the contents as a sorted int slice. // List returns the contents as a sorted int slice.
func (s Int) List() []int { func (s Int) List() []int {
res := make(sortableSliceOfInt, 0, len(s)) return List(cast(s))
for key := range s {
res = append(res, key)
}
sort.Sort(res)
return []int(res)
} }
// UnsortedList returns the slice with contents in random order. // UnsortedList returns the slice with contents in random order.
func (s Int) UnsortedList() []int { func (s Int) UnsortedList() []int {
res := make([]int, 0, len(s)) return cast(s).UnsortedList()
for key := range s {
res = append(res, key)
}
return res
} }
// Returns a single element from the set. // PopAny returns a single element from the set.
func (s Int) PopAny() (int, bool) { func (s Int) PopAny() (int, bool) {
for key := range s { return cast(s).PopAny()
s.Delete(key)
return key, true
}
var zeroValue int
return zeroValue, false
} }
// Len returns the size of the set. // Len returns the size of the set.
func (s Int) Len() int { func (s Int) Len() int {
return len(s) return len(s)
} }
func lessInt(lhs, rhs int) bool {
return lhs < rhs
}

View File

@ -1,5 +1,5 @@
/* /*
Copyright The Kubernetes Authors. Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -14,86 +14,55 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
// Code generated by set-gen. DO NOT EDIT.
package sets package sets
import ( // Int32 is a set of int32s, implemented via map[int32]struct{} for minimal memory consumption.
"reflect" //
"sort" // Deprecated: use generic Set instead.
) // new ways:
// s1 := Set[int32]{}
// sets.Int32 is a set of int32s, implemented via map[int32]struct{} for minimal memory consumption. // s2 := New[int32]()
type Int32 map[int32]Empty type Int32 map[int32]Empty
// NewInt32 creates a Int32 from a list of values. // NewInt32 creates a Int32 from a list of values.
func NewInt32(items ...int32) Int32 { func NewInt32(items ...int32) Int32 {
ss := make(Int32, len(items)) return Int32(New[int32](items...))
ss.Insert(items...)
return ss
} }
// Int32KeySet creates a Int32 from a keys of a map[int32](? extends interface{}). // Int32KeySet creates a Int32 from a keys of a map[int32](? extends interface{}).
// If the value passed in is not actually a map, this will panic. // If the value passed in is not actually a map, this will panic.
func Int32KeySet(theMap interface{}) Int32 { func Int32KeySet[T any](theMap map[int32]T) Int32 {
v := reflect.ValueOf(theMap) return Int32(KeySet(theMap))
ret := Int32{}
for _, keyValue := range v.MapKeys() {
ret.Insert(keyValue.Interface().(int32))
}
return ret
} }
// Insert adds items to the set. // Insert adds items to the set.
func (s Int32) Insert(items ...int32) Int32 { func (s Int32) Insert(items ...int32) Int32 {
for _, item := range items { return Int32(cast(s).Insert(items...))
s[item] = Empty{}
}
return s
} }
// Delete removes all items from the set. // Delete removes all items from the set.
func (s Int32) Delete(items ...int32) Int32 { func (s Int32) Delete(items ...int32) Int32 {
for _, item := range items { return Int32(cast(s).Delete(items...))
delete(s, item)
}
return s
} }
// Has returns true if and only if item is contained in the set. // Has returns true if and only if item is contained in the set.
func (s Int32) Has(item int32) bool { func (s Int32) Has(item int32) bool {
_, contained := s[item] return cast(s).Has(item)
return contained
} }
// HasAll returns true if and only if all items are contained in the set. // HasAll returns true if and only if all items are contained in the set.
func (s Int32) HasAll(items ...int32) bool { func (s Int32) HasAll(items ...int32) bool {
for _, item := range items { return cast(s).HasAll(items...)
if !s.Has(item) {
return false
}
}
return true
} }
// HasAny returns true if any items are contained in the set. // HasAny returns true if any items are contained in the set.
func (s Int32) HasAny(items ...int32) bool { func (s Int32) HasAny(items ...int32) bool {
for _, item := range items { return cast(s).HasAny(items...)
if s.Has(item) {
return true
}
}
return false
} }
// Clone returns a new set which is a copy of the current set. // Clone returns a new set which is a copy of the current set.
func (s Int32) Clone() Int32 { func (s Int32) Clone() Int32 {
result := make(Int32, len(s)) return Int32(cast(s).Clone())
for key := range s {
result.Insert(key)
}
return result
} }
// Difference returns a set of objects that are not in s2. // Difference returns a set of objects that are not in s2.
@ -103,13 +72,7 @@ func (s Int32) Clone() Int32 {
// s1.Difference(s2) = {a3} // s1.Difference(s2) = {a3}
// s2.Difference(s1) = {a4, a5} // s2.Difference(s1) = {a4, a5}
func (s1 Int32) Difference(s2 Int32) Int32 { func (s1 Int32) Difference(s2 Int32) Int32 {
result := NewInt32() return Int32(cast(s1).Difference(cast(s2)))
for key := range s1 {
if !s2.Has(key) {
result.Insert(key)
}
}
return result
} }
// SymmetricDifference returns a set of elements which are in either of the sets, but not in their intersection. // SymmetricDifference returns a set of elements which are in either of the sets, but not in their intersection.
@ -119,7 +82,7 @@ func (s1 Int32) Difference(s2 Int32) Int32 {
// s1.SymmetricDifference(s2) = {a3, a4, a5} // s1.SymmetricDifference(s2) = {a3, a4, a5}
// s2.SymmetricDifference(s1) = {a3, a4, a5} // s2.SymmetricDifference(s1) = {a3, a4, a5}
func (s1 Int32) SymmetricDifference(s2 Int32) Int32 { func (s1 Int32) SymmetricDifference(s2 Int32) Int32 {
return s1.Difference(s2).Union(s2.Difference(s1)) return Int32(cast(s1).SymmetricDifference(cast(s2)))
} }
// Union returns a new set which includes items in either s1 or s2. // Union returns a new set which includes items in either s1 or s2.
@ -129,11 +92,7 @@ func (s1 Int32) SymmetricDifference(s2 Int32) Int32 {
// s1.Union(s2) = {a1, a2, a3, a4} // s1.Union(s2) = {a1, a2, a3, a4}
// s2.Union(s1) = {a1, a2, a3, a4} // s2.Union(s1) = {a1, a2, a3, a4}
func (s1 Int32) Union(s2 Int32) Int32 { func (s1 Int32) Union(s2 Int32) Int32 {
result := s1.Clone() return Int32(cast(s1).Union(cast(s2)))
for key := range s2 {
result.Insert(key)
}
return result
} }
// Intersection returns a new set which includes the item in BOTH s1 and s2 // Intersection returns a new set which includes the item in BOTH s1 and s2
@ -142,80 +101,37 @@ func (s1 Int32) Union(s2 Int32) Int32 {
// s2 = {a2, a3} // s2 = {a2, a3}
// s1.Intersection(s2) = {a2} // s1.Intersection(s2) = {a2}
func (s1 Int32) Intersection(s2 Int32) Int32 { func (s1 Int32) Intersection(s2 Int32) Int32 {
var walk, other Int32 return Int32(cast(s1).Intersection(cast(s2)))
result := NewInt32()
if s1.Len() < s2.Len() {
walk = s1
other = s2
} else {
walk = s2
other = s1
}
for key := range walk {
if other.Has(key) {
result.Insert(key)
}
}
return result
} }
// IsSuperset returns true if and only if s1 is a superset of s2. // IsSuperset returns true if and only if s1 is a superset of s2.
func (s1 Int32) IsSuperset(s2 Int32) bool { func (s1 Int32) IsSuperset(s2 Int32) bool {
for item := range s2 { return cast(s1).IsSuperset(cast(s2))
if !s1.Has(item) {
return false
}
}
return true
} }
// Equal returns true if and only if s1 is equal (as a set) to s2. // Equal returns true if and only if s1 is equal (as a set) to s2.
// Two sets are equal if their membership is identical. // Two sets are equal if their membership is identical.
// (In practice, this means same elements, order doesn't matter) // (In practice, this means same elements, order doesn't matter)
func (s1 Int32) Equal(s2 Int32) bool { func (s1 Int32) Equal(s2 Int32) bool {
return len(s1) == len(s2) && s1.IsSuperset(s2) return cast(s1).Equal(cast(s2))
} }
type sortableSliceOfInt32 []int32
func (s sortableSliceOfInt32) Len() int { return len(s) }
func (s sortableSliceOfInt32) Less(i, j int) bool { return lessInt32(s[i], s[j]) }
func (s sortableSliceOfInt32) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
// List returns the contents as a sorted int32 slice. // List returns the contents as a sorted int32 slice.
func (s Int32) List() []int32 { func (s Int32) List() []int32 {
res := make(sortableSliceOfInt32, 0, len(s)) return List(cast(s))
for key := range s {
res = append(res, key)
}
sort.Sort(res)
return []int32(res)
} }
// UnsortedList returns the slice with contents in random order. // UnsortedList returns the slice with contents in random order.
func (s Int32) UnsortedList() []int32 { func (s Int32) UnsortedList() []int32 {
res := make([]int32, 0, len(s)) return cast(s).UnsortedList()
for key := range s {
res = append(res, key)
}
return res
} }
// Returns a single element from the set. // PopAny returns a single element from the set.
func (s Int32) PopAny() (int32, bool) { func (s Int32) PopAny() (int32, bool) {
for key := range s { return cast(s).PopAny()
s.Delete(key)
return key, true
}
var zeroValue int32
return zeroValue, false
} }
// Len returns the size of the set. // Len returns the size of the set.
func (s Int32) Len() int { func (s Int32) Len() int {
return len(s) return len(s)
} }
func lessInt32(lhs, rhs int32) bool {
return lhs < rhs
}

View File

@ -1,5 +1,5 @@
/* /*
Copyright The Kubernetes Authors. Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -14,86 +14,55 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
// Code generated by set-gen. DO NOT EDIT.
package sets package sets
import ( // Int64 is a set of int64s, implemented via map[int64]struct{} for minimal memory consumption.
"reflect" //
"sort" // Deprecated: use generic Set instead.
) // new ways:
// s1 := Set[int64]{}
// sets.Int64 is a set of int64s, implemented via map[int64]struct{} for minimal memory consumption. // s2 := New[int64]()
type Int64 map[int64]Empty type Int64 map[int64]Empty
// NewInt64 creates a Int64 from a list of values. // NewInt64 creates a Int64 from a list of values.
func NewInt64(items ...int64) Int64 { func NewInt64(items ...int64) Int64 {
ss := make(Int64, len(items)) return Int64(New[int64](items...))
ss.Insert(items...)
return ss
} }
// Int64KeySet creates a Int64 from a keys of a map[int64](? extends interface{}). // Int64KeySet creates a Int64 from a keys of a map[int64](? extends interface{}).
// If the value passed in is not actually a map, this will panic. // If the value passed in is not actually a map, this will panic.
func Int64KeySet(theMap interface{}) Int64 { func Int64KeySet[T any](theMap map[int64]T) Int64 {
v := reflect.ValueOf(theMap) return Int64(KeySet(theMap))
ret := Int64{}
for _, keyValue := range v.MapKeys() {
ret.Insert(keyValue.Interface().(int64))
}
return ret
} }
// Insert adds items to the set. // Insert adds items to the set.
func (s Int64) Insert(items ...int64) Int64 { func (s Int64) Insert(items ...int64) Int64 {
for _, item := range items { return Int64(cast(s).Insert(items...))
s[item] = Empty{}
}
return s
} }
// Delete removes all items from the set. // Delete removes all items from the set.
func (s Int64) Delete(items ...int64) Int64 { func (s Int64) Delete(items ...int64) Int64 {
for _, item := range items { return Int64(cast(s).Delete(items...))
delete(s, item)
}
return s
} }
// Has returns true if and only if item is contained in the set. // Has returns true if and only if item is contained in the set.
func (s Int64) Has(item int64) bool { func (s Int64) Has(item int64) bool {
_, contained := s[item] return cast(s).Has(item)
return contained
} }
// HasAll returns true if and only if all items are contained in the set. // HasAll returns true if and only if all items are contained in the set.
func (s Int64) HasAll(items ...int64) bool { func (s Int64) HasAll(items ...int64) bool {
for _, item := range items { return cast(s).HasAll(items...)
if !s.Has(item) {
return false
}
}
return true
} }
// HasAny returns true if any items are contained in the set. // HasAny returns true if any items are contained in the set.
func (s Int64) HasAny(items ...int64) bool { func (s Int64) HasAny(items ...int64) bool {
for _, item := range items { return cast(s).HasAny(items...)
if s.Has(item) {
return true
}
}
return false
} }
// Clone returns a new set which is a copy of the current set. // Clone returns a new set which is a copy of the current set.
func (s Int64) Clone() Int64 { func (s Int64) Clone() Int64 {
result := make(Int64, len(s)) return Int64(cast(s).Clone())
for key := range s {
result.Insert(key)
}
return result
} }
// Difference returns a set of objects that are not in s2. // Difference returns a set of objects that are not in s2.
@ -103,13 +72,7 @@ func (s Int64) Clone() Int64 {
// s1.Difference(s2) = {a3} // s1.Difference(s2) = {a3}
// s2.Difference(s1) = {a4, a5} // s2.Difference(s1) = {a4, a5}
func (s1 Int64) Difference(s2 Int64) Int64 { func (s1 Int64) Difference(s2 Int64) Int64 {
result := NewInt64() return Int64(cast(s1).Difference(cast(s2)))
for key := range s1 {
if !s2.Has(key) {
result.Insert(key)
}
}
return result
} }
// SymmetricDifference returns a set of elements which are in either of the sets, but not in their intersection. // SymmetricDifference returns a set of elements which are in either of the sets, but not in their intersection.
@ -119,7 +82,7 @@ func (s1 Int64) Difference(s2 Int64) Int64 {
// s1.SymmetricDifference(s2) = {a3, a4, a5} // s1.SymmetricDifference(s2) = {a3, a4, a5}
// s2.SymmetricDifference(s1) = {a3, a4, a5} // s2.SymmetricDifference(s1) = {a3, a4, a5}
func (s1 Int64) SymmetricDifference(s2 Int64) Int64 { func (s1 Int64) SymmetricDifference(s2 Int64) Int64 {
return s1.Difference(s2).Union(s2.Difference(s1)) return Int64(cast(s1).SymmetricDifference(cast(s2)))
} }
// Union returns a new set which includes items in either s1 or s2. // Union returns a new set which includes items in either s1 or s2.
@ -129,11 +92,7 @@ func (s1 Int64) SymmetricDifference(s2 Int64) Int64 {
// s1.Union(s2) = {a1, a2, a3, a4} // s1.Union(s2) = {a1, a2, a3, a4}
// s2.Union(s1) = {a1, a2, a3, a4} // s2.Union(s1) = {a1, a2, a3, a4}
func (s1 Int64) Union(s2 Int64) Int64 { func (s1 Int64) Union(s2 Int64) Int64 {
result := s1.Clone() return Int64(cast(s1).Union(cast(s2)))
for key := range s2 {
result.Insert(key)
}
return result
} }
// Intersection returns a new set which includes the item in BOTH s1 and s2 // Intersection returns a new set which includes the item in BOTH s1 and s2
@ -142,80 +101,37 @@ func (s1 Int64) Union(s2 Int64) Int64 {
// s2 = {a2, a3} // s2 = {a2, a3}
// s1.Intersection(s2) = {a2} // s1.Intersection(s2) = {a2}
func (s1 Int64) Intersection(s2 Int64) Int64 { func (s1 Int64) Intersection(s2 Int64) Int64 {
var walk, other Int64 return Int64(cast(s1).Intersection(cast(s2)))
result := NewInt64()
if s1.Len() < s2.Len() {
walk = s1
other = s2
} else {
walk = s2
other = s1
}
for key := range walk {
if other.Has(key) {
result.Insert(key)
}
}
return result
} }
// IsSuperset returns true if and only if s1 is a superset of s2. // IsSuperset returns true if and only if s1 is a superset of s2.
func (s1 Int64) IsSuperset(s2 Int64) bool { func (s1 Int64) IsSuperset(s2 Int64) bool {
for item := range s2 { return cast(s1).IsSuperset(cast(s2))
if !s1.Has(item) {
return false
}
}
return true
} }
// Equal returns true if and only if s1 is equal (as a set) to s2. // Equal returns true if and only if s1 is equal (as a set) to s2.
// Two sets are equal if their membership is identical. // Two sets are equal if their membership is identical.
// (In practice, this means same elements, order doesn't matter) // (In practice, this means same elements, order doesn't matter)
func (s1 Int64) Equal(s2 Int64) bool { func (s1 Int64) Equal(s2 Int64) bool {
return len(s1) == len(s2) && s1.IsSuperset(s2) return cast(s1).Equal(cast(s2))
} }
type sortableSliceOfInt64 []int64
func (s sortableSliceOfInt64) Len() int { return len(s) }
func (s sortableSliceOfInt64) Less(i, j int) bool { return lessInt64(s[i], s[j]) }
func (s sortableSliceOfInt64) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
// List returns the contents as a sorted int64 slice. // List returns the contents as a sorted int64 slice.
func (s Int64) List() []int64 { func (s Int64) List() []int64 {
res := make(sortableSliceOfInt64, 0, len(s)) return List(cast(s))
for key := range s {
res = append(res, key)
}
sort.Sort(res)
return []int64(res)
} }
// UnsortedList returns the slice with contents in random order. // UnsortedList returns the slice with contents in random order.
func (s Int64) UnsortedList() []int64 { func (s Int64) UnsortedList() []int64 {
res := make([]int64, 0, len(s)) return cast(s).UnsortedList()
for key := range s {
res = append(res, key)
}
return res
} }
// Returns a single element from the set. // PopAny returns a single element from the set.
func (s Int64) PopAny() (int64, bool) { func (s Int64) PopAny() (int64, bool) {
for key := range s { return cast(s).PopAny()
s.Delete(key)
return key, true
}
var zeroValue int64
return zeroValue, false
} }
// Len returns the size of the set. // Len returns the size of the set.
func (s Int64) Len() int { func (s Int64) Len() int {
return len(s) return len(s)
} }
func lessInt64(lhs, rhs int64) bool {
return lhs < rhs
}

View File

@ -0,0 +1,53 @@
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package sets
// ordered is a constraint that permits any ordered type: any type
// that supports the operators < <= >= >.
// If future releases of Go add new ordered types,
// this constraint will be modified to include them.
type ordered interface {
integer | float | ~string
}
// integer is a constraint that permits any integer type.
// If future releases of Go add new predeclared integer types,
// this constraint will be modified to include them.
type integer interface {
signed | unsigned
}
// float is a constraint that permits any floating-point type.
// If future releases of Go add new predeclared floating-point types,
// this constraint will be modified to include them.
type float interface {
~float32 | ~float64
}
// signed is a constraint that permits any signed integer type.
// If future releases of Go add new predeclared signed integer types,
// this constraint will be modified to include them.
type signed interface {
~int | ~int8 | ~int16 | ~int32 | ~int64
}
// unsigned is a constraint that permits any unsigned integer type.
// If future releases of Go add new predeclared unsigned integer types,
// this constraint will be modified to include them.
type unsigned interface {
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

View File

@ -0,0 +1,227 @@
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package sets
import (
"sort"
)
// Set is a set of the same type elements, implemented via map[comparable]struct{} for minimal memory consumption.
type Set[T comparable] map[T]Empty
// cast transforms specified set to generic Set[T].
func cast[T comparable](s map[T]Empty) Set[T] { return s }
// New creates a Set from a list of values.
// NOTE: type param must be explicitly instantiated if given items are empty.
func New[T comparable](items ...T) Set[T] {
ss := make(Set[T], len(items))
ss.Insert(items...)
return ss
}
// KeySet creates a Set from a keys of a map[comparable](? extends interface{}).
// If the value passed in is not actually a map, this will panic.
func KeySet[T comparable, V any](theMap map[T]V) Set[T] {
ret := Set[T]{}
for keyValue := range theMap {
ret.Insert(keyValue)
}
return ret
}
// Insert adds items to the set.
func (s Set[T]) Insert(items ...T) Set[T] {
for _, item := range items {
s[item] = Empty{}
}
return s
}
func Insert[T comparable](set Set[T], items ...T) Set[T] {
return set.Insert(items...)
}
// Delete removes all items from the set.
func (s Set[T]) Delete(items ...T) Set[T] {
for _, item := range items {
delete(s, item)
}
return s
}
// Has returns true if and only if item is contained in the set.
func (s Set[T]) Has(item T) bool {
_, contained := s[item]
return contained
}
// HasAll returns true if and only if all items are contained in the set.
func (s Set[T]) HasAll(items ...T) bool {
for _, item := range items {
if !s.Has(item) {
return false
}
}
return true
}
// HasAny returns true if any items are contained in the set.
func (s Set[T]) HasAny(items ...T) bool {
for _, item := range items {
if s.Has(item) {
return true
}
}
return false
}
// Clone returns a new set which is a copy of the current set.
func (s Set[T]) Clone() Set[T] {
result := make(Set[T], len(s))
for key := range s {
result.Insert(key)
}
return result
}
// Difference returns a set of objects that are not in s2.
// For example:
// s1 = {a1, a2, a3}
// s2 = {a1, a2, a4, a5}
// s1.Difference(s2) = {a3}
// s2.Difference(s1) = {a4, a5}
func (s1 Set[T]) Difference(s2 Set[T]) Set[T] {
result := New[T]()
for key := range s1 {
if !s2.Has(key) {
result.Insert(key)
}
}
return result
}
// SymmetricDifference returns a set of elements which are in either of the sets, but not in their intersection.
// For example:
// s1 = {a1, a2, a3}
// s2 = {a1, a2, a4, a5}
// s1.SymmetricDifference(s2) = {a3, a4, a5}
// s2.SymmetricDifference(s1) = {a3, a4, a5}
func (s1 Set[T]) SymmetricDifference(s2 Set[T]) Set[T] {
return s1.Difference(s2).Union(s2.Difference(s1))
}
// Union returns a new set which includes items in either s1 or s2.
// For example:
// s1 = {a1, a2}
// s2 = {a3, a4}
// s1.Union(s2) = {a1, a2, a3, a4}
// s2.Union(s1) = {a1, a2, a3, a4}
func (s1 Set[T]) Union(s2 Set[T]) Set[T] {
result := s1.Clone()
for key := range s2 {
result.Insert(key)
}
return result
}
// Intersection returns a new set which includes the item in BOTH s1 and s2
// For example:
// s1 = {a1, a2}
// s2 = {a2, a3}
// s1.Intersection(s2) = {a2}
func (s1 Set[T]) Intersection(s2 Set[T]) Set[T] {
var walk, other Set[T]
result := New[T]()
if s1.Len() < s2.Len() {
walk = s1
other = s2
} else {
walk = s2
other = s1
}
for key := range walk {
if other.Has(key) {
result.Insert(key)
}
}
return result
}
// IsSuperset returns true if and only if s1 is a superset of s2.
func (s1 Set[T]) IsSuperset(s2 Set[T]) bool {
for item := range s2 {
if !s1.Has(item) {
return false
}
}
return true
}
// Equal returns true if and only if s1 is equal (as a set) to s2.
// Two sets are equal if their membership is identical.
// (In practice, this means same elements, order doesn't matter)
func (s1 Set[T]) Equal(s2 Set[T]) bool {
return len(s1) == len(s2) && s1.IsSuperset(s2)
}
type sortableSliceOfGeneric[T ordered] []T
func (g sortableSliceOfGeneric[T]) Len() int { return len(g) }
func (g sortableSliceOfGeneric[T]) Less(i, j int) bool { return less[T](g[i], g[j]) }
func (g sortableSliceOfGeneric[T]) Swap(i, j int) { g[i], g[j] = g[j], g[i] }
// List returns the contents as a sorted T slice.
//
// This is a separate function and not a method because not all types supported
// by Generic are ordered and only those can be sorted.
func List[T ordered](s Set[T]) []T {
res := make(sortableSliceOfGeneric[T], 0, len(s))
for key := range s {
res = append(res, key)
}
sort.Sort(res)
return res
}
// UnsortedList returns the slice with contents in random order.
func (s Set[T]) UnsortedList() []T {
res := make([]T, 0, len(s))
for key := range s {
res = append(res, key)
}
return res
}
// PopAny returns a single element from the set.
func (s Set[T]) PopAny() (T, bool) {
for key := range s {
s.Delete(key)
return key, true
}
var zeroValue T
return zeroValue, false
}
// Len returns the size of the set.
func (s Set[T]) Len() int {
return len(s)
}
func less[T ordered](lhs, rhs T) bool {
return lhs < rhs
}

View File

@ -0,0 +1,307 @@
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package sets_test
import (
"reflect"
"testing"
"k8s.io/apimachinery/pkg/util/sets"
)
func TestSet(t *testing.T) {
s := sets.Set[string]{}
s2 := sets.Set[string]{}
if len(s) != 0 {
t.Errorf("Expected len=0: %d", len(s))
}
s.Insert("a", "b")
if len(s) != 2 {
t.Errorf("Expected len=2: %d", len(s))
}
s.Insert("c")
if s.Has("d") {
t.Errorf("Unexpected contents: %#v", s)
}
if !s.Has("a") {
t.Errorf("Missing contents: %#v", s)
}
s.Delete("a")
if s.Has("a") {
t.Errorf("Unexpected contents: %#v", s)
}
s.Insert("a")
if s.HasAll("a", "b", "d") {
t.Errorf("Unexpected contents: %#v", s)
}
if !s.HasAll("a", "b") {
t.Errorf("Missing contents: %#v", s)
}
s2.Insert("a", "b", "d")
if s.IsSuperset(s2) {
t.Errorf("Unexpected contents: %#v", s)
}
s2.Delete("d")
if !s.IsSuperset(s2) {
t.Errorf("Missing contents: %#v", s)
}
}
func TestSetDeleteMultiples(t *testing.T) {
s := sets.Set[string]{}
s.Insert("a", "b", "c")
if len(s) != 3 {
t.Errorf("Expected len=3: %d", len(s))
}
s.Delete("a", "c")
if len(s) != 1 {
t.Errorf("Expected len=1: %d", len(s))
}
if s.Has("a") {
t.Errorf("Unexpected contents: %#v", s)
}
if s.Has("c") {
t.Errorf("Unexpected contents: %#v", s)
}
if !s.Has("b") {
t.Errorf("Missing contents: %#v", s)
}
}
func TestNewSet(t *testing.T) {
s := sets.New("a", "b", "c")
if len(s) != 3 {
t.Errorf("Expected len=3: %d", len(s))
}
if !s.Has("a") || !s.Has("b") || !s.Has("c") {
t.Errorf("Unexpected contents: %#v", s)
}
}
func TestKeySet(t *testing.T) {
m := map[string]int{"a": 1, "b": 2, "c": 3}
ss := sets.KeySet[string](m)
if !ss.Equal(sets.New("a", "b", "c")) {
t.Errorf("Unexpected contents: %#v", sets.List(ss))
}
}
func TestNewEmptySet(t *testing.T) {
s := sets.New[string]()
if len(s) != 0 {
t.Errorf("Expected len=0: %d", len(s))
}
s.Insert("a", "b", "c")
if len(s) != 3 {
t.Errorf("Expected len=3: %d", len(s))
}
if !s.Has("a") || !s.Has("b") || !s.Has("c") {
t.Errorf("Unexpected contents: %#v", s)
}
}
func TestSortedList(t *testing.T) {
s := sets.New("z", "y", "x", "a")
if !reflect.DeepEqual(sets.List(s), []string{"a", "x", "y", "z"}) {
t.Errorf("List gave unexpected result: %#v", sets.List(s))
}
}
func TestSetDifference(t *testing.T) {
a := sets.New("1", "2", "3")
b := sets.New("1", "2", "4", "5")
c := a.Difference(b)
d := b.Difference(a)
if len(c) != 1 {
t.Errorf("Expected len=1: %d", len(c))
}
if !c.Has("3") {
t.Errorf("Unexpected contents: %#v", sets.List(c))
}
if len(d) != 2 {
t.Errorf("Expected len=2: %d", len(d))
}
if !d.Has("4") || !d.Has("5") {
t.Errorf("Unexpected contents: %#v", sets.List(d))
}
}
func TestSetSymmetricDifference(t *testing.T) {
a := sets.New("1", "2", "3")
b := sets.New("1", "2", "4", "5")
c := a.SymmetricDifference(b)
d := b.SymmetricDifference(a)
if !c.Equal(sets.New("3", "4", "5")) {
t.Errorf("Unexpected contents: %#v", sets.List(c))
}
if !d.Equal(sets.New("3", "4", "5")) {
t.Errorf("Unexpected contents: %#v", sets.List(d))
}
}
func TestSetHasAny(t *testing.T) {
a := sets.New("1", "2", "3")
if !a.HasAny("1", "4") {
t.Errorf("expected true, got false")
}
if a.HasAny("0", "4") {
t.Errorf("expected false, got true")
}
}
func TestSetEquals(t *testing.T) {
// Simple case (order doesn't matter)
a := sets.New("1", "2")
b := sets.New("2", "1")
if !a.Equal(b) {
t.Errorf("Expected to be equal: %v vs %v", a, b)
}
// It is a set; duplicates are ignored
b = sets.New("2", "2", "1")
if !a.Equal(b) {
t.Errorf("Expected to be equal: %v vs %v", a, b)
}
// Edge cases around empty sets / empty strings
a = sets.New[string]()
b = sets.New[string]()
if !a.Equal(b) {
t.Errorf("Expected to be equal: %v vs %v", a, b)
}
b = sets.New("1", "2", "3")
if a.Equal(b) {
t.Errorf("Expected to be not-equal: %v vs %v", a, b)
}
b = sets.New("1", "2", "")
if a.Equal(b) {
t.Errorf("Expected to be not-equal: %v vs %v", a, b)
}
// Check for equality after mutation
a = sets.New[string]()
a.Insert("1")
if a.Equal(b) {
t.Errorf("Expected to be not-equal: %v vs %v", a, b)
}
a.Insert("2")
if a.Equal(b) {
t.Errorf("Expected to be not-equal: %v vs %v", a, b)
}
a.Insert("")
if !a.Equal(b) {
t.Errorf("Expected to be equal: %v vs %v", a, b)
}
a.Delete("")
if a.Equal(b) {
t.Errorf("Expected to be not-equal: %v vs %v", a, b)
}
}
func TestUnion(t *testing.T) {
tests := []struct {
s1 sets.Set[string]
s2 sets.Set[string]
expected sets.Set[string]
}{
{
sets.New("1", "2", "3", "4"),
sets.New("3", "4", "5", "6"),
sets.New("1", "2", "3", "4", "5", "6"),
},
{
sets.New("1", "2", "3", "4"),
sets.New[string](),
sets.New("1", "2", "3", "4"),
},
{
sets.New[string](),
sets.New("1", "2", "3", "4"),
sets.New("1", "2", "3", "4"),
},
{
sets.New[string](),
sets.New[string](),
sets.New[string](),
},
}
for _, test := range tests {
union := test.s1.Union(test.s2)
if union.Len() != test.expected.Len() {
t.Errorf("Expected union.Len()=%d but got %d", test.expected.Len(), union.Len())
}
if !union.Equal(test.expected) {
t.Errorf("Expected union.Equal(expected) but not true. union:%v expected:%v", sets.List(union), sets.List(test.expected))
}
}
}
func TestIntersection(t *testing.T) {
tests := []struct {
s1 sets.Set[string]
s2 sets.Set[string]
expected sets.Set[string]
}{
{
sets.New("1", "2", "3", "4"),
sets.New("3", "4", "5", "6"),
sets.New("3", "4"),
},
{
sets.New("1", "2", "3", "4"),
sets.New("1", "2", "3", "4"),
sets.New("1", "2", "3", "4"),
},
{
sets.New("1", "2", "3", "4"),
sets.New[string](),
sets.New[string](),
},
{
sets.New[string](),
sets.New("1", "2", "3", "4"),
sets.New[string](),
},
{
sets.New[string](),
sets.New[string](),
sets.New[string](),
},
}
for _, test := range tests {
intersection := test.s1.Intersection(test.s2)
if intersection.Len() != test.expected.Len() {
t.Errorf("Expected intersection.Len()=%d but got %d", test.expected.Len(), intersection.Len())
}
if !intersection.Equal(test.expected) {
t.Errorf("Expected intersection.Equal(expected) but not true. intersection:%v expected:%v", sets.List(intersection), sets.List(intersection))
}
}
}

View File

@ -17,6 +17,8 @@ limitations under the License.
package sets package sets
import ( import (
"fmt"
"math/rand"
"reflect" "reflect"
"testing" "testing"
) )
@ -281,3 +283,91 @@ func TestStringIntersection(t *testing.T) {
} }
} }
} }
type randomStringAlphabet string
func (a randomStringAlphabet) makeString(minLen, maxLen int) string {
n := minLen
if minLen < maxLen {
n += rand.Intn(maxLen - minLen)
}
var s string
for i := 0; i < n; i++ {
s += string(a[rand.Intn(len(a))])
}
return s
}
var randomStringMaker = randomStringAlphabet("abcdefghijklmnopqrstuvwxyz0123456789")
func BenchmarkStringSet(b *testing.B) {
cases := []struct {
size int
minStringLen int
maxStringLen int
}{
{20, 10, 20},
{50, 10, 30},
{100, 20, 40},
{500, 20, 50},
{1000, 20, 60},
}
for i := range cases {
here := cases[i]
makeSet := func() String {
s := NewString()
for j := 0; j < here.size; j++ {
s.Insert(randomStringMaker.makeString(here.minStringLen, here.maxStringLen))
}
return s
}
operands := make([]String, 500)
for i := range operands {
operands[i] = makeSet()
}
randOperand := func() String { return operands[rand.Intn(len(operands))] }
b.Run(fmt.Sprintf("insert-%v", here.size), func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
makeSet()
}
})
b.Run(fmt.Sprintf("key-set-%v", here.size), func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
StringKeySet(randOperand())
}
})
b.Run(fmt.Sprintf("has-%v", here.size), func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
randOperand().Has(randomStringMaker.makeString(here.minStringLen, here.maxStringLen))
}
})
b.Run(fmt.Sprintf("intersection-%v", here.size), func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
randOperand().Intersection(randOperand())
}
})
b.Run(fmt.Sprintf("symmetric-difference-%v", here.size), func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
randOperand().SymmetricDifference(randOperand())
}
})
b.Run(fmt.Sprintf("list-%v", here.size), func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
randOperand().List()
}
})
}
}

View File

@ -1,5 +1,5 @@
/* /*
Copyright The Kubernetes Authors. Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -14,86 +14,55 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
// Code generated by set-gen. DO NOT EDIT.
package sets package sets
import ( // String is a set of strings, implemented via map[string]struct{} for minimal memory consumption.
"reflect" //
"sort" // Deprecated: use generic Set instead.
) // new ways:
// s1 := Set[string]{}
// sets.String is a set of strings, implemented via map[string]struct{} for minimal memory consumption. // s2 := New[string]()
type String map[string]Empty type String map[string]Empty
// NewString creates a String from a list of values. // NewString creates a String from a list of values.
func NewString(items ...string) String { func NewString(items ...string) String {
ss := make(String, len(items)) return String(New[string](items...))
ss.Insert(items...)
return ss
} }
// StringKeySet creates a String from a keys of a map[string](? extends interface{}). // StringKeySet creates a String from a keys of a map[string](? extends interface{}).
// If the value passed in is not actually a map, this will panic. // If the value passed in is not actually a map, this will panic.
func StringKeySet(theMap interface{}) String { func StringKeySet[T any](theMap map[string]T) String {
v := reflect.ValueOf(theMap) return String(KeySet(theMap))
ret := String{}
for _, keyValue := range v.MapKeys() {
ret.Insert(keyValue.Interface().(string))
}
return ret
} }
// Insert adds items to the set. // Insert adds items to the set.
func (s String) Insert(items ...string) String { func (s String) Insert(items ...string) String {
for _, item := range items { return String(cast(s).Insert(items...))
s[item] = Empty{}
}
return s
} }
// Delete removes all items from the set. // Delete removes all items from the set.
func (s String) Delete(items ...string) String { func (s String) Delete(items ...string) String {
for _, item := range items { return String(cast(s).Delete(items...))
delete(s, item)
}
return s
} }
// Has returns true if and only if item is contained in the set. // Has returns true if and only if item is contained in the set.
func (s String) Has(item string) bool { func (s String) Has(item string) bool {
_, contained := s[item] return cast(s).Has(item)
return contained
} }
// HasAll returns true if and only if all items are contained in the set. // HasAll returns true if and only if all items are contained in the set.
func (s String) HasAll(items ...string) bool { func (s String) HasAll(items ...string) bool {
for _, item := range items { return cast(s).HasAll(items...)
if !s.Has(item) {
return false
}
}
return true
} }
// HasAny returns true if any items are contained in the set. // HasAny returns true if any items are contained in the set.
func (s String) HasAny(items ...string) bool { func (s String) HasAny(items ...string) bool {
for _, item := range items { return cast(s).HasAny(items...)
if s.Has(item) {
return true
}
}
return false
} }
// Clone returns a new set which is a copy of the current set. // Clone returns a new set which is a copy of the current set.
func (s String) Clone() String { func (s String) Clone() String {
result := make(String, len(s)) return String(cast(s).Clone())
for key := range s {
result.Insert(key)
}
return result
} }
// Difference returns a set of objects that are not in s2. // Difference returns a set of objects that are not in s2.
@ -103,13 +72,7 @@ func (s String) Clone() String {
// s1.Difference(s2) = {a3} // s1.Difference(s2) = {a3}
// s2.Difference(s1) = {a4, a5} // s2.Difference(s1) = {a4, a5}
func (s1 String) Difference(s2 String) String { func (s1 String) Difference(s2 String) String {
result := NewString() return String(cast(s1).Difference(cast(s2)))
for key := range s1 {
if !s2.Has(key) {
result.Insert(key)
}
}
return result
} }
// SymmetricDifference returns a set of elements which are in either of the sets, but not in their intersection. // SymmetricDifference returns a set of elements which are in either of the sets, but not in their intersection.
@ -119,7 +82,7 @@ func (s1 String) Difference(s2 String) String {
// s1.SymmetricDifference(s2) = {a3, a4, a5} // s1.SymmetricDifference(s2) = {a3, a4, a5}
// s2.SymmetricDifference(s1) = {a3, a4, a5} // s2.SymmetricDifference(s1) = {a3, a4, a5}
func (s1 String) SymmetricDifference(s2 String) String { func (s1 String) SymmetricDifference(s2 String) String {
return s1.Difference(s2).Union(s2.Difference(s1)) return String(cast(s1).SymmetricDifference(cast(s2)))
} }
// Union returns a new set which includes items in either s1 or s2. // Union returns a new set which includes items in either s1 or s2.
@ -129,11 +92,7 @@ func (s1 String) SymmetricDifference(s2 String) String {
// s1.Union(s2) = {a1, a2, a3, a4} // s1.Union(s2) = {a1, a2, a3, a4}
// s2.Union(s1) = {a1, a2, a3, a4} // s2.Union(s1) = {a1, a2, a3, a4}
func (s1 String) Union(s2 String) String { func (s1 String) Union(s2 String) String {
result := s1.Clone() return String(cast(s1).Union(cast(s2)))
for key := range s2 {
result.Insert(key)
}
return result
} }
// Intersection returns a new set which includes the item in BOTH s1 and s2 // Intersection returns a new set which includes the item in BOTH s1 and s2
@ -142,80 +101,37 @@ func (s1 String) Union(s2 String) String {
// s2 = {a2, a3} // s2 = {a2, a3}
// s1.Intersection(s2) = {a2} // s1.Intersection(s2) = {a2}
func (s1 String) Intersection(s2 String) String { func (s1 String) Intersection(s2 String) String {
var walk, other String return String(cast(s1).Intersection(cast(s2)))
result := NewString()
if s1.Len() < s2.Len() {
walk = s1
other = s2
} else {
walk = s2
other = s1
}
for key := range walk {
if other.Has(key) {
result.Insert(key)
}
}
return result
} }
// IsSuperset returns true if and only if s1 is a superset of s2. // IsSuperset returns true if and only if s1 is a superset of s2.
func (s1 String) IsSuperset(s2 String) bool { func (s1 String) IsSuperset(s2 String) bool {
for item := range s2 { return cast(s1).IsSuperset(cast(s2))
if !s1.Has(item) {
return false
}
}
return true
} }
// Equal returns true if and only if s1 is equal (as a set) to s2. // Equal returns true if and only if s1 is equal (as a set) to s2.
// Two sets are equal if their membership is identical. // Two sets are equal if their membership is identical.
// (In practice, this means same elements, order doesn't matter) // (In practice, this means same elements, order doesn't matter)
func (s1 String) Equal(s2 String) bool { func (s1 String) Equal(s2 String) bool {
return len(s1) == len(s2) && s1.IsSuperset(s2) return cast(s1).Equal(cast(s2))
} }
type sortableSliceOfString []string
func (s sortableSliceOfString) Len() int { return len(s) }
func (s sortableSliceOfString) Less(i, j int) bool { return lessString(s[i], s[j]) }
func (s sortableSliceOfString) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
// List returns the contents as a sorted string slice. // List returns the contents as a sorted string slice.
func (s String) List() []string { func (s String) List() []string {
res := make(sortableSliceOfString, 0, len(s)) return List(cast(s))
for key := range s {
res = append(res, key)
}
sort.Sort(res)
return []string(res)
} }
// UnsortedList returns the slice with contents in random order. // UnsortedList returns the slice with contents in random order.
func (s String) UnsortedList() []string { func (s String) UnsortedList() []string {
res := make([]string, 0, len(s)) return cast(s).UnsortedList()
for key := range s {
res = append(res, key)
}
return res
} }
// Returns a single element from the set. // PopAny returns a single element from the set.
func (s String) PopAny() (string, bool) { func (s String) PopAny() (string, bool) {
for key := range s { return cast(s).PopAny()
s.Delete(key)
return key, true
}
var zeroValue string
return zeroValue, false
} }
// Len returns the size of the set. // Len returns the size of the set.
func (s String) Len() int { func (s String) Len() int {
return len(s) return len(s)
} }
func lessString(lhs, rhs string) bool {
return lhs < rhs
}

View File

@ -1,34 +0,0 @@
/*
Copyright 2015 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package types just provides input types to the set generator. It also
// contains a "go generate" block.
// (You must first `go install k8s.io/code-generator/cmd/set-gen`)
package types
//go:generate set-gen -i k8s.io/kubernetes/pkg/util/sets/types
//lint:file-ignore U1000 Ignore unused fields
type ReferenceSetTypes struct {
// These types all cause files to be generated.
// These types should be reflected in the output of
// the "//pkg/util/sets:set-gen" genrule.
a int64
b int
c byte
d string
e int32
}