| author | |
| committer | |
| log | 100510135aa7be25b8b465ac32450678de009103 |
| tree | c35b77083071f07030df133d60ecdbdd5aae733d |
| parent | b46cbe7b2f31d0f4ea5e720f2d3657437b22e6c7 |
3 files changed, 45 insertions(+), 45 deletions(-)
src/MultiArray.zig deleted-44| ... | @@ -1,44 +0,0 @@ | ||
| 1 | const std = @import("std"); | ||
| 2 | const string = []const u8; | ||
| 3 | const extras = @import("./lib.zig"); | ||
| 4 | const StructOfSlices = extras.StructOfSlices; | ||
| 5 | const StructOfArrays = extras.StructOfArrays; | ||
| 6 | const expectSimilarType = extras.expectSimilarType; | ||
| 7 | |||
| 8 | pub fn MultiArray(T: type) type { | ||
| 9 | return struct { | ||
| 10 | items: StructOfSlices(T), | ||
| 11 | |||
| 12 | pub fn initComptime(comptime data: []const T) @This() { | ||
| 13 | return comptime blk: { | ||
| 14 | var temp: StructOfArrays(data.len, T) = undefined; | ||
| 15 | for (data, 0..) |item, i| { | ||
| 16 | for (std.meta.fieldNames(T)) |name| { | ||
| 17 | @field(temp, name)[i] = @field(item, name); | ||
| 18 | } | ||
| 19 | } | ||
| 20 | var result: @This() = undefined; | ||
| 21 | for (std.meta.fields(T)) |field| { | ||
| 22 | const constant = @field(temp, field.name)[0..data.len].*; | ||
| 23 | @field(result.items, field.name) = &constant; | ||
| 24 | } | ||
| 25 | break :blk result; | ||
| 26 | }; | ||
| 27 | } | ||
| 28 | }; | ||
| 29 | } | ||
| 30 | |||
| 31 | test { | ||
| 32 | try expectSimilarType( | ||
| 33 | MultiArray(struct { | ||
| 34 | x: u8, | ||
| 35 | y: u16, | ||
| 36 | }), | ||
| 37 | struct { | ||
| 38 | items: struct { | ||
| 39 | x: []const u8, | ||
| 40 | y: []const u16, | ||
| 41 | }, | ||
| 42 | }, | ||
| 43 | ); | ||
| 44 | } | ||
src/StaticMultiList.zig created+44| ... | @@ -0,0 +1,44 @@ | ||
| 1 | const std = @import("std"); | ||
| 2 | const string = []const u8; | ||
| 3 | const extras = @import("./lib.zig"); | ||
| 4 | const StructOfSlices = extras.StructOfSlices; | ||
| 5 | const StructOfArrays = extras.StructOfArrays; | ||
| 6 | const expectSimilarType = extras.expectSimilarType; | ||
| 7 | |||
| 8 | pub fn StaticMultiList(T: type) type { | ||
| 9 | return struct { | ||
| 10 | items: StructOfSlices(T), | ||
| 11 | |||
| 12 | pub fn initComptime(comptime data: []const T) @This() { | ||
| 13 | return comptime blk: { | ||
| 14 | var temp: StructOfArrays(data.len, T) = undefined; | ||
| 15 | for (data, 0..) |item, i| { | ||
| 16 | for (std.meta.fieldNames(T)) |name| { | ||
| 17 | @field(temp, name)[i] = @field(item, name); | ||
| 18 | } | ||
| 19 | } | ||
| 20 | var result: @This() = undefined; | ||
| 21 | for (std.meta.fields(T)) |field| { | ||
| 22 | const constant = @field(temp, field.name)[0..data.len].*; | ||
| 23 | @field(result.items, field.name) = &constant; | ||
| 24 | } | ||
| 25 | break :blk result; | ||
| 26 | }; | ||
| 27 | } | ||
| 28 | }; | ||
| 29 | } | ||
| 30 | |||
| 31 | test { | ||
| 32 | try expectSimilarType( | ||
| 33 | StaticMultiList(struct { | ||
| 34 | x: u8, | ||
| 35 | y: u16, | ||
| 36 | }), | ||
| 37 | struct { | ||
| 38 | items: struct { | ||
| 39 | x: []const u8, | ||
| 40 | y: []const u16, | ||
| 41 | }, | ||
| 42 | }, | ||
| 43 | ); | ||
| 44 | } | ||
src/lib.zig+1-1| ... | @@ -107,4 +107,4 @@ pub usingnamespace @import("./hasFn.zig"); | ... | @@ -107,4 +107,4 @@ pub usingnamespace @import("./hasFn.zig"); |
| 107 | pub usingnamespace @import("./hasFields.zig"); | 107 | pub usingnamespace @import("./hasFields.zig"); |
| 108 | pub usingnamespace @import("./StructOfSlices.zig"); | 108 | pub usingnamespace @import("./StructOfSlices.zig"); |
| 109 | pub usingnamespace @import("./StructOfArrays.zig"); | 109 | pub usingnamespace @import("./StructOfArrays.zig"); |
| 110 | pub usingnamespace @import("./MultiArray.zig"); | 110 | pub usingnamespace @import("./StaticMultiList.zig"); |