diff --git a/src/MultiArray.zig b/src/MultiArray.zig deleted file mode 100644 index 5154699aa502ebd9e8eb90a1dbc33f4d279c3f95..0000000000000000000000000000000000000000 --- a/src/MultiArray.zig +++ /dev/null @@ -1,44 +0,0 @@ -const std = @import("std"); -const string = []const u8; -const extras = @import("./lib.zig"); -const StructOfSlices = extras.StructOfSlices; -const StructOfArrays = extras.StructOfArrays; -const expectSimilarType = extras.expectSimilarType; - -pub fn MultiArray(T: type) type { - return struct { - items: StructOfSlices(T), - - pub fn initComptime(comptime data: []const T) @This() { - return comptime blk: { - var temp: StructOfArrays(data.len, T) = undefined; - for (data, 0..) |item, i| { - for (std.meta.fieldNames(T)) |name| { - @field(temp, name)[i] = @field(item, name); - } - } - var result: @This() = undefined; - for (std.meta.fields(T)) |field| { - const constant = @field(temp, field.name)[0..data.len].*; - @field(result.items, field.name) = &constant; - } - break :blk result; - }; - } - }; -} - -test { - try expectSimilarType( - MultiArray(struct { - x: u8, - y: u16, - }), - struct { - items: struct { - x: []const u8, - y: []const u16, - }, - }, - ); -} diff --git a/src/StaticMultiList.zig b/src/StaticMultiList.zig new file mode 100644 index 0000000000000000000000000000000000000000..186787987bfe32c5aad62a490f446471d811af85 --- /dev/null +++ b/src/StaticMultiList.zig @@ -0,0 +1,44 @@ +const std = @import("std"); +const string = []const u8; +const extras = @import("./lib.zig"); +const StructOfSlices = extras.StructOfSlices; +const StructOfArrays = extras.StructOfArrays; +const expectSimilarType = extras.expectSimilarType; + +pub fn StaticMultiList(T: type) type { + return struct { + items: StructOfSlices(T), + + pub fn initComptime(comptime data: []const T) @This() { + return comptime blk: { + var temp: StructOfArrays(data.len, T) = undefined; + for (data, 0..) |item, i| { + for (std.meta.fieldNames(T)) |name| { + @field(temp, name)[i] = @field(item, name); + } + } + var result: @This() = undefined; + for (std.meta.fields(T)) |field| { + const constant = @field(temp, field.name)[0..data.len].*; + @field(result.items, field.name) = &constant; + } + break :blk result; + }; + } + }; +} + +test { + try expectSimilarType( + StaticMultiList(struct { + x: u8, + y: u16, + }), + struct { + items: struct { + x: []const u8, + y: []const u16, + }, + }, + ); +} diff --git a/src/lib.zig b/src/lib.zig index 99a78ab8186200b813d268d1fb3f3aab4d028637..232381d9467a3773680801b851b96973f9cdecca 100644 --- a/src/lib.zig +++ b/src/lib.zig @@ -107,4 +107,4 @@ pub usingnamespace @import("./hasFn.zig"); pub usingnamespace @import("./hasFields.zig"); pub usingnamespace @import("./StructOfSlices.zig"); pub usingnamespace @import("./StructOfArrays.zig"); -pub usingnamespace @import("./MultiArray.zig"); +pub usingnamespace @import("./StaticMultiList.zig");