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

2.6.0: use while let #284

Merged
merged 3 commits into from
Mar 6, 2024
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
14 changes: 5 additions & 9 deletions src/bytes/src/utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,12 @@ 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;
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; },
while let Option::Some(v) = input
.pop_front() {
let value_size = uint_min(size, 16);
keccak_add_uint128_be(ref keccak_input, *v, value_size);
size -= value_size;
};
};

let aligned = n_bytes % 8 == 0;
if aligned {
Expand Down
96 changes: 40 additions & 56 deletions src/data_structures/src/array_ext.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,8 @@ impl ArrayImpl<T, +Copy<T>, +Drop<T>> of ArrayTraitExt<T> {
}

fn concat_span<+Destruct<T>>(ref self: Array<T>, mut arr2: Span<T>) {
loop {
match arr2.pop_front() {
Option::Some(elem) => self.append(*elem),
Option::None => { break; }
};
while let Option::Some(elem) = arr2.pop_front() {
self.append(*elem);
}
}

Expand Down Expand Up @@ -195,29 +192,25 @@ impl SpanImpl<T, +Copy<T>, +Drop<T>> of SpanTraitExt<T> {

fn occurrences_of<+PartialEq<T>>(mut self: Span<T>, item: T) -> usize {
let mut count = 0_usize;
loop {
match self.pop_front() {
Option::Some(v) => { if *v == item {
count += 1;
} },
Option::None => { break count; },
};
}
while let Option::Some(v) = self.pop_front() {
if *v == item {
count += 1;
}
};
count
}

fn min<+PartialEq<T>, +PartialOrd<T>>(mut self: Span<T>) -> Option<T> {
let mut min = match self.pop_front() {
Option::Some(item) => *item,
Option::None => { return Option::None; },
};
loop {
match self.pop_front() {
Option::Some(item) => { if *item < min {
min = *item
} },
Option::None => { break Option::Some(min); },
};
}
while let Option::Some(item) = self.pop_front() {
if *item < min {
min = *item
}
};
Option::Some(min)
}

fn index_of_min<+PartialEq<T>, +PartialOrd<T>>(mut self: Span<T>) -> Option<usize> {
Expand All @@ -227,31 +220,28 @@ impl SpanImpl<T, +Copy<T>, +Drop<T>> of SpanTraitExt<T> {
Option::Some(item) => *item,
Option::None => { return Option::None; },
};
loop {
match self.pop_front() {
Option::Some(item) => { if *item < min {
while let Option::Some(item) = self
.pop_front() {
if *item < min {
index_of_min = index + 1;
min = *item;
} },
Option::None => { break Option::Some(index_of_min); },
}
index += 1;
};
index += 1;
}
Option::Some(index_of_min)
}

fn max<+PartialEq<T>, +PartialOrd<T>>(mut self: Span<T>) -> Option<T> {
let mut max = match self.pop_front() {
Option::Some(item) => *item,
Option::None => { return Option::None; },
};
loop {
match self.pop_front() {
Option::Some(item) => { if *item > max {
max = *item
} },
Option::None => { break Option::Some(max); },
};
}
while let Option::Some(item) = self.pop_front() {
if *item > max {
max = *item
}
};
Option::Some(max)
}

fn index_of_max<+PartialEq<T>, +PartialOrd<T>>(mut self: Span<T>) -> Option<usize> {
Expand All @@ -261,16 +251,15 @@ impl SpanImpl<T, +Copy<T>, +Drop<T>> of SpanTraitExt<T> {
Option::Some(item) => *item,
Option::None => { return Option::None; },
};
loop {
match self.pop_front() {
Option::Some(item) => { if *item > max {
while let Option::Some(item) = self
.pop_front() {
if *item > max {
index_of_max = index + 1;
max = *item
} },
Option::None => { break Option::Some(index_of_max); },
}
index += 1;
};
index += 1;
}
Option::Some(index_of_max)
}

fn dedup<+PartialEq<T>>(mut self: Span<T>) -> Array<T> {
Expand All @@ -281,28 +270,23 @@ impl SpanImpl<T, +Copy<T>, +Drop<T>> of SpanTraitExt<T> {
let mut last_value = self.pop_front().unwrap();
let mut ret = array![*last_value];

loop {
match self.pop_front() {
Option::Some(v) => { if (last_value != v) {
while let Option::Some(v) = self
.pop_front() {
if (last_value != v) {
last_value = v;
ret.append(*v);
}; },
Option::None => { break; }
}
};
};

ret
}

fn unique<+PartialEq<T>>(mut self: Span<T>) -> Array<T> {
let mut ret = array![];
loop {
match self.pop_front() {
Option::Some(v) => { if !ret.contains(*v) {
ret.append(*v);
} },
Option::None => { break; }
};
while let Option::Some(v) = self.pop_front() {
if !ret.contains(*v) {
ret.append(*v);
}
};
ret
}
Expand Down
21 changes: 8 additions & 13 deletions src/linalg/src/kron.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,15 @@ fn kron<T, +Mul<T>, +AddEq<T>, +Zeroable<T>, +Copy<T>, +Drop<T>,>(

// [Compute] Kronecker product in a loop
let mut array = array![];
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; },

while let Option::Some(x_value) = xs
.pop_front() {
let mut ys_clone = ys;
while let Option::Some(y_value) = ys_clone
.pop_front() {
array.append(*x_value * *y_value);
}
};
};

Result::Ok(array)
}
20 changes: 8 additions & 12 deletions src/linalg/src/norm.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,16 @@ fn norm<T, +Into<T, u128>, +Zeroable<T>, +Copy<T>>(
mut xs: Span<T>, ord: u128, iter: usize
) -> u128 {
let mut norm: u128 = 0;
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);
while let Option::Some(x_value) = xs
.pop_front() {
if ord == 0 {
if (*x_value).is_non_zero() {
norm += 1;
}
},
Option::None => { break; },
} else {
norm += pow((*x_value).into(), ord);
};
};
};

if ord == 0 {
return norm;
Expand Down
10 changes: 4 additions & 6 deletions src/math/src/gcd_of_n_numbers.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ fn gcd(mut n: Span<u128>) -> u128 {
panic_with_felt252('EI')
}
let mut a = *n.pop_front().unwrap();
loop {
match n.pop_front() {
Option::Some(b) => { a = gcd_two_numbers(a, *b); },
Option::None => { break a; },
};
}
while let Option::Some(b) = n.pop_front() {
a = gcd_two_numbers(a, *b);
};
a
}

// Internal function to calculate the gcd between two numbers
Expand Down
13 changes: 5 additions & 8 deletions src/math/src/lcm_of_n_numbers.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,10 @@ 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();
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); },
while let Option::Some(b) = n
.pop_front() {
let gcd: T = gcd_two_numbers(a.into(), (*b).into()).into();
a = (a * *b) / gcd;
};
}
Result::Ok(a)
}
48 changes: 20 additions & 28 deletions src/math/src/sha256.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,17 @@ fn sha256(mut data: Array<u8>) -> Array<u8> {

fn from_u32Array_to_u8Array(mut data: Span<u32>) -> Array<u8> {
let mut result = array![];
loop {
match data.pop_front() {
Option::Some(val) => {
let mut res = (*val & 0xff000000) / 0x1000000;
result.append(res.try_into().unwrap());
res = (*val & 0xff0000) / 0x10000;
result.append(res.try_into().unwrap());
res = (*val & 0xff00) / 0x100;
result.append(res.try_into().unwrap());
res = *val & 0xff;
result.append(res.try_into().unwrap());
},
Option::None => { break; },
while let Option::Some(val) = data
.pop_front() {
let mut res = (*val & 0xff000000) / 0x1000000;
result.append(res.try_into().unwrap());
res = (*val & 0xff0000) / 0x10000;
result.append(res.try_into().unwrap());
res = (*val & 0xff00) / 0x100;
result.append(res.try_into().unwrap());
res = *val & 0xff;
result.append(res.try_into().unwrap());
};
};
result
}

Expand Down Expand Up @@ -167,21 +163,17 @@ fn create_message_schedule(data: Span<u32>, i: usize) -> Span<u32> {

fn from_u8Array_to_u32Array(mut data: Span<u8>) -> Array<u32> {
let mut result = array![];
loop {
match data.pop_front() {
Option::Some(val1) => {
let val2 = data.pop_front().unwrap();
let val3 = data.pop_front().unwrap();
let val4 = data.pop_front().unwrap();
let mut value = (*val1).into() * 0x1000000;
value = value + (*val2).into() * 0x10000;
value = value + (*val3).into() * 0x100;
value = value + (*val4).into();
result.append(value);
},
Option::None => { break; },
while let Option::Some(val1) = data
.pop_front() {
let val2 = data.pop_front().unwrap();
let val3 = data.pop_front().unwrap();
let val4 = data.pop_front().unwrap();
let mut value = (*val1).into() * 0x1000000;
value = value + (*val2).into() * 0x10000;
value = value + (*val3).into() * 0x100;
value = value + (*val4).into();
result.append(value);
};
};
result
}

Expand Down
Loading
Loading