Removed lots of usless inline and comptime in Scales and Dimensions
This commit is contained in:
parent
698e968ef8
commit
168312b78e
@ -49,22 +49,22 @@ data: std.EnumArray(Dimension, comptime_int),
|
||||
|
||||
/// Create a `Dimensions` from a struct literal, e.g. `.{ .L = 1, .T = -1 }`.
|
||||
/// 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) };
|
||||
for (std.meta.fields(@TypeOf(init_val))) |f|
|
||||
s.data.set(@field(Dimension, f.name), @field(init_val, f.name));
|
||||
return s;
|
||||
}
|
||||
|
||||
pub fn initFill(comptime val: comptime_int) Self {
|
||||
comptime return .{ .data = std.EnumArray(Dimension, comptime_int).initFill(val) };
|
||||
pub fn initFill(val: comptime_int) Self {
|
||||
return .{ .data = std.EnumArray(Dimension, comptime_int).initFill(val) };
|
||||
}
|
||||
|
||||
pub fn get(comptime self: Self, comptime key: Dimension) comptime_int {
|
||||
comptime return self.data.get(key);
|
||||
pub fn get(self: Self, key: Dimension) comptime_int {
|
||||
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);
|
||||
}
|
||||
|
||||
@ -72,58 +72,58 @@ pub fn argsOpt(self: Self) ArgOpts {
|
||||
var args: ArgOpts = undefined;
|
||||
for (std.enums.values(Dimension)) |d|
|
||||
@field(args, @tagName(d)) = self.get(d);
|
||||
comptime return args;
|
||||
return args;
|
||||
}
|
||||
|
||||
/// 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);
|
||||
for (std.enums.values(Dimension)) |d|
|
||||
result.set(d, a.get(d) + b.get(d));
|
||||
comptime return result;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// 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);
|
||||
for (std.enums.values(Dimension)) |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.
|
||||
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);
|
||||
for (std.enums.values(Dimension)) |d|
|
||||
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);
|
||||
inline for (std.enums.values(Dimension)) |d|
|
||||
for (std.enums.values(Dimension)) |d|
|
||||
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`.
|
||||
pub fn eql(comptime a: Self, comptime b: Self) bool {
|
||||
inline for (std.enums.values(Dimension)) |d|
|
||||
pub fn eql(a: Self, b: Self) bool {
|
||||
for (std.enums.values(Dimension)) |d|
|
||||
if (a.get(d) != b.get(d)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
pub fn isSquare(comptime a: Self) bool {
|
||||
inline for (std.enums.values(Dimension)) |d|
|
||||
pub fn isSquare(a: Self) bool {
|
||||
for (std.enums.values(Dimension)) |d|
|
||||
if (a.get(d) % 2 != 0) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
pub fn str(comptime a: Self) []const u8 {
|
||||
pub fn str(a: Self) []const u8 {
|
||||
var out: []const u8 = "";
|
||||
const dims = std.enums.values(Dimension);
|
||||
|
||||
inline for (dims) |d| {
|
||||
for (dims) |d| {
|
||||
const val = a.get(d);
|
||||
if (val != 0) {
|
||||
out = out ++ @tagName(d) ++ std.fmt.comptimePrint("{d}", .{val});
|
||||
|
||||
@ -55,33 +55,33 @@ pub const UnitScale = enum(isize) {
|
||||
// Undefined
|
||||
_,
|
||||
|
||||
pub inline fn str(self: @This()) []const u8 {
|
||||
pub fn str(self: @This()) []const u8 {
|
||||
var buf: [16]u8 = undefined;
|
||||
return switch (self) {
|
||||
inline .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),
|
||||
.none => "",
|
||||
.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
|
||||
};
|
||||
}
|
||||
|
||||
pub inline fn getFactor(self: @This()) comptime_float {
|
||||
comptime return switch (self) {
|
||||
pub fn getFactor(self: @This()) comptime_float {
|
||||
return switch (self) {
|
||||
// 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
|
||||
inline .min, .hour, .year => @floatFromInt(@intFromEnum(self)),
|
||||
.min, .hour, .year => @floatFromInt(@intFromEnum(self)),
|
||||
|
||||
// Imperial Length (metres)
|
||||
inline .inch => 0.0254,
|
||||
inline .ft => 0.3048,
|
||||
inline .yd => 0.9144,
|
||||
inline .mi => 1609.344,
|
||||
.inch => 0.0254,
|
||||
.ft => 0.3048,
|
||||
.yd => 0.9144,
|
||||
.mi => 1609.344,
|
||||
|
||||
// Imperial Mass (grams — base unit for M is gram, i.e. .none = 1 g)
|
||||
inline .oz => 28.3495231,
|
||||
inline .lb => 453.59237,
|
||||
inline .st => 6350.29318,
|
||||
.oz => 28.3495231,
|
||||
.lb => 453.59237,
|
||||
.st => 6350.29318,
|
||||
|
||||
else => @floatFromInt(@intFromEnum(self)),
|
||||
};
|
||||
@ -97,7 +97,7 @@ data: std.EnumArray(Dimension, UnitScale),
|
||||
/// Unspecified dimensions default to `.none` (factor 1).
|
||||
pub fn init(comptime init_val: ArgOpts) Self {
|
||||
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)
|
||||
s.data.set(@field(Dimension, f.name), @enumFromInt(@field(init_val, f.name)))
|
||||
else
|
||||
@ -106,31 +106,31 @@ pub fn init(comptime init_val: ArgOpts) Self {
|
||||
return comptime s;
|
||||
}
|
||||
|
||||
pub fn initFill(comptime val: UnitScale) Self {
|
||||
comptime return .{ .data = std.EnumArray(Dimension, UnitScale).initFill(val) };
|
||||
pub fn initFill(val: UnitScale) Self {
|
||||
return .{ .data = std.EnumArray(Dimension, UnitScale).initFill(val) };
|
||||
}
|
||||
|
||||
pub fn get(comptime self: Self, comptime key: Dimension) UnitScale {
|
||||
return comptime self.data.get(key);
|
||||
pub fn get(self: Self, key: Dimension) UnitScale {
|
||||
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);
|
||||
}
|
||||
|
||||
pub fn argsOpt(self: Self) ArgOpts {
|
||||
var args: ArgOpts = undefined;
|
||||
inline for (std.enums.values(Dimension)) |d|
|
||||
for (std.enums.values(Dimension)) |d|
|
||||
@field(args, @tagName(d)) = self.get(d);
|
||||
return args;
|
||||
}
|
||||
|
||||
/// Compute the combined scale factor for a given dimension signature.
|
||||
/// 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;
|
||||
for (std.enums.values(Dimension)) |dim| {
|
||||
const power = comptime d.get(dim);
|
||||
const power = d.get(dim);
|
||||
if (power == 0) continue;
|
||||
|
||||
const base = s.get(dim).getFactor();
|
||||
@ -144,5 +144,5 @@ pub inline fn getFactor(comptime s: Self, comptime d: Dimensions) comptime_float
|
||||
factor /= base;
|
||||
}
|
||||
}
|
||||
comptime return factor;
|
||||
return factor;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user