Skip to content

Commit

Permalink
Implement Iterator for BoundedVec
Browse files Browse the repository at this point in the history
  • Loading branch information
Pasifaee committed Jun 6, 2024
1 parent c4c662b commit 3e35876
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
19 changes: 14 additions & 5 deletions ethereum/circuits/lib/src/misc/bounded_vecs.nr
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::misc::iterator::Iterator;

pub fn bounded_vec_from_array<T, N, M>(array: [T; N]) -> BoundedVec<T, M> {
assert(N <= M, "Array size exceeds bounded vector size");
let mut vec = BoundedVec::new();
Expand All @@ -10,11 +12,18 @@ pub fn bounded_vec_map<U, T, N, Env>(
bounded_vec: BoundedVec<T, N> ,
f: fn[Env](T) -> U
) -> BoundedVec<U, N> {
let mut result: BoundedVec<U, N> = BoundedVec::new();
for i in 0..N {
if (i < bounded_vec.len) {
result.push(f(bounded_vec.get(i)));
let result: &mut BoundedVec<U, N> = &mut BoundedVec::new();
bounded_vec.for_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) -> ()) {
for i in 0..N {
if i < self.len {
f(self.get(i));
}
}
}
result
}
2 changes: 1 addition & 1 deletion ethereum/circuits/lib/src/misc/fragment.nr
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl<MAX_DATA_LEN, T> Fragment<MAX_DATA_LEN, T> {
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));

*bounded_vec
}

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) -> ());
}
}

0 comments on commit 3e35876

Please sign in to comment.