| ... | ... | @@ -39,14 +39,16 @@ pub fn BoundedArrayAligned( |
| 39 | 39 | ) type { |
| 40 | 40 | return struct { |
| 41 | 41 | const Self = @This(); |
| 42 | const Len = std.math.IntFittingRange(0, buffer_capacity); |
| 43 | |
| 42 | 44 | buffer: [buffer_capacity]T align(alignment) = undefined, |
| 43 | | len: usize = 0, |
| 45 | len: Len = 0, |
| 44 | 46 | |
| 45 | 47 | /// Set the actual length of the slice. |
| 46 | 48 | /// Returns error.Overflow if it exceeds the length of the backing array. |
| 47 | 49 | pub fn init(len: usize) error{Overflow}!Self { |
| 48 | 50 | if (len > buffer_capacity) return error.Overflow; |
| 49 | | return Self{ .len = len }; |
| 51 | return Self{ .len = @intCast(len) }; |
| 50 | 52 | } |
| 51 | 53 | |
| 52 | 54 | /// View the internal array as a slice whose size was previously set. |
| ... | ... | @@ -67,7 +69,7 @@ pub fn BoundedArrayAligned( |
| 67 | 69 | /// Does not initialize added items if any. |
| 68 | 70 | pub fn resize(self: *Self, len: usize) error{Overflow}!void { |
| 69 | 71 | if (len > buffer_capacity) return error.Overflow; |
| 70 | | self.len = len; |
| 72 | self.len = @intCast(len); |
| 71 | 73 | } |
| 72 | 74 | |
| 73 | 75 | /// Copy the content of an existing slice. |
| ... | ... | @@ -163,7 +165,7 @@ pub fn BoundedArrayAligned( |
| 163 | 165 | /// This operation is O(N). |
| 164 | 166 | pub fn insertSlice(self: *Self, i: usize, items: []const T) error{Overflow}!void { |
| 165 | 167 | try self.ensureUnusedCapacity(items.len); |
| 166 | | self.len += items.len; |
| 168 | self.len = @intCast(self.len + items.len); |
| 167 | 169 | mem.copyBackwards(T, self.slice()[i + items.len .. self.len], self.constSlice()[i .. self.len - items.len]); |
| 168 | 170 | @memcpy(self.slice()[i..][0..items.len], items); |
| 169 | 171 | } |
| ... | ... | @@ -193,7 +195,7 @@ pub fn BoundedArrayAligned( |
| 193 | 195 | for (self.constSlice()[after_range..], 0..) |item, i| { |
| 194 | 196 | self.slice()[after_subrange..][i] = item; |
| 195 | 197 | } |
| 196 | | self.len -= len - new_items.len; |
| 198 | self.len = @intCast(self.len - len + new_items.len); |
| 197 | 199 | } |
| 198 | 200 | } |
| 199 | 201 | |
| ... | ... | @@ -244,7 +246,7 @@ pub fn BoundedArrayAligned( |
| 244 | 246 | /// enough to store the new items. |
| 245 | 247 | pub fn appendSliceAssumeCapacity(self: *Self, items: []const T) void { |
| 246 | 248 | const old_len = self.len; |
| 247 | | self.len += items.len; |
| 249 | self.len = @intCast(self.len + items.len); |
| 248 | 250 | @memcpy(self.slice()[old_len..][0..items.len], items); |
| 249 | 251 | } |
| 250 | 252 | |
| ... | ... | @@ -260,8 +262,8 @@ pub fn BoundedArrayAligned( |
| 260 | 262 | /// Asserts the capacity is enough. |
| 261 | 263 | pub fn appendNTimesAssumeCapacity(self: *Self, value: T, n: usize) void { |
| 262 | 264 | const old_len = self.len; |
| 263 | | self.len += n; |
| 264 | | assert(self.len <= buffer_capacity); |
| 265 | assert(self.len + n <= buffer_capacity); |
| 266 | self.len = @intCast(self.len + n); |
| 265 | 267 | @memset(self.slice()[old_len..self.len], value); |
| 266 | 268 | } |
| 267 | 269 | |
| ... | ... | @@ -387,6 +389,18 @@ test "BoundedArray" { |
| 387 | 389 | try testing.expectEqualStrings(s, a.constSlice()); |
| 388 | 390 | } |
| 389 | 391 | |
| 392 | test "BoundedArray sizeOf" { |
| 393 | // Just sanity check size on one CPU |
| 394 | if (@import("builtin").cpu.arch != .x86_64) |
| 395 | return; |
| 396 | |
| 397 | try testing.expectEqual(@sizeOf(BoundedArray(u8, 3)), 4); |
| 398 | |
| 399 | // `len` is the minimum required size to hold the maximum capacity |
| 400 | try testing.expectEqual(@TypeOf(@as(BoundedArray(u8, 15), undefined).len), u4); |
| 401 | try testing.expectEqual(@TypeOf(@as(BoundedArray(u8, 16), undefined).len), u5); |
| 402 | } |
| 403 | |
| 390 | 404 | test "BoundedArrayAligned" { |
| 391 | 405 | var a = try BoundedArrayAligned(u8, 16, 4).init(0); |
| 392 | 406 | try a.append(0); |