mirror of
https://github.com/Ed94/Odin.git
synced 2026-07-29 02:40:05 +00:00
add sort_by_indices overload and test
This commit is contained in:
+11
-1
@@ -39,7 +39,9 @@ sort :: proc(data: $T/[]$E) where ORD(E) {
|
||||
}
|
||||
|
||||
|
||||
sort_by_indices :: proc(data: $T/[]$E, indices: []int, allocator := context.allocator) -> (sorted: T) {
|
||||
sort_by_indices :: proc{ sort_by_indices_allocate, _sort_by_indices}
|
||||
|
||||
sort_by_indices_allocate :: proc(data: $T/[]$E, indices: []int, allocator := context.allocator) -> (sorted: T) {
|
||||
assert(len(data) == len(indices))
|
||||
sorted = make([]int, len(data), allocator)
|
||||
for v, i in indices {
|
||||
@@ -48,6 +50,14 @@ sort_by_indices :: proc(data: $T/[]$E, indices: []int, allocator := context.allo
|
||||
return
|
||||
}
|
||||
|
||||
_sort_by_indices :: proc(data, sorted: $T/[]$E, indices: []int) {
|
||||
assert(len(data) == len(indices))
|
||||
assert(len(data) == len(sorted))
|
||||
for v, i in indices {
|
||||
sorted[i] = data[v]
|
||||
}
|
||||
}
|
||||
|
||||
sort_by_indices_overwrite :: proc(data: $T/[]$E, indices: []int) {
|
||||
assert(len(data) == len(indices))
|
||||
temp := make([]int, len(data), context.allocator)
|
||||
|
||||
Reference in New Issue
Block a user