Fix sort with key implementations

This commit is contained in:
Kovid Goyal 2023-03-14 12:54:35 +05:30
parent 21954937fb
commit 0c20a4d980
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -75,22 +75,29 @@ func StableSort[T any](s []T, less func(a, b T) bool) []T {
return s
}
func SortWithKey[T any, C constraints.Ordered](s []T, key func(a T) C) []T {
mem := make([]C, len(s))
func sort_with_key[T any, C constraints.Ordered](impl func(any, func(int, int) bool), s []T, key func(a T) C) []T {
temp := make([]struct {
key C
val T
}, len(s))
for i, x := range s {
mem[i] = key(x)
temp[i].val, temp[i].key = x, key(x)
}
impl(temp, func(i, j int) bool {
return temp[i].key < temp[j].key
})
for i, x := range temp {
s[i] = x.val
}
sort.Slice(s, func(i, j int) bool { return mem[i] < mem[j] })
return s
}
func StableSortWithKey[T any, C constraints.Ordered](s []T, key func(a T) C) []T {
mem := make([]C, len(s))
for i, x := range s {
mem[i] = key(x)
func SortWithKey[T any, C constraints.Ordered](s []T, key func(a T) C) []T {
return sort_with_key(sort.Slice, s, key)
}
sort.SliceStable(s, func(i, j int) bool { return mem[i] < mem[j] })
return s
func StableSortWithKey[T any, C constraints.Ordered](s []T, key func(a T) C) []T {
return sort_with_key(sort.SliceStable, s, key)
}
func Max[T constraints.Ordered](a T, items ...T) (ans T) {