| ... | @@ -134,20 +134,14 @@ pub fn BoundedArrayAligned( | ... | @@ -134,20 +134,14 @@ pub fn BoundedArrayAligned( |
| 134 | return self.slice()[prev_len..][0..n]; | 134 | return self.slice()[prev_len..][0..n]; |
| 135 | } | 135 | } |
| 136 | | 136 | |
| 137 | /// Remove and return the last element from the slice. | 137 | /// Remove and return the last element from the slice, or return `null` if the slice is empty. |
| 138 | /// Asserts the slice has at least one item. | 138 | pub fn pop(self: *Self) ?T { |
| 139 | pub fn pop(self: *Self) T { | 139 | if (self.len == 0) return null; |
| 140 | const item = self.get(self.len - 1); | 140 | const item = self.get(self.len - 1); |
| 141 | self.len -= 1; | 141 | self.len -= 1; |
| 142 | return item; | 142 | return item; |
| 143 | } | 143 | } |
| 144 | | 144 | |
| 145 | /// Remove and return the last element from the slice, or | | |
| 146 | /// return `null` if the slice is empty. | | |
| 147 | pub fn popOrNull(self: *Self) ?T { | | |
| 148 | return if (self.len == 0) null else self.pop(); | | |
| 149 | } | | |
| 150 | | | |
| 151 | /// Return a slice of only the extra capacity after items. | 145 | /// Return a slice of only the extra capacity after items. |
| 152 | /// This can be useful for writing directly into it. | 146 | /// This can be useful for writing directly into it. |
| 153 | /// Note that such an operation must be followed up with a | 147 | /// Note that such an operation must be followed up with a |
| ... | @@ -229,7 +223,7 @@ pub fn BoundedArrayAligned( | ... | @@ -229,7 +223,7 @@ pub fn BoundedArrayAligned( |
| 229 | /// This operation is O(N). | 223 | /// This operation is O(N). |
| 230 | pub fn orderedRemove(self: *Self, i: usize) T { | 224 | pub fn orderedRemove(self: *Self, i: usize) T { |
| 231 | const newlen = self.len - 1; | 225 | const newlen = self.len - 1; |
| 232 | if (newlen == i) return self.pop(); | 226 | if (newlen == i) return self.pop().?; |
| 233 | const old_item = self.get(i); | 227 | const old_item = self.get(i); |
| 234 | for (self.slice()[i..newlen], 0..) |*b, j| b.* = self.get(i + 1 + j); | 228 | for (self.slice()[i..newlen], 0..) |*b, j| b.* = self.get(i + 1 + j); |
| 235 | self.set(newlen, undefined); | 229 | self.set(newlen, undefined); |
| ... | @@ -241,9 +235,9 @@ pub fn BoundedArrayAligned( | ... | @@ -241,9 +235,9 @@ pub fn BoundedArrayAligned( |
| 241 | /// The empty slot is filled from the end of the slice. | 235 | /// The empty slot is filled from the end of the slice. |
| 242 | /// This operation is O(1). | 236 | /// This operation is O(1). |
| 243 | pub fn swapRemove(self: *Self, i: usize) T { | 237 | pub fn swapRemove(self: *Self, i: usize) T { |
| 244 | if (self.len - 1 == i) return self.pop(); | 238 | if (self.len - 1 == i) return self.pop().?; |
| 245 | const old_item = self.get(i); | 239 | const old_item = self.get(i); |
| 246 | self.set(i, self.pop()); | 240 | self.set(i, self.pop().?); |
| 247 | return old_item; | 241 | return old_item; |
| 248 | } | 242 | } |
| 249 | | 243 | |
| ... | @@ -339,8 +333,8 @@ test BoundedArray { | ... | @@ -339,8 +333,8 @@ test BoundedArray { |
| 339 | try testing.expectEqual(a.pop(), 0xff); | 333 | try testing.expectEqual(a.pop(), 0xff); |
| 340 | | 334 | |
| 341 | try a.resize(1); | 335 | try a.resize(1); |
| 342 | try testing.expectEqual(a.popOrNull(), 0); | 336 | try testing.expectEqual(a.pop(), 0); |
| 343 | try testing.expectEqual(a.popOrNull(), null); | 337 | try testing.expectEqual(a.pop(), null); |
| 344 | var unused = a.unusedCapacitySlice(); | 338 | var unused = a.unusedCapacitySlice(); |
| 345 | @memset(unused[0..8], 2); | 339 | @memset(unused[0..8], 2); |
| 346 | unused[8] = 3; | 340 | unused[8] = 3; |
| ... | @@ -397,7 +391,7 @@ test BoundedArray { | ... | @@ -397,7 +391,7 @@ test BoundedArray { |
| 397 | try testing.expectEqual(added_slice.len, 3); | 391 | try testing.expectEqual(added_slice.len, 3); |
| 398 | try testing.expectEqual(a.len, 36); | 392 | try testing.expectEqual(a.len, 36); |
| 399 | | 393 | |
| 400 | while (a.popOrNull()) |_| {} | 394 | while (a.pop()) |_| {} |
| 401 | const w = a.writer(); | 395 | const w = a.writer(); |
| 402 | const s = "hello, this is a test string"; | 396 | const s = "hello, this is a test string"; |
| 403 | try w.writeAll(s); | 397 | try w.writeAll(s); |