Skip to content

Commit

Permalink
Added high level support and tests for multi pops.
Browse files Browse the repository at this point in the history
commit-id:699134cc
  • Loading branch information
orizi committed May 19, 2024
1 parent 3960647 commit 38ef8ae
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
14 changes: 14 additions & 0 deletions corelib/src/array.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ extern fn array_pop_front<T>(ref arr: Array<T>) -> Option<Box<T>> nopanic;
extern fn array_pop_front_consume<T>(arr: Array<T>) -> Option<(Array<T>, Box<T>)> nopanic;
pub(crate) extern fn array_snapshot_pop_front<T>(ref arr: @Array<T>) -> Option<Box<@T>> nopanic;
extern fn array_snapshot_pop_back<T>(ref arr: @Array<T>) -> Option<Box<@T>> nopanic;
extern fn array_snapshot_multi_pop_front<T, const SIZE: usize>(
ref arr: @Array<T>
) -> Option<@Box<[T; SIZE]>> implicits(RangeCheck) nopanic;
extern fn array_snapshot_multi_pop_back<T, const SIZE: usize>(
ref arr: @Array<T>
) -> Option<@Box<[T; SIZE]>> implicits(RangeCheck) nopanic;
#[panic_with('Index out of bounds', array_at)]
extern fn array_get<T>(
arr: @Array<T>, index: usize
Expand Down Expand Up @@ -187,6 +193,14 @@ pub impl SpanImpl<T> of SpanTrait<T> {
Option::None => Option::None,
}
}
/// Pops multiple values from the front of the span.
fn multi_pop_front<const SIZE: usize>(ref self: Span<T>) -> Option<@Box<[T; SIZE]>> {
array_snapshot_multi_pop_front(ref self.snapshot)
}
/// Pops multiple values from the back of the span.
fn multi_pop_back<const SIZE: usize>(ref self: Span<T>) -> Option<@Box<[T; SIZE]>> {
array_snapshot_multi_pop_back(ref self.snapshot)
}
#[inline(always)]
fn get(self: Span<T>, index: usize) -> Option<Box<@T>> {
array_get(self.snapshot, index)
Expand Down
14 changes: 14 additions & 0 deletions corelib/src/test/array_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,17 @@ fn test_span_into_fixed_size_array() {
assert!(debox::<felt252, 1>([].span().try_into()).is_none());
assert!(debox::<felt252, 0>([].span().try_into()) == Option::Some(@[]));
}

#[test]
fn test_span_multi_pop() {
let mut span = array![10, 11, 12, 13].span();
assert!(span.multi_pop_front::<5>().is_none());
assert!(debox(span.multi_pop_front::<4>()) == Option::Some(@[10, 11, 12, 13]));
let mut span = array![10, 11, 12, 13].span();
assert!(debox(span.multi_pop_front::<3>()) == Option::Some(@[10, 11, 12]));
let mut span = array![10, 11, 12, 13].span();
assert!(span.multi_pop_back::<5>().is_none());
assert!(debox(span.multi_pop_back::<4>()) == Option::Some(@[10, 11, 12, 13]));
let mut span = array![10, 11, 12, 13].span();
assert!(debox(span.multi_pop_back::<3>()) == Option::Some(@[11, 12, 13]));
}
8 changes: 4 additions & 4 deletions crates/cairo-lang-runner/src/casm_run/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1653,17 +1653,17 @@ pub fn execute_core_hint(
insert_value_to_cellref!(vm, dst, segment)?;
}
CoreHint::TestLessThan { lhs, rhs, dst } => {
let lhs_val = get_val(vm, lhs)?;
let rhs_val = get_val(vm, rhs)?;
let lhs_val = get_maybe(vm, lhs)?;
let rhs_val = get_maybe(vm, rhs)?;
insert_value_to_cellref!(
vm,
dst,
if lhs_val < rhs_val { Felt252::from(1) } else { Felt252::from(0) }
)?;
}
CoreHint::TestLessThanOrEqual { lhs, rhs, dst } => {
let lhs_val = get_val(vm, lhs)?;
let rhs_val = get_val(vm, rhs)?;
let lhs_val = get_maybe(vm, lhs)?;
let rhs_val = get_maybe(vm, rhs)?;
insert_value_to_cellref!(
vm,
dst,
Expand Down

0 comments on commit 38ef8ae

Please sign in to comment.