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

Add index as a parameter to Iterator's each() method #331

Merged
merged 1 commit into from
Jun 7, 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
6 changes: 3 additions & 3 deletions ethereum/circuits/lib/src/misc/bounded_vecs.nr
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ pub fn bounded_vec_map<U, T, N, Env>(
f: fn[Env](T) -> U
) -> BoundedVec<U, N> {
let result: &mut BoundedVec<U, N> = &mut BoundedVec::new();
bounded_vec.for_each(|x| result.push(f(x)));
bounded_vec.each(|x, _| result.push(f(x)));

*result
}

impl<T, N> Iterator<T> for BoundedVec<T, N> {
fn for_each<Env>(self, f: fn[Env](T) -> ()) {
fn each<Env>(self, f: fn[Env](T, u64) -> ()) {
for i in 0..N {
if i < self.len {
f(self.get(i));
f(self.get(i), i);
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions ethereum/circuits/lib/src/misc/bytes.nr
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ pub fn bytes_to_nibbles<MAX_NIBBLE_LEN>(bytes: Fragment<MAX_NIBBLE_LEN, u8>) ->
MAX_NIBBLE_LEN >= 2 * bytes.length, "Bytes implicit data length must be at least 2 times larger than bytes explicit length"
);

let mut nibbles = Fragment::new_with_length(2 * bytes.length, [0; MAX_NIBBLE_LEN]);
for i in 0..MAX_NIBBLE_LEN {
if i < bytes.length {
let (hi, lo) = byte_to_nibbles(bytes.at(i));

nibbles.set(2 * i, hi);
nibbles.set(2 * i + 1, lo);
}
let nibbles = &mut Fragment::new_with_length(2 * bytes.length, [0; MAX_NIBBLE_LEN]);
bytes.each(
|byte, i| {
let (hi, lo) = byte_to_nibbles(byte);

nibbles.set(2 * i, hi);
nibbles.set(2 * i + 1, lo);
}
);

nibbles
*nibbles
}

pub fn nibbles_to_byte(upper: u8, lower: u8) -> u8 {
Expand Down
37 changes: 19 additions & 18 deletions ethereum/circuits/lib/src/misc/fragment.nr
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl<MAX_DATA_LEN, T> Fragment<MAX_DATA_LEN, T> {
pub fn to_bounded_vec<N>(self) -> BoundedVec<T, N> {
assert(self.length <= N, "Fragment length exceeds BoundedVec max length");
let bounded_vec: &mut BoundedVec<T, N> = &mut BoundedVec::new();
self.for_each(|x| bounded_vec.push(x));
self.each(|x, _| bounded_vec.push(x));

*bounded_vec
}
Expand Down Expand Up @@ -118,41 +118,42 @@ impl<MAX_DATA_LEN, T> Fragment<MAX_DATA_LEN, T> {
}

pub fn eq<OTHER_MAX_DATA_LEN>(self, other: Fragment<OTHER_MAX_DATA_LEN, T>) -> bool where T: Eq {
let mut res = true;
let res = &mut true;
if (self.length != other.length) {
res = false;
*res = false;
}
for i in 0..MAX_DATA_LEN {
if i < self.length & i < other.length {
res &= self.at(i) == other.at(i);
self.each(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example usage:

self.iter().each(|| {})
self.iter().map(|| {})

Implementation stub:

trait Iterable<T> {
  fn iter() -> impl Iterator<T>;
}


trait Iterator<T> {
  fn next() -> Optional<T>
  fn each() {
    ...
  }
  fn map() {
     ..    
  }
}

struct BoundedVecIterator<T> implements Iterator<T> {
  u64 i;
  BoundedVec<T> & vec;
  fn next() -> Optional<T> {
  }
}

|el, i| {
if i < other.length {
*res &= el == other.at(i);
}
}
res
);

*res
}
}

impl<MAX_DATA_LEN, T> Eq for Fragment<MAX_DATA_LEN, T> where T: Eq {
fn eq(self, other: Fragment<MAX_DATA_LEN, T>) -> bool {
let mut res = true;
let res = &mut true;
if self.length != other.length {
res = false;
*res = false;
} else {
for i in 0..MAX_DATA_LEN {
if i < self.length {
res &= self.at(i) == other.at(i);
}
}
}
self.each(|el, i| {
*res &= el == other.at(i);
});
};

res
*res
}
}

impl<MAX_DATA_LEN, T> Iterator<T> for Fragment<MAX_DATA_LEN, T> {
fn for_each<Env>(self, f: fn[Env](T) -> ()) {
fn each<Env>(self, f: fn[Env](T, u64) -> ()) {
for i in 0..MAX_DATA_LEN {
if i < self.length {
f(self.at(i));
f(self.at(i), i);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion ethereum/circuits/lib/src/misc/iterator.nr
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
trait Iterator<T> {
fn for_each<Env>(self, f: fn[Env](T) -> ());
fn each<Env>(self, f: fn[Env](T, u64) -> ());
}
Loading