authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2025-04-15 10:53:48 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2025-04-15 10:53:48 -07:00
log100510135aa7be25b8b465ac32450678de009103
treec35b77083071f07030df133d60ecdbdd5aae733d
parentb46cbe7b2f31d0f4ea5e720f2d3657437b22e6c7

MultiArray -> StaticMultiList


3 files changed, 45 insertions(+), 45 deletions(-)

src/MultiArray.zig deleted-44
......@@ -1,44 +0,0 @@
1const std = @import("std");
2const string = []const u8;
3const extras = @import("./lib.zig");
4const StructOfSlices = extras.StructOfSlices;
5const StructOfArrays = extras.StructOfArrays;
6const expectSimilarType = extras.expectSimilarType;
7
8pub 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
31test {
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 @@
1const std = @import("std");
2const string = []const u8;
3const extras = @import("./lib.zig");
4const StructOfSlices = extras.StructOfSlices;
5const StructOfArrays = extras.StructOfArrays;
6const expectSimilarType = extras.expectSimilarType;
7
8pub 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
31test {
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");
107107pub usingnamespace @import("./hasFields.zig");
108108pub usingnamespace @import("./StructOfSlices.zig");
109109pub usingnamespace @import("./StructOfArrays.zig");
110pub usingnamespace @import("./MultiArray.zig");
110pub usingnamespace @import("./StaticMultiList.zig");