-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- SliceCompare - SliceDedupe Signed-off-by: Vicente Cheng <[email protected]>
- Loading branch information
1 parent
8d42a1e
commit 07753b2
Showing
4 changed files
with
127 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package gocommon | ||
|
||
import "golang.org/x/exp/slices" | ||
|
||
// SliceCompare compares two slices and returns true if they are the same. | ||
func SliceCompare[T comparable](x, y []T) bool { | ||
if len(x) == 0 && len(y) == 0 { | ||
return true | ||
} | ||
if len(x) != len(y) { | ||
return false | ||
} | ||
|
||
yMap := make(map[T]int, len(y)) | ||
for _, item := range y { | ||
yMap[item]++ | ||
} | ||
|
||
for _, xItem := range x { | ||
if counter, exist := yMap[xItem]; exist { | ||
if counter == 0 { | ||
return false | ||
} | ||
yMap[xItem]-- | ||
} else { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
// SliceDedupe removes duplicated items and return a non-deplucated items slice. | ||
func SliceDedupe[T comparable](x []T) []T { | ||
if len(x) == 0 { | ||
return x | ||
} | ||
|
||
result := make([]T, 0) | ||
for _, item := range x { | ||
if !slices.Contains(result, item) { | ||
result = append(result, item) | ||
} | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package gocommon | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type SliceFuncs struct { | ||
suite.Suite | ||
} | ||
|
||
func TestSliceFuncs(t *testing.T) { | ||
suite.Run(t, new(SliceFuncs)) | ||
} | ||
|
||
func (s *SliceFuncs) SetupSuite() { | ||
// you could do something here before all tests | ||
} | ||
|
||
func (s *SliceFuncs) TestSliceCompareInt() { | ||
a := []int{1, 1, 2, 3} | ||
b := []int{3, 2, 1, 1} | ||
c := []int{1, 2, 3, 4} | ||
d := []int{} | ||
e := []int{} | ||
f := []int{1, 1, 2} | ||
g := []int{1, 2, 2} | ||
fCpy := []int{1, 1, 2} | ||
gCpy := []int{1, 2, 2} | ||
|
||
s.Equal(true, SliceCompare(a, b), "SliceCompare should return true") | ||
s.Equal(false, SliceCompare(a, c), "SliceCompare should return false") | ||
s.Equal(true, SliceCompare(d, e), "SliceCompare should return true") | ||
s.Equal(false, SliceCompare(f, g), "SliceCompare should return false") | ||
s.Equal(f, fCpy, "original slice should not change.") | ||
s.Equal(g, gCpy, "original slice should not change.") | ||
|
||
} | ||
|
||
func (s *SliceFuncs) TestSliceCompareString() { | ||
a := []string{"a", "b", "c"} | ||
b := []string{"b", "c", "a"} | ||
c := []string{"a", "b", "c", "d"} | ||
d := []string{} | ||
e := []string{} | ||
f := []string{"a", "a", "b"} | ||
g := []string{"a", "b", "b"} | ||
fCpy := []string{"a", "a", "b"} | ||
gCpy := []string{"a", "b", "b"} | ||
|
||
s.Equal(true, SliceCompare(a, b), "SliceCompare should return true") | ||
s.Equal(false, SliceCompare(a, c), "SliceCompare should return false") | ||
s.Equal(true, SliceCompare(d, e), "SliceCompare should return true") | ||
s.Equal(false, SliceCompare(f, g), "SliceCompare should return false") | ||
s.Equal(f, fCpy, "original slice should not change.") | ||
s.Equal(g, gCpy, "original slice should not change.") | ||
} | ||
|
||
func (s *SliceFuncs) TestSliceDedupeInt() { | ||
a := []int{1, 1, 2, 2, 3, 3} | ||
b := []int{1, 2, 3, 4, 5, 6} | ||
c := []int{1, 2, 3, 3, 4} | ||
|
||
require.Equal(s.T(), []int{1, 2, 3}, SliceDedupe(a), "SliceDedupe should return the same slice with {1, 2, 3}") | ||
require.Equal(s.T(), []int{1, 2, 3, 4, 5, 6}, SliceDedupe(b), "SliceDedupe should return the same slice with {1, 2, 3, 4, 5, 6}") | ||
require.Equal(s.T(), []int{1, 2, 3, 4}, SliceDedupe(c), "SliceDedupe should return the same slice with {1, 2, 3, 4}") | ||
} | ||
|
||
func (s *SliceFuncs) TestSliceDedupeString() { | ||
a := []string{"foo", "bar", "bar", "foo", "roll", "roll"} | ||
b := []string{"apple", "book", "clock", "duck", "escape", "field"} | ||
c := []string{"foo", "bar", "roll", "roll", "desk"} | ||
|
||
require.Equal(s.T(), []string{"foo", "bar", "roll"}, SliceDedupe(a), "SliceDedupe should return the same slice with {foo, bar, roll}") | ||
require.Equal(s.T(), []string{"apple", "book", "clock", "duck", "escape", "field"}, SliceDedupe(b), "SliceDedupe should return the same slice with {apple, book, clock, duck, escape, field}") | ||
require.Equal(s.T(), []string{"foo", "bar", "roll", "desk"}, SliceDedupe(c), "SliceDedupe should return the same slice with {foo, bar, roll, desk}") | ||
} |