-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add generic Set type and use it (#2243)
Based upon expanding the `StringSet` type. Note that we cannot substitute uses of `map[T]struct{}` where `T` is an interface because interface types are [not comparable](golang/go#51338), even though the `map[T]struct{}` version works. `AreEqual` is a standalone function at the moment because making it a method [crashes the compiler](golang/go#51840).
- Loading branch information
Showing
23 changed files
with
172 additions
and
172 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright (c) Microsoft Corporation. | ||
* Licensed under the MIT license. | ||
*/ | ||
|
||
package set | ||
|
||
import ( | ||
"golang.org/x/exp/constraints" | ||
"golang.org/x/exp/maps" | ||
"golang.org/x/exp/slices" | ||
) | ||
|
||
// Set provides a standard way to have a set of distinct things | ||
type Set[T comparable] map[T]struct{} | ||
|
||
// Make creates a new set with the given values | ||
func Make[T comparable](ts ...T) Set[T] { | ||
result := make(Set[T], len(ts)) | ||
|
||
for _, x := range ts { | ||
result.Add(x) | ||
} | ||
|
||
return result | ||
} | ||
|
||
// Contains does a check to see if the provided value is in the set | ||
func (set Set[T]) Contains(x T) bool { | ||
_, ok := set[x] | ||
return ok | ||
} | ||
|
||
// Add adds the provided value into the set | ||
// Nothing happens if the value is already present | ||
func (set Set[T]) Add(x T) { | ||
set[x] = struct{}{} | ||
} | ||
|
||
// Remove deletes the provided value from the set | ||
// Nothing happens if the value is not present | ||
func (set Set[T]) Remove(x T) { | ||
delete(set, x) | ||
} | ||
|
||
// Copy returns an independent copy of the set | ||
func (set Set[T]) Copy() Set[T] { | ||
return maps.Clone(set) | ||
} | ||
|
||
/* compiler crashes at the moment: https://github.com/golang/go/issues/51840 | ||
func (set Set[T]) Equals(other Set[T]) bool { | ||
return maps.Equal(set, other) | ||
} | ||
*/ | ||
|
||
func AreEqual[T comparable](left, right Set[T]) bool { | ||
return maps.Equal(left, right) | ||
} | ||
|
||
// Values returns a slice of all the values in the set | ||
func (set Set[T]) Values() []T { | ||
return maps.Keys(set) | ||
} | ||
|
||
// AsSortedSlice returns a sorted slice of values from this set | ||
func AsSortedSlice[T constraints.Ordered](set Set[T]) []T { | ||
result := maps.Keys(set) | ||
slices.Sort(result) | ||
return result | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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.