Skip to content
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

fix: apply proper types to function arguments #17

Merged
merged 1 commit into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/lib.nr
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ where
**/
pub fn sort_extended<T, let N: u32>(
input: [T; N],
sortfn: fn(T, T) -> bool,
sortfn: unconstrained fn(T, T) -> bool,
sortfn_assert: fn(T, T) -> (),
) -> [T; N]
where
Expand All @@ -82,7 +82,7 @@ pub struct SortResult<T, let N: u32> {
}
pub fn sort_advanced<T, let N: u32>(
input: [T; N],
sortfn: fn(T, T) -> bool,
sortfn: unconstrained fn(T, T) -> bool,
sortfn_assert: fn(T, T) -> (),
) -> SortResult<T, N>
where
Expand All @@ -107,6 +107,10 @@ mod test {
a <= b
}

unconstrained fn __sort_u32(a: u32, b: u32) -> bool {
a <= b
}

// unconditional_lt will cost fewer constraints than the `<=` operator
// as we do not need to constrain the case where `a > b`, and assign a boolean variable to the result
fn unconditional_lt(_a: u32, _b: u32) {
Expand Down Expand Up @@ -141,7 +145,7 @@ mod test {
fn test_sort_extended() {
let mut arr: [u32; 7] = [3, 6, 8, 10, 1, 2, 1];

let sorted = sort_extended(arr, sort_u32, unconditional_lt);
let sorted = sort_extended(arr, __sort_u32, unconditional_lt);

let expected: [u32; 7] = [1, 1, 2, 3, 6, 8, 10];
assert(sorted == expected);
Expand Down
9 changes: 6 additions & 3 deletions src/quicksort/quicksort_explicit.nr
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ unconstrained fn partition<T, let N: u32>(
arr: &mut [T; N],
low: u32,
high: u32,
sortfn: fn(T, T) -> bool,
sortfn: unconstrained fn(T, T) -> bool,
) -> u32 {
let pivot = high;
let mut i = low;
Expand All @@ -32,7 +32,7 @@ unconstrained fn quicksort_recursive<T, let N: u32>(
arr: &mut [T; N],
low: u32,
high: u32,
sortfn: fn(T, T) -> bool,
sortfn: unconstrained fn(T, T) -> bool,
) {
if low < high {
let pivot_index = partition(arr, low, high, sortfn);
Expand All @@ -43,7 +43,10 @@ unconstrained fn quicksort_recursive<T, let N: u32>(
}
}

pub unconstrained fn quicksort<T, let N: u32>(_arr: [T; N], sortfn: fn(T, T) -> bool) -> [T; N] {
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_recursive(&mut arr, 0, arr.len() - 1, sortfn);
Expand Down
Loading