authorgravatar for greg@gpanders.comGregory Anders <greg@gpanders.com> 2021-11-10 14:36:08-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-11-14 22:52:01-05:00
logb26b72f54051b89e6fc349f453aafe9f3a3135a5
tree8edca2d2ba92b1cfecb511182257a99b44f8f047
parent46af3a320c717cfcb13dbf3071a41c161b0a33f9

BoundedArray: add appendAssumeCapacity


1 files changed, 11 insertions(+), 0 deletions(-)

lib/std/bounded_array.zig+11
......@@ -169,6 +169,13 @@ pub fn BoundedArray(comptime T: type, comptime capacity: usize) type {
169169 new_item_ptr.* = item;
170170 }
171171
172 /// Extend the slice by 1 element, asserting the capacity is already
173 /// enough to store the new item.
174 pub fn appendAssumeCapacity(self: *Self, item: T) void {
175 const new_item_ptr = self.addOneAssumeCapacity();
176 new_item_ptr.* = item;
177 }
178
172179 /// Remove the element at index `i`, shift elements after index
173180 /// `i` forward, and return the removed element.
174181 /// Asserts the slice has at least one item.
......@@ -262,6 +269,10 @@ test "BoundedArray" {
262269 try testing.expectEqual(a.len, 6);
263270 try testing.expectEqual(a.pop(), 0xff);
264271
272 a.appendAssumeCapacity(0xff);
273 try testing.expectEqual(a.len, 6);
274 try testing.expectEqual(a.pop(), 0xff);
275
265276 try a.resize(1);
266277 try testing.expectEqual(a.popOrNull(), 0);
267278 try testing.expectEqual(a.popOrNull(), null);