mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-05 15:18:49 +00:00
cleanup slice sorting with indices changes
This commit is contained in:
@@ -60,17 +60,17 @@ sort_by_indices_overwrite :: proc(data: $T/[]$E, indices: []int) {
|
|||||||
// sort sorts a slice and returns a slice of the original indices
|
// sort sorts a slice and returns a slice of the original indices
|
||||||
// This sort is not guaranteed to be stable
|
// This sort is not guaranteed to be stable
|
||||||
sort_with_indices :: proc(data: $T/[]$E, allocator := context.allocator) -> (indices: []int) where ORD(E) {
|
sort_with_indices :: proc(data: $T/[]$E, allocator := context.allocator) -> (indices: []int) where ORD(E) {
|
||||||
|
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
|
||||||
}
|
}
|
||||||
_quick_sort_general_with_indices(data, indices, 0, n, _max_depth(n), struct{}{}, .Ordered)
|
_quick_sort_general_with_indices(data, indices, 0, n, _max_depth(n), struct{}{}, .Ordered)
|
||||||
return indices
|
|
||||||
}
|
}
|
||||||
|
return indices
|
||||||
}
|
}
|
||||||
return nil
|
return indices
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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"
|
||||||
|
|||||||
@@ -51,8 +51,11 @@ test_sort_with_indices :: proc(t: ^testing.T) {
|
|||||||
r := rand.create(seed)
|
r := rand.create(seed)
|
||||||
|
|
||||||
vals := make([]u64, test_size)
|
vals := make([]u64, test_size)
|
||||||
defer delete(vals)
|
|
||||||
r_idx := make([]int, test_size) // Reverse index
|
r_idx := make([]int, test_size) // Reverse index
|
||||||
|
defer {
|
||||||
|
delete(vals)
|
||||||
|
delete(r_idx)
|
||||||
|
}
|
||||||
|
|
||||||
// Set up test values
|
// Set up test values
|
||||||
for _, i in vals {
|
for _, i in vals {
|
||||||
|
|||||||
Reference in New Issue
Block a user