authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2025-04-15 03:14:41 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2025-04-15 03:14:41 -07:00
logb46cbe7b2f31d0f4ea5e720f2d3657437b22e6c7
tree118d79a90071a192c6da6db79d9f8d533c603f15
parent9af9dd24bed9a6b351beae3b6414e807faeeaeb3

add MultiArray


2 files changed, 45 insertions(+), 0 deletions(-)

src/MultiArray.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 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/lib.zig+1
......@@ -107,3 +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");