authorgravatar for me@arnavion.devArnav Singh <me@arnavion.dev> 2021-12-15 11:10:34-08:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-12-15 20:10:34+01:00
log09f70bdd91bf498644f7a42800f3e4e4df7040aa
tree6baacfaa15f3d330b0c8434d84a86376e1bc9c58
parent87b843ef08426e1b14b2128987dfb52bb7de6788
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

std.bounded_array: support inserting a new value at the end (#10340)

Since `BoundedArray.insert` internally reserves space for the element to be inserted, it can support inserting at the position that is the current length of the array. Change the check for the insertion position to allow this.

1 files changed, 5 insertions(+), 1 deletions(-)

lib/std/bounded_array.zig+5-1
...@@ -121,7 +121,7 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type {...@@ -121,7 +121,7 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type {
121 /// Insert `item` at index `i` by moving `slice[n .. slice.len]` to make room.121 /// Insert `item` at index `i` by moving `slice[n .. slice.len]` to make room.
122 /// This operation is O(N).122 /// This operation is O(N).
123 pub fn insert(self: *Self, i: usize, item: T) !void {123 pub fn insert(self: *Self, i: usize, item: T) !void {
124 if (i >= self.len) {124 if (i > self.len) {
125 return error.IndexOutOfBounds;125 return error.IndexOutOfBounds;
126 }126 }
127 _ = try self.addOne();127 _ = try self.addOne();
...@@ -289,6 +289,10 @@ test "BoundedArray" {...@@ -289,6 +289,10 @@ test "BoundedArray" {
289 try testing.expectEqual(a.get(9), 3);289 try testing.expectEqual(a.get(9), 3);
290 try testing.expectEqual(a.get(10), 4);290 try testing.expectEqual(a.get(10), 4);
291291
292 try a.insert(11, 0xbb);
293 try testing.expectEqual(a.len, 12);
294 try testing.expectEqual(a.pop(), 0xbb);
295
292 try a.appendSlice(&x);296 try a.appendSlice(&x);
293 try testing.expectEqual(a.len, 11 + x.len);297 try testing.expectEqual(a.len, 11 + x.len);
294298