Skip to content

Commit

Permalink
Merge pull request #31 from HerodotusDev/feat/generate_queries
Browse files Browse the repository at this point in the history
Feat/generate queries
  • Loading branch information
Okm165 authored Jan 4, 2024
2 parents c706be7 + 82316ae commit dac4908
Show file tree
Hide file tree
Showing 12 changed files with 3,616 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/common.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ mod array_append;
mod math;
mod array_print;
mod array_extend;
mod array_split;
mod consts;
mod merge_sort;

#[cfg(test)]
mod tests;
14 changes: 14 additions & 0 deletions src/common/array_split.cairo
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)
}
}
65 changes: 65 additions & 0 deletions src/common/merge_sort.cairo
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;
}
}
}
1 change: 1 addition & 0 deletions src/common/tests.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ mod test_from_span;
mod test_horner_eval;
mod test_array_append;
mod test_math;
mod test_merge_sort;
Loading

0 comments on commit dac4908

Please sign in to comment.