| ... | ... | @@ -39,16 +39,14 @@ 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 | | |
| 44 | 42 | buffer: [buffer_capacity]T align(alignment) = undefined, |
| 45 | | len: Len = 0, |
| 43 | len: usize = 0, |
| 46 | 44 | |
| 47 | 45 | /// Set the actual length of the slice. |
| 48 | 46 | /// Returns error.Overflow if it exceeds the length of the backing array. |
| 49 | 47 | pub fn init(len: usize) error{Overflow}!Self { |
| 50 | 48 | if (len > buffer_capacity) return error.Overflow; |
| 51 | | return Self{ .len = @intCast(len) }; |
| 49 | return Self{ .len = len }; |
| 52 | 50 | } |
| 53 | 51 | |
| 54 | 52 | /// View the internal array as a slice whose size was previously set. |
| ... | ... | @@ -69,7 +67,7 @@ pub fn BoundedArrayAligned( |
| 69 | 67 | /// Does not initialize added items if any. |
| 70 | 68 | pub fn resize(self: *Self, len: usize) error{Overflow}!void { |
| 71 | 69 | if (len > buffer_capacity) return error.Overflow; |
| 72 | | self.len = @intCast(len); |
| 70 | self.len = len; |
| 73 | 71 | } |
| 74 | 72 | |
| 75 | 73 | /// Remove all elements from the slice. |
| ... | ... | @@ -178,7 +176,7 @@ pub fn BoundedArrayAligned( |
| 178 | 176 | /// This operation is O(N). |
| 179 | 177 | pub fn insertSlice(self: *Self, i: usize, items: []const T) error{Overflow}!void { |
| 180 | 178 | try self.ensureUnusedCapacity(items.len); |
| 181 | | self.len = @intCast(self.len + items.len); |
| 179 | self.len += items.len; |
| 182 | 180 | mem.copyBackwards(T, self.slice()[i + items.len .. self.len], self.constSlice()[i .. self.len - items.len]); |
| 183 | 181 | @memcpy(self.slice()[i..][0..items.len], items); |
| 184 | 182 | } |
| ... | ... | @@ -208,7 +206,7 @@ pub fn BoundedArrayAligned( |
| 208 | 206 | for (self.constSlice()[after_range..], 0..) |item, i| { |
| 209 | 207 | self.slice()[after_subrange..][i] = item; |
| 210 | 208 | } |
| 211 | | self.len = @intCast(self.len - len + new_items.len); |
| 209 | self.len -= len - new_items.len; |
| 212 | 210 | } |
| 213 | 211 | } |
| 214 | 212 | |
| ... | ... | @@ -259,7 +257,7 @@ pub fn BoundedArrayAligned( |
| 259 | 257 | /// enough to store the new items. |
| 260 | 258 | pub fn appendSliceAssumeCapacity(self: *Self, items: []const T) void { |
| 261 | 259 | const old_len = self.len; |
| 262 | | self.len = @intCast(self.len + items.len); |
| 260 | self.len += items.len; |
| 263 | 261 | @memcpy(self.slice()[old_len..][0..items.len], items); |
| 264 | 262 | } |
| 265 | 263 | |
| ... | ... | @@ -275,8 +273,8 @@ pub fn BoundedArrayAligned( |
| 275 | 273 | /// Asserts the capacity is enough. |
| 276 | 274 | pub fn appendNTimesAssumeCapacity(self: *Self, value: T, n: usize) void { |
| 277 | 275 | const old_len = self.len; |
| 278 | | assert(self.len + n <= buffer_capacity); |
| 279 | | self.len = @intCast(self.len + n); |
| 276 | self.len += n; |
| 277 | assert(self.len <= buffer_capacity); |
| 280 | 278 | @memset(self.slice()[old_len..self.len], value); |
| 281 | 279 | } |
| 282 | 280 | |
| ... | ... | @@ -406,18 +404,6 @@ test BoundedArray { |
| 406 | 404 | try testing.expectEqualStrings(s, a.constSlice()); |
| 407 | 405 | } |
| 408 | 406 | |
| 409 | | test "BoundedArray sizeOf" { |
| 410 | | // Just sanity check size on one CPU |
| 411 | | if (@import("builtin").cpu.arch != .x86_64) |
| 412 | | return; |
| 413 | | |
| 414 | | try testing.expectEqual(@sizeOf(BoundedArray(u8, 3)), 4); |
| 415 | | |
| 416 | | // `len` is the minimum required size to hold the maximum capacity |
| 417 | | try testing.expectEqual(@TypeOf(@as(BoundedArray(u8, 15), undefined).len), u4); |
| 418 | | try testing.expectEqual(@TypeOf(@as(BoundedArray(u8, 16), undefined).len), u5); |
| 419 | | } |
| 420 | | |
| 421 | 407 | test "BoundedArrayAligned" { |
| 422 | 408 | var a = try BoundedArrayAligned(u8, 16, 4).init(0); |
| 423 | 409 | try a.append(0); |