338 lines
14 KiB
C#
338 lines
14 KiB
C#
using System.Buffers;
|
|
using System.Runtime.Intrinsics;
|
|
using System.Runtime.Intrinsics.X86;
|
|
|
|
namespace QWERTYkez.Mensura;
|
|
|
|
public static partial class Extensions2
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ==========================================
|
|
// CORE
|
|
// ==========================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ==========================================
|
|
// === MULTIPLY ===
|
|
// ==========================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ==========================================
|
|
// === DIVIDE ===
|
|
// ==========================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ==========================================
|
|
// === PLUS ===
|
|
// ==========================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ==========================================
|
|
// === MINUS ===
|
|
// ==========================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//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 Protect_Value(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.Protect_Value() < T2.Protect_Value() ? 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.Protect_Value()));
|
|
|
|
//public static U Max<U>(this U? T1, U? T2) where U : class, IMetric<U>, new() => (T1.Protect_Value() > T2.Protect_Value() ? 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.Protect_Value()));
|
|
|
|
|
|
////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.Protect_Value() };
|
|
//public static U Abs<U>(this U? metric) where U : class, IMetric<U>, new() => new() { _Value = Math.Abs(metric.Protect_Value()) };
|
|
|
|
///// <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.Protect_Value();
|
|
// var b = B.Protect_Value();
|
|
// 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.Protect_Value();
|
|
// var c = C.Protect_Value();
|
|
// 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.Protect_Value();
|
|
// var c = C.Protect_Value();
|
|
// 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.Protect_Value(), val ?? 2) };
|
|
//public static Length Sqrt(this Area? metric) => new() { _Value = Math.Sqrt(metric.Protect_Value()) };
|
|
|
|
|
|
//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.Protect_Value()) ?? 0d };
|
|
//public static U MetricAverage<U>(this IEnumerable<U> args) where U : IMetric, new() => new() { _Value = args?.Average(m => m.Protect_Value()) ?? double.NaN };
|
|
//public static U MetricMax<U>(this IEnumerable<U> args) where U : IMetric, new() => new() { _Value = args.Max(m => m.Protect_Value()) };
|
|
//public static U MetricMin<U>(this IEnumerable<U> args) where U : IMetric, new() => new() { _Value = args.Min(m => m.Protect_Value()) };
|
|
|
|
|
|
|
|
//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.Sum() / 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.Protect_Value()) };
|
|
//}
|
|
//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.Protect_Value()) };
|
|
//}
|
|
|
|
|
|
//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).Sum();
|
|
//}
|
|
//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).Average();
|
|
//}
|
|
|
|
|
|
|
|
//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);
|
|
} |