This commit is contained in:
melekhin
2026-05-28 16:44:39 +07:00
parent d6bb2ff84b
commit ca673c737d
8 changed files with 1504 additions and 7 deletions

View File

@@ -14,7 +14,7 @@ internal class CollectionsOperatorsGenerator : IIncrementalGenerator
private const string AttributeShortName = "CollectionsOperatorsGenerator"; private const string AttributeShortName = "CollectionsOperatorsGenerator";
private const string AttributeFullName = AttributeShortName + "Attribute"; private const string AttributeFullName = AttributeShortName + "Attribute";
private const string AttributeSource = @"namespace MetricSystem.Generator; private const string AttributeSource = @"namespace QWERTYkez.Mensura;
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Method, AllowMultiple = false, Inherited = false)] [System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
internal sealed class CollectionsOperatorsGeneratorAttribute : System.Attribute { }"; internal sealed class CollectionsOperatorsGeneratorAttribute : System.Attribute { }";

View File

@@ -15,12 +15,12 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" /> <PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.2" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="4.14.0"> <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="5.3.0">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> </PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="5.0.0" /> <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="5.3.0" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -18,7 +18,7 @@ public class UnitOperatorsGenerator : IIncrementalGenerator
context.RegisterPostInitializationOutput(ctx => context.RegisterPostInitializationOutput(ctx =>
{ {
ctx.AddSource("UnitOperatorsGeneratorAttribute.g.cs", ctx.AddSource("UnitOperatorsGeneratorAttribute.g.cs",
SourceText.From(@"namespace MetricSystem.Generator SourceText.From(@"namespace QWERTYkez.Mensura
{ {
[System.AttributeUsage(System.AttributeTargets.Struct | System.AttributeTargets.RecordStruct, AllowMultiple = false)] [System.AttributeUsage(System.AttributeTargets.Struct | System.AttributeTargets.RecordStruct, AllowMultiple = false)]
public sealed class UnitOperatorsGeneratorAttribute : System.Attribute { } public sealed class UnitOperatorsGeneratorAttribute : System.Attribute { }

View File

@@ -0,0 +1,315 @@
namespace QWERTYkez.Mensura;
public static partial class Extensions
{
internal static double ToDouble(this sbyte number) => Convert.ToDouble(number);
internal static double ToDouble(this short number) => Convert.ToDouble(number);
internal static double ToDouble(this int number) => Convert.ToDouble(number);
internal static double ToDouble(this long number) => Convert.ToDouble(number);
internal static double ToDouble(this byte number) => Convert.ToDouble(number);
internal static double ToDouble(this ushort number) => Convert.ToDouble(number);
internal static double ToDouble(this uint number) => Convert.ToDouble(number);
internal static double ToDouble(this ulong number) => Convert.ToDouble(number);
internal static double ToDouble(this nint number) => Convert.ToDouble(number);
internal static double ToDouble(this nuint number) => Convert.ToDouble(number);
internal static double ToDouble(this float number) => Convert.ToDouble(number);
internal static double ToDouble(this decimal number) => Convert.ToDouble(number);
internal static double ToDouble(this sbyte? number) => number is null ? 0d : Convert.ToDouble(number);
internal static double ToDouble(this short? number) => number is null ? 0d : Convert.ToDouble(number);
internal static double ToDouble(this int? number) => number is null ? 0d : Convert.ToDouble(number);
internal static double ToDouble(this long? number) => number is null ? 0d : Convert.ToDouble(number);
internal static double ToDouble(this byte? number) => number is null ? 0d : Convert.ToDouble(number);
internal static double ToDouble(this ushort? number) => number is null ? 0d : Convert.ToDouble(number);
internal static double ToDouble(this uint? number) => number is null ? 0d : Convert.ToDouble(number);
internal static double ToDouble(this ulong? number) => number is null ? 0d : Convert.ToDouble(number);
internal static double ToDouble(this nint? number) => number is null ? 0d : Convert.ToDouble(number);
internal static double ToDouble(this nuint? number) => number is null ? 0d : Convert.ToDouble(number);
internal static double ToDouble(this float? number) => number is null ? 0d : Convert.ToDouble(number);
internal static double ToDouble(this decimal? number) => number is null ? 0d : Convert.ToDouble(number);
#if NET7_0_OR_GREATER
internal static double ToDouble(this Int128 number) => Convert.ToDouble(number);
internal static double ToDouble(this UInt128 number) => Convert.ToDouble(number);
internal static double ToDouble(this Int128? number) => number is null ? 0d : Convert.ToDouble(number);
internal static double ToDouble(this UInt128? number) => number is null ? 0d : Convert.ToDouble(number);
#endif
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static unsafe void SetCountUnsafe<T>(this List<T> list, int count)
{
// Берем адрес управляемого объекта List в памяти
// Объект передается по ref-ссылке, преобразуется в указатель
ref var mimic = ref Unsafe.As<List<T>, ListLayoutMimic<T>>(ref list);
// Меняем приватный размер напрямую в памяти объекта!
mimic.Size = count;
}
internal static U Protect<U>(this U? metric) where U : class, IMetric<U>, new() => metric ?? new();
internal static C Protect<C, U>(this C? collection)
where C : IMetricCollection<U>, new() where U : class, IMetric<U>, new() => collection ?? new();
public static C MetricSelect<C, U>(this C? collection, Func<U, U>? selector)
where C : IMetricCollection<U>, new() where U : class, IMetric<U>, new()
{
var source = (collection ??= new());
var nColl = (C)source.CreateByInstanceU(source.Count);
if (selector is not null)
{
for (int i = 0; i < nColl.Count; i++)
nColl[i] = selector(source[i]);
return nColl;
}
return new();
}
public static IEnumerable<double> MetricSelect<U>(this IMetricCollection<U> collection, Func<U, double> selector)
where U : class, IMetric<U>, new()
{
if (collection is not null)
{
if (selector is not null)
return collection.Select(selector);
return collection.Select(u => double.NaN);
}
else return [];
}
public static IMetricCollection<Uz> MetricSelect<Ux, Uz>(this IMetricCollection<Ux>? collection, Func<Ux, Uz>? selector)
where Ux : class, IMetric<Ux>, new() where Uz : class, IMetric<Uz>, new()
{
if (collection is not null && selector is not null)
{
var destCollection = collection.CreateByInstance<Uz>(collection.Count);
for (int i = 0; i < collection.Count; i++)
destCollection[i] = selector(collection[i]);
return destCollection;
}
return null!;
}
public static MetricCollection<Uz> MetricSelect<Ux, Uz>(this MetricCollection<Ux>? collection, Func<Ux, Uz>? selector)
where Ux : class, IMetric<Ux>, new() where Uz : class, IMetric<Uz>, new()
{
if (collection is not null && selector is not null)
{
var destCollection = collection.CreateByInstance<Uz>(collection.Count());
for (int i = 0; i < collection.Count(); i++)
destCollection[i] = selector(collection[i]);
return destCollection;
}
return null!;
}
public static MetricArray<Uz> MetricSelect<Ux, Uz>(this MetricArray<Ux>? collection, Func<Ux, Uz>? selector)
where Ux : class, IMetric<Ux>, new() where Uz : class, IMetric<Uz>, new()
{
var coll = collection?.ToArray();
if (coll is not null && selector is not null)
{
var destCollection = new MetricArray<Uz>(coll.Length);
for (int i = 0; i < coll.Length; i++)
destCollection[i] = selector(coll[i]);
return destCollection;
}
return null!;
}
public static MetricList<Uz> MetricSelect<Ux, Uz>(this MetricList<Ux>? collection, Func<Ux, Uz>? selector)
where Ux : class, IMetric<Ux>, new() where Uz : class, IMetric<Uz>, new()
{
if (collection is not null && selector is not null)
{
var destCollection = new MetricList<Uz>(collection.Count);
for (int i = 0; i < collection.Count; i++)
destCollection[i] = selector(collection[i]);
return destCollection;
}
return null!;
}
public static MetricObservableCollection<Uz> MetricSelect<Ux, Uz>(this MetricObservableCollection<Ux>? collection, Func<Ux, Uz>? selector)
where Ux : class, IMetric<Ux>, new() where Uz : class, IMetric<Uz>, new()
{
if (collection is not null && selector is not null)
{
var destCollection = new MetricObservableCollection<Uz>(collection.Count);
for (int i = 0; i < collection.Count; i++)
destCollection[i] = selector(collection[i]);
return destCollection;
}
return null!;
}
public static double[] MetricSelect<U>(this MetricArray<U> collection, Func<U, double> selector)
where U : class, IMetric<U>, new()
{
var coll = collection ?? [];
var arr = new double[coll.Length];
if (selector is not null)
for (int i = 0; i < arr.Length; i++)
arr[i] = selector(coll[i]);
return arr;
}
public static List<double> MetricSelect<U>(this MetricList<U> collection, Func<U, double> selector)
where U : class, IMetric<U>, new()
{
var coll = collection ?? [];
var list = new List<double>(coll.Count);
if (selector is not null)
for (int i = 0; i < list.Count; i++)
list[i] = selector(coll[i]);
return list;
}
public static ObservableCollection<double> MetricSelect<U>(this MetricObservableCollection<U> collection, Func<U, double> selector)
where U : class, IMetric<U>, new()
{
var coll = collection ?? [];
var list = new List<double>(coll.Count);
if (selector is not null)
for (int i = 0; i < list.Count; i++)
list[i] = selector(coll[i]);
return new(list);
}
internal static C ForEachC<C, U>(this C? collection, Func<U, U>? Set)
where C : IMetricCollection<U>, new() where U : class, IMetric<U>, new()
{
var nColl = (C)(collection ??= new()).CreateByInstanceU(collection.Count);
if (Set is not null)
for (int i = 0; i < nColl.Count; i++)
nColl[i] = Set(nColl[i]);
return nColl;
}
internal static double ProtectValue(this IMetric? metric) => metric is null ? 0d : metric.Value;
public static U Min<U>(this U? T1, U? T2) where U : class, IMetric<U>, new() => (T1.ProtectValue() < T2.ProtectValue() ? T1 : T2).Protect();
public static U Min<U>(this U T1, IEnumerable<U> units) where U : class, IMetric<U>, new() => (T1 ?? new()).Min((units ?? []).MaxBy(u => u.ProtectValue()));
public static U Max<U>(this U? T1, U? T2) where U : class, IMetric<U>, new() => (T1.ProtectValue() > T2.ProtectValue() ? T1 : T2).Protect();
public static U Max<U>(this U T1, IEnumerable<U> units) where U : class, IMetric<U>, new() => (T1 ?? new()).Max((units ?? []).MaxBy(u => u.ProtectValue()));
//internal static double ToDouble(this double number) => number;
//internal static double ToDouble(this double? number) => number ?? 0d;
//internal static double ToDouble<N>(this N number) where N : INumber<N> => Convert.ToDouble(number);
//internal static double ToDouble<N>(this N? number) where N : struct, INumber<N> => number is not null ? Convert.ToDouble(number) : 0d;
internal static IEnumerable<U> MetricSelect<U>(this double[] nums, Func<double, U> selector) where U : class, IMetric<U>, new() => nums.Select(selector);
internal static IEnumerable<U> MetricSelect<U>(this double?[] nums, Func<double, U> selector) where U : class, IMetric<U>, new() => nums.Select(num => selector(num.ToDouble()));
internal static IEnumerable<U> MetricSelect<U, N>(this N[] nums, Func<double, U> selector) where N : INumber<N> where U : class, IMetric<U>, new() => nums.Select(num => selector(num.ToDouble()));
internal static IEnumerable<U> MetricSelect<U, N>(this N?[] nums, Func<double, U> selector) where N : struct, INumber<N> where U : class, IMetric<U>, new() => nums.Select(num => selector(num.ToDouble()));
internal static IEnumerable<U> MetricSelect<U>(this IEnumerable<double> nums, Func<double, U> selector) where U : class, IMetric<U>, new() => nums.Select(selector);
internal static IEnumerable<U> MetricSelect<U>(this IEnumerable<double?> nums, Func<double, U> selector) where U : class, IMetric<U>, new() => nums.Select(num => selector(num.ToDouble()));
internal static IEnumerable<U> MetricSelect<U, N>(this IEnumerable<N> nums, Func<double, U> selector) where N : INumber<N> where U : class, IMetric<U>, new() => nums.Select(num => selector(num.ToDouble()));
internal static IEnumerable<U> MetricSelect<U, N>(this IEnumerable<N?> nums, Func<double, U> selector) where N : struct, INumber<N> where U : class, IMetric<U>, new() => nums.Select(num => selector(num.ToDouble()));
public static U Clone<U>(this U? metric) where U : class, IMetric<U>, new() => new() { Value = metric.ProtectValue() };
public static U Abs<U>(this U? metric) where U : class, IMetric<U>, new() => new() { Value = Math.Abs(metric.ProtectValue()) };
/// <summary>C^2 = A^2 + B^2</summary>
/// <returns>C = (A^2 + B^2).Sqrt(2)</returns>
public static U Hypotenuse<U>(this U? A, U? B) where U : class, IMetric<U>, new()
{
var a = A.ProtectValue();
var b = B.ProtectValue();
return new U() { Value = Math.Sqrt(a * a + b * b) };
}
/// <summary>C^2 = A^2 + B^2</summary>
/// <returns>B = (C^2 - A^2).Sqrt(2)</returns>
public static U KatetFromHyp<U>(this U? A, U? C) where U : class, IMetric<U>, new()
{
var a = A.ProtectValue();
var c = C.ProtectValue();
return new U() { Value = Math.Sqrt(c * c - a * a) };
}
/// <summary>C^2 = A^2 + B^2</summary>
/// <returns>B = (C^2 - A^2).Sqrt(2)</returns>
public static U KatetFromKatet<U>(this U? C, U? A) where U : class, IMetric<U>, new()
{
var a = A.ProtectValue();
var c = C.ProtectValue();
return new U() { Value = Math.Sqrt(c * c - a * a) };
}
public static Area Pow(this Length? metric, double? val = 2) => new() { Value = Math.Pow(metric.ProtectValue(), val ?? 2) };
public static Length Sqrt(this Area? metric) => new() { Value = Math.Sqrt(metric.ProtectValue()) };
public static U MetricSum<U>(this IEnumerable<U> args) where U : IMetric, new() => new() { Value = args?.Where(t => t is not null).Sum(m => m.ProtectValue()) ?? 0d };
public static U MetricAverage<U>(this IEnumerable<U> args) where U : IMetric, new() => new() { Value = args?.Average(m => m.ProtectValue()) ?? double.NaN };
public static U MetricMax<U>(this IEnumerable<U> args) where U : IMetric, new() => new() { Value = args.Max(m => m.ProtectValue()) };
public static U MetricMin<U>(this IEnumerable<U> args) where U : IMetric, new() => new() { Value = args.Min(m => m.ProtectValue()) };
public static C MetricSum<C, U>(this IEnumerable<MetricCollection<C, U>> collections)
where C : MetricCollection<C, U>, ICreateByCapacity, new() where U : class, IMetric<U>, new()
{
var cArr = collections.ToArray();
C accumulator = (C)cArr.FirstOrDefault(new C());
for (int i = 1; i < cArr.Length; i++)
accumulator = accumulator.FuncByPairOrOneToMany(cArr[i], (a, b) => a + b, out C _);
return accumulator;
}
public static C MetricAverage<C, U>(this IEnumerable<MetricCollection<C, U>> collections)
where C : MetricCollection<C, U>, ICreateByCapacity, new() where U : class, IMetric<U>, new()
=> collections.MetricSum() / collections.Count();
public static U MetricSumBy<TSource, U>(this IEnumerable<TSource> source, Func<TSource, U> selector)
where U : class, IMetric<U>, new()
{
if (source is null) return new();
if (selector is null) throw new ArgumentNullException("selector is null");
return new() { Value = source.Select(selector).Where(t => t is not null).Sum(t => t.ProtectValue()) };
}
public static U MetricAverageBy<TSource, U>(this IEnumerable<TSource> source, Func<TSource, U> selector)
where U : class, IMetric<U>, new()
{
if (source is null) return new();
if (selector is null) throw new ArgumentNullException("selector is null");
return new() { Value = source.Select(selector).Average(t => t.ProtectValue()) };
}
public static C MetricSumBy<TSource, C, U>(this IEnumerable<TSource> source, Func<TSource, MetricCollection<C, U>> selector)
where C : MetricCollection<C, U>, ICreateByCapacity, new() where U : class, IMetric<U>, new()
{
if (source is null) return new();
if (selector is null) throw new ArgumentNullException("selector is null");
return source.Select(selector).MetricSum();
}
public static C MetricAverageBy<TSource, C, U>(this IEnumerable<TSource> source, Func<TSource, MetricCollection<C, U>> selector)
where C : MetricCollection<C, U>, ICreateByCapacity, new() where U : class, IMetric<U>, new()
{
if (source is null) return new();
if (selector is null) throw new ArgumentNullException("selector is null");
return source.Select(selector).MetricAverage();
}
public static MetricArray<U> ToMetricArray<U>(this IEnumerable<U> source) where U : class, IMetric<U>, new() => new(source);
public static MetricList<U> ToMetricList<U>(this IEnumerable<U> source) where U : class, IMetric<U>, new() => new(source);
}

View File

@@ -0,0 +1,9 @@
namespace QWERTYkez.Mensura;
[StructLayout(LayoutKind.Sequential)]
internal unsafe struct ListLayoutMimic<T>
{
public T[] Items;
public int Size;
public int Version;
}

View File

@@ -1,9 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0</TargetFramework> <TargetFrameworks>net6.0;net7.0;net8.0;net9.0;net10.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\QWERTYkez.Mensura.Generator\QWERTYkez.Mensura.Generator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net10.0'">
<PackageReference Include="Microsoft.EntityFrameworkCore">
<Version>10.0.8</Version>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions">
<Version>10.0.8</Version>
</PackageReference>
</ItemGroup>
</Project> </Project>

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,12 @@
global using System.Collections;
global using System.Collections.ObjectModel;
global using System.Collections.Specialized;
global using System.ComponentModel;
global using System.ComponentModel.DataAnnotations;
global using System.ComponentModel.DataAnnotations.Schema;
global using System.Diagnostics;
global using System.Numerics;
global using System.Runtime.CompilerServices;
global using System.Runtime.InteropServices;
global using System.Text.Json;
global using System.Text.Json.Serialization;