-
Notifications
You must be signed in to change notification settings - Fork 219
Align set with k/k apimachinery #294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
skitt
wants to merge
2
commits into
kubernetes:master
Choose a base branch
from
skitt:align-apimachinery-set
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+49
−93
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,19 +17,21 @@ limitations under the License. | |
| package set | ||
|
|
||
| import ( | ||
| "cmp" | ||
| "sort" | ||
| ) | ||
|
|
||
| // Empty is public since it is used by some internal API objects for conversions between external | ||
| // string arrays and internal sets, and conversion logic requires public types today. | ||
| type Empty struct{} | ||
|
|
||
| // Set is a set of the same type elements, implemented via map[ordered]struct{} for minimal memory consumption. | ||
| type Set[E ordered] map[E]Empty | ||
| // Set is a set of the same type elements, implemented via map[comparable]struct{} for minimal memory consumption. | ||
| type Set[E comparable] map[E]Empty | ||
|
|
||
| // New creates a new set. | ||
| func New[E ordered](items ...E) Set[E] { | ||
| ss := Set[E]{} | ||
| // NOTE: type param must be explicitly instantiated if given items are empty. | ||
| func New[E comparable](items ...E) Set[E] { | ||
| ss := make(Set[E], len(items)) | ||
| ss.Insert(items...) | ||
| return ss | ||
| } | ||
|
|
@@ -43,23 +45,24 @@ func (s Set[T]) Clear() Set[T] { | |
| } | ||
|
|
||
| // KeySet creates a Set[E] from a keys of a map[E](? extends interface{}). | ||
| func KeySet[E ordered, A any](theMap map[E]A) Set[E] { | ||
| // If the value passed in is not actually a map, this will panic. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, I bite: how can |
||
| func KeySet[E comparable, A any](theMap map[E]A) Set[E] { | ||
| ret := Set[E]{} | ||
| for key := range theMap { | ||
| ret.Insert(key) | ||
| } | ||
| return ret | ||
| } | ||
|
|
||
| // Insert adds items to the set. | ||
| // Insert adds the given items to the set. | ||
| func (s Set[E]) Insert(items ...E) Set[E] { | ||
| for _, item := range items { | ||
| s[item] = Empty{} | ||
| } | ||
| return s | ||
| } | ||
|
|
||
| // Delete removes all items from the set. | ||
| // Delete removes the given items from the set. | ||
| func (s Set[E]) Delete(items ...E) Set[E] { | ||
| for _, item := range items { | ||
| delete(s, item) | ||
|
|
@@ -93,16 +96,17 @@ func (s Set[E]) HasAny(items ...E) bool { | |
| return false | ||
| } | ||
|
|
||
| // Union returns a new set which includes items in either s1 or s2. | ||
| // Union returns a new set which includes items in either s or s2. | ||
| // For example: | ||
| // s1 = {a1, a2} | ||
| // s = {a1, a2} | ||
| // s2 = {a3, a4} | ||
| // s1.Union(s2) = {a1, a2, a3, a4} | ||
| // s2.Union(s1) = {a1, a2, a3, a4} | ||
| // s.Union(s2) = {a1, a2, a3, a4} | ||
| // s2.Union(s) = {a1, a2, a3, a4} | ||
| func (s Set[E]) Union(s2 Set[E]) Set[E] { | ||
| result := Set[E]{} | ||
| result.Insert(s.UnsortedList()...) | ||
| result.Insert(s2.UnsortedList()...) | ||
| result := s.Clone() | ||
| for k2 := range s2 { | ||
| result.Insert(k2) | ||
| } | ||
| return result | ||
| } | ||
|
|
||
|
|
@@ -111,11 +115,11 @@ func (s Set[E]) Len() int { | |
| return len(s) | ||
| } | ||
|
|
||
| // 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 s and s2 | ||
| // For example: | ||
| // s1 = {a1, a2} | ||
| // s = {a1, a2} | ||
| // s2 = {a2, a3} | ||
| // s1.Intersection(s2) = {a2} | ||
| // s.Intersection(s2) = {a2} | ||
| func (s Set[E]) Intersection(s2 Set[E]) Set[E] { | ||
| var walk, other Set[E] | ||
| result := Set[E]{} | ||
|
|
@@ -134,7 +138,7 @@ func (s Set[E]) Intersection(s2 Set[E]) Set[E] { | |
| return result | ||
| } | ||
|
|
||
| // IsSuperset returns true if and only if s1 is a superset of s2. | ||
| // IsSuperset returns true if and only if s is a superset of s2. | ||
| func (s Set[E]) IsSuperset(s2 Set[E]) bool { | ||
| for item := range s2 { | ||
| if !s.Has(item) { | ||
|
|
@@ -146,10 +150,10 @@ func (s Set[E]) IsSuperset(s2 Set[E]) bool { | |
|
|
||
| // Difference returns a set of objects that are not in s2 | ||
| // For example: | ||
| // s1 = {a1, a2, a3} | ||
| // s = {a1, a2, a3} | ||
| // s2 = {a1, a2, a4, a5} | ||
| // s1.Difference(s2) = {a3} | ||
| // s2.Difference(s1) = {a4, a5} | ||
| // s.Difference(s2) = {a3} | ||
| // s2.Difference(s) = {a4, a5} | ||
| func (s Set[E]) Difference(s2 Set[E]) Set[E] { | ||
| result := Set[E]{} | ||
| for key := range s { | ||
|
|
@@ -160,23 +164,26 @@ func (s Set[E]) Difference(s2 Set[E]) Set[E] { | |
| return result | ||
| } | ||
|
|
||
| // Equal returns true if and only if s1 is equal (as a set) to s2. | ||
| // Equal returns true if and only if s is equal (as a set) to s2. | ||
| // Two sets are equal if their membership is identical. | ||
| func (s Set[E]) Equal(s2 Set[E]) bool { | ||
| return s.Len() == s2.Len() && s.IsSuperset(s2) | ||
| } | ||
|
|
||
| type sortableSlice[E ordered] []E | ||
| type sortableSlice[E cmp.Ordered] []E | ||
|
|
||
| func (s sortableSlice[E]) Len() int { | ||
| return len(s) | ||
| } | ||
| func (s sortableSlice[E]) Less(i, j int) bool { return s[i] < s[j] } | ||
| func (s sortableSlice[E]) Swap(i, j int) { s[i], s[j] = s[j], s[i] } | ||
|
|
||
| // SortedList returns the contents as a sorted slice. | ||
| func (s Set[E]) SortedList() []E { | ||
| res := make(sortableSlice[E], 0, s.Len()) | ||
| // 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[E cmp.Ordered](s Set[E]) []E { | ||
| res := make(sortableSlice[E], 0, len(s)) | ||
| for key := range s { | ||
| res = append(res, key) | ||
| } | ||
|
|
@@ -214,10 +221,10 @@ func (s Set[T]) Clone() Set[T] { | |
|
|
||
| // SymmetricDifference returns a set of elements which are in either of the sets, but not in their intersection. | ||
| // For example: | ||
| // s1 = {a1, a2, a3} | ||
| // s = {a1, a2, a3} | ||
| // s2 = {a1, a2, a4, a5} | ||
| // s1.SymmetricDifference(s2) = {a3, a4, a5} | ||
| // s2.SymmetricDifference(s1) = {a3, a4, a5} | ||
| // s.SymmetricDifference(s2) = {a3, a4, a5} | ||
| // s2.SymmetricDifference(s) = {a3, a4, a5} | ||
| func (s Set[T]) SymmetricDifference(s2 Set[T]) Set[T] { | ||
| return s.Difference(s2).Union(s2.Difference(s)) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The main obstacle for transitioning from k8s.io/apimachinery/pkg/util/sets to k8s.io/utils/set: the former uses plural and thus code has
sets.New, the latter singular and thus would have to change imports tosets "k8s.io/utils/set"(ugly!) or change the code (also ugly!)."set" is arguably the better name (there's only one set type here). Also, these files have all the commit history. Maybe we can create a mirror package
k8s.io/utils/setswith atypedef Set = set.Setand function wrappers for the few functions here (New,List,KeySet)?