+++
This commit is contained in:
315
QWERTYkez.Mensura/Extensions.cs
Normal file
315
QWERTYkez.Mensura/Extensions.cs
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user