Skip to content

Commit

Permalink
removing one more IndexView
Browse files Browse the repository at this point in the history
  • Loading branch information
gaetbout committed Aug 5, 2024
1 parent 2df8094 commit 7f506ec
Showing 1 changed file with 71 additions and 78 deletions.
149 changes: 71 additions & 78 deletions packages/data_structures/src/byte_reader.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,13 @@ pub trait ByteReader<T> {
}

// use core::ops::index::IndexView;
// impl ByteReaderImpl<T, u8, +Drop<T>, +Len<T>, +IndexView<T, usize>> of ByteReader<T> {
impl ByteReaderImpl<T, +Drop<T>, +Len<T>, +IndexView<T, usize, @u8>> of ByteReader<T> {
impl ByteReaderImpl<
T,
+Drop<T>,
+Len<T>,
impl IndexViewImpl: core::ops::index::IndexView<T, usize>,
+Into<IndexViewImpl::Target, @u8>
> of ByteReader<T> {
#[inline]
fn reader(self: @T) -> ByteReaderState<T> {
ByteReaderState { data: self, index: 0 }
Expand All @@ -186,8 +191,8 @@ impl ByteReaderImpl<T, +Drop<T>, +Len<T>, +IndexView<T, usize, @u8>> of ByteRead
#[inline]
fn word_u16(self: @T, offset: usize) -> Option<u16> {
if self.remaining(offset, 2) {
let b1 = *self[offset];
let b2 = *self[offset + 1];
let b1 = *self[offset].into();
let b2 = *self[offset + 1].into();
Option::Some(b1.into() * one_shift_left_bytes_u128(1).try_into().unwrap() + b2.into())
} else {
Option::None
Expand All @@ -197,8 +202,8 @@ impl ByteReaderImpl<T, +Drop<T>, +Len<T>, +IndexView<T, usize, @u8>> of ByteRead
#[inline]
fn word_u16_le(self: @T, offset: usize) -> Option<u16> {
if self.remaining(offset, 2) {
let b1 = *self[offset];
let b2 = *self[offset + 1];
let b1 = *self[offset].into();
let b2 = *self[offset + 1].into();
Option::Some(b1.into() + b2.into() * one_shift_left_bytes_u128(1).try_into().unwrap())
} else {
Option::None
Expand All @@ -208,10 +213,10 @@ impl ByteReaderImpl<T, +Drop<T>, +Len<T>, +IndexView<T, usize, @u8>> of ByteRead
#[inline]
fn word_u32(self: @T, offset: usize) -> Option<u32> {
if self.remaining(offset, 4) {
let b1 = *self[offset];
let b2 = *self[offset + 1];
let b3 = *self[offset + 2];
let b4 = *self[offset + 3];
let b1 = *self[offset].into();
let b2 = *self[offset + 1].into();
let b3 = *self[offset + 2].into();
let b4 = *self[offset + 3].into();
Option::Some(
b1.into() * one_shift_left_bytes_u128(3).try_into().unwrap()
+ b2.into() * one_shift_left_bytes_u128(2).try_into().unwrap()
Expand All @@ -226,10 +231,10 @@ impl ByteReaderImpl<T, +Drop<T>, +Len<T>, +IndexView<T, usize, @u8>> of ByteRead
#[inline]
fn word_u32_le(self: @T, offset: usize) -> Option<u32> {
if self.remaining(offset, 4) {
let b1 = *self[offset];
let b2 = *self[offset + 1];
let b3 = *self[offset + 2];
let b4 = *self[offset + 3];
let b1 = *self[offset].into();
let b2 = *self[offset + 1].into();
let b3 = *self[offset + 2].into();
let b4 = *self[offset + 3].into();
Option::Some(
b1.into()
+ b2.into() * one_shift_left_bytes_u128(1).try_into().unwrap()
Expand All @@ -244,14 +249,14 @@ impl ByteReaderImpl<T, +Drop<T>, +Len<T>, +IndexView<T, usize, @u8>> of ByteRead
#[inline]
fn word_u64(self: @T, offset: usize) -> Option<u64> {
if self.remaining(offset, 8) {
let b1 = *self[offset];
let b2 = *self[offset + 1];
let b3 = *self[offset + 2];
let b4 = *self[offset + 3];
let b5 = *self[offset + 4];
let b6 = *self[offset + 5];
let b7 = *self[offset + 6];
let b8 = *self[offset + 7];
let b1 = *self[offset].into();
let b2 = *self[offset + 1].into();
let b3 = *self[offset + 2].into();
let b4 = *self[offset + 3].into();
let b5 = *self[offset + 4].into();
let b6 = *self[offset + 5].into();
let b7 = *self[offset + 6].into();
let b8 = *self[offset + 7].into();
Option::Some(
b1.into() * one_shift_left_bytes_u128(7).try_into().unwrap()
+ b2.into() * one_shift_left_bytes_u128(6).try_into().unwrap()
Expand All @@ -270,14 +275,14 @@ impl ByteReaderImpl<T, +Drop<T>, +Len<T>, +IndexView<T, usize, @u8>> of ByteRead
#[inline]
fn word_u64_le(self: @T, offset: usize) -> Option<u64> {
if self.remaining(offset, 8) {
let b1 = *self[offset];
let b2 = *self[offset + 1];
let b3 = *self[offset + 2];
let b4 = *self[offset + 3];
let b5 = *self[offset + 4];
let b6 = *self[offset + 5];
let b7 = *self[offset + 6];
let b8 = *self[offset + 7];
let b1 = *self[offset].into();
let b2 = *self[offset + 1].into();
let b3 = *self[offset + 2].into();
let b4 = *self[offset + 3].into();
let b5 = *self[offset + 4].into();
let b6 = *self[offset + 5].into();
let b7 = *self[offset + 6].into();
let b8 = *self[offset + 7].into();
Option::Some(
b1.into()
+ b2.into() * one_shift_left_bytes_u128(1).try_into().unwrap()
Expand All @@ -296,22 +301,22 @@ impl ByteReaderImpl<T, +Drop<T>, +Len<T>, +IndexView<T, usize, @u8>> of ByteRead
#[inline]
fn word_u128(self: @T, offset: usize) -> Option<u128> {
if self.remaining(offset, 16) {
let b01 = *self[offset];
let b02 = *self[offset + 1];
let b03 = *self[offset + 2];
let b04 = *self[offset + 3];
let b05 = *self[offset + 4];
let b06 = *self[offset + 5];
let b07 = *self[offset + 6];
let b08 = *self[offset + 7];
let b09 = *self[offset + 8];
let b10 = *self[offset + 9];
let b11 = *self[offset + 10];
let b12 = *self[offset + 11];
let b13 = *self[offset + 12];
let b14 = *self[offset + 13];
let b15 = *self[offset + 14];
let b16 = *self[offset + 15];
let b01 = *self[offset].into();
let b02 = *self[offset + 1].into();
let b03 = *self[offset + 2].into();
let b04 = *self[offset + 3].into();
let b05 = *self[offset + 4].into();
let b06 = *self[offset + 5].into();
let b07 = *self[offset + 6].into();
let b08 = *self[offset + 7].into();
let b09 = *self[offset + 8].into();
let b10 = *self[offset + 9].into();
let b11 = *self[offset + 10].into();
let b12 = *self[offset + 11].into();
let b13 = *self[offset + 12].into();
let b14 = *self[offset + 13].into();
let b15 = *self[offset + 14].into();
let b16 = *self[offset + 15].into();
Option::Some(
b01.into() * one_shift_left_bytes_u128(15).try_into().unwrap()
+ b02.into() * one_shift_left_bytes_u128(14).try_into().unwrap()
Expand All @@ -338,22 +343,22 @@ impl ByteReaderImpl<T, +Drop<T>, +Len<T>, +IndexView<T, usize, @u8>> of ByteRead
#[inline]
fn word_u128_le(self: @T, offset: usize) -> Option<u128> {
if self.remaining(offset, 16) {
let b01 = *self[offset];
let b02 = *self[offset + 1];
let b03 = *self[offset + 2];
let b04 = *self[offset + 3];
let b05 = *self[offset + 4];
let b06 = *self[offset + 5];
let b07 = *self[offset + 6];
let b08 = *self[offset + 7];
let b09 = *self[offset + 8];
let b10 = *self[offset + 9];
let b11 = *self[offset + 10];
let b12 = *self[offset + 11];
let b13 = *self[offset + 12];
let b14 = *self[offset + 13];
let b15 = *self[offset + 14];
let b16 = *self[offset + 15];
let b01 = *self[offset].into();
let b02 = *self[offset + 1].into();
let b03 = *self[offset + 2].into();
let b04 = *self[offset + 3].into();
let b05 = *self[offset + 4].into();
let b06 = *self[offset + 5].into();
let b07 = *self[offset + 6].into();
let b08 = *self[offset + 7].into();
let b09 = *self[offset + 8].into();
let b10 = *self[offset + 9].into();
let b11 = *self[offset + 10].into();
let b12 = *self[offset + 11].into();
let b13 = *self[offset + 12].into();
let b14 = *self[offset + 13].into();
let b15 = *self[offset + 14].into();
let b16 = *self[offset + 15].into();
Option::Some(
b01.into()
+ b02.into() * one_shift_left_bytes_u128(01).try_into().unwrap()
Expand All @@ -379,7 +384,7 @@ impl ByteReaderImpl<T, +Drop<T>, +Len<T>, +IndexView<T, usize, @u8>> of ByteRead

fn read_u8(ref self: ByteReaderState<T>) -> Option<u8> {
if self.data.remaining(self.index, 1) {
let result = *self.data[self.index];
let result = *self.data[self.index].into();
self.index += 1;
Option::Some(result)
} else {
Expand Down Expand Up @@ -574,22 +579,10 @@ impl ByteReaderLenImpl<T, +Len<T>> of Len<ByteReaderState<T>> {
}
}

impl ByteArrayIndexViewAsSnapshotImpl of IndexView<ByteArray, usize, @u8> {
impl IntoU8Impl of Into<u8, @u8> {
#[inline(always)]
fn index(self: @ByteArray, index: usize) -> @u8 {
@self.at(index).expect('Index out of bounds')
}
}

// use core::ops::index::IndexView;
impl ByteReaderIndexViewImpl<
T, impl TIndexView: core::ops::index::IndexView<T, usize>, +Into<TIndexView::Target, u8>
> of core::ops::index::IndexView<ByteReaderState<T>, usize> {
type Target = @u8;

#[inline(always)]
fn index(self: @ByteReaderState<T>, index: usize) -> @u8 {
@TIndexView::index(*self.data, index).into()
fn into(self: u8) -> @u8 {
@self
}
}

Expand Down

0 comments on commit 7f506ec

Please sign in to comment.