| ... | ... | @@ -1,5 +1,8 @@ |
| 1 | 1 | const assert = @import("debug.zig").assert; |
| 2 | 2 | const str = @import("str.zig"); |
| 3 | const math = @import("math.zig"); |
| 4 | |
| 5 | pub const Cmp = math.Cmp; |
| 3 | 6 | |
| 4 | 7 | pub fn sort(inline T: type, array: []T) { |
| 5 | 8 | if (array.len > 0) { |
| ... | ... | @@ -32,6 +35,43 @@ fn quicksort(inline T: type, array: []T, left: usize, right: usize) { |
| 32 | 35 | if (i < right) quicksort(T, array, i, right); |
| 33 | 36 | } |
| 34 | 37 | |
| 38 | // --------------------------------------- |
| 39 | // sortCmp |
| 40 | |
| 41 | pub fn sortCmp(inline T: type, array: []T, inline cmp: fn(a: T, b: T)->Cmp) { |
| 42 | if (array.len > 0) { |
| 43 | quicksortCmp(T, array, 0, array.len - 1, cmp); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | fn quicksortCmp(inline T: type, array: []T, left: usize, right: usize, inline cmp: fn(a: T, b: T)->Cmp) { |
| 48 | var i = left; |
| 49 | var j = right; |
| 50 | var p = (i + j) / 2; |
| 51 | |
| 52 | while (i <= j) { |
| 53 | while (cmp(array[i], array[p]) == Cmp.Less) { |
| 54 | i += 1; |
| 55 | } |
| 56 | while (cmp(array[j], array[p]) == Cmp.Greater) { |
| 57 | j -= 1; |
| 58 | } |
| 59 | if (i <= j) { |
| 60 | const tmp = array[i]; |
| 61 | array[i] = array[j]; |
| 62 | array[j] = tmp; |
| 63 | i += 1; |
| 64 | if (j > 0) j -= 1; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | if (left < j) quicksortCmp(T, array, left, j, cmp); |
| 69 | if (i < right) quicksortCmp(T, array, i, right, cmp); |
| 70 | } |
| 71 | |
| 72 | // --------------------------------------- |
| 73 | // tests |
| 74 | |
| 35 | 75 | fn testSort() { |
| 36 | 76 | @setFnTest(this, true); |
| 37 | 77 | |
| ... | ... | @@ -63,3 +103,44 @@ fn testSort() { |
| 63 | 103 | assert(str.sliceEql(i32, case[0], case[1])); |
| 64 | 104 | } |
| 65 | 105 | } |
| 106 | |
| 107 | fn testSortCmp() { |
| 108 | @setFnTest(this, true); |
| 109 | |
| 110 | const i32cases = [][][]i32 { |
| 111 | [][]i32{[]i32{}, []i32{}}, |
| 112 | [][]i32{[]i32{1}, []i32{1}}, |
| 113 | [][]i32{[]i32{0, 1}, []i32{0, 1}}, |
| 114 | [][]i32{[]i32{1, 0}, []i32{0, 1}}, |
| 115 | [][]i32{[]i32{1, -1, 0}, []i32{-1, 0, 1}}, |
| 116 | [][]i32{[]i32{2, 1, 3}, []i32{1, 2, 3}}, |
| 117 | }; |
| 118 | |
| 119 | for (i32cases) |case| { |
| 120 | sortCmp(i32, case[0], normalCmp); |
| 121 | assert(str.sliceEql(i32, case[0], case[1])); |
| 122 | } |
| 123 | |
| 124 | const revCases = [][][]i32 { |
| 125 | [][]i32{[]i32{}, []i32{}}, |
| 126 | [][]i32{[]i32{1}, []i32{1}}, |
| 127 | [][]i32{[]i32{0, 1}, []i32{1, 0}}, |
| 128 | [][]i32{[]i32{1, 0}, []i32{1, 0}}, |
| 129 | [][]i32{[]i32{1, -1, 0}, []i32{1, 0, -1}}, |
| 130 | [][]i32{[]i32{2, 1, 3}, []i32{3, 2, 1}}, |
| 131 | }; |
| 132 | |
| 133 | for (revCases) |case| { |
| 134 | sortCmp(i32, case[0], revCmp); |
| 135 | assert(str.sliceEql(i32, case[0], case[1])); |
| 136 | } |
| 137 | |
| 138 | } |
| 139 | |
| 140 | fn normalCmp(a: i32, b: i32) -> Cmp { |
| 141 | return if (a > b) Cmp.Greater else if (a < b) Cmp.Less else Cmp.Equal; |
| 142 | } |
| 143 | |
| 144 | fn revCmp(a: i32, b: i32) -> Cmp { |
| 145 | return if (a < b) Cmp.Greater else if (a > b) Cmp.Less else Cmp.Equal; |
| 146 | } |