Pow Sqrt ....

This commit is contained in:
melekhin
2026-06-10 16:44:55 +07:00
parent dfdee6450a
commit 4c0a5890bc
5 changed files with 249 additions and 160 deletions

View File

@@ -111,17 +111,16 @@ public class TestsGenerator : IIncrementalGenerator
var pars = method.Parameters; var pars = method.Parameters;
if (pars.Length != 2) continue; if (pars.Length != 2) continue;
var leftType = pars[0].Type as INamedTypeSymbol; if (pars[0].Type is not INamedTypeSymbol leftType
var rightType = pars[1].Type as INamedTypeSymbol; || pars[1].Type is not INamedTypeSymbol rightType
var returnType = method.ReturnType as INamedTypeSymbol; || method.ReturnType is not INamedTypeSymbol returnType) continue;
if (leftType == null || rightType == null || returnType == null) continue;
if (!unitTypes.Contains(leftType, SymbolEqualityComparer.Default) || if (!unitTypes.Contains(leftType, SymbolEqualityComparer.Default) ||
!unitTypes.Contains(rightType, SymbolEqualityComparer.Default) || !unitTypes.Contains(rightType, SymbolEqualityComparer.Default) ||
!unitTypes.Contains(returnType, SymbolEqualityComparer.Default)) !unitTypes.Contains(returnType, SymbolEqualityComparer.Default))
continue; continue;
string? coeffField = null; string coeffField = null;
foreach (var attr in method.GetAttributes()) foreach (var attr in method.GetAttributes())
{ {
if (attr.AttributeClass?.Name == "OperatorsGeneratorAttribute" || if (attr.AttributeClass?.Name == "OperatorsGeneratorAttribute" ||
@@ -142,7 +141,7 @@ public class TestsGenerator : IIncrementalGenerator
result.Add(new OperatorInfo(opSymbol, leftType, rightType, returnType, coeffField, containingType)); result.Add(new OperatorInfo(opSymbol, leftType, rightType, returnType, coeffField, containingType));
} }
} }
return result.ToImmutableArray(); return [.. result];
} }
private static void GenerateOperatorTests(SourceProductionContext spc, ImmutableArray<OperatorInfo> operators) private static void GenerateOperatorTests(SourceProductionContext spc, ImmutableArray<OperatorInfo> operators)
@@ -397,7 +396,7 @@ public class TestsGenerator : IIncrementalGenerator
CollectTypesRecursive(childNs, result, targetNamespacePrefix); CollectTypesRecursive(childNs, result, targetNamespacePrefix);
} }
private static IPropertySymbol? GetBaseUnitProperty(ITypeSymbol type) private static IPropertySymbol GetBaseUnitProperty(ITypeSymbol type)
{ {
foreach (var member in type.GetMembers()) foreach (var member in type.GetMembers())
{ {
@@ -407,13 +406,13 @@ public class TestsGenerator : IIncrementalGenerator
return null; return null;
} }
private readonly struct OperatorInfo(string symbol, ITypeSymbol left, ITypeSymbol right, ITypeSymbol ret, string? coeff, ITypeSymbol containing) private readonly struct OperatorInfo(string symbol, ITypeSymbol left, ITypeSymbol right, ITypeSymbol ret, string coeff, ITypeSymbol containing)
{ {
public string OperatorSymbol { get; } = symbol; public string OperatorSymbol { get; } = symbol;
public ITypeSymbol LeftType { get; } = left; public ITypeSymbol LeftType { get; } = left;
public ITypeSymbol RightType { get; } = right; public ITypeSymbol RightType { get; } = right;
public ITypeSymbol ReturnType { get; } = ret; public ITypeSymbol ReturnType { get; } = ret;
public string? CoefficientField { get; } = coeff; public string CoefficientField { get; } = coeff;
public ITypeSymbol ContainingType { get; } = containing; public ITypeSymbol ContainingType { get; } = containing;
} }
} }

File diff suppressed because it is too large Load Diff

View File

@@ -57,6 +57,27 @@ public readonly partial record struct Area
public Area AddKiloMetersSquared(double value) => new(_Value + AreaConv.KiloMetersSquared.To(value)); public Area AddKiloMetersSquared(double value) => new(_Value + AreaConv.KiloMetersSquared.To(value));
} }
public static class AreaSqrtExtension
{
// === ReadOnlySpan
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Sqrt(
this ReadOnlySpan<Area> units, Span<Length> destination) => units.Sqrt<Area, Length>(destination);
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Sqrt(
this ReadOnlySpan<Area?> units, Span<Length?> destination) => units.Sqrt<Area, Length>(destination);
// === Array ===
[MethodImpl(MethodImplOptions.AggressiveInlining)]public static Length[] Sqrt(this Area[] units) => units.Sqrt<Area, Length>();
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static Length?[] Sqrt(this Area?[] units) => units.Sqrt<Area, Length>();
// === List<T> ===
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<Length> Sqrt(this List<Area> units) => units.Sqrt<Area, Length>();
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<Length?> Sqrt(this List<Area?> units) => units.Sqrt<Area, Length>();
// === IEnumerable<T> ===
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<Length> Sqrt(this IEnumerable<Area> units) => units.Sqrt<Area, Length>();
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<Length?> Sqrt(this IEnumerable<Area?> units) => units.Sqrt<Area, Length>();
}
internal readonly struct AreaConv internal readonly struct AreaConv
{ {
private AreaConv(double multiplicator) => this.Multiplicator = multiplicator; private AreaConv(double multiplicator) => this.Multiplicator = multiplicator;

View File

@@ -83,6 +83,52 @@ public readonly partial record struct Length
} }
} }
public static class LengthPowExtension
{
// === ReadOnlySpan
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Pow(
this ReadOnlySpan<Length> units, Span<Area> destination) => units.Pow<Length, Area>(destination);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Pow(
this ReadOnlySpan<Length?> units, Span<Area?> destination) => units.Pow<Length, Area>(destination);
// === Array ===
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static Area[] Pow(this Length[] units) => units.Pow<Length, Area>();
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static Area?[] Pow(this Length?[] units) => units.Pow<Length, Area>();
// === List<T> ===
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<Area> Pow(this List<Length> units) => units.Pow<Length, Area>();
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<Area?> Pow(this List<Length?> units) => units.Pow<Length, Area>();
// === IEnumerable<T> ===
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<Area> Pow(this IEnumerable<Length> units) => units.Pow<Length, Area>();
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<Area?> Pow(this IEnumerable<Length?> units) => units.Pow<Length, Area>();
// === ReadOnlySpan
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Pow3(
this ReadOnlySpan<Length> units, Span<Volume> destination) => units.Pow3<Length, Volume>(destination);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Pow3(
this ReadOnlySpan<Length?> units, Span<Volume?> destination) => units.Pow3<Length, Volume>(destination);
// === Array ===
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static Volume[] Pow3(this Length[] units) => units.Pow3<Length, Volume>();
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static Volume?[] Pow3(this Length?[] units) => units.Pow3<Length, Volume>();
// === List<T> ===
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<Volume> Pow3(this List<Length> units) => units.Pow3<Length, Volume>();
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<Volume?> Pow3(this List<Length?> units) => units.Pow3<Length, Volume>();
// === IEnumerable<T> ===
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<Volume> Pow3(this IEnumerable<Length> units) => units.Pow3<Length, Volume>();
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<Volume?> Pow3(this IEnumerable<Length?> units) => units.Pow3<Length, Volume>();
}
internal readonly struct LengthConv internal readonly struct LengthConv
{ {

View File

@@ -35,6 +35,29 @@ public readonly partial record struct Voltage
public Voltage AddMegaVolts(double value) => new(_Value + VoltageConv.MegaVolts.To(value)); public Voltage AddMegaVolts(double value) => new(_Value + VoltageConv.MegaVolts.To(value));
} }
public static class VolumeSqrtExtension
{
// === ReadOnlySpan
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Sqrt3(
this ReadOnlySpan<Volume> units, Span<Length> destination) => units.Sqrt3<Volume, Length>(destination);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Sqrt3(
this ReadOnlySpan<Volume?> units, Span<Length?> destination) => units.Sqrt3<Volume, Length>(destination);
// === Array ===
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static Length[] Sqrt3(this Volume[] units) => units.Sqrt3<Volume, Length>();
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static Length?[] Sqrt3(this Volume?[] units) => units.Sqrt3<Volume, Length>();
// === List<T> ===
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<Length> Sqrt3(this List<Volume> units) => units.Sqrt3<Volume, Length>();
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static List<Length?> Sqrt3(this List<Volume?> units) => units.Sqrt3<Volume, Length>();
// === IEnumerable<T> ===
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<Length> Sqrt3(this IEnumerable<Volume> units) => units.Sqrt3<Volume, Length>();
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable<Length?> Sqrt3(this IEnumerable<Volume?> units) => units.Sqrt3<Volume, Length>();
}
internal readonly struct VoltageConv internal readonly struct VoltageConv
{ {
private VoltageConv(double multiplicator) => this.Multiplicator = multiplicator; private VoltageConv(double multiplicator) => this.Multiplicator = multiplicator;