Merge "Do not modify input in-place" into udc-dev

This commit is contained in:
Yu Liu
2023-05-23 23:27:34 +00:00
committed by Android (Google) Code Review

View File

@@ -280,6 +280,8 @@ func RemoveFromList(s string, list []string) (bool, []string) {
// FirstUniqueStrings returns all unique elements of a slice of strings, keeping the first copy of
// each. It modifies the slice contents in place, and returns a subslice of the original slice.
func FirstUniqueStrings(list []string) []string {
// Do not moodify the input in-place, operate on a copy instead.
list = CopyOf(list)
// 128 was chosen based on BenchmarkFirstUniqueStrings results.
if len(list) > 128 {
return firstUniqueStringsMap(list)
@@ -336,6 +338,7 @@ func LastUniqueStrings(list []string) []string {
// SortedUniqueStrings returns what the name says
func SortedUniqueStrings(list []string) []string {
// FirstUniqueStrings creates a copy of `list`, so the input remains untouched.
unique := FirstUniqueStrings(list)
sort.Strings(unique)
return unique