authorgravatar for quae@daurnimator.comdaurnimator <quae@daurnimator.com> 2021-01-02 00:52:47+11:00
committergravatar for quae@daurnimator.comdaurnimator <quae@daurnimator.com> 2021-01-02 00:52:47+11:00
log31ba9d569b14a3e147d9bdfe88d63a1ed134bc5f
tree897c0cb5ef09f705cbcb607848686e2eac07df8b
parent93bb1d93cd18e739410009e7c77048d151a93b10
signaturelock-open Commit is signed but in an unrecognized format.

Add PackedIntArray .initAllTo function


1 files changed, 24 insertions(+), 1 deletions(-)

lib/std/packed_int_array.zig+24-1
...@@ -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 }
205205
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 index213 ///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 }
217224
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 end233 ///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
386test "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}
370393
371test "PackedIntSlice" {394test "PackedIntSlice" {