| ... | @@ -203,6 +203,14 @@ pub fn PackedIntArrayEndian(comptime Int: type, comptime endian: builtin.Endian, | ... | @@ -203,6 +203,14 @@ pub fn PackedIntArrayEndian(comptime Int: type, comptime endian: builtin.Endian, |
| 203 | return self; | 203 | return self; |
| 204 | } | 204 | } |
| 205 | | 205 | |
| | 206 | ///Initialize all entries of a packed array to the same value |
| | 207 | pub fn initAllTo(int: Int) Self { |
| | 208 | // TODO: use `var self = @as(Self, undefined);` https://github.com/ziglang/zig/issues/7635 |
| | 209 | var self = Self{ .bytes = [_]u8{0} ** total_bytes }; |
| | 210 | self.setAll(int); |
| | 211 | return self; |
| | 212 | } |
| | 213 | |
| 206 | ///Return the Int stored at index | 214 | ///Return the Int stored at index |
| 207 | pub fn get(self: Self, index: usize) Int { | 215 | pub fn get(self: Self, index: usize) Int { |
| 208 | debug.assert(index < int_count); | 216 | debug.assert(index < int_count); |
| ... | @@ -215,6 +223,14 @@ pub fn PackedIntArrayEndian(comptime Int: type, comptime endian: builtin.Endian, | ... | @@ -215,6 +223,14 @@ pub fn PackedIntArrayEndian(comptime Int: type, comptime endian: builtin.Endian, |
| 215 | return Io.set(&self.bytes, index, 0, int); | 223 | return Io.set(&self.bytes, index, 0, int); |
| 216 | } | 224 | } |
| 217 | | 225 | |
| | 226 | ///Set all entries of a packed array to the same value |
| | 227 | pub fn setAll(self: *Self, int: Int) void { |
| | 228 | var i: usize = 0; |
| | 229 | while (i < int_count) : (i += 1) { |
| | 230 | self.set(i, int); |
| | 231 | } |
| | 232 | } |
| | 233 | |
| 218 | ///Create a PackedIntSlice of the array from given start to given end | 234 | ///Create a PackedIntSlice of the array from given start to given end |
| 219 | pub fn slice(self: *Self, start: usize, end: usize) PackedIntSliceEndian(Int, endian) { | 235 | pub fn slice(self: *Self, start: usize, end: usize) PackedIntSliceEndian(Int, endian) { |
| 220 | debug.assert(start < int_count); | 236 | debug.assert(start < int_count); |
| ... | @@ -365,7 +381,15 @@ test "PackedIntArray init" { | ... | @@ -365,7 +381,15 @@ test "PackedIntArray init" { |
| 365 | const PackedArray = PackedIntArray(u3, 8); | 381 | const PackedArray = PackedIntArray(u3, 8); |
| 366 | var packed_array = PackedArray.init([_]u3{ 0, 1, 2, 3, 4, 5, 6, 7 }); | 382 | var packed_array = PackedArray.init([_]u3{ 0, 1, 2, 3, 4, 5, 6, 7 }); |
| 367 | var i = @as(usize, 0); | 383 | var i = @as(usize, 0); |
| 368 | while (i < packed_array.len()) : (i += 1) testing.expect(packed_array.get(i) == i); | 384 | while (i < packed_array.len()) : (i += 1) testing.expectEqual(@intCast(u3, i), packed_array.get(i)); |
| | 385 | } |
| | 386 | |
| | 387 | test "PackedIntArray initAllTo" { |
| | 388 | if (we_are_testing_this_with_stage1_which_leaks_comptime_memory) return error.SkipZigTest; |
| | 389 | const PackedArray = PackedIntArray(u3, 8); |
| | 390 | var packed_array = PackedArray.initAllTo(5); |
| | 391 | var i = @as(usize, 0); |
| | 392 | while (i < packed_array.len()) : (i += 1) testing.expectEqual(@as(u3, 5), packed_array.get(i)); |
| 369 | } | 393 | } |
| 370 | | 394 | |
| 371 | test "PackedIntSlice" { | 395 | test "PackedIntSlice" { |