-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
241 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
test_programs/noir_test_success/regression_noir_sort/Nargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[package] | ||
name = "regression_noir_sort" | ||
type = "bin" | ||
authors = [""] | ||
|
||
[dependencies] |
46 changes: 46 additions & 0 deletions
46
test_programs/noir_test_success/regression_noir_sort/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
mod quicksort; | ||
|
||
pub fn sort<T, let N: u32>(input: [T; N]) -> [T; N] | ||
where | ||
T: Ord, | ||
{ | ||
sort_via(input, |a, b| a <= b) | ||
} | ||
|
||
pub fn sort_via<T, let N: u32>(input: [T; N], sortfn: fn(T, T) -> bool) -> [T; N] { | ||
// Safety: test | ||
let sorted = unsafe { quicksort::quicksort(input, sortfn) }; | ||
|
||
for i in 0..N - 1 { | ||
assert( | ||
sortfn(sorted[i], sorted[i + 1]), | ||
"Array has not been sorted correctly according to `ordering`.", | ||
); | ||
} | ||
|
||
sorted | ||
} | ||
|
||
mod test { | ||
use crate::sort; | ||
|
||
global arr_comptime: [u32; 7] = [3, 6, 8, 10, 1, 2, 1]; | ||
global expected_comptime: [u32; 7] = [1, 1, 2, 3, 6, 8, 10]; | ||
|
||
#[test] | ||
fn test_sort() { | ||
let mut arr: [u32; 7] = [3, 6, 8, 10, 1, 2, 1]; | ||
|
||
let sorted = sort(arr); | ||
|
||
let expected: [u32; 7] = [1, 1, 2, 3, 6, 8, 10]; | ||
assert(sorted == expected); | ||
} | ||
|
||
#[test] | ||
fn test_sort_comptime() { | ||
let sorted = sort(arr_comptime); | ||
|
||
assert(sorted == expected_comptime); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
test_programs/noir_test_success/regression_noir_sort/src/quicksort.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
unconstrained fn partition<T, let N: u32>( | ||
arr: &mut [T; N], | ||
low: u32, | ||
high: u32, | ||
sortfn: unconstrained fn(T, T) -> bool, | ||
) -> u32 { | ||
let pivot = high; | ||
let mut i = low; | ||
for j in low..high { | ||
if (sortfn(arr[j], arr[pivot])) { | ||
let temp = arr[i]; | ||
arr[i] = arr[j]; | ||
arr[j] = temp; | ||
i += 1; | ||
} | ||
} | ||
|
||
let temp = arr[i]; | ||
arr[i] = arr[pivot]; | ||
arr[pivot] = temp; | ||
i | ||
} | ||
|
||
unconstrained fn quicksort_loop<T, let N: u32>( | ||
arr: &mut [T; N], | ||
low: u32, | ||
high: u32, | ||
sortfn: unconstrained fn(T, T) -> bool, | ||
) { | ||
let mut stack: [(u32, u32)] = &[(low, high)]; | ||
// TODO(https://github.com/noir-lang/noir_sort/issues/22): use 'loop' once it's stabilized | ||
for _ in 0..2 * N { | ||
if stack.len() == 0 { | ||
break; | ||
} | ||
|
||
let (new_stack, (new_low, new_high)) = stack.pop_back(); | ||
stack = new_stack; | ||
|
||
if new_high < new_low + 1 { | ||
continue; | ||
} | ||
|
||
let pivot_index = partition(arr, new_low, new_high, sortfn); | ||
stack = stack.push_back((pivot_index + 1, new_high)); | ||
if 0 < pivot_index { | ||
stack = stack.push_back((new_low, pivot_index - 1)); | ||
} | ||
} | ||
} | ||
|
||
pub unconstrained fn quicksort<T, let N: u32>( | ||
arr: [T; N], | ||
sortfn: unconstrained fn(T, T) -> bool, | ||
) -> [T; N] { | ||
let mut arr: [T; N] = arr; | ||
if arr.len() <= 1 {} else { | ||
quicksort_loop(&mut arr, 0, arr.len() - 1, sortfn); | ||
} | ||
arr | ||
} |