Removed lots of usless inline and comptime in Scales and Dimensions

This commit is contained in:
adrien 2026-04-27 19:09:55 +02:00
parent 698e968ef8
commit 168312b78e
2 changed files with 46 additions and 46 deletions

View File

@ -49,22 +49,22 @@ data: std.EnumArray(Dimension, comptime_int),
/// Create a `Dimensions` from a struct literal, e.g. `.{ .L = 1, .T = -1 }`. /// Create a `Dimensions` from a struct literal, e.g. `.{ .L = 1, .T = -1 }`.
/// Unspecified dimensions default to 0. /// Unspecified dimensions default to 0.
pub fn init(comptime init_val: ArgOpts) Self { pub fn init(init_val: ArgOpts) Self {
var s = Self{ .data = std.EnumArray(Dimension, comptime_int).initFill(0) }; var s = Self{ .data = std.EnumArray(Dimension, comptime_int).initFill(0) };
for (std.meta.fields(@TypeOf(init_val))) |f| for (std.meta.fields(@TypeOf(init_val))) |f|
s.data.set(@field(Dimension, f.name), @field(init_val, f.name)); s.data.set(@field(Dimension, f.name), @field(init_val, f.name));
return s; return s;
} }
pub fn initFill(comptime val: comptime_int) Self { pub fn initFill(val: comptime_int) Self {
comptime return .{ .data = std.EnumArray(Dimension, comptime_int).initFill(val) }; return .{ .data = std.EnumArray(Dimension, comptime_int).initFill(val) };
} }
pub fn get(comptime self: Self, comptime key: Dimension) comptime_int { pub fn get(self: Self, key: Dimension) comptime_int {
comptime return self.data.get(key); return self.data.get(key);
} }
pub fn set(comptime self: *Self, comptime key: Dimension, comptime val: i8) void { pub fn set(self: *Self, key: Dimension, val: i8) void {
self.data.set(key, val); self.data.set(key, val);
} }
@ -72,58 +72,58 @@ pub fn argsOpt(self: Self) ArgOpts {
var args: ArgOpts = undefined; var args: ArgOpts = undefined;
for (std.enums.values(Dimension)) |d| for (std.enums.values(Dimension)) |d|
@field(args, @tagName(d)) = self.get(d); @field(args, @tagName(d)) = self.get(d);
comptime return args; return args;
} }
/// Add exponents component-wise. Used internally by `mul`. /// Add exponents component-wise. Used internally by `mul`.
pub fn add(comptime a: Self, comptime b: Self) Self { pub fn add(a: Self, b: Self) Self {
var result = Self.initFill(0); var result = Self.initFill(0);
for (std.enums.values(Dimension)) |d| for (std.enums.values(Dimension)) |d|
result.set(d, a.get(d) + b.get(d)); result.set(d, a.get(d) + b.get(d));
comptime return result; return result;
} }
/// Subtract exponents component-wise. Used internally by `div`. /// Subtract exponents component-wise. Used internally by `div`.
pub fn sub(comptime a: Self, comptime b: Self) Self { pub fn sub(a: Self, b: Self) Self {
var result = Self.initFill(0); var result = Self.initFill(0);
for (std.enums.values(Dimension)) |d| for (std.enums.values(Dimension)) |d|
result.set(d, a.get(d) - b.get(d)); result.set(d, a.get(d) - b.get(d));
comptime return result; return result;
} }
/// Multiply exponents by a scalar integer. Used internally by `pow` in Scalar. /// Multiply exponents by a scalar integer. Used internally by `pow` in Scalar.
pub fn scale(comptime a: Self, comptime exp: comptime_int) Self { pub fn scale(a: Self, exp: comptime_int) Self {
var result = Self.initFill(0); var result = Self.initFill(0);
for (std.enums.values(Dimension)) |d| for (std.enums.values(Dimension)) |d|
result.set(d, a.get(d) * exp); result.set(d, a.get(d) * exp);
comptime return result; return result;
} }
pub fn div(comptime a: Self, comptime exp: comptime_int) Self { pub fn div(a: Self, exp: comptime_int) Self {
var result = Self.initFill(0); var result = Self.initFill(0);
inline for (std.enums.values(Dimension)) |d| for (std.enums.values(Dimension)) |d|
result.set(d, a.get(d) / exp); result.set(d, a.get(d) / exp);
comptime return result; return result;
} }
/// Returns true if every dimension exponent is equal. Used to enforce type compatibility in `add`, `sub`, `to`. /// Returns true if every dimension exponent is equal. Used to enforce type compatibility in `add`, `sub`, `to`.
pub fn eql(comptime a: Self, comptime b: Self) bool { pub fn eql(a: Self, b: Self) bool {
inline for (std.enums.values(Dimension)) |d| for (std.enums.values(Dimension)) |d|
if (a.get(d) != b.get(d)) return false; if (a.get(d) != b.get(d)) return false;
return true; return true;
} }
pub fn isSquare(comptime a: Self) bool { pub fn isSquare(a: Self) bool {
inline for (std.enums.values(Dimension)) |d| for (std.enums.values(Dimension)) |d|
if (a.get(d) % 2 != 0) return false; if (a.get(d) % 2 != 0) return false;
return true; return true;
} }
pub fn str(comptime a: Self) []const u8 { pub fn str(a: Self) []const u8 {
var out: []const u8 = ""; var out: []const u8 = "";
const dims = std.enums.values(Dimension); const dims = std.enums.values(Dimension);
inline for (dims) |d| { for (dims) |d| {
const val = a.get(d); const val = a.get(d);
if (val != 0) { if (val != 0) {
out = out ++ @tagName(d) ++ std.fmt.comptimePrint("{d}", .{val}); out = out ++ @tagName(d) ++ std.fmt.comptimePrint("{d}", .{val});

View File

@ -55,33 +55,33 @@ pub const UnitScale = enum(isize) {
// Undefined // Undefined
_, _,
pub inline fn str(self: @This()) []const u8 { pub fn str(self: @This()) []const u8 {
var buf: [16]u8 = undefined; var buf: [16]u8 = undefined;
return switch (self) { return switch (self) {
inline .none => "", .none => "",
inline .P, .T, .G, .M, .k, .h, .da, .d, .c, .m, .u, .n, .p, .f, .min, .hour, .year, .inch, .ft, .yd, .mi, .oz, .lb, .st => @tagName(self), .P, .T, .G, .M, .k, .h, .da, .d, .c, .m, .u, .n, .p, .f, .min, .hour, .year, .inch, .ft, .yd, .mi, .oz, .lb, .st => @tagName(self),
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 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
}; };
} }
pub inline fn getFactor(self: @This()) comptime_float { pub fn getFactor(self: @This()) comptime_float {
comptime return switch (self) { return switch (self) {
// Standard SI Exponents // Standard SI Exponents
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))), .P, .T, .G, .M, .k, .h, .da, .none, .d, .c, .m, .u, .n, .p, .f => std.math.pow(f64, 10.0, @floatFromInt(@intFromEnum(self))),
// Time Factors // Time Factors
inline .min, .hour, .year => @floatFromInt(@intFromEnum(self)), .min, .hour, .year => @floatFromInt(@intFromEnum(self)),
// Imperial Length (metres) // Imperial Length (metres)
inline .inch => 0.0254, .inch => 0.0254,
inline .ft => 0.3048, .ft => 0.3048,
inline .yd => 0.9144, .yd => 0.9144,
inline .mi => 1609.344, .mi => 1609.344,
// Imperial Mass (grams base unit for M is gram, i.e. .none = 1 g) // Imperial Mass (grams base unit for M is gram, i.e. .none = 1 g)
inline .oz => 28.3495231, .oz => 28.3495231,
inline .lb => 453.59237, .lb => 453.59237,
inline .st => 6350.29318, .st => 6350.29318,
else => @floatFromInt(@intFromEnum(self)), else => @floatFromInt(@intFromEnum(self)),
}; };
@ -97,7 +97,7 @@ data: std.EnumArray(Dimension, UnitScale),
/// Unspecified dimensions default to `.none` (factor 1). /// Unspecified dimensions default to `.none` (factor 1).
pub fn init(comptime init_val: ArgOpts) Self { pub fn init(comptime init_val: ArgOpts) Self {
comptime var s = Self{ .data = std.EnumArray(Dimension, UnitScale).initFill(.none) }; comptime var s = Self{ .data = std.EnumArray(Dimension, UnitScale).initFill(.none) };
inline for (std.meta.fields(@TypeOf(init_val))) |f| { for (std.meta.fields(@TypeOf(init_val))) |f| {
if (comptime @typeInfo(@TypeOf(@field(init_val, f.name))) == .comptime_int) if (comptime @typeInfo(@TypeOf(@field(init_val, f.name))) == .comptime_int)
s.data.set(@field(Dimension, f.name), @enumFromInt(@field(init_val, f.name))) s.data.set(@field(Dimension, f.name), @enumFromInt(@field(init_val, f.name)))
else else
@ -106,31 +106,31 @@ pub fn init(comptime init_val: ArgOpts) Self {
return comptime s; return comptime s;
} }
pub fn initFill(comptime val: UnitScale) Self { pub fn initFill(val: UnitScale) Self {
comptime return .{ .data = std.EnumArray(Dimension, UnitScale).initFill(val) }; return .{ .data = std.EnumArray(Dimension, UnitScale).initFill(val) };
} }
pub fn get(comptime self: Self, comptime key: Dimension) UnitScale { pub fn get(self: Self, key: Dimension) UnitScale {
return comptime self.data.get(key); return self.data.get(key);
} }
pub fn set(comptime self: *Self, comptime key: Dimension, comptime val: UnitScale) void { pub fn set(self: *Self, key: Dimension, val: UnitScale) void {
self.data.set(key, val); self.data.set(key, val);
} }
pub fn argsOpt(self: Self) ArgOpts { pub fn argsOpt(self: Self) ArgOpts {
var args: ArgOpts = undefined; var args: ArgOpts = undefined;
inline for (std.enums.values(Dimension)) |d| for (std.enums.values(Dimension)) |d|
@field(args, @tagName(d)) = self.get(d); @field(args, @tagName(d)) = self.get(d);
return args; return args;
} }
/// Compute the combined scale factor for a given dimension signature. /// Compute the combined scale factor for a given dimension signature.
/// Each dimension's prefix is raised to its exponent and multiplied together. /// Each dimension's prefix is raised to its exponent and multiplied together.
pub inline fn getFactor(comptime s: Self, comptime d: Dimensions) comptime_float { pub fn getFactor(s: Self, d: Dimensions) comptime_float {
var factor: f64 = 1.0; var factor: f64 = 1.0;
for (std.enums.values(Dimension)) |dim| { for (std.enums.values(Dimension)) |dim| {
const power = comptime d.get(dim); const power = d.get(dim);
if (power == 0) continue; if (power == 0) continue;
const base = s.get(dim).getFactor(); const base = s.get(dim).getFactor();
@ -144,5 +144,5 @@ pub inline fn getFactor(comptime s: Self, comptime d: Dimensions) comptime_float
factor /= base; factor /= base;
} }
} }
comptime return factor; return factor;
} }