-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from HerodotusDev/feat/generate_queries
Feat/generate queries
- Loading branch information
Showing
12 changed files
with
3,616 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use cairo_verifier::common::array_extend::ArrayExtendTrait; | ||
|
||
#[generate_trait] | ||
impl ArraySplit<T, +Copy<T>, +Drop<T>> of ArraySplitTrait<T> { | ||
fn split(self: Array<T>, index: u32) -> (Array<T>, Array<T>) { | ||
let mut arr1 = array![]; | ||
let mut arr2 = array![]; | ||
|
||
arr1.extend(self.span().slice(0, index)); | ||
arr2.extend(self.span().slice(index, self.len() - index)); | ||
|
||
(arr1, arr2) | ||
} | ||
} |
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,65 @@ | ||
use cairo_verifier::common::array_split::ArraySplitTrait; | ||
|
||
// Merge Sort | ||
/// # Arguments | ||
/// * `arr` - Array to sort | ||
/// # Returns | ||
/// * `Array<T>` - Sorted array | ||
fn merge_sort<T, +Copy<T>, +Drop<T>, +PartialOrd<T>>(arr: Array<T>) -> Array<T> { | ||
let len = arr.len(); | ||
if len <= 1 { | ||
return arr; | ||
} | ||
|
||
// Create left and right arrays | ||
let (left_arr, right_arr) = arr.split(len / 2); | ||
|
||
// Recursively sort the left and right arrays | ||
let sorted_left = merge_sort(left_arr); | ||
let sorted_right = merge_sort(right_arr); | ||
|
||
let mut result_arr = array![]; | ||
merge_iterative(sorted_left.span(), sorted_right.span(), ref result_arr); | ||
result_arr | ||
} | ||
|
||
fn merge_iterative<T, +Copy<T>, +Drop<T>, +PartialOrd<T>>( | ||
left_arr: Span<T>, right_arr: Span<T>, ref result_arr: Array<T>, | ||
) { | ||
let mut left_arr_ix = 0; | ||
let mut right_arr_ix = 0; | ||
|
||
loop { | ||
if left_arr_ix < left_arr.len() && right_arr_ix < right_arr.len() { | ||
if *left_arr.at(left_arr_ix) < *right_arr.at(right_arr_ix) { | ||
result_arr.append(*left_arr.at(left_arr_ix)); | ||
left_arr_ix += 1; | ||
} else { | ||
result_arr.append(*right_arr.at(right_arr_ix)); | ||
right_arr_ix += 1; | ||
} | ||
} else { | ||
break; | ||
} | ||
}; | ||
|
||
// Append the remaining elements from left_arr, if any | ||
loop { | ||
if left_arr_ix < left_arr.len() { | ||
result_arr.append(*left_arr.at(left_arr_ix)); | ||
left_arr_ix += 1; | ||
} else { | ||
break; | ||
} | ||
}; | ||
|
||
// Append the remaining elements from right_arr, if any | ||
loop { | ||
if right_arr_ix < right_arr.len() { | ||
result_arr.append(*right_arr.at(right_arr_ix)); | ||
right_arr_ix += 1; | ||
} else { | ||
break; | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -4,3 +4,4 @@ mod test_from_span; | |
mod test_horner_eval; | ||
mod test_array_append; | ||
mod test_math; | ||
mod test_merge_sort; |
Oops, something went wrong.