From 96bbbbd292f836684c9f14137a21443c51bb88b1 Mon Sep 17 00:00:00 2001 From: melekhin Date: Thu, 4 Jun 2026 12:03:39 +0700 Subject: [PATCH] 26.06.04 --- .../UnitOperatorsGenerator.cs | 3 +- .../Extensions/CastExtensions.cs | 32 + .../Extensions/CollectionsDivideExtensions.cs | 404 +++++++++++- .../Extensions/CollectionsMinusExtensions.cs | 398 +++++++++++- .../CollectionsMultiplyExtensions.cs | 268 +++++++- .../Extensions/CollectionsPlusExtensions.cs | 264 +++++++- .../Extensions/CollectionsPowExtensions.cs | 48 +- .../Extensions/CollectionsSqrtExtensions.cs | 24 +- QWERTYkez.Mensura/IMensuraUnit.cs | 5 +- .../Units/Pogon/PogonXXXXXXXX.Gen.cs | 605 +++++++----------- QWERTYkez.Mensura/Units/XXXXXXXX.Gen.cs | 3 +- 11 files changed, 1587 insertions(+), 467 deletions(-) diff --git a/QWERTYkez.Mensura.Generator/UnitOperatorsGenerator.cs b/QWERTYkez.Mensura.Generator/UnitOperatorsGenerator.cs index e31148f..2bcdaa9 100644 --- a/QWERTYkez.Mensura.Generator/UnitOperatorsGenerator.cs +++ b/QWERTYkez.Mensura.Generator/UnitOperatorsGenerator.cs @@ -103,9 +103,8 @@ public class {typeName}Converter : UnitJsonConverter<{typeName}> { } [JsonConverter(typeof({typeName}Converter))] public readonly partial record struct {typeName} : IMensuraUnit<{typeName}>, IEquatable<{typeName}>, IMensuraUnit { - [JsonInclude, DataMember, JsonPropertyName(""v"")] // для JSON / EF на случай сбоев, если пробелма с _Value + [JsonInclude, DataMember, JsonPropertyName(""v""), Obsolete] // для JSON / EF на случай сбоев, если пробелма с _Value internal double Value { get => _Value; init => _Value = value; } - double IMensuraUnit.Value { get => _Value; init => _Value = value; } internal readonly double _Value; internal {typeName}(double value) => _Value = value; diff --git a/QWERTYkez.Mensura/Extensions/CastExtensions.cs b/QWERTYkez.Mensura/Extensions/CastExtensions.cs index 0f81a68..d5c5174 100644 --- a/QWERTYkez.Mensura/Extensions/CastExtensions.cs +++ b/QWERTYkez.Mensura/Extensions/CastExtensions.cs @@ -53,4 +53,36 @@ public static partial class CastExtensions { return Unsafe.As(ref array).WrapAsList(); } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static List ReCast(this List list) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + return Unsafe.As, List>(ref list); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static List ReCast(this List list) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + return Unsafe.As, List>(ref list); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static R[] ReCast(this T[] array) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + return Unsafe.As(ref array); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static R?[] ReCast(this T?[] array) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + return Unsafe.As(ref array); + } } \ No newline at end of file diff --git a/QWERTYkez.Mensura/Extensions/CollectionsDivideExtensions.cs b/QWERTYkez.Mensura/Extensions/CollectionsDivideExtensions.cs index 2284e03..f7778d1 100644 --- a/QWERTYkez.Mensura/Extensions/CollectionsDivideExtensions.cs +++ b/QWERTYkez.Mensura/Extensions/CollectionsDivideExtensions.cs @@ -164,6 +164,378 @@ public static partial class CollectionsDivideExtensions + // === Array === + internal static R[] Divide(this T[] units, double divisor) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return null!; + int len = units.Length; + if (len == 0) return []; + + var result = new R[len]; + units.DivideCore(divisor, len, result); + return result; + } + internal static R?[] Divide(this T?[] units, double divisor) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return null!; + int len = units.Length; + if (len == 0) return []; + + var result = new R?[len]; + units.DivideCore(divisor, len, result); + return result; + } + internal static R[] Divide(this double dividend, T[] units) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return null!; + int len = units.Length; + if (len == 0) return []; + + var result = new R[len]; + dividend.DivideCore(units, len, result); + return result; + } + internal static R?[] Divide(this double dividend, T?[] units) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return null!; + int len = units.Length; + if (len == 0) return []; + + var result = new R?[len]; + dividend.DivideCore(units, len, result); + return result; + } + + // === List === + internal static List Divide(this List units, double divisor) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return null!; + int count = units.Count; + if (count == 0) return []; + + var resultArray = new R[count]; + DivideCore(CollectionsMarshal.AsSpan(units), divisor, count, resultArray); + return resultArray.WrapAsList(); + } + internal static List Divide(this List units, double divisor) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return null!; + int count = units.Count; + if (count == 0) return []; + + var resultArray = new R?[count]; + DivideCore(CollectionsMarshal.AsSpan(units), divisor, count, resultArray); + return resultArray.WrapAsList(); + } + internal static List Divide(this double dividend, List units) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return null!; + int count = units.Count; + if (count == 0) return []; + + var resultArray = new R[count]; + DivideCore(dividend, CollectionsMarshal.AsSpan(units), count, resultArray); + return resultArray.WrapAsList(); + } + internal static List Divide(this double dividend, List units) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return null!; + int count = units.Count; + if (count == 0) return []; + + var resultArray = new R?[count]; + DivideCore(dividend, CollectionsMarshal.AsSpan(units), count, resultArray); + return resultArray.WrapAsList(); + } + + // === ICollection === + internal static void Divide(this ICollection units, double divisor, Span destination) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return; + int count = units.Count; + if (count == 0) return; + if (destination.Length < count) + throw new ArgumentException("Destination too short"); + + if (units is T[] array) { DivideCore(array, divisor, count, destination); return; } + if (units is List list) { CollectionsMarshal.AsSpan(list).DivideCore(divisor, count, destination); return; } + + int i = 0; + double invDivisor = 1.0 / divisor; + foreach (var item in units) + destination[i++] = (item.ToDouble() * invDivisor).ToUnit(); + } + internal static void Divide(this ICollection units, double divisor, Span destination) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return; + int count = units.Count; + if (count == 0) return; + if (destination.Length < count) + throw new ArgumentException("Destination too short"); + + if (units is T?[] array) { array.DivideCore(divisor, count, destination); return; } + if (units is List list) { CollectionsMarshal.AsSpan(list).DivideCore(divisor, count, destination); return; } + + int i = 0; + double invDivisor = 1.0 / divisor; + foreach (var item in units) + destination[i++] = item.HasValue + ? (item.Value.ToDouble() * invDivisor).ToUnit() : null; + } + internal static void Divide(this double dividend, ICollection units, Span destination) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return; + int count = units.Count; + if (count == 0) return; + if (destination.Length < count) + throw new ArgumentException("Destination too short"); + + if (units is T[] array) { dividend.DivideCore(array, count, destination); return; } + if (units is List list) { dividend.DivideCore(CollectionsMarshal.AsSpan(list), count, destination); return; } + + int i = 0; + foreach (var item in units) + destination[i++] = (dividend / item.ToDouble()).ToUnit(); + } + internal static void Divide(this double dividend, ICollection units, Span destination) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return; + int count = units.Count; + if (count == 0) return; + if (destination.Length < count) + throw new ArgumentException("Destination too short"); + + if (units is T?[] array) { dividend.DivideCore(array, count, destination); return; } + if (units is List list) { dividend.DivideCore(CollectionsMarshal.AsSpan(list), count, destination); return; } + + int i = 0; + foreach (var item in units) + destination[i++] = item.HasValue + ? (dividend / item.Value.ToDouble()).ToUnit() : null; + } + + // === IReadOnlyCollection === + internal static void Divide(this IReadOnlyCollection units, double divisor, Span destination) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return; + int count = units.Count; + if (count == 0) return; + if (destination.Length < count) + throw new ArgumentException("Destination too short"); + + if (units is T[] array) { array.DivideCore(divisor, count, destination); return; } + if (units is List list) { CollectionsMarshal.AsSpan(list).DivideCore(divisor, count, destination); return; } + + int i = 0; + double invDivisor = 1.0 / divisor; + foreach (var item in units) + destination[i++] = (item.ToDouble() * invDivisor).ToUnit(); + } + internal static void Divide(this IReadOnlyCollection units, double divisor, Span destination) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return; + int count = units.Count; + if (count == 0) return; + if (destination.Length < count) + throw new ArgumentException("Destination too short"); + + if (units is T?[] array) { array.DivideCore(divisor, count, destination); return; } + if (units is List list) { CollectionsMarshal.AsSpan(list).DivideCore(divisor, count, destination); return; } + + int i = 0; + double invDivisor = 1.0 / divisor; + foreach (var item in units) + destination[i++] = item.HasValue + ? (item.Value.ToDouble() * invDivisor).ToUnit() : null; + } + internal static void Divide(this double dividend, IReadOnlyCollection units, Span destination) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return; + int count = units.Count; + if (count == 0) return; + if (destination.Length < count) + throw new ArgumentException("Destination too short"); + + if (units is T[] array) { dividend.DivideCore(array, count, destination); return; } + if (units is List list) { dividend.DivideCore(CollectionsMarshal.AsSpan(list), count, destination); return; } + + int i = 0; + foreach (var item in units) + destination[i++] = (dividend / item.ToDouble()).ToUnit(); + } + internal static void Divide(this double dividend, IReadOnlyCollection units, Span destination) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return; + int count = units.Count; + if (count == 0) return; + if (destination.Length < count) + throw new ArgumentException("Destination too short"); + + if (units is T?[] array) { dividend.DivideCore(array, count, destination); return; } + if (units is List list) { dividend.DivideCore(CollectionsMarshal.AsSpan(list), count, destination); return; } + + int i = 0; + foreach (var item in units) + destination[i++] = item.HasValue + ? (dividend / item.Value.ToDouble()).ToUnit() : null; + } + + // === IEnumerable + yeild === + static IEnumerable DivideIterator(IEnumerable units, double divisor) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + double invDivisor = 1.0 / divisor; + foreach (var item in units) + yield return (item.ToDouble() * invDivisor).ToUnit(); + } + static IEnumerable DivideNullableIterator(IEnumerable units, double divisor) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + double invDivisor = 1.0 / divisor; + foreach (T? item in units) + yield return item.HasValue + ? (item.Value.ToDouble() * invDivisor).ToUnit() : null; + } + static IEnumerable DivideIterator(double dividend, IEnumerable units) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + foreach (var item in units) + yield return (dividend / item.ToDouble()).ToUnit(); + } + static IEnumerable DivideNullableIterator(double dividend, IEnumerable units) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + foreach (T? item in units) + yield return item.HasValue + ? (dividend / item.Value.ToDouble()).ToUnit() : null; + } + + // === IEnumerable === + internal static IEnumerable Divide(this IEnumerable units, double divisor) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return null!; + if (units is T[] array) return array.Divide(divisor); + if (units is List list) return list.Divide(divisor); + if (units is ICollection col) + { + var arr = col.ToArray(); + arr.Divide(divisor, arr); + return arr.ReCast(); + } + if (units is IReadOnlyCollection roc) + { + var arr = roc.ToArray(); + arr.Divide(divisor, arr); + return arr.ReCast(); + } + return DivideIterator(units, divisor); + } + internal static IEnumerable Divide(this IEnumerable units, double divisor) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return null!; + if (units is T?[] array) return array.Divide(divisor); + if (units is List list) return list.Divide(divisor); + if (units is ICollection col) + { + var arr = col.ToArray(); + arr.Divide(divisor, arr); + return arr.ReCast(); + } + if (units is IReadOnlyCollection roc) + { + var arr = roc.ToArray(); + arr.Divide(divisor, arr); + return arr.ReCast(); + } + return DivideNullableIterator(units, divisor); + } + internal static IEnumerable Divide(this double dividend, IEnumerable units) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return null!; + if (units is T[] array) return dividend.Divide(array); + if (units is List list) return dividend.Divide(list); + if (units is ICollection col) + { + var arr = col.ToArray(); + dividend.Divide(arr, arr); + return arr.ReCast(); + } + if (units is IReadOnlyCollection roc) + { + var arr = roc.ToArray(); + dividend.Divide(arr, arr); + return arr.ReCast(); + } + return DivideIterator(dividend, units); + } + internal static IEnumerable Divide(this double dividend, IEnumerable units) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return null!; + if (units is T?[] array) return dividend.Divide(array); + if (units is List list) return dividend.Divide(list); + if (units is ICollection col) + { + var arr = col.ToArray(); + dividend.Divide(arr, arr); + return arr.ReCast(); + } + if (units is IReadOnlyCollection roc) + { + var arr = roc.ToArray(); + dividend.Divide(arr, arr); + return arr.ReCast(); + } + return DivideNullableIterator(dividend, units); + } + + + + + // === ReadOnlySpan public static void Divide(this ReadOnlySpan units, double divisor, Span destination) where T : struct, IMensuraUnit, IEquatable @@ -477,14 +849,14 @@ public static partial class CollectionsDivideExtensions if (units is List list) return list.Divide(divisor); if (units is ICollection col) { - var arr = new T[col.Count]; - col.Divide(divisor, arr); + var arr = col.ToArray(); + arr.Divide(divisor, arr); return arr; } if (units is IReadOnlyCollection roc) { - var arr = new T[roc.Count]; - roc.Divide(divisor, arr); + var arr = roc.ToArray(); + arr.Divide(divisor, arr); return arr; } return DivideIterator(units, divisor); @@ -497,14 +869,14 @@ public static partial class CollectionsDivideExtensions if (units is List list) return list.Divide(divisor); if (units is ICollection col) { - var arr = new T?[col.Count]; - col.Divide(divisor, arr); + var arr = col.ToArray(); + arr.Divide(divisor, arr); return arr; } if (units is IReadOnlyCollection roc) { - var arr = new T?[roc.Count]; - roc.Divide(divisor, arr); + var arr = roc.ToArray(); + arr.Divide(divisor, arr); return arr; } return DivideNullableIterator(units, divisor); @@ -517,14 +889,14 @@ public static partial class CollectionsDivideExtensions if (units is List list) return dividend.Divide(list); if (units is ICollection col) { - var arr = new T[col.Count]; - dividend.Divide(col, arr); + var arr = col.ToArray(); + dividend.Divide(arr, arr); return arr; } if (units is IReadOnlyCollection roc) { - var arr = new T[roc.Count]; - dividend.Divide(roc, arr); + var arr = roc.ToArray(); + dividend.Divide(arr, arr); return arr; } return DivideIterator(dividend, units); @@ -537,14 +909,14 @@ public static partial class CollectionsDivideExtensions if (units is List list) return dividend.Divide(list); if (units is ICollection col) { - var arr = new T?[col.Count]; - dividend.Divide(col, arr); + var arr = col.ToArray(); + dividend.Divide(arr, arr); return arr; } if (units is IReadOnlyCollection roc) { - var arr = new T?[roc.Count]; - dividend.Divide(roc, arr); + var arr = roc.ToArray(); + dividend.Divide(arr, arr); return arr; } return DivideNullableIterator(dividend, units); diff --git a/QWERTYkez.Mensura/Extensions/CollectionsMinusExtensions.cs b/QWERTYkez.Mensura/Extensions/CollectionsMinusExtensions.cs index 5099295..6864bda 100644 --- a/QWERTYkez.Mensura/Extensions/CollectionsMinusExtensions.cs +++ b/QWERTYkez.Mensura/Extensions/CollectionsMinusExtensions.cs @@ -159,6 +159,372 @@ public static partial class CollectionsMinusExtensions + // === Array === + internal static R[] Minus(this T[] units, double subtrahend) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return null!; + int len = units.Length; + if (len == 0) return []; + + var result = new R[len]; + MinusCore(units, subtrahend, len, result); + return result; + } + internal static R?[] Minus(this T?[] units, double subtrahend) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return null!; + int len = units.Length; + if (len == 0) return []; + + var result = new R?[len]; + MinusCore(units, subtrahend, len, result); + return result; + } + internal static R[] Minus(this double minuend, T[] units) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return null!; + int len = units.Length; + if (len == 0) return []; + + var result = new R[len]; + MinusCore(minuend, units, len, result); + return result; + } + internal static R?[] Minus(this double minuend, T?[] units) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return null!; + int len = units.Length; + if (len == 0) return []; + + var result = new R?[len]; + MinusCore(minuend, units, len, result); + return result; + } + + // === List === + internal static List Minus(this List units, double subtrahend) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return null!; + int count = units.Count; + if (count == 0) return []; + + var resultArray = new R[count]; + MinusCore(CollectionsMarshal.AsSpan(units), subtrahend, count, resultArray); + return resultArray.WrapAsList(); + } + internal static List Minus(this List units, double subtrahend) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return null!; + int count = units.Count; + if (count == 0) return []; + + var resultArray = new R?[count]; + MinusCore(CollectionsMarshal.AsSpan(units), subtrahend, count, resultArray); + return resultArray.WrapAsList(); + } + internal static List Minus(this double minuend, List units) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return null!; + int count = units.Count; + if (count == 0) return []; + + var resultArray = new R[count]; + MinusCore(minuend, CollectionsMarshal.AsSpan(units), count, resultArray); + return resultArray.WrapAsList(); + } + internal static List Minus(this double minuend, List units) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return null!; + int count = units.Count; + if (count == 0) return []; + + var resultArray = new R?[count]; + MinusCore(minuend, CollectionsMarshal.AsSpan(units), count, resultArray); + return resultArray.WrapAsList(); + } + + // === ICollection === + internal static void Minus(this ICollection units, double subtrahend, Span destination) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return; + int count = units.Count; + if (count == 0) return; + if (destination.Length < count) + throw new ArgumentException("Destination too short"); + + if (units is T[] array) { array.MinusCore(subtrahend, count, destination); return; } + if (units is List list) { CollectionsMarshal.AsSpan(list).MinusCore(subtrahend, count, destination); return; } + + int i = 0; + foreach (var item in units) + destination[i++] = (item.ToDouble() - subtrahend).ToUnit(); + } + internal static void Minus(this ICollection units, double subtrahend, Span destination) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return; + int count = units.Count; + if (count == 0) return; + if (destination.Length < count) + throw new ArgumentException("Destination too short"); + + if (units is T?[] array) { array.MinusCore(subtrahend, count, destination); return; } + if (units is List list) { CollectionsMarshal.AsSpan(list).MinusCore(subtrahend, count, destination); return; } + + int i = 0; + foreach (var item in units) + destination[i++] = item.HasValue + ? (item.Value.ToDouble() - subtrahend).ToUnit() : null; + } + internal static void Minus(this double minuend, ICollection units, Span destination) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return; + int count = units.Count; + if (count == 0) return; + if (destination.Length < count) + throw new ArgumentException("Destination too short"); + + if (units is T[] array) { minuend.MinusCore(array, count, destination); return; } + if (units is List list) { minuend.MinusCore(CollectionsMarshal.AsSpan(list), count, destination); return; } + + int i = 0; + foreach (var item in units) + destination[i++] = (minuend - item.ToDouble()).ToUnit(); + } + internal static void Minus(this double minuend, ICollection units, Span destination) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return; + int count = units.Count; + if (count == 0) return; + if (destination.Length < count) + throw new ArgumentException("Destination too short"); + + if (units is T?[] array) { minuend.MinusCore(array, count, destination); return; } + if (units is List list) { minuend.MinusCore(CollectionsMarshal.AsSpan(list), count, destination); return; } + + int i = 0; + foreach (var item in units) + destination[i++] = item.HasValue + ? (minuend - item.Value.ToDouble()).ToUnit() : null; + } + + // === IReadOnlyCollection === + internal static void Minus(this IReadOnlyCollection units, double subtrahend, Span destination) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return; + int count = units.Count; + if (count == 0) return; + if (destination.Length < count) + throw new ArgumentException("Destination too short"); + + if (units is T[] array) { array.MinusCore(subtrahend, count, destination); return; } + if (units is List list) { CollectionsMarshal.AsSpan(list).MinusCore(subtrahend, count, destination); return; } + + int i = 0; + foreach (var item in units) + destination[i++] = (item.ToDouble() - subtrahend).ToUnit(); + } + internal static void Minus(this IReadOnlyCollection units, double subtrahend, Span destination) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return; + int count = units.Count; + if (count == 0) return; + if (destination.Length < count) + throw new ArgumentException("Destination too short"); + + if (units is T?[] array) { array.MinusCore(subtrahend, count, destination); return; } + if (units is List list) { CollectionsMarshal.AsSpan(list).MinusCore(subtrahend, count, destination); return; } + + int i = 0; + foreach (var item in units) + destination[i++] = item.HasValue + ? (item.Value.ToDouble() - subtrahend).ToUnit() : null; + } + internal static void Minus(this double minuend, IReadOnlyCollection units, Span destination) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return; + int count = units.Count; + if (count == 0) return; + if (destination.Length < count) + throw new ArgumentException("Destination too short"); + + if (units is T[] array) { minuend.MinusCore(array, count, destination); return; } + if (units is List list) { minuend.MinusCore(CollectionsMarshal.AsSpan(list), count, destination); return; } + + int i = 0; + foreach (var item in units) + destination[i++] = (minuend - item.ToDouble()).ToUnit(); + } + internal static void Minus(this double minuend, IReadOnlyCollection units, Span destination) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return; + int count = units.Count; + if (count == 0) return; + if (destination.Length < count) + throw new ArgumentException("Destination too short"); + + if (units is T?[] array) { minuend.MinusCore(array, count, destination); return; } + if (units is List list) { minuend.MinusCore(CollectionsMarshal.AsSpan(list), count, destination); return; } + + int i = 0; + foreach (var item in units) + destination[i++] = item.HasValue + ? (minuend - item.Value.ToDouble()).ToUnit() : null; + } + + // === IEnumerable + yeild === + static IEnumerable MinusIterator(IEnumerable units, double subtrahend) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + foreach (var item in units) + yield return (item.ToDouble() - subtrahend).ToUnit(); + } + static IEnumerable MinusNullableIterator(IEnumerable units, double subtrahend) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + foreach (T? item in units) + yield return item.HasValue + ? (item.Value.ToDouble() - subtrahend).ToUnit() : null; + } + static IEnumerable MinusIterator(double minuend, IEnumerable units) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + foreach (var item in units) + yield return (minuend - item.ToDouble()).ToUnit(); + } + static IEnumerable MinusNullableIterator(double minuend, IEnumerable units) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + foreach (T? item in units) + yield return item.HasValue + ? (minuend - item.Value.ToDouble()).ToUnit() : null; + } + + // === IEnumerable === + internal static IEnumerable Minus(this IEnumerable units, double subtrahend) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return null!; + if (units is T[] array) return array.Minus(subtrahend); + if (units is List list) return list.Minus(subtrahend); + if (units is ICollection col) + { + var arr = col.ToArray(); + arr.Minus(subtrahend, arr); + return arr.ReCast(); + } + if (units is IReadOnlyCollection roc) + { + var arr = roc.ToArray(); + arr.Minus(subtrahend, arr); + return arr.ReCast(); + } + return MinusIterator(units, subtrahend); + } + internal static IEnumerable Minus(this IEnumerable units, double subtrahend) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return null!; + if (units is T?[] array) return array.Minus(subtrahend); + if (units is List list) return list.Minus(subtrahend); + if (units is ICollection col) + { + var arr = col.ToArray(); + arr.Minus(subtrahend, arr); + return arr.ReCast(); + } + if (units is IReadOnlyCollection roc) + { + var arr = roc.ToArray(); + arr.Minus(subtrahend, arr); + return arr.ReCast(); + } + return MinusNullableIterator(units, subtrahend); + } + internal static IEnumerable Minus(this double minuend, IEnumerable units) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return null!; + if (units is T[] array) return minuend.Minus(array); + if (units is List list) return minuend.Minus(list); + if (units is ICollection col) + { + var arr = col.ToArray(); + minuend.Minus(arr, arr); + return arr.ReCast(); + } + if (units is IReadOnlyCollection roc) + { + var arr = roc.ToArray(); + minuend.Minus(arr, arr); + return arr.ReCast(); + } + return MinusIterator(minuend, units); + } + internal static IEnumerable Minus(this double minuend, IEnumerable units) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return null!; + if (units is T?[] array) return minuend.Minus(array); + if (units is List list) return minuend.Minus(list); + if (units is ICollection col) + { + var arr = col.ToArray(); + minuend.Minus(arr, arr); + return arr.ReCast(); + } + if (units is IReadOnlyCollection roc) + { + var arr = roc.ToArray(); + minuend.Minus(arr, arr); + return arr.ReCast(); + } + return MinusNullableIterator(minuend, units); + } + + + + + // === ReadOnlySpan public static void Minus(this ReadOnlySpan units, double subtrahend, Span destination) where T : struct, IMensuraUnit, IEquatable @@ -467,14 +833,14 @@ public static partial class CollectionsMinusExtensions if (units is List list) return list.Minus(subtrahend); if (units is ICollection col) { - var arr = new T[col.Count]; - col.Minus(subtrahend, arr); + var arr = col.ToArray(); + arr.Minus(subtrahend, arr); return arr; } if (units is IReadOnlyCollection roc) { - var arr = new T[roc.Count]; - roc.Minus(subtrahend, arr); + var arr = roc.ToArray(); + arr.Minus(subtrahend, arr); return arr; } return MinusIterator(units, subtrahend); @@ -487,14 +853,14 @@ public static partial class CollectionsMinusExtensions if (units is List list) return list.Minus(subtrahend); if (units is ICollection col) { - var arr = new T?[col.Count]; - col.Minus(subtrahend, arr); + var arr = col.ToArray(); + arr.Minus(subtrahend, arr); return arr; } if (units is IReadOnlyCollection roc) { - var arr = new T?[roc.Count]; - roc.Minus(subtrahend, arr); + var arr = roc.ToArray(); + arr.Minus(subtrahend, arr); return arr; } return MinusNullableIterator(units, subtrahend); @@ -507,14 +873,14 @@ public static partial class CollectionsMinusExtensions if (units is List list) return minuend.Minus(list); if (units is ICollection col) { - var arr = new T[col.Count]; - minuend.Minus(col, arr); + var arr = col.ToArray(); + minuend.Minus(arr, arr); return arr; } if (units is IReadOnlyCollection roc) { - var arr = new T[roc.Count]; - minuend.Minus(roc, arr); + var arr = roc.ToArray(); + minuend.Minus(arr, arr); return arr; } return MinusIterator(minuend, units); @@ -527,14 +893,14 @@ public static partial class CollectionsMinusExtensions if (units is List list) return minuend.Minus(list); if (units is ICollection col) { - var arr = new T?[col.Count]; - minuend.Minus(col, arr); + var arr = col.ToArray(); + minuend.Minus(arr, arr); return arr; } if (units is IReadOnlyCollection roc) { - var arr = new T?[roc.Count]; - minuend.Minus(roc, arr); + var arr = roc.ToArray(); + minuend.Minus(arr, arr); return arr; } return MinusNullableIterator(minuend, units); diff --git a/QWERTYkez.Mensura/Extensions/CollectionsMultiplyExtensions.cs b/QWERTYkez.Mensura/Extensions/CollectionsMultiplyExtensions.cs index 0c2a014..18bd241 100644 --- a/QWERTYkez.Mensura/Extensions/CollectionsMultiplyExtensions.cs +++ b/QWERTYkez.Mensura/Extensions/CollectionsMultiplyExtensions.cs @@ -90,6 +90,246 @@ public static partial class CollectionsMultiplyExtensions + // === Array === + internal static R[] Multiply(this T[] units, double multiplicator) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return null!; + int len = units.Length; + if (len == 0) return []; + + var result = new R[len]; + units.MultiplyCore(multiplicator, len, result); + return result; + } + internal static R?[] Multiply(this T?[] units, double multiplicator) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return null!; + int len = units.Length; + if (len == 0) return []; + + var result = new R?[len]; + units.MultiplyCore(multiplicator, len, result); + return result; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static R[] Multiply(this double multiplicator, T[] units) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable => units.Multiply(multiplicator); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static R?[] Multiply(this double multiplicator, T?[] units) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable => units.Multiply(multiplicator); + + // === List === + internal static List Multiply(this List units, double multiplicator) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return null!; + int count = units.Count; + if (count == 0) return []; + + var resultArray = new R[count]; + MultiplyCore(CollectionsMarshal.AsSpan(units), multiplicator, count, resultArray); + return resultArray.WrapAsList(); + } + internal static List Multiply(this List units, double multiplicator) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return null!; + int count = units.Count; + if (count == 0) return []; + + var resultArray = new R?[count]; + MultiplyCore(CollectionsMarshal.AsSpan(units), multiplicator, count, resultArray); + return resultArray.WrapAsList(); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static List Multiply(this double multiplicator, List units) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable => units.Multiply(multiplicator); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static List Multiply(this double multiplicator, List units) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable => units.Multiply(multiplicator); + + // === ICollection === + internal static void Multiply(this ICollection units, double multiplicator, Span destination) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return; + int count = units.Count; + if (count == 0) return; + if (destination.Length < count) + throw new ArgumentException("Destination too short"); + + if (units is T[] array) { MultiplyCore(array, multiplicator, count, destination); return; } + if (units is List list) { MultiplyCore(CollectionsMarshal.AsSpan(list), multiplicator, count, destination); return; } + + int i = 0; + foreach (var item in units) + destination[i++] = (item.ToDouble() * multiplicator).ToUnit(); + } + internal static void Multiply(this ICollection units, double multiplicator, Span destination) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return; + int count = units.Count; + if (count == 0) return; + if (destination.Length < count) + throw new ArgumentException("Destination too short"); + + if (units is T?[] array) { MultiplyCore(array, multiplicator, count, destination); return; } + if (units is List list) { MultiplyCore(CollectionsMarshal.AsSpan(list), multiplicator, count, destination); return; } + + int i = 0; + foreach (var item in units) + destination[i++] = item.HasValue + ? (item.Value.ToDouble() * multiplicator).ToUnit() : null; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static void Multiply(this double multiplicator, ICollection units, Span destination) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable => units.Multiply(multiplicator, destination); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static void Multiply(this double multiplicator, ICollection units, Span destination) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable => units.Multiply(multiplicator, destination); + + // === IReadOnlyCollection === + internal static void Multiply(this IReadOnlyCollection units, double multiplicator, Span destination) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return; + int count = units.Count; + if (count == 0) return; + if (destination.Length < count) + throw new ArgumentException("Destination too short"); + + if (units is T[] array) { MultiplyCore(array, multiplicator, count, destination); return; } + if (units is List list) { MultiplyCore(CollectionsMarshal.AsSpan(list), multiplicator, count, destination); return; } + + int i = 0; + foreach (var item in units) + destination[i++] = (item.ToDouble() * multiplicator).ToUnit(); + } + internal static void Multiply(this IReadOnlyCollection units, double multiplicator, Span destination) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return; + int count = units.Count; + if (count == 0) return; + if (destination.Length < count) + throw new ArgumentException("Destination too short"); + + if (units is T?[] array) { MultiplyCore(array, multiplicator, count, destination); return; } + if (units is List list) { MultiplyCore(CollectionsMarshal.AsSpan(list), multiplicator, count, destination); return; } + + int i = 0; + foreach (var item in units) + destination[i++] = item.HasValue + ? (item.Value.ToDouble() * multiplicator).ToUnit() : null; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static void Multiply(this double multiplicator, IReadOnlyCollection units, Span destination) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable => units.Multiply(multiplicator, destination); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static void Multiply(this double multiplicator, IReadOnlyCollection units, Span destination) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable => units.Multiply(multiplicator, destination); + + // === IEnumerable + yeild === + internal static IEnumerable MultiplyIterator(this IEnumerable units, double multiplicator) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + foreach (var item in units) + yield return (item.ToDouble() * multiplicator).ToUnit(); + } + internal static IEnumerable MultiplyNullableIterator(this IEnumerable units, double multiplicator) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + foreach (T? item in units) + yield return item.HasValue + ? (item.Value.ToDouble() * multiplicator).ToUnit() : null; + } + + // === IEnumerable === + internal static IEnumerable Multiply(this IEnumerable units, double multiplicator) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return null!; + if (units is T[] array) return array.Multiply(multiplicator); + if (units is List list) return list.Multiply(multiplicator); + if (units is ICollection col) + { + var arr = col.ToArray(); + arr.Multiply(multiplicator, arr); + return arr.ReCast(); + } + if (units is IReadOnlyCollection roc) + { + var arr = roc.ToArray(); + arr.Multiply(multiplicator, arr); + return arr.ReCast(); + } + return MultiplyIterator(units, multiplicator); + } + internal static IEnumerable Multiply(this IEnumerable units, double multiplicator) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return null!; + if (units is T?[] array) return array.Multiply(multiplicator); + if (units is List list) return list.Multiply(multiplicator); + if (units is ICollection col) + { + var arr = col.ToArray(); + arr.Multiply(multiplicator, arr); + return arr.ReCast(); + } + if (units is IReadOnlyCollection roc) + { + var arr = roc.ToArray(); + arr.Multiply(multiplicator, arr); + return arr.ReCast(); + } + return MultiplyNullableIterator(units, multiplicator); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static IEnumerable Multiply(this double multiplicator, IEnumerable units) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable => Multiply(units, multiplicator); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static IEnumerable Multiply(this double multiplicator, IEnumerable units) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable => Multiply(units, multiplicator); + + + + // === ReadOnlySpan public static void Multiply(this ReadOnlySpan units, double multiplicator, Span destination) where T : struct, IMensuraUnit, IEquatable @@ -221,11 +461,11 @@ public static partial class CollectionsMultiplyExtensions [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Multiply(this double multiplicator, ICollection units, Span destination) - where T : struct, IMensuraUnit, IEquatable => units.Multiply(multiplicator, destination); + where T : struct, IMensuraUnit, IEquatable => units.Multiply(multiplicator, destination); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Multiply(this double multiplicator, ICollection units, Span destination) - where T : struct, IMensuraUnit, IEquatable => units.Multiply(multiplicator, destination); + where T : struct, IMensuraUnit, IEquatable => units.Multiply(multiplicator, destination); // === IReadOnlyCollection === public static void Multiply(this IReadOnlyCollection units, double multiplicator, Span destination) @@ -264,20 +504,20 @@ public static partial class CollectionsMultiplyExtensions [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Multiply(this double multiplicator, IReadOnlyCollection units, Span destination) - where T : struct, IMensuraUnit, IEquatable => units.Multiply(multiplicator, destination); + where T : struct, IMensuraUnit, IEquatable => units.Multiply(multiplicator, destination); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Multiply(this double multiplicator, IReadOnlyCollection units, Span destination) - where T : struct, IMensuraUnit, IEquatable => units.Multiply(multiplicator, destination); + where T : struct, IMensuraUnit, IEquatable => units.Multiply(multiplicator, destination); // === IEnumerable + yeild === - static IEnumerable MultiplyIterator(IEnumerable units, double multiplicator) + internal static IEnumerable MultiplyIterator(this IEnumerable units, double multiplicator) where T : struct, IMensuraUnit, IEquatable { foreach (var item in units) yield return (item.ToDouble() * multiplicator).ToUnit(); } - static IEnumerable MultiplyNullableIterator(IEnumerable units, double multiplicator) + internal static IEnumerable MultiplyNullableIterator(this IEnumerable units, double multiplicator) where T : struct, IMensuraUnit, IEquatable { foreach (T? item in units) @@ -294,14 +534,14 @@ public static partial class CollectionsMultiplyExtensions if (units is List list) return list.Multiply(multiplicator); if (units is ICollection col) { - var arr = new T[col.Count]; - col.Multiply(multiplicator, arr); + var arr = col.ToArray(); + arr.Multiply(multiplicator, arr); return arr; } if (units is IReadOnlyCollection roc) { - var arr = new T[roc.Count]; - roc.Multiply(multiplicator, arr); + var arr = roc.ToArray(); + arr.Multiply(multiplicator, arr); return arr; } return MultiplyIterator(units, multiplicator); @@ -314,14 +554,14 @@ public static partial class CollectionsMultiplyExtensions if (units is List list) return list.Multiply(multiplicator); if (units is ICollection col) { - var arr = new T?[col.Count]; - col.Multiply(multiplicator, arr); + var arr = col.ToArray(); + arr.Multiply(multiplicator, arr); return arr; } if (units is IReadOnlyCollection roc) { - var arr = new T?[roc.Count]; - roc.Multiply(multiplicator, arr); + var arr = roc.ToArray(); + arr.Multiply(multiplicator, arr); return arr; } return MultiplyNullableIterator(units, multiplicator); diff --git a/QWERTYkez.Mensura/Extensions/CollectionsPlusExtensions.cs b/QWERTYkez.Mensura/Extensions/CollectionsPlusExtensions.cs index c050416..bc4262c 100644 --- a/QWERTYkez.Mensura/Extensions/CollectionsPlusExtensions.cs +++ b/QWERTYkez.Mensura/Extensions/CollectionsPlusExtensions.cs @@ -91,6 +91,246 @@ public static partial class CollectionsPlusExtensions + // === Array === + internal static R[] Plus(this T[] units, double summand) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return null!; + int len = units.Length; + if (len == 0) return []; + + var result = new R[len]; + units.PlusCore(summand, len, result); + return result; + } + internal static R?[] Plus(this T?[] units, double summand) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return null!; + int len = units.Length; + if (len == 0) return []; + + var result = new R?[len]; + units.PlusCore(summand, len, result); + return result; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static R[] Plus(this double summand, T[] units) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable => units.Plus(summand); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static R?[] Plus(this double summand, T?[] units) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable => units.Plus(summand); + + // === List === + internal static List Plus(this List units, double summand) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return null!; + int count = units.Count; + if (count == 0) return []; + + var resultArray = new R[count]; + PlusCore(CollectionsMarshal.AsSpan(units), summand, count, resultArray); + return resultArray.WrapAsList(); + } + internal static List Plus(this List units, double summand) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return null!; + int count = units.Count; + if (count == 0) return []; + + var resultArray = new R?[count]; + PlusCore(CollectionsMarshal.AsSpan(units), summand, count, resultArray); + return resultArray.WrapAsList(); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static List Plus(this double summand, List units) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable => units.Plus(summand); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static List Plus(this double summand, List units) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable => units.Plus(summand); + + // === ICollection === + internal static void Plus(this ICollection units, double summand, Span destination) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return; + int count = units.Count; + if (count == 0) return; + if (destination.Length < count) + throw new ArgumentException("Destination too short"); + + if (units is T[] array) { PlusCore(array, summand, count, destination); return; } + if (units is List list) { PlusCore(CollectionsMarshal.AsSpan(list), summand, count, destination); return; } + + int i = 0; + foreach (var item in units) + destination[i++] = (item.ToDouble() + summand).ToUnit(); + } + internal static void Plus(this ICollection units, double summand, Span destination) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return; + int count = units.Count; + if (count == 0) return; + if (destination.Length < count) + throw new ArgumentException("Destination too short"); + + if (units is T?[] array) { PlusCore(array, summand, count, destination); return; } + if (units is List list) { PlusCore(CollectionsMarshal.AsSpan(list), summand, count, destination); return; } + + int i = 0; + foreach (var item in units) + destination[i++] = item.HasValue + ? (item.Value.ToDouble() + summand).ToUnit() : null; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static void Plus(this double summand, ICollection units, Span destination) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable => units.Plus(summand, destination); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static void Plus(this double summand, ICollection units, Span destination) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable => units.Plus(summand, destination); + + // === IReadOnlyCollection === + internal static void Plus(this IReadOnlyCollection units, double summand, Span destination) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return; + int count = units.Count; + if (count == 0) return; + if (destination.Length < count) + throw new ArgumentException("Destination too short"); + + if (units is T[] array) { PlusCore(array, summand, count, destination); return; } + if (units is List list) { PlusCore(CollectionsMarshal.AsSpan(list), summand, count, destination); return; } + + int i = 0; + foreach (var item in units) + destination[i++] = (item.ToDouble() + summand).ToUnit(); + } + internal static void Plus(this IReadOnlyCollection units, double summand, Span destination) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return; + int count = units.Count; + if (count == 0) return; + if (destination.Length < count) + throw new ArgumentException("Destination too short"); + + if (units is T?[] array) { PlusCore(array, summand, count, destination); return; } + if (units is List list) { PlusCore(CollectionsMarshal.AsSpan(list), summand, count, destination); return; } + + int i = 0; + foreach (var item in units) + destination[i++] = item.HasValue + ? (item.Value.ToDouble() + summand).ToUnit() : null; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static void Plus(this double summand, IReadOnlyCollection units, Span destination) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable => units.Plus(summand, destination); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static void Plus(this double summand, IReadOnlyCollection units, Span destination) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable => units.Plus(summand, destination); + + // === IEnumerable + yeild === + static IEnumerable PlusIterator(IEnumerable units, double summand) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + foreach (var item in units) + yield return (item.ToDouble() + summand).ToUnit(); + } + static IEnumerable PlusNullableIterator(IEnumerable units, double summand) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + foreach (T? item in units) + yield return item.HasValue + ? (item.Value.ToDouble() + summand).ToUnit() : null; + } + + // === IEnumerable === + internal static IEnumerable Plus(this IEnumerable units, double summand) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return null!; + if (units is T[] array) return array.Plus(summand); + if (units is List list) return list.Plus(summand); + if (units is ICollection col) + { + var arr = col.ToArray(); + arr.Plus(summand, arr); + return arr.ReCast(); + } + if (units is IReadOnlyCollection roc) + { + var arr = roc.ToArray(); + arr.Plus(summand, arr); + return arr.ReCast(); + } + return PlusIterator(units, summand); + } + internal static IEnumerable Plus(this IEnumerable units, double summand) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable + { + if (units is null) return null!; + if (units is T?[] array) return array.Plus(summand); + if (units is List list) return list.Plus(summand); + if (units is ICollection col) + { + var arr = col.ToArray(); + arr.Plus(summand, arr); + return arr.ReCast(); + } + if (units is IReadOnlyCollection roc) + { + var arr = roc.ToArray(); + arr.Plus(summand, arr); + return arr.ReCast(); + } + return PlusNullableIterator(units, summand); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static IEnumerable Plus(this double summand, IEnumerable units) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable => Plus(units, summand); + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static IEnumerable Plus(this double summand, IEnumerable units) + where T : struct, IMensuraUnit, IEquatable + where R : struct, IMensuraUnit, IEquatable => Plus(units, summand); + + + + // === ReadOnlySpan public static void Plus(this ReadOnlySpan units, double multiplicator, Span destination) where T : struct, IMensuraUnit, IEquatable @@ -222,11 +462,11 @@ public static partial class CollectionsPlusExtensions [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Plus(this double summand, ICollection units, Span destination) - where T : struct, IMensuraUnit, IEquatable => units.Plus(summand, destination); + where T : struct, IMensuraUnit, IEquatable => units.Plus(summand, destination); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Plus(this double summand, ICollection units, Span destination) - where T : struct, IMensuraUnit, IEquatable => units.Plus(summand, destination); + where T : struct, IMensuraUnit, IEquatable => units.Plus(summand, destination); // === IReadOnlyCollection === public static void Plus(this IReadOnlyCollection units, double summand, Span destination) @@ -265,11 +505,11 @@ public static partial class CollectionsPlusExtensions [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Plus(this double summand, IReadOnlyCollection units, Span destination) - where T : struct, IMensuraUnit, IEquatable => units.Plus(summand, destination); + where T : struct, IMensuraUnit, IEquatable => units.Plus(summand, destination); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Plus(this double summand, IReadOnlyCollection units, Span destination) - where T : struct, IMensuraUnit, IEquatable => units.Plus(summand, destination); + where T : struct, IMensuraUnit, IEquatable => units.Plus(summand, destination); // === IEnumerable + yeild === static IEnumerable PlusIterator(IEnumerable units, double summand) @@ -295,14 +535,14 @@ public static partial class CollectionsPlusExtensions if (units is List list) return list.Plus(summand); if (units is ICollection col) { - var arr = new T[col.Count]; - col.Plus(summand, arr); + var arr = col.ToArray(); + arr.Plus(summand, arr); return arr; } if (units is IReadOnlyCollection roc) { - var arr = new T[roc.Count]; - roc.Plus(summand, arr); + var arr = roc.ToArray(); + arr.Plus(summand, arr); return arr; } return PlusIterator(units, summand); @@ -315,14 +555,14 @@ public static partial class CollectionsPlusExtensions if (units is List list) return list.Plus(summand); if (units is ICollection col) { - var arr = new T?[col.Count]; - col.Plus(summand, arr); + var arr = col.ToArray(); + arr.Plus(summand, arr); return arr; } if (units is IReadOnlyCollection roc) { - var arr = new T?[roc.Count]; - roc.Plus(summand, arr); + var arr = roc.ToArray(); + arr.Plus(summand, arr); return arr; } return PlusNullableIterator(units, summand); diff --git a/QWERTYkez.Mensura/Extensions/CollectionsPowExtensions.cs b/QWERTYkez.Mensura/Extensions/CollectionsPowExtensions.cs index 76be5aa..48f38f0 100644 --- a/QWERTYkez.Mensura/Extensions/CollectionsPowExtensions.cs +++ b/QWERTYkez.Mensura/Extensions/CollectionsPowExtensions.cs @@ -325,15 +325,15 @@ public static partial class CollectionsPowExtensions if (units is List list) return list.Pow(power); if (units is ICollection col) { - var arr = new R[col.Count]; - col.Pow(power, arr); - return arr; + var arr = col.ToArray(); + arr.PowCore(power, arr.Length, arr); + return arr.ReCast(); } if (units is IReadOnlyCollection roc) { - var arr = new R[roc.Count]; - roc.Pow(power, arr); - return arr; + var arr = roc.ToArray(); + arr.Pow(power, arr); + return arr.ReCast(); } else return PowIterator(units, power); } @@ -346,15 +346,15 @@ public static partial class CollectionsPowExtensions if (units is List list) return list.Pow(power); if (units is ICollection col) { - var arr = new R?[col.Count]; - col.Pow(power, arr); - return arr; + var arr = col.ToArray(); + arr.Pow(power, arr); + return arr.ReCast(); } if (units is IReadOnlyCollection roc) { - var arr = new R?[roc.Count]; - roc.Pow(power, arr); - return arr; + var arr = roc.ToArray(); + arr.Pow(power, arr); + return arr.ReCast(); } else return PowNullableIterator(units, power); } @@ -637,15 +637,15 @@ public static partial class CollectionsPowExtensions if (units is List list) return list.Pow(power); if (units is ICollection col) { - var arr = new R[col.Count]; - col.Pow(power, arr); - return arr; + var arr = col.ToArray(); + arr.Pow(power, arr); + return arr.ReCast(); } if (units is IReadOnlyCollection roc) { - var arr = new R[roc.Count]; - roc.Pow(power, arr); - return arr; + var arr = roc.ToArray(); + arr.Pow(power, arr); + return arr.ReCast(); } else return PowIterator(units, power); } @@ -658,15 +658,15 @@ public static partial class CollectionsPowExtensions if (units is List list) return list.Pow(power); if (units is ICollection col) { - var arr = new R?[col.Count]; - col.Pow(power, arr); - return arr; + var arr = col.ToArray(); + arr.Pow(power, arr); + return arr.ReCast(); } if (units is IReadOnlyCollection roc) { - var arr = new R?[roc.Count]; - roc.Pow(power, arr); - return arr; + var arr = roc.ToArray(); + arr.Pow(power, arr); + return arr.ReCast(); } else return PowNullableIterator(units, power); } diff --git a/QWERTYkez.Mensura/Extensions/CollectionsSqrtExtensions.cs b/QWERTYkez.Mensura/Extensions/CollectionsSqrtExtensions.cs index 8988231..816eff3 100644 --- a/QWERTYkez.Mensura/Extensions/CollectionsSqrtExtensions.cs +++ b/QWERTYkez.Mensura/Extensions/CollectionsSqrtExtensions.cs @@ -281,15 +281,15 @@ public static partial class CollectionsSqrtExtensions if (units is List list) return list.Sqrt(); if (units is ICollection col) { - var arr = new R[col.Count]; - col.Sqrt(arr); - return arr; + var arr = col.ToArray(); + arr.Sqrt(arr); + return arr.ReCast(); } if (units is IReadOnlyCollection roc) { - var arr = new R[roc.Count]; - roc.Sqrt(arr); - return arr; + var arr = roc.ToArray(); + arr.Sqrt(arr); + return arr.ReCast(); } else return SqrtIterator(units); } @@ -302,15 +302,15 @@ public static partial class CollectionsSqrtExtensions if (units is List list) return list.Sqrt(); if (units is ICollection col) { - var arr = new R?[col.Count]; - col.Sqrt(arr); - return arr; + var arr = col.ToArray(); + arr.Sqrt(arr); + return arr.ReCast(); } if (units is IReadOnlyCollection roc) { - var arr = new R?[roc.Count]; - roc.Sqrt(arr); - return arr; + var arr = roc.ToArray(); + arr.Sqrt(arr); + return arr.ReCast(); } else return SqrtNullableIterator(units); } diff --git a/QWERTYkez.Mensura/IMensuraUnit.cs b/QWERTYkez.Mensura/IMensuraUnit.cs index 6a2ce41..d833165 100644 --- a/QWERTYkez.Mensura/IMensuraUnit.cs +++ b/QWERTYkez.Mensura/IMensuraUnit.cs @@ -1,8 +1,5 @@ namespace QWERTYkez.Mensura; -public interface IMensuraUnit -{ - internal double Value { get; init; } -} +public interface IMensuraUnit { } public interface IMensuraUnit where U : struct, IMensuraUnit, IEquatable { } \ No newline at end of file diff --git a/QWERTYkez.Mensura/Units/Pogon/PogonXXXXXXXX.Gen.cs b/QWERTYkez.Mensura/Units/Pogon/PogonXXXXXXXX.Gen.cs index 17d640f..26bfdfd 100644 --- a/QWERTYkez.Mensura/Units/Pogon/PogonXXXXXXXX.Gen.cs +++ b/QWERTYkez.Mensura/Units/Pogon/PogonXXXXXXXX.Gen.cs @@ -10,101 +10,60 @@ namespace QWERTYkez.Mensura.Units { public readonly partial record struct Length { + // === Array === - public static Mass[] operator *(PogonMass[] units, Length multiplicator) - { - if (units is null) return null!; - int len = units.Length; - if (len == 0) return []; - - var result = new Mass[len]; - units.MultiplyCore(multiplicator._Value, result); - return result; - } - public static Mass?[] operator *(PogonMass?[] units, Length multiplicator) - { - if (units is null) return null!; - int len = units.Length; - if (len == 0) return []; - - var result = new Mass?[len]; - units.MultiplyCore(multiplicator._Value, result); - return result; - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Mass[] operator *(Length multiplicator, PogonMass[] units) => units * multiplicator; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Mass?[] operator *(Length multiplicator, PogonMass?[] units) => units * multiplicator; + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Mass[] operator + *(PogonMass[] units, Length multiplicator) => units.Multiply(multiplicator._Value); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Mass?[] operator + *(PogonMass?[] units, Length multiplicator) => units.Multiply(multiplicator._Value); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Mass[] operator + *(Length multiplicator, PogonMass[] units) => units.Multiply(multiplicator._Value); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Mass?[] operator + *(Length multiplicator, PogonMass?[] units) => units.Multiply(multiplicator._Value); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Mass[] operator *(PogonMass[] units, Length? multiplicator) => + multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : new Mass[units.Length]); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Mass?[] operator *(PogonMass?[] units, Length? multiplicator) => + multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : new Mass?[units.Length]); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Mass[] operator *(Length? multiplicator, PogonMass[] units) => + multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : new Mass[units.Length]); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Mass?[] operator *(Length? multiplicator, PogonMass?[] units) => + multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : new Mass?[units.Length]); // === List === - public static List operator *(List units, Length multiplicator) - { - if (units is null) return null!; - int len = units.Count; - if (len == 0) return []; - - var resultArray = new Mass[len]; - Multiply(CollectionsMarshal.AsSpan(units), multiplicator, resultArray); - return resultArray.WrapAsList(); - } - public static List operator *(List units, Length multiplicator) - { - if (units is null) return null!; - int count = units.Count; - if (count == 0) return []; - - var resultArray = new Mass?[count]; - Multiply(CollectionsMarshal.AsSpan(units), multiplicator, resultArray); - return resultArray.WrapAsList(); - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] public static List operator *( - Length multiplicator, List units) => units * multiplicator; - [MethodImpl(MethodImplOptions.AggressiveInlining)] public static List operator *( - Length multiplicator, List units) => units * multiplicator; + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static List operator + *(List units, Length multiplicator) => units.Multiply(multiplicator._Value); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static List operator + *(List units, Length multiplicator) => units.Multiply(multiplicator._Value); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static List operator + *(Length multiplicator, List units) => units.Multiply(multiplicator._Value); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static List operator + *(Length multiplicator, List units) => units.Multiply(multiplicator._Value); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static List operator *(List units, Length? multiplicator) => + multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : new List(units.Count)); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static List operator *(List units, Length? multiplicator) => + multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : new List(units.Count)); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static List operator *(Length? multiplicator, List units) => + multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : new List(units.Count)); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static List operator *(Length? multiplicator, List units) => + multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : new List(units.Count)); // === IEnumerable === - public static IEnumerable Multiply(IEnumerable units, Length multiplicator) - { - if (units is null) return null!; - if (units is PogonMass[] array) return array.Multiply(multiplicator); - if (units is List list) return list.Multiply(multiplicator); - if (units is ICollection col) - { - var arr = new Mass[col.Count]; - col.Multiply(multiplicator, arr); - return arr; - } - if (units is IReadOnlyCollection roc) - { - var arr = new Mass[roc.Count]; - roc.Multiply(multiplicator, arr); - return arr; - } - return MultiplyIterator(units, multiplicator); - } - public static IEnumerable Multiply(IEnumerable units, Length multiplicator) - { - if (units is null) return null!; - if (units is PogonMass?[] array) return array.Multiply(multiplicator); - if (units is List list) return list.Multiply(multiplicator); - if (units is ICollection col) - { - var arr = new Mass?[col.Count]; - col.Multiply(multiplicator, arr); - return arr; - } - if (units is IReadOnlyCollection roc) - { - var arr = new Mass?[roc.Count]; - roc.Multiply(multiplicator, arr); - return arr; - } - return MultiplyNullableIterator(units, multiplicator); - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable Multiply( - Length multiplicator, IEnumerable units) => Multiply(units, multiplicator); - [MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable Multiply( - Length multiplicator, IEnumerable units) => Multiply(units, multiplicator); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable operator + *(IEnumerable units, Length multiplicator) => units.Multiply(multiplicator._Value); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable operator + *(IEnumerable units, Length multiplicator) => units.Multiply(multiplicator._Value); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable operator + *(Length multiplicator, IEnumerable units) => units.Multiply(multiplicator._Value); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable operator + *(Length multiplicator, IEnumerable units) => units.Multiply(multiplicator._Value); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable operator *(IEnumerable units, Length? multiplicator) => + multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : units.Select(u => new Mass(0d))); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable operator *(IEnumerable units, Length? multiplicator) => + multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : units.Select(u => u is null ? (Mass?)null : new Mass(0d))); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable operator *(Length? multiplicator, IEnumerable units) => + multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : units.Select(u => new Mass(0d))); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable operator *(Length? multiplicator, IEnumerable units) => + multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : units.Select(u => u is null ? (Mass?)null : new Mass(0d))); @@ -150,149 +109,151 @@ namespace QWERTYkez.Mensura.Units - // ========================================== - // === MULTIPLY === - // ========================================== - public static Mass[] operator *(PogonMass[] left, Length right) - { - if (left is null) return null!; - int len = left.Length; - if (len == 0) return []; - var result = new Mass[len]; - PogonMassExtensions.MultiplyCore(left, right, result); - return result; - } - public static Mass[] operator *(PogonMass[] left, Length? right) - { - if (left is null) return null!; - int len = left.Length; - if (len == 0) return []; + //// ========================================== + //// === MULTIPLY === + //// ========================================== - var result = new Mass[len]; - if (right is { } val) - for (int i = 0; i < len; i++) - result[i] = val * left[i]; - return result; - } - public static Mass[] operator *(Length left, PogonMass[] right) - { - if (right is null) return null!; - int len = right.Length; - if (len == 0) return []; + //public static Mass[] operator *(PogonMass[] left, Length right) + //{ + // if (left is null) return null!; + // int len = left.Length; + // if (len == 0) return []; - var result = new Mass[len]; - for (int i = 0; i < len; i++) - result[i] = left * right[i]; - return result; - } - public static Mass[] operator *(Length? left, PogonMass[] right) - { - if (right is null) return null!; - int len = right.Length; - if (len == 0) return []; + // var result = new Mass[len]; + // PogonMassExtensions.MultiplyCore(left, right, result); + // return result; + //} + //public static Mass[] operator *(PogonMass[] left, Length? right) + //{ + // if (left is null) return null!; + // int len = left.Length; + // if (len == 0) return []; - var result = new Mass[len]; - if (left is { } val) - for (int i = 0; i < len; i++) - result[i] = val * right[i]; - return result; - } + // var result = new Mass[len]; + // if (right is { } val) + // for (int i = 0; i < len; i++) + // result[i] = val * left[i]; + // return result; + //} + //public static Mass[] operator *(Length left, PogonMass[] right) + //{ + // if (right is null) return null!; + // int len = right.Length; + // if (len == 0) return []; - // === ReadOnlySpan === + // var result = new Mass[len]; + // for (int i = 0; i < len; i++) + // result[i] = left * right[i]; + // return result; + //} + //public static Mass[] operator *(Length? left, PogonMass[] right) + //{ + // if (right is null) return null!; + // int len = right.Length; + // if (len == 0) return []; - public static Span operator *(Length left, ReadOnlySpan right) - { - int len = right.Length; - if (len == 0) return []; + // var result = new Mass[len]; + // if (left is { } val) + // for (int i = 0; i < len; i++) + // result[i] = val * right[i]; + // return result; + //} - Span result = new Mass[len]; - for (int i = 0; i < len; i++) - result[i] = left * right[i]; - return result; - } - public static Span operator *(Length? left, ReadOnlySpan right) - { - int len = right.Length; - if (len == 0) return []; + //// === ReadOnlySpan === - Span result = new Mass[len]; - if (left is { } val) - for (int i = 0; i < len; i++) - result[i] = val * right[i]; - return result; - } - public static Span operator *(ReadOnlySpan left, Length right) - { - int len = left.Length; - if (len == 0) return []; + //public static Span operator *(Length left, ReadOnlySpan right) + //{ + // int len = right.Length; + // if (len == 0) return []; - Span result = new Mass[len]; - for (int i = 0; i < len; i++) - result[i] = right * left[i]; - return result; - } - public static Span operator *(ReadOnlySpan left, Length? right) - { - int len = left.Length; - if (len == 0) return []; + // Span result = new Mass[len]; + // for (int i = 0; i < len; i++) + // result[i] = left * right[i]; + // return result; + //} + //public static Span operator *(Length? left, ReadOnlySpan right) + //{ + // int len = right.Length; + // if (len == 0) return []; - Span result = new Mass[len]; - if (right is { } val) - for (int i = 0; i < len; i++) - result[i] = val * left[i]; - return result; - } + // Span result = new Mass[len]; + // if (left is { } val) + // for (int i = 0; i < len; i++) + // result[i] = val * right[i]; + // return result; + //} + //public static Span operator *(ReadOnlySpan left, Length right) + //{ + // int len = left.Length; + // if (len == 0) return []; - // === List === + // Span result = new Mass[len]; + // for (int i = 0; i < len; i++) + // result[i] = right * left[i]; + // return result; + //} + //public static Span operator *(ReadOnlySpan left, Length? right) + //{ + // int len = left.Length; + // if (len == 0) return []; - public static List operator *(Length left, List right) - { - if (right is null) return null!; - int len = right.Count; - if (len == 0) return []; + // Span result = new Mass[len]; + // if (right is { } val) + // for (int i = 0; i < len; i++) + // result[i] = val * left[i]; + // return result; + //} - var result = new List(len); - for (int i = 0; i < len; i++) - result[i] = left * right[i]; - return result; - } - public static List operator *(Length? left, List right) - { - if (right is null) return null!; - int len = right.Count; - if (len == 0) return []; + //// === List === - var result = new List(len); - if (left is { } val) - for (int i = 0; i < len; i++) - result[i] = val * right[i]; - return result; - } - public static List operator *(List left, Length right) - { - if (left is null) return null!; - int len = left.Count; - if (len == 0) return []; + //public static List operator *(Length left, List right) + //{ + // if (right is null) return null!; + // int len = right.Count; + // if (len == 0) return []; - var result = new List(len); - for (int i = 0; i < len; i++) - result[i] = right * left[i]; - return result; - } - public static List operator *(List left, Length? right) - { - if (left is null) return null!; - int len = left.Count; - if (len == 0) return []; + // var result = new List(len); + // for (int i = 0; i < len; i++) + // result[i] = left * right[i]; + // return result; + //} + //public static List operator *(Length? left, List right) + //{ + // if (right is null) return null!; + // int len = right.Count; + // if (len == 0) return []; - var result = new List(len); - if (right is { } val) - for (int i = 0; i < len; i++) - result[i] = val * left[i]; - return result; - } + // var result = new List(len); + // if (left is { } val) + // for (int i = 0; i < len; i++) + // result[i] = val * right[i]; + // return result; + //} + //public static List operator *(List left, Length right) + //{ + // if (left is null) return null!; + // int len = left.Count; + // if (len == 0) return []; + + // var result = new List(len); + // for (int i = 0; i < len; i++) + // result[i] = right * left[i]; + // return result; + //} + //public static List operator *(List left, Length? right) + //{ + // if (left is null) return null!; + // int len = left.Count; + // if (len == 0) return []; + + // var result = new List(len); + // if (right is { } val) + // for (int i = 0; i < len; i++) + // result[i] = val * left[i]; + // return result; + //} } } @@ -303,9 +264,8 @@ namespace QWERTYkez.Mensura.Units.Pogon public readonly partial record struct PogonMass : IMensuraUnit, IEquatable, IMensuraUnit { - [JsonInclude, DataMember, JsonPropertyName("v")] // для JSON / EF на случай сбоев, если пробелма с _Value + [JsonInclude, DataMember, JsonPropertyName("v"), Obsolete] // для JSON / EF на случай сбоев, если пробелма с _Value internal double Value { get => _Value; init => _Value = value; } - double IMensuraUnit.Value { get => _Value; init => _Value = value; } internal readonly double _Value; internal PogonMass(double value) => _Value = value; @@ -343,167 +303,82 @@ namespace QWERTYkez.Mensura.Units.Pogon public static Length operator /(Mass? left, PogonMass? right) => new((left ?? default) / (right ?? default).PerValue); - // ========================================== - // === MULTIPLY === - // ========================================== + - public static Mass[] operator *(PogonMass left, Length[] right) - { - if (right is null) return null!; - int len = right.Length; - if (len == 0) return []; - var result = new Mass[len]; - for (int i = 0; i < len; i++) - result[i] = left * right[i]; - return result; - } - public static Mass[] operator *(PogonMass? left, Length[] right) - { - if (right is null) return null!; - int len = right.Length; - if (len == 0) return []; - var result = new Mass[len]; - if (left is { } val) - for (int i = 0; i < len; i++) - result[i] = val * right[i]; - return result; - } - public static Mass[] operator *(Length[] left, PogonMass right) - { - if (left is null) return null!; - int len = left.Length; - if (len == 0) return []; + - var result = new Mass[len]; - for (int i = 0; i < len; i++) - result[i] = right * left[i]; - return result; - } - public static Mass[] operator *(Length[] left, PogonMass? right) - { - if (left is null) return null!; - int len = left.Length; - if (len == 0) return []; + // === Array === + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Mass[] operator + *(Length[] units, PogonMass multiplicator) => units.Multiply(multiplicator._Value); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Mass?[] operator + *(Length?[] units, PogonMass multiplicator) => units.Multiply(multiplicator._Value); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Mass[] operator + *(PogonMass multiplicator, Length[] units) => units.Multiply(multiplicator._Value); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Mass?[] operator + *(PogonMass multiplicator, Length?[] units) => units.Multiply(multiplicator._Value); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Mass[] operator *(Length[] units, PogonMass? multiplicator) => + multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : new Mass[units.Length]); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Mass?[] operator *(Length?[] units, PogonMass? multiplicator) => + multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : new Mass?[units.Length]); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Mass[] operator *(PogonMass? multiplicator, Length[] units) => + multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : new Mass[units.Length]); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Mass?[] operator *(PogonMass? multiplicator, Length?[] units) => + multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : new Mass?[units.Length]); - var result = new Mass[len]; - if (right is { } val) - for (int i = 0; i < len; i++) - result[i] = val * left[i]; - return result; - } + // === List === + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static List operator + *(List units, PogonMass multiplicator) => units.Multiply(multiplicator._Value); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static List operator + *(List units, PogonMass multiplicator) => units.Multiply(multiplicator._Value); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static List operator + *(PogonMass multiplicator, List units) => units.Multiply(multiplicator._Value); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static List operator + *(PogonMass multiplicator, List units) => units.Multiply(multiplicator._Value); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static List operator *(List units, PogonMass? multiplicator) => + multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : new List(units.Count)); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static List operator *(List units, PogonMass? multiplicator) => + multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : new List(units.Count)); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static List operator *(PogonMass? multiplicator, List units) => + multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : new List(units.Count)); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static List operator *(PogonMass? multiplicator, List units) => + multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : new List(units.Count)); - // === ReadOnlySpan === - - public static Span operator *(PogonMass left, ReadOnlySpan right) - { - int len = right.Length; - if (len == 0) return []; - - Span result = new Mass[len]; - for (int i = 0; i < len; i++) - result[i] = left * right[i]; - return result; - } - public static Span operator *(PogonMass? left, ReadOnlySpan right) - { - int len = right.Length; - if (len == 0) return []; - - Span result = new Mass[len]; - if (left is { } val) - for (int i = 0; i < len; i++) - result[i] = val * right[i]; - return result; - } - public static Span operator *(ReadOnlySpan left, PogonMass right) - { - int len = left.Length; - if (len == 0) return []; - - Span result = new Mass[len]; - for (int i = 0; i < len; i++) - result[i] = right * left[i]; - return result; - } - public static Span operator *(ReadOnlySpan left, PogonMass? right) - { - int len = left.Length; - if (len == 0) return []; - - Span result = new Mass[len]; - if (right is { } val) - for (int i = 0; i < len; i++) - result[i] = val * left[i]; - return result; - } - - // === List === - - public static List operator *(PogonMass left, List right) - { - if (right is null) return null!; - int len = right.Count; - if (len == 0) return []; - - var result = new List(len); - for (int i = 0; i < len; i++) - result[i] = left * right[i]; - return result; - } - public static List operator *(PogonMass? left, List right) - { - if (right is null) return null!; - int len = right.Count; - if (len == 0) return []; - - var result = new List(len); - if (left is { } val) - for (int i = 0; i < len; i++) - result[i] = val * right[i]; - return result; - } - public static List operator *(List left, PogonMass right) - { - if (left is null) return null!; - int len = left.Count; - if (len == 0) return []; - - var result = new List(len); - for (int i = 0; i < len; i++) - result[i] = right * left[i]; - return result; - } - public static List operator *(List left, PogonMass? right) - { - if (left is null) return null!; - int len = left.Count; - if (len == 0) return []; - - var result = new List(len); - if (right is { } val) - for (int i = 0; i < len; i++) - result[i] = val * left[i]; - return result; - } + // === IEnumerable === + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable operator + *(IEnumerable units, PogonMass multiplicator) => units.Multiply(multiplicator._Value); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable operator + *(IEnumerable units, PogonMass multiplicator) => units.Multiply(multiplicator._Value); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable operator + *(PogonMass multiplicator, IEnumerable units) => units.Multiply(multiplicator._Value); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable operator + *(PogonMass multiplicator, IEnumerable units) => units.Multiply(multiplicator._Value); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable operator *(IEnumerable units, PogonMass? multiplicator) => + multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : units.Select(u => new Mass(0d))); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable operator *(IEnumerable units, PogonMass? multiplicator) => + multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : units.Select(u => u is null ? (Mass?)null : new Mass(0d))); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable operator *(PogonMass? multiplicator, IEnumerable units) => + multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : units.Select(u => new Mass(0d))); + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static IEnumerable operator *(PogonMass? multiplicator, IEnumerable units) => + multiplicator.HasValue ? units * multiplicator.Value : (units is null ? null! : units.Select(u => u is null ? (Mass?)null : new Mass(0d))); } internal static class PogonMassExtensions { internal static Mass Protect(this Mass? unit) => unit ?? default; - internal static double ToDouble(this PogonMass? unit) => unit?._Value ?? 0d; + internal static double ToDouble(this Length? unit) => unit?._Value ?? 0d; - public static PogonMass MetricSum(this IEnumerable units) => new(units?.Sum(m => m._Value) ?? 0d); - public static PogonMass MetricAverage(this IEnumerable units) => new(units?.Average(m => m._Value) ?? double.NaN); - public static PogonMass MetricMax(this IEnumerable units) => new(units?.Max(m => m._Value) ?? double.MinValue); - public static PogonMass MetricMin(this IEnumerable units) => new(units?.Min(m => m._Value) ?? double.MaxValue); + public static Length MetricSum(this IEnumerable units) => new(units?.Sum(m => m._Value) ?? 0d); + public static Length MetricAverage(this IEnumerable units) => new(units?.Average(m => m._Value) ?? double.NaN); + public static Length MetricMax(this IEnumerable units) => new(units?.Max(m => m._Value) ?? double.MinValue); + public static Length MetricMin(this IEnumerable units) => new(units?.Min(m => m._Value) ?? double.MaxValue); - public static PogonMass MetricSum(this IEnumerable units) => new(units?.Sum(m => m.ToDouble()) ?? 0d); - public static PogonMass MetricAverage(this IEnumerable units) => new(units?.Average(m => m.ToDouble()) ?? double.NaN); - public static PogonMass MetricMax(this IEnumerable units) => new(units?.Max(m => m.ToDouble()) ?? double.MinValue); - public static PogonMass MetricMin(this IEnumerable units) => new(units?.Min(m => m.ToDouble()) ?? double.MaxValue); + public static Length MetricSum(this IEnumerable units) => new(units?.Sum(m => m.ToDouble()) ?? 0d); + public static Length MetricAverage(this IEnumerable units) => new(units?.Average(m => m.ToDouble()) ?? double.NaN); + public static Length MetricMax(this IEnumerable units) => new(units?.Max(m => m.ToDouble()) ?? double.MinValue); + public static Length MetricMin(this IEnumerable units) => new(units?.Min(m => m.ToDouble()) ?? double.MaxValue); diff --git a/QWERTYkez.Mensura/Units/XXXXXXXX.Gen.cs b/QWERTYkez.Mensura/Units/XXXXXXXX.Gen.cs index b19fac3..dc0b397 100644 --- a/QWERTYkez.Mensura/Units/XXXXXXXX.Gen.cs +++ b/QWERTYkez.Mensura/Units/XXXXXXXX.Gen.cs @@ -11,9 +11,8 @@ public class XXXXXXXXXXXXXXConverter : UnitJsonConverter { } [JsonConverter(typeof(XXXXXXXXXXXXXXConverter))] public readonly partial record struct XXXXXXXXXXXXXX : IMensuraUnit, IEquatable, IMensuraUnit { - [JsonInclude, DataMember, JsonPropertyName("v")] // для JSON / EF на случай сбоев, если пробелма с _Value + [JsonInclude, DataMember, JsonPropertyName("v"), Obsolete] // для JSON / EF на случай сбоев, если пробелма с _Value internal double Value { get => _Value; init => _Value = value; } - double IMensuraUnit.Value { get => _Value; init => _Value = value; } internal readonly double _Value; internal XXXXXXXXXXXXXX(double value) => _Value = value;