mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-04 14:48:47 +00:00
Merge pull request #2006 from thePHTest/sort_with_indices
fix sort_by_with_indices for zero and one length slices
This commit is contained in:
@@ -97,9 +97,9 @@ sort_by :: proc(data: $T/[]$E, less: proc(i, j: E) -> bool) {
|
|||||||
// sort_by sorts a slice with a given procedure to test whether two values are ordered "i < j"
|
// sort_by sorts a slice with a given procedure to test whether two values are ordered "i < j"
|
||||||
// This sort is not guaranteed to be stable
|
// This sort is not guaranteed to be stable
|
||||||
sort_by_with_indices :: proc(data: $T/[]$E, less: proc(i, j: E) -> bool, allocator := context.allocator) -> (indices : []int) {
|
sort_by_with_indices :: proc(data: $T/[]$E, less: proc(i, j: E) -> bool, allocator := context.allocator) -> (indices : []int) {
|
||||||
|
indices = make([]int, len(data), allocator)
|
||||||
when size_of(E) != 0 {
|
when size_of(E) != 0 {
|
||||||
if n := len(data); n > 1 {
|
if n := len(data); n > 1 {
|
||||||
indices = make([]int, len(data), allocator)
|
|
||||||
for _, idx in indices {
|
for _, idx in indices {
|
||||||
indices[idx] = idx
|
indices[idx] = idx
|
||||||
}
|
}
|
||||||
@@ -107,7 +107,7 @@ sort_by_with_indices :: proc(data: $T/[]$E, less: proc(i, j: E) -> bool, allocat
|
|||||||
return indices
|
return indices
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return indices
|
||||||
}
|
}
|
||||||
|
|
||||||
sort_by_cmp :: proc(data: $T/[]$E, cmp: proc(i, j: E) -> Ordering) {
|
sort_by_cmp :: proc(data: $T/[]$E, cmp: proc(i, j: E) -> Ordering) {
|
||||||
|
|||||||
Reference in New Issue
Block a user