-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a new slice method: UniqueStable (#203)
UniqueStable works similar to Unique. However, unlike Unique the slice returned will be in previous relative order. Co-authored-by: Westrious <[email protected]>
- Loading branch information
Showing
6 changed files
with
94 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package pie | ||
|
||
// UniqueStable works similar to Unique. However, unlike Unique | ||
// the slice returned will be in previous relative order | ||
func UniqueStable[T comparable](ss []T) []T { | ||
// Avoid the allocation. If there is one element or less it is already | ||
// unique. | ||
if len(ss) < 2 { | ||
return ss | ||
} | ||
|
||
seen := map[T]struct{}{} | ||
ret := make([]T, 0) | ||
|
||
for _, value := range ss { | ||
if _, ok := seen[value]; ok { | ||
continue | ||
} | ||
seen[value] = struct{}{} | ||
ret = append(ret, value) | ||
} | ||
|
||
return ret | ||
} |
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,42 @@ | ||
package pie_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/elliotchance/pie/v2" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var uniqueStableTests = []struct { | ||
ss []float64 | ||
uniqueStable []float64 | ||
}{ | ||
{ | ||
nil, | ||
nil, | ||
}, | ||
{ | ||
[]float64{}, | ||
[]float64{}, | ||
}, | ||
{ | ||
[]float64{789}, | ||
[]float64{789}, | ||
}, | ||
{ | ||
[]float64{12.789, -13.2, 12.789}, | ||
[]float64{12.789, -13.2}, | ||
}, | ||
{ | ||
[]float64{12.789, -13.2, 1.234e6, 789}, | ||
[]float64{12.789, -13.2, 1.234e6, 789}, | ||
}, | ||
} | ||
|
||
func TestUniqueStable(t *testing.T) { | ||
for _, test := range uniqueStableTests { | ||
t.Run("", func(t *testing.T) { | ||
assert.Equal(t, test.uniqueStable, pie.UniqueStable(test.ss)) | ||
}) | ||
} | ||
} |