Switched EVERYTHING in Scales and Dimensions to comptime
This commit is contained in:
+15
-15
@@ -31,9 +31,9 @@ pub const UnitScale = enum(i32) {
|
||||
pub fn str(self: @This()) []const u8 {
|
||||
var buf: [16]u8 = undefined;
|
||||
return switch (self) {
|
||||
.none => "",
|
||||
inline .none => "",
|
||||
inline .P, .T, .G, .M, .k, .h, .da, .d, .c, .m, .u, .n, .p, .f, .min, .hour, .year => @tagName(self),
|
||||
else => std.fmt.bufPrint(&buf, "[{d}]", .{@intFromEnum(self)}) catch "[]",
|
||||
else => std.fmt.bufPrint(&buf, "[{d}]", .{@intFromEnum(self)}) catch "[]", // This cannot be inline because of non exhaustive enum, but that's ok, it is just str, not calculation
|
||||
};
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ pub const UnitScale = enum(i32) {
|
||||
pub fn getFactor(self: @This()) f64 {
|
||||
return switch (self) {
|
||||
inline .P, .T, .G, .M, .k, .h, .da, .none, .d, .c, .m, .u, .n, .p, .f => std.math.pow(f64, 10.0, @floatFromInt(@intFromEnum(self))),
|
||||
else => @floatFromInt(@intFromEnum(self)),
|
||||
inline else => @floatFromInt(@intFromEnum(self)),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ pub const UnitScale = enum(i32) {
|
||||
pub fn getFactorInt(self: @This()) i32 {
|
||||
return switch (self) {
|
||||
inline .P, .T, .G, .M, .k, .h, .da, .none, .d, .c, .m, .u, .n, .p, .f => std.math.powi(i32, 10.0, @intFromEnum(self)) catch 0,
|
||||
else => @intFromEnum(self),
|
||||
inline else => @intFromEnum(self),
|
||||
};
|
||||
}
|
||||
};
|
||||
@@ -59,7 +59,7 @@ const Scales = @This();
|
||||
data: std.EnumArray(Dimension, UnitScale),
|
||||
|
||||
pub fn init(comptime init_val: anytype) Scales {
|
||||
var s = Scales{ .data = std.EnumArray(Dimension, UnitScale).initFill(.none) };
|
||||
comptime var s = Scales{ .data = std.EnumArray(Dimension, UnitScale).initFill(.none) };
|
||||
inline for (std.meta.fields(@TypeOf(init_val))) |f| {
|
||||
if (comptime hlp.isInt(@TypeOf(@field(init_val, f.name))))
|
||||
s.data.set(@field(Dimension, f.name), @enumFromInt(@field(init_val, f.name)))
|
||||
@@ -69,30 +69,30 @@ pub fn init(comptime init_val: anytype) Scales {
|
||||
return s;
|
||||
}
|
||||
|
||||
pub fn initFill(val: UnitScale) Scales {
|
||||
return .{ .data = std.EnumArray(Dimension, UnitScale).initFill(val) };
|
||||
pub fn initFill(comptime val: UnitScale) Scales {
|
||||
return comptime .{ .data = std.EnumArray(Dimension, UnitScale).initFill(val) };
|
||||
}
|
||||
|
||||
pub fn get(self: Scales, key: Dimension) UnitScale {
|
||||
return self.data.get(key);
|
||||
pub fn get(comptime self: Scales, comptime key: Dimension) UnitScale {
|
||||
return comptime self.data.get(key);
|
||||
}
|
||||
|
||||
pub fn set(self: *Scales, key: Dimension, val: UnitScale) void {
|
||||
self.data.set(key, val);
|
||||
pub fn set(comptime self: *Scales, comptime key: Dimension, comptime val: UnitScale) void {
|
||||
comptime self.data.set(key, val);
|
||||
}
|
||||
|
||||
pub fn min(comptime s1: Scales, comptime s2: Scales) Scales {
|
||||
@setEvalBranchQuota(10_000);
|
||||
var out = Scales.initFill(.none);
|
||||
for (std.enums.values(Dimension)) |dim|
|
||||
comptime var out = Scales.initFill(.none);
|
||||
inline for (std.enums.values(Dimension)) |dim|
|
||||
out.set(dim, if (s1.get(dim).getFactorInt() > s2.get(dim).getFactorInt()) s2.get(dim) else s1.get(dim));
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
pub fn getFactor(comptime s: Scales, comptime d: Dimensions) f64 {
|
||||
var factor: f64 = 1.0;
|
||||
for (std.enums.values(Dimension)) |dim| {
|
||||
comptime var factor: f64 = 1.0;
|
||||
inline for (std.enums.values(Dimension)) |dim| {
|
||||
const power = d.get(dim);
|
||||
if (power == 0) continue;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user