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