- Notifications
You must be signed in to change notification settings - Fork1.1k
chore: implement generalized symmetric difference for set comparison#14407
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
6 commits Select commitHold shift + click to select a range
c184fe1 chore: implement generalized symmetric difference for set comparison
Emyrk00fc33d chore: linting
Emyrkae75e5d add tests
Emyrked8972f use same function for all types
Emyrkcc4a462 add a go example
Emyrk3cc9b08 linting
EmyrkFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
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
21 changes: 21 additions & 0 deletionscoderd/util/slice/example_test.go
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 |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package slice_test | ||
| import ( | ||
| "fmt" | ||
| "github.com/coder/coder/v2/coderd/util/slice" | ||
| ) | ||
| //nolint:revive // They want me to error check my Printlns | ||
| func ExampleSymmetricDifference() { | ||
| // The goal of this function is to find the elements to add & remove from | ||
| // set 'a' to make it equal to set 'b'. | ||
| a := []int{1, 2, 5, 6} | ||
| b := []int{2, 3, 4, 5} | ||
| add, remove := slice.SymmetricDifference(a, b) | ||
| fmt.Println("Elements to add:", add) | ||
| fmt.Println("Elements to remove:", remove) | ||
| // Output: | ||
| // Elements to add: [3 4] | ||
| // Elements to remove: [1 6] | ||
| } |
32 changes: 32 additions & 0 deletionscoderd/util/slice/slice.go
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 |
|---|---|---|
| @@ -107,3 +107,35 @@ func Ascending[T constraints.Ordered](a, b T) int { | ||
| func Descending[T constraints.Ordered](a, b T) int { | ||
| return -Ascending[T](a, b) | ||
| } | ||
| // SymmetricDifference returns the elements that need to be added and removed | ||
| // to get from set 'a' to set 'b'. | ||
| // In classical set theory notation, SymmetricDifference returns | ||
| // all elements of {add} and {remove} together. It is more useful to | ||
| // return them as their own slices. | ||
Emyrk marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| // Notation: A Δ B = (A\B) ∪ (B\A) | ||
| // Example: | ||
Emyrk marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| // | ||
| //a := []int{1, 3, 4} | ||
| //b := []int{1, 2} | ||
| //add, remove := SymmetricDifference(a, b) | ||
| //fmt.Println(add) // [2] | ||
| //fmt.Println(remove) // [3, 4] | ||
| func SymmetricDifference[T comparable](a, b []T) (add []T, remove []T) { | ||
| f := func(a, b T) bool { return a == b } | ||
| return SymmetricDifferenceFunc(a, b, f) | ||
| } | ||
| func SymmetricDifferenceFunc[T any](a, b []T, equal func(a, b T) bool) (add []T, remove []T) { | ||
| return DifferenceFunc(b, a, equal), DifferenceFunc(a, b, equal) | ||
| } | ||
| func DifferenceFunc[T any](a []T, b []T, equal func(a, b T) bool) []T { | ||
| tmp := make([]T, 0, len(a)) | ||
| for _, v := range a { | ||
| if !ContainsCompare(b, v, equal) { | ||
| tmp = append(tmp, v) | ||
| } | ||
| } | ||
| return tmp | ||
| } | ||
100 changes: 100 additions & 0 deletionscoderd/util/slice/slice_test.go
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
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.