authorgravatar for si@sjbrown.co.ukSimon Brown <si@sjbrown.co.uk> 2024-05-13 19:43:48+01:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2024-05-21 13:46:05+03:00
log33d7815813cd166a35772da46d74dbb50c68b6c8
tree7c1902127db5848f55aa9b67e25c9e4f924b074e
parent28476a5ee94d311319941b54e9da66210690ce70

Implement addManyAsSlice for BoundedArray


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

lib/std/bounded_array.zig+13-1
...@@ -116,13 +116,21 @@ pub fn BoundedArrayAligned(...@@ -116,13 +116,21 @@ pub fn BoundedArrayAligned(
116 }116 }
117117
118 /// Resize the slice, adding `n` new elements, which have `undefined` values.118 /// Resize the slice, adding `n` new elements, which have `undefined` values.
119 /// The return value is a slice pointing to the uninitialized elements.119 /// The return value is a pointer to the array of uninitialized elements.
120 pub fn addManyAsArray(self: *Self, comptime n: usize) error{Overflow}!*align(alignment) [n]T {120 pub fn addManyAsArray(self: *Self, comptime n: usize) error{Overflow}!*align(alignment) [n]T {
121 const prev_len = self.len;121 const prev_len = self.len;
122 try self.resize(self.len + n);122 try self.resize(self.len + n);
123 return self.slice()[prev_len..][0..n];123 return self.slice()[prev_len..][0..n];
124 }124 }
125125
126 /// Resize the slice, adding `n` new elements, which have `undefined` values.
127 /// The return value is a slice pointing to the uninitialized elements.
128 pub fn addManyAsSlice(self: *Self, n: usize) error{Overflow}![]align(alignment) T {
129 const prev_len = self.len;
130 try self.resize(self.len + n);
131 return self.slice()[prev_len..][0..n];
132 }
133
126 /// Remove and return the last element from the slice.134 /// Remove and return the last element from the slice.
127 /// Asserts the slice has at least one item.135 /// Asserts the slice has at least one item.
128 pub fn pop(self: *Self) T {136 pub fn pop(self: *Self) T {
...@@ -382,6 +390,10 @@ test BoundedArray {...@@ -382,6 +390,10 @@ test BoundedArray {
382 try testing.expectEqual(swapped, 0xdd);390 try testing.expectEqual(swapped, 0xdd);
383 try testing.expectEqual(a.get(0), 0xee);391 try testing.expectEqual(a.get(0), 0xee);
384392
393 const added_slice = try a.addManyAsSlice(3);
394 try testing.expectEqual(added_slice.len, 3);
395 try testing.expectEqual(a.len, 36);
396
385 while (a.popOrNull()) |_| {}397 while (a.popOrNull()) |_| {}
386 const w = a.writer();398 const w = a.writer();
387 const s = "hello, this is a test string";399 const s = "hello, this is a test string";