Rename slice.sort_proc to slice.sort_by; add slice.sort_by_key

This commit is contained in:
gingerBill
2020-10-16 14:55:36 +01:00
parent 289908e0b8
commit 41f6a684e1
4 changed files with 43 additions and 18 deletions
+1 -1
View File
@@ -284,7 +284,7 @@ _glob :: proc(dir, pattern: string, matches: ^[dynamic]string) -> (m: [dynamic]s
fis, _ := os.read_dir(d, -1); fis, _ := os.read_dir(d, -1);
slice.sort_proc(fis, proc(a, b: os.File_Info) -> bool { slice.sort_by(fis, proc(a, b: os.File_Info) -> bool {
return a.name < b.name; return a.name < b.name;
}); });
defer { defer {
+1 -1
View File
@@ -81,7 +81,7 @@ read_dir :: proc(dir_name: string, allocator := context.temp_allocator) -> ([]os
if err != 0 { if err != 0 {
return nil, err; return nil, err;
} }
slice.sort_proc(fis, proc(a, b: os.File_Info) -> bool { slice.sort_by(fis, proc(a, b: os.File_Info) -> bool {
return a.name < b.name; return a.name < b.name;
}); });
return fis, 0; return fis, 0;
+37 -12
View File
@@ -16,9 +16,9 @@ sort :: proc(data: $T/[]$E) where ORD(E) {
} }
} }
// sort_proc 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_proc :: proc(data: $T/[]$E, less: proc(i, j: E) -> bool) { sort_by :: proc(data: $T/[]$E, less: proc(i, j: E) -> bool) {
when size_of(E) != 0 { when size_of(E) != 0 {
if n := len(data); n > 1 { if n := len(data); n > 1 {
_quick_sort_proc(data, 0, n, _max_depth(n), less); _quick_sort_proc(data, 0, n, _max_depth(n), less);
@@ -26,15 +26,6 @@ sort_proc :: proc(data: $T/[]$E, less: proc(i, j: E) -> bool) {
} }
} }
reverse_sort :: proc(data: $T/[]$E) where ORD(E) {
sort_proc(data, proc(i, j: E) -> bool {
return j < i;
});
}
is_sorted :: proc(array: $T/[]$E) -> bool where ORD(E) { is_sorted :: proc(array: $T/[]$E) -> bool where ORD(E) {
for i := len(array)-1; i > 0; i -= 1 { for i := len(array)-1; i > 0; i -= 1 {
if array[i] < array[i-1] { if array[i] < array[i-1] {
@@ -44,7 +35,7 @@ is_sorted :: proc(array: $T/[]$E) -> bool where ORD(E) {
return true; return true;
} }
is_sorted_proc :: proc(array: $T/[]$E, less: proc(i, j: E) -> bool) -> bool { is_sorted_by :: proc(array: $T/[]$E, less: proc(i, j: E) -> bool) -> bool {
for i := len(array)-1; i > 0; i -= 1 { for i := len(array)-1; i > 0; i -= 1 {
if less(array[i], array[i-1]) { if less(array[i], array[i-1]) {
return false; return false;
@@ -54,6 +45,40 @@ is_sorted_proc :: proc(array: $T/[]$E, less: proc(i, j: E) -> bool) -> bool {
} }
reverse_sort :: proc(data: $T/[]$E) where ORD(E) {
sort_by(data, proc(i, j: E) -> bool {
return j < i;
});
}
// TODO(bill): Should `sort_by_key` exist or is `sort_by` more than enough?
sort_by_key :: proc(data: $T/[]$E, key: proc(E) -> $K) where ORD(K) {
context.user_ptr = rawptr(key);
sort_by(data, proc(i, j: E) -> bool {
k := (proc(E) -> K)(context.user_ptr);
return k(i) < k(j);
});
}
reverse_sort_by_key :: proc(data: $T/[]$E, key: proc(E) -> $K) where ORD(K) {
context.user_ptr = rawptr(key);
sort_by(data, proc(i, j: E) -> bool {
k := (proc(E) -> K)(context.user_ptr);
return k(j) < k(i);
});
}
is_sorted_by_key :: proc(array: $T/[]$E, key: proc(E) -> $K) -> bool where ORD(K) {
for i := len(array)-1; i > 0; i -= 1 {
if key(array[i]) < key(array[i-1]) {
return false;
}
}
return true;
}
@(private) @(private)
_max_depth :: proc(n: int) -> int { // 2*ceil(log2(n+1)) _max_depth :: proc(n: int) -> int { // 2*ceil(log2(n+1))
+4 -4
View File
@@ -290,7 +290,7 @@ _insertion_sort :: proc(it: Interface, a, b: int) {
// @(deprecated="use sort.sort or slice.sort_proc") // @(deprecated="use sort.sort or slice.sort_by")
bubble_sort_proc :: proc(array: $A/[]$T, f: proc(T, T) -> int) { bubble_sort_proc :: proc(array: $A/[]$T, f: proc(T, T) -> int) {
assert(f != nil); assert(f != nil);
count := len(array); count := len(array);
@@ -347,7 +347,7 @@ bubble_sort :: proc(array: $A/[]$T) where intrinsics.type_is_ordered(T) {
} }
} }
// @(deprecated="use sort.sort or slice.sort_proc") // @(deprecated="use sort.sort or slice.sort_by")
quick_sort_proc :: proc(array: $A/[]$T, f: proc(T, T) -> int) { quick_sort_proc :: proc(array: $A/[]$T, f: proc(T, T) -> int) {
assert(f != nil); assert(f != nil);
a := array; a := array;
@@ -412,7 +412,7 @@ _log2 :: proc(x: int) -> int {
return res; return res;
} }
// @(deprecated="use sort.sort or slice.sort_proc") // @(deprecated="use sort.sort or slice.sort_by")
merge_sort_proc :: proc(array: $A/[]$T, f: proc(T, T) -> int) { merge_sort_proc :: proc(array: $A/[]$T, f: proc(T, T) -> int) {
merge :: proc(a: A, start, mid, end: int, f: proc(T, T) -> int) { merge :: proc(a: A, start, mid, end: int, f: proc(T, T) -> int) {
s, m := start, mid; s, m := start, mid;
@@ -497,7 +497,7 @@ merge_sort :: proc(array: $A/[]$T) where intrinsics.type_is_ordered(T) {
} }
// @(deprecated="use sort.sort or slice.sort_proc") // @(deprecated="use sort.sort or slice.sort_by")
heap_sort_proc :: proc(array: $A/[]$T, f: proc(T, T) -> int) { heap_sort_proc :: proc(array: $A/[]$T, f: proc(T, T) -> int) {
sift_proc :: proc(a: A, pi: int, n: int, f: proc(T, T) -> int) #no_bounds_check { sift_proc :: proc(a: A, pi: int, n: int, f: proc(T, T) -> int) #no_bounds_check {
p := pi; p := pi;