add sort_with_indices family of procs

This commit is contained in:
Phil
2022-08-31 14:06:21 -07:00
parent 768abf83f6
commit 15aaf7dfa0
2 changed files with 205 additions and 0 deletions
+28
View File
@@ -38,6 +38,20 @@ sort :: proc(data: $T/[]$E) where ORD(E) {
}
}
// sort sorts a slice
// This sort is not guaranteed to be stable
sort_with_indices :: proc(data: $T/[]$E, indices: []int) where ORD(E) {
when size_of(E) != 0 {
if n := len(data); n > 1 {
assert(len(data) == len(indices))
for _, idx in indices {
indices[idx] = idx
}
_quick_sort_general_with_indices(data, indices, 0, n, _max_depth(n), struct{}{}, .Ordered)
}
}
}
// 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
sort_by :: proc(data: $T/[]$E, less: proc(i, j: E) -> bool) {
@@ -48,6 +62,20 @@ 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"
// This sort is not guaranteed to be stable
sort_by_with_indices :: proc(data: $T/[]$E, indices: []int, less: proc(i, j: E) -> bool) {
when size_of(E) != 0 {
if n := len(data); n > 1 {
assert(len(data) == len(indices))
for _, idx in indices {
indices[idx] = idx
}
_quick_sort_general_with_indices(data, indices, 0, n, _max_depth(n), less, .Less)
}
}
}
sort_by_cmp :: proc(data: $T/[]$E, cmp: proc(i, j: E) -> Ordering) {
when size_of(E) != 0 {
if n := len(data); n > 1 {