Skip to content

Commit

Permalink
Revert loops
Browse files Browse the repository at this point in the history
Revert changes
  • Loading branch information
delaaxe committed Feb 2, 2024
1 parent 13992b3 commit bbedc21
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 38 deletions.
6 changes: 6 additions & 0 deletions src/ascii/src/integer.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ impl SmallIntegerToAsciiTraitImpl<
let val = *inverse_ascii_arr.pop_back().unwrap();
ascii = ascii * 256 + val;
};
loop {
match inverse_ascii_arr.pop_back() {
Option::Some(val) => { ascii = ascii * 256 + *val; },
Option::None(_) => { break; },
};
};

ascii
}
Expand Down
14 changes: 9 additions & 5 deletions src/bytes/src/utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ use keccak::{u128_to_u64, u128_split as u128_split_to_u64, cairo_keccak};
fn keccak_u128s_be(mut input: Span<u128>, n_bytes: usize) -> u256 {
let mut keccak_input = array![];
let mut size = n_bytes;
while !input.is_empty() {
let v = *input.pop_front().unwrap();
let value_size = uint_min(size, 16);
keccak_add_uint128_be(ref keccak_input, v, value_size);
size -= value_size;
loop {
match input.pop_front() {
Option::Some(v) => {
let value_size = uint_min(size, 16);
keccak_add_uint128_be(ref keccak_input, *v, value_size);
size -= value_size;
},
Option::None => { break; },
};
};

let aligned = n_bytes % 8 == 0;
Expand Down
18 changes: 12 additions & 6 deletions src/linalg/src/kron.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@ fn kron<T, +Mul<T>, +AddEq<T>, +Zeroable<T>, +Copy<T>, +Drop<T>,>(

// [Compute] Kronecker product in a loop
let mut array = array![];
while !xs.is_empty() {
let x = *xs.pop_front().unwrap();
let mut ys_copy = ys;
while !ys_copy.is_empty() {
let y = *ys_copy.pop_front().unwrap();
array.append(x * y);
loop {
match xs.pop_front() {
Option::Some(x_value) => {
let mut ys_clone = ys;
loop {
match ys_clone.pop_front() {
Option::Some(y_value) => { array.append(*x_value * *y_value); },
Option::None => { break; },
};
};
},
Option::None => { break; },
};
};

Expand Down
22 changes: 13 additions & 9 deletions src/linalg/src/norm.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@ fn norm<T, +Into<T, u128>, +Zeroable<T>, +Copy<T>>(
mut xs: Span<T>, ord: u128, iter: usize
) -> u128 {
let mut norm: u128 = 0;
while !xs.is_empty() {
let x = xs.pop_front().unwrap();
if ord == 0 {
if (*x).is_non_zero() {
norm += 1;
}
} else {
norm += pow((*x).into(), ord);
}
loop {
match xs.pop_front() {
Option::Some(x_value) => {
if ord == 0 {
if (*x_value).is_non_zero() {
norm += 1;
}
} else {
norm += pow((*x_value).into(), ord);
}
},
Option::None => { break; },
};
};

if ord == 0 {
Expand Down
15 changes: 9 additions & 6 deletions src/math/src/lcm_of_n_numbers.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ fn lcm<T, +Into<T, u128>, +Into<u128, T>, +Mul<T>, +Div<T>, +Copy<T>, +Drop<T>>(
return Result::Err(LCMError::EmptyInput);
}
let mut a = *n.pop_front().unwrap();
while !n.is_empty() {
let b = *n.pop_front().unwrap();
let gcd: T = gcd_two_numbers(a.into(), b.into()).into();
a = (a * b) / gcd;
};
Result::Ok(a)
loop {
match n.pop_front() {
Option::Some(b) => {
let gcd: T = gcd_two_numbers(a.into(), (*b).into()).into();
a = (a * *b) / gcd;
},
Option::None => { break Result::Ok(a); },
};
}
}
14 changes: 9 additions & 5 deletions src/numeric/src/cumprod.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ fn cumprod<T, +Mul<T>, +Copy<T>, +Drop<T>,>(mut sequence: Span<T>) -> Array<T> {
// [Compute] Interpolation
let mut prev_value = *sequence.pop_front().unwrap();
let mut array = array![prev_value];
while !sequence.is_empty() {
let current_value = *sequence.pop_front().unwrap();
let product = current_value * prev_value;
array.append(product);
prev_value = product;
loop {
match sequence.pop_front() {
Option::Some(current_value) => {
let prod = *current_value * prev_value;
array.append(prod);
prev_value = prod;
},
Option::None => { break; },
};
};
array
}
16 changes: 9 additions & 7 deletions src/numeric/src/cumsum.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use core::array::SpanTrait;
use core::option::OptionTrait;
//! The cumulative sum of the elements.

/// Compute the cumulative sum of a sequence.
Expand All @@ -14,11 +12,15 @@ fn cumsum<T, +Add<T>, +Copy<T>, +Drop<T>,>(mut sequence: Span<T>) -> Array<T> {
// [Compute] Interpolation
let mut prev_value = *sequence.pop_front().unwrap();
let mut array = array![prev_value];
while !sequence.is_empty() {
let current_value = *sequence.pop_front().unwrap();
let sum = current_value + prev_value;
array.append(sum);
prev_value = sum;
loop {
match sequence.pop_front() {
Option::Some(current_value) => {
let sum = *current_value + prev_value;
array.append(sum);
prev_value = sum;
},
Option::None => { break; },
};
};
array
}

0 comments on commit bbedc21

Please sign in to comment.