Skip to content

Commit

Permalink
Merge 3e3bdf8 into ea3bb7f
Browse files Browse the repository at this point in the history
  • Loading branch information
jfecher authored Mar 22, 2024
2 parents ea3bb7f + 3e3bdf8 commit 6e58e74
Show file tree
Hide file tree
Showing 3 changed files with 361 additions and 369 deletions.
21 changes: 19 additions & 2 deletions noir_stdlib/src/collections/bounded_vec.nr
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ impl<T, MaxLen> BoundedVec<T, MaxLen> {
BoundedVec { storage: [zeroed; MaxLen], len: 0 }
}

pub fn get(mut self: Self, index: u64) -> T {
pub fn get(self: Self, index: u64) -> T {
assert(index as u64 < self.len);
self.storage[index]
}

pub fn get_unchecked(mut self: Self, index: u64) -> T {
pub fn get_unchecked(self: Self, index: u64) -> T {
self.storage[index]
}

Expand All @@ -25,6 +25,23 @@ impl<T, MaxLen> BoundedVec<T, MaxLen> {
self.len += 1;
}

pub fn insert(&mut self, index: u64, value: T) {
assert(index as u64 < self.len);
self.storage[index] = value;
}

/// Remove an item by swapping it with the element in the
/// last index then popping. This is faster than remove but
/// does not preserve ordering.
pub fn swap_remove(&mut self, index: u64) -> T {
assert(self.len < MaxLen, "swap_remove out of bounds");
let last = self.len() - 1;
let last_element = self.storage[last];
self.storage[last] = self.storage[index];
self.storage[index] = last_element;
self.pop()
}

pub fn len(self) -> u64 {
self.len
}
Expand Down
Loading

0 comments on commit 6e58e74

Please sign in to comment.