Fix sort with key implementations
This commit is contained in:
parent
21954937fb
commit
0c20a4d980
@ -75,22 +75,29 @@ func StableSort[T any](s []T, less func(a, b T) bool) []T {
|
|||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func SortWithKey[T any, C constraints.Ordered](s []T, key func(a T) C) []T {
|
func sort_with_key[T any, C constraints.Ordered](impl func(any, func(int, int) bool), s []T, key func(a T) C) []T {
|
||||||
mem := make([]C, len(s))
|
temp := make([]struct {
|
||||||
|
key C
|
||||||
|
val T
|
||||||
|
}, len(s))
|
||||||
for i, x := range 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
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SortWithKey[T any, C constraints.Ordered](s []T, key func(a T) C) []T {
|
||||||
|
return sort_with_key(sort.Slice, s, key)
|
||||||
|
}
|
||||||
|
|
||||||
func StableSortWithKey[T any, C constraints.Ordered](s []T, key func(a T) C) []T {
|
func StableSortWithKey[T any, C constraints.Ordered](s []T, key func(a T) C) []T {
|
||||||
mem := make([]C, len(s))
|
return sort_with_key(sort.SliceStable, s, key)
|
||||||
for i, x := range s {
|
|
||||||
mem[i] = key(x)
|
|
||||||
}
|
|
||||||
sort.SliceStable(s, func(i, j int) bool { return mem[i] < mem[j] })
|
|
||||||
return s
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Max[T constraints.Ordered](a T, items ...T) (ans T) {
|
func Max[T constraints.Ordered](a T, items ...T) (ans T) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user