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

Fix array_ext trait and format with new scarb #345

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
12 changes: 6 additions & 6 deletions packages/ascii/src/integer.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl ToAsciiArrayTraitImpl<
);
new_arr.append(remainder.into() + 48);
num = quotient;
};
}
new_arr
}
}
Expand Down Expand Up @@ -75,7 +75,7 @@ impl SmallIntegerToAsciiTraitImpl<
Option::Some(val) => { ascii = ascii * 256 + *val; },
Option::None(_) => { break; },
};
};
}

ascii
}
Expand Down Expand Up @@ -126,9 +126,9 @@ impl BigIntegerToAsciiTraitImpl<
}
break;
},
};
}
index += 1;
};
}
data
}
}
Expand Down Expand Up @@ -158,7 +158,7 @@ impl U256ToAsciiArrayTraitImpl of ToAsciiArrayTrait<u256> {
);
new_arr.append(remainder.try_into().expect('number overflow felt252') + 48);
num = quotient;
};
}
new_arr
}
}
Expand Down Expand Up @@ -187,7 +187,7 @@ impl U256ToAsciiTraitImpl of ToAsciiTrait<u256, Array<felt252>> {
new_ascii
};
index += 1;
};
}

if ascii.is_non_zero() {
data.append(ascii);
Expand Down
18 changes: 9 additions & 9 deletions packages/bytes/src/bytes.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ pub trait BytesTrait {

impl BytesImpl of BytesTrait {
#[inline(always)]
fn new(size: usize, data: Array::<u128>) -> Bytes {
fn new(size: usize, data: Array<u128>) -> Bytes {
Bytes { size, data }
}

Expand All @@ -156,7 +156,7 @@ impl BytesImpl of BytesTrait {
while data_len != 0 {
data.append(0_u128);
data_len -= 1;
};
}

Bytes { size, data }
}
Expand Down Expand Up @@ -254,7 +254,7 @@ impl BytesImpl of BytesTrait {
array.append(value);
offset = new_offset;
i -= 1;
};
}
(offset, array)
}

Expand Down Expand Up @@ -341,7 +341,7 @@ impl BytesImpl of BytesTrait {
array.append(value);
offset = new_offset;
i -= 1;
};
}
(offset, array)
}

Expand All @@ -364,7 +364,7 @@ impl BytesImpl of BytesTrait {
array.append(value);
offset = new_offset;
sub_bytes_full_array_len -= 1;
};
}

// process last array element for sub_bytes
// 1. read last element real value;
Expand Down Expand Up @@ -524,7 +524,7 @@ impl BytesImpl of BytesTrait {
self.append_u128(value);
offset = new_offset;
sub_bytes_full_array_len -= 1;
};
}

// process last array element for right
let sub_bytes_last_element_size = *other.size % BYTES_PER_ELEMENT;
Expand Down Expand Up @@ -560,7 +560,7 @@ impl BytesImpl of BytesTrait {
hash_data_byte_array.append_byte(hash_data_item);
offset = new_offset;
i += 1;
};
}

let output = sha256::compute_sha256_byte_array(@hash_data_byte_array);
u32s_to_u256(output.span())
Expand All @@ -574,7 +574,7 @@ pub impl ByteArrayIntoBytes of Into<ByteArray, Bytes> {
while len < self.len() {
res.append_u8(self[len]);
len += 1;
};
}
res
}
}
Expand All @@ -593,7 +593,7 @@ pub impl BytesIntoByteArray of Into<Bytes, ByteArray> {
res.append_byte(value);
offset = new_offset;
}
};
}
res
}
}
2 changes: 1 addition & 1 deletion packages/bytes/src/storage.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ fn inner_write_bytes(
) {
Result::Ok(_) => {},
Result::Err(err) => { break Result::Err(err); },
};
}

match index_in_chunk.checked_add(1) {
Option::Some(x) => { index_in_chunk = x; },
Expand Down
20 changes: 10 additions & 10 deletions packages/bytes/src/utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub impl BytesDebug of Debug<Bytes> {
break;
}
i = new_i;
};
}
res
}
}
Expand All @@ -46,7 +46,7 @@ pub impl BytesDisplay of Display<Bytes> {
break;
}
i = new_i;
};
}
res
}
}
Expand All @@ -61,7 +61,7 @@ pub fn keccak_u128s_be(input: Span<u128>, n_bytes: usize) -> u256 {
let value_size = core::cmp::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 All @@ -80,7 +80,7 @@ fn u256_reverse_endian(input: u256) -> u256 {
u256 { low, high }
}

fn keccak_add_uint128_be(ref keccak_input: Array::<u64>, value: u128, value_size: usize) {
fn keccak_add_uint128_be(ref keccak_input: Array<u64>, value: u128, value_size: usize) {
if value_size == 16 {
let (high, low) = core::integer::u128_safe_divmod(
u128_byte_reverse(value), 0x10000000000000000_u128.try_into().unwrap(),
Expand Down Expand Up @@ -114,7 +114,7 @@ fn update_u256_array_at(arr: @Array<u256>, index: usize, value: u256) -> Array<u
new_arr.append(*arr[i]);
}
i += 1;
};
}
new_arr
}

Expand All @@ -129,12 +129,12 @@ pub fn u8_array_to_u256(arr: Span<u8>) -> u256 {
while i < arr.len() && i != 16 {
high = u128_join(high, (*arr[i]).into(), 1);
i += 1;
};
}
// process low
while i < arr.len() && i != 32 {
low = u128_join(low, (*arr[i]).into(), 1);
i += 1;
};
}

u256 { low, high }
}
Expand All @@ -160,7 +160,7 @@ fn u64_array_slice(src: @Array<u64>, mut begin: usize, len: usize) -> Array<u64>
while begin < end && begin < src.len() {
slice.append(*src[begin]);
begin += 1;
};
}
slice
}

Expand All @@ -176,7 +176,7 @@ pub fn u128_array_slice(src: @Array<u128>, mut begin: usize, len: usize) -> Arra
while begin < end && begin < src.len() {
slice.append(*src[begin]);
begin += 1;
};
}
slice
}

Expand All @@ -186,7 +186,7 @@ fn array_slice<T, +Drop<T>, +Copy<T>>(src: @Array<T>, mut begin: usize, len: usi
while begin < end && begin < src.len() {
slice.append(*src[begin]);
begin += 1;
};
}
slice
}

Expand Down
16 changes: 8 additions & 8 deletions packages/data_structures/src/array_ext.cairo
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use super::span_ext::SpanTraitExt;

pub trait ArrayTraitExt<T> {
pub trait ArrayTraitExt<T, +Clone<T>, +Drop<T>> {
/// Moves all the elements of `other` into `self`, leaving `other` empty.
fn append_all(ref self: Array<T>, ref other: Array<T>);
/// Clones and appends all the elements of `other` into `self`.
fn extend_from_span<+Drop<T>>(ref self: Array<T>, other: Span<T>);
fn extend_from_span(ref self: Array<T>, other: Span<T>);
/// Removes up to `n` elements from the front of `self` and returns them in a new array.
fn pop_front_n(ref self: Array<T>, n: usize) -> Array<T>;
/// Removes up to `n` elements from the front of `self`.
Expand All @@ -20,13 +20,13 @@ pub trait ArrayTraitExt<T> {
/// Returns the number of elements in the array with the given value.
fn occurrences<+PartialEq<T>>(self: @Array<T>, item: @T) -> usize;
/// Returns the minimum element of an array.
fn min<+PartialOrd<T>>(self: @Array<T>) -> Option<T>;
fn min<+PartialOrd<@T>>(self: @Array<T>) -> Option<T>;
/// Returns the position of the minimum element of an array.
fn min_position<+PartialOrd<T>>(self: @Array<T>) -> Option<usize>;
fn min_position<+PartialOrd<@T>>(self: @Array<T>) -> Option<usize>;
/// Returns the maximum element of an array.
fn max<+PartialOrd<T>>(self: @Array<T>) -> Option<T>;
fn max<+PartialOrd<@T>>(self: @Array<T>) -> Option<T>;
/// Returns the position of the maximum element of an array.
fn max_position<+PartialOrd<T>>(self: @Array<T>) -> Option<usize>;
fn max_position<+PartialOrd<@T>>(self: @Array<T>) -> Option<usize>;
/// Returns a new array, cloned from `self` but removes consecutive repeated elements.
/// If the array is sorted, this removes all duplicates.
fn dedup<+PartialEq<T>>(self: @Array<T>) -> Array<T>;
Expand All @@ -41,7 +41,7 @@ impl ArrayImpl<T, +Clone<T>, +Drop<T>> of ArrayTraitExt<T> {
}
}

fn extend_from_span<+Destruct<T>>(ref self: Array<T>, mut other: Span<T>) {
fn extend_from_span(ref self: Array<T>, mut other: Span<T>) {
while let Option::Some(elem) = other.pop_front() {
self.append(elem.clone());
}
Expand All @@ -58,7 +58,7 @@ impl ArrayImpl<T, +Clone<T>, +Drop<T>> of ArrayTraitExt<T> {
},
Option::None => { break; },
};
};
}

res
}
Expand Down
2 changes: 1 addition & 1 deletion packages/data_structures/src/bit_array.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ impl BitArrayImpl of BitArrayTrait {
} else {
bit_offset += 1;
}
};
}
result
}

Expand Down
4 changes: 2 additions & 2 deletions packages/data_structures/src/byte_array_ext.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub impl SpanU8IntoBytearray of Into<Span<u8>, ByteArray> {
Option::Some(byte) => result.append_byte(byte),
Option::None => { break; },
}
};
}
result
}
}
Expand All @@ -27,7 +27,7 @@ pub impl ByteArrayIntoArrayU8 of Into<ByteArray, Array<u8>> {
let mut result = array![];
while let Option::Some(byte) = reader.read_u8() {
result.append(byte);
};
}
result
}
}
18 changes: 9 additions & 9 deletions packages/data_structures/src/span_ext.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::cmp::min;
use core::num::traits::CheckedSub;
use super::array_ext::ArrayTraitExt;

pub trait SpanTraitExt<T> {
pub trait SpanTraitExt<T, +Clone<T>, +Drop<T>> {
/// Removes up to `n` elements from the front of `self` and returns them in a new span.
fn pop_front_n(ref self: Span<T>, n: usize) -> Span<T>;
/// Removes up to `n` elements from the back of `self` and returns them in a new span.
Expand Down Expand Up @@ -88,7 +88,7 @@ impl SpanImpl<T, +Clone<T>, +Drop<T>> of SpanTraitExt<T> {

while let Option::Some(v) = self.pop_back() {
res.append(v.clone());
};
}

res
}
Expand Down Expand Up @@ -125,7 +125,7 @@ impl SpanImpl<T, +Clone<T>, +Drop<T>> of SpanTraitExt<T> {
if v == item {
count += 1;
}
};
}
count
}

Expand All @@ -139,7 +139,7 @@ impl SpanImpl<T, +Clone<T>, +Drop<T>> of SpanTraitExt<T> {
if item < min {
min = item
}
};
}

Option::Some(min.clone())
}
Expand All @@ -157,7 +157,7 @@ impl SpanImpl<T, +Clone<T>, +Drop<T>> of SpanTraitExt<T> {
min = item;
}
index += 1;
};
}

Option::Some(min_position)
}
Expand All @@ -172,7 +172,7 @@ impl SpanImpl<T, +Clone<T>, +Drop<T>> of SpanTraitExt<T> {
if item > max {
max = item
}
};
}

Option::Some(max.clone())
}
Expand All @@ -191,7 +191,7 @@ impl SpanImpl<T, +Clone<T>, +Drop<T>> of SpanTraitExt<T> {
max = item
}
index += 1;
};
}

Option::Some(max_position)
}
Expand All @@ -210,7 +210,7 @@ impl SpanImpl<T, +Clone<T>, +Drop<T>> of SpanTraitExt<T> {
last_value = v;
ret.append(v.clone());
}
};
}

ret
}
Expand All @@ -222,7 +222,7 @@ impl SpanImpl<T, +Clone<T>, +Drop<T>> of SpanTraitExt<T> {
if !ret.span().contains(v) {
ret.append(v.clone());
}
};
}

ret
}
Expand Down
4 changes: 2 additions & 2 deletions packages/data_structures/src/tests/bit_array.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn test_append_bit() {
ba.append_bit(true);
ba.append_bit(false);
c -= 1;
};
}
let val: bytes31 = 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
.try_into()
.unwrap();
Expand Down Expand Up @@ -252,7 +252,7 @@ fn test_stress_test() {
ba.write_word_be(value, 248);
ba.write_word_le(value, 248);
index += 1;
};
}
index = 0;
while (index != limit) {
let value = ba.read_word_be(248).unwrap();
Expand Down
Loading
Loading