Use ordered interface from cmp package
I just learned this exists. Test: m nothing --no-skip-soong-tests Change-Id: Ic6f0d11c24b41b394b1760fcca9e0da53243e6d9
This commit is contained in:
@@ -15,6 +15,7 @@
|
|||||||
package android
|
package android
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"cmp"
|
||||||
"fmt"
|
"fmt"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
@@ -106,15 +107,8 @@ func SortedStringKeys[V any](m map[string]V) []string {
|
|||||||
return SortedKeys(m)
|
return SortedKeys(m)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Ordered interface {
|
|
||||||
~string |
|
|
||||||
~float32 | ~float64 |
|
|
||||||
~int | ~int8 | ~int16 | ~int32 | ~int64 |
|
|
||||||
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
|
|
||||||
}
|
|
||||||
|
|
||||||
// SortedKeys returns the keys of the given map in the ascending order.
|
// SortedKeys returns the keys of the given map in the ascending order.
|
||||||
func SortedKeys[T Ordered, V any](m map[T]V) []T {
|
func SortedKeys[T cmp.Ordered, V any](m map[T]V) []T {
|
||||||
if len(m) == 0 {
|
if len(m) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@@ -15,6 +15,7 @@
|
|||||||
package android
|
package android
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"cmp"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -650,7 +651,7 @@ func BenchmarkFirstUniqueStrings(b *testing.B) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testSortedKeysHelper[K Ordered, V any](t *testing.T, name string, input map[K]V, expected []K) {
|
func testSortedKeysHelper[K cmp.Ordered, V any](t *testing.T, name string, input map[K]V, expected []K) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
t.Run(name, func(t *testing.T) {
|
t.Run(name, func(t *testing.T) {
|
||||||
actual := SortedKeys(input)
|
actual := SortedKeys(input)
|
||||||
|
Reference in New Issue
Block a user