mirror of
https://github.com/ziglang/zig.git
synced 2024-11-16 09:03:12 +00:00
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.
This commit is contained in:
parent
87b843ef08
commit
09f70bdd91
@ -121,7 +121,7 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type {
|
||||
/// Insert `item` at index `i` by moving `slice[n .. slice.len]` to make room.
|
||||
/// This operation is O(N).
|
||||
pub fn insert(self: *Self, i: usize, item: T) !void {
|
||||
if (i >= self.len) {
|
||||
if (i > self.len) {
|
||||
return error.IndexOutOfBounds;
|
||||
}
|
||||
_ = try self.addOne();
|
||||
@ -289,6 +289,10 @@ test "BoundedArray" {
|
||||
try testing.expectEqual(a.get(9), 3);
|
||||
try testing.expectEqual(a.get(10), 4);
|
||||
|
||||
try a.insert(11, 0xbb);
|
||||
try testing.expectEqual(a.len, 12);
|
||||
try testing.expectEqual(a.pop(), 0xbb);
|
||||
|
||||
try a.appendSlice(&x);
|
||||
try testing.expectEqual(a.len, 11 + x.len);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user