-
Notifications
You must be signed in to change notification settings - Fork 375
chore: update quicksort from iterative noir_sort version
#7348
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
0e79794
chore: update quicksort from iterative noir_sort version, wip debuggi…
michaeljklein f174f16
Merge branch 'master' into michaeljklein/iterative-quicksort
michaeljklein eb04358
add regression test
michaeljklein e39bdce
Merge branch 'master' into michaeljklein/iterative-quicksort
TomAFrench ff114ec
.
TomAFrench 71c04a4
.
TomAFrench 1f21300
.
TomAFrench ab441f1
Merge branch 'master' into michaeljklein/iterative-quicksort
michaeljklein 6a81368
Merge branch 'master' into michaeljklein/iterative-quicksort
michaeljklein b36039c
Merge branch 'master' into michaeljklein/iterative-quicksort
michaeljklein 0107384
Merge branch 'master' into michaeljklein/iterative-quicksort
michaeljklein 5d61d00
Merge branch 'master' into michaeljklein/iterative-quicksort
michaeljklein fcd620c
Merge branch 'master' into michaeljklein/iterative-quicksort
michaeljklein 67a150b
Merge branch 'master' into michaeljklein/iterative-quicksort
michaeljklein 657d360
Merge branch 'master' into michaeljklein/iterative-quicksort
michaeljklein 80b5d46
fix parser error from merging master
michaeljklein 3c34884
Merge branch 'master' into michaeljklein/iterative-quicksort
michaeljklein 313579e
Merge branch 'master' into michaeljklein/iterative-quicksort
michaeljklein ff2c2b4
cargo insta
michaeljklein 36de989
Merge branch 'master' into michaeljklein/iterative-quicksort
michaeljklein b5f702e
Merge branch 'master' into michaeljklein/iterative-quicksort
michaeljklein d161d97
restore missing 'fn[Env]' from debugging, try to fix snap files
michaeljklein aea2eb4
Merge branch 'master' into michaeljklein/iterative-quicksort
michaeljklein b099b16
skip tests timing out
michaeljklein File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 | ||
| } |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -10,3 +10,5 @@ test_pow | |
| test_pow_and_show | ||
| test_add | ||
| test_add_and_show | ||
| test_sort | ||
| test_sort_comptime | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.