From 2d98e47bf00380346c994cded463cffb53d335ff Mon Sep 17 00:00:00 2001 From: John Colvin Date: Tue, 3 Oct 2023 02:05:09 -0400 Subject: [PATCH] refactor: Reorganize NumericComparer, support all types --- .../spark_core/Collections/NumericComparer.cs | 131 +------ src/client/spark_core/Math/NumericOneof.cs | 351 ++++++++++++++++++ 2 files changed, 355 insertions(+), 127 deletions(-) create mode 100644 src/client/spark_core/Math/NumericOneof.cs diff --git a/src/client/spark_core/Collections/NumericComparer.cs b/src/client/spark_core/Collections/NumericComparer.cs index 7b9002317..48d7cd53f 100644 --- a/src/client/spark_core/Collections/NumericComparer.cs +++ b/src/client/spark_core/Collections/NumericComparer.cs @@ -1,138 +1,15 @@ #nullable enable namespace Spark.Core.Collections; -using System; +using Spark.Core.Math; + using System.Collections; + /// /// An IComparer implementation that supports cross type comparisons of any combination of int, /// uint, double, and decimal. /// public class NumericComparer : IComparer { public int Compare(object? x, object? y) => - createNumeric(x).CompareTo(y); - - static NumericValue createNumeric(object? obj) { - if (obj is int i) { - return new(i); - } - if (obj is uint u) { - return new(u); - } - if (obj is double dbl) { - return new(dbl); - } - if (obj is decimal dec) { - return new(dec); - } - throw new ArgumentException("Obj is not of a supported numeric type (int, uint, double).", nameof(obj)); - } - - readonly struct NumericValue : IComparable { - readonly int? integer; - readonly uint? unsignedInteger; - readonly double? real; - readonly decimal? realDecimal; - - public NumericValue(int integer) { - this.integer = integer; - unsignedInteger = null; - real = null; - realDecimal = null; - } - - public NumericValue(uint unsignedInteger) => - this.unsignedInteger = unsignedInteger; - - public NumericValue(double real) => - this.real = real; - - public NumericValue(decimal realDecimal) => - this.realDecimal = realDecimal; - - public int CompareTo(object? obj) { - if (obj is int i) { - return -i.CompareTo(this); - } - if (obj is uint u) { - return -u.CompareTo(this); - } - if (obj is double dbl) { - return -dbl.CompareTo(this); - } - if (obj is decimal dec) { - return -dec.CompareTo(this); - } - throw new ArgumentException( - "Obj is not of a supported numeric type (int, uint, double, decimal).", nameof(obj)); - } - - public static implicit operator int(NumericValue w) { - if (w.integer.HasValue) { - return w.integer.Value; - } - if (w.real.HasValue) { - return (int)w.real.Value; - } - if (w.unsignedInteger.HasValue) { - return (int)w.unsignedInteger.Value; - } - if (w.realDecimal.HasValue) { - return (int)w.realDecimal.Value; - } - return 0; - } - - public static implicit operator uint(NumericValue w) { - if (w.unsignedInteger.HasValue) { - return w.unsignedInteger.Value; - } - if (w.integer.HasValue) { - return (uint)w.integer.Value; - } - if (w.real.HasValue) { - return (uint)w.real.Value; - } - if (w.realDecimal.HasValue) { - return (uint)w.realDecimal.Value; - } - return 0; - } - - public static implicit operator double(NumericValue w) { - if (w.real.HasValue) { - return w.real.Value; - } - if (w.integer.HasValue) { - return w.integer.Value; - } - if (w.unsignedInteger.HasValue) { - return w.unsignedInteger.Value; - } - if (w.realDecimal.HasValue) { - return (double)w.realDecimal.Value; - } - return 0; - } - - public static implicit operator decimal(NumericValue w) { - if (w.real.HasValue) { - return (decimal)w.real.Value; - } - if (w.integer.HasValue) { - return w.integer.Value; - } - if (w.unsignedInteger.HasValue) { - return w.unsignedInteger.Value; - } - if (w.realDecimal.HasValue) { - return w.realDecimal.Value; - } - return 0; - } - - public static implicit operator NumericValue(int i) => new(i); - public static implicit operator NumericValue(uint u) => new(u); - public static implicit operator NumericValue(double d) => new(d); - public static implicit operator NumericValue(decimal d) => new(d); - } + NumericOneof.Create(x).CompareTo(y); } diff --git a/src/client/spark_core/Math/NumericOneof.cs b/src/client/spark_core/Math/NumericOneof.cs new file mode 100644 index 000000000..6ba25fce2 --- /dev/null +++ b/src/client/spark_core/Math/NumericOneof.cs @@ -0,0 +1,351 @@ +#nullable enable +namespace Spark.Core.Math; + +using bts; + +using System; + +public readonly struct NumericOneof : IComparable { + public NumericOneof(short value) => Int16 = value; + public NumericOneof(int value) => Int32 = value; + public NumericOneof(long value) => Int64 = value; + public NumericOneof(ushort value) => UInt16 = value; + public NumericOneof(uint value) => UInt32 = value; + public NumericOneof(ulong value) => UInt64 = value; + public NumericOneof(float value) => Single = value; + public NumericOneof(double value) => Double = value; + public NumericOneof(decimal value) => Decimal = value; + + public int? Int32 { get; } = null; + public short? Int16 { get; } = null; + public long? Int64 { get; } = null; + + public ushort? UInt16 { get; } = null; + public uint? UInt32 { get; } = null; + public ulong? UInt64 { get; } = null; + + public float? Single { get; } = null; + public double? Double { get; } = null; + public decimal? Decimal { get; } = null; + + public object? Value => + Int16 as object ?? + Int32 as object ?? + Int64 as object ?? + UInt32 as object ?? + UInt64 as object ?? + UInt16 as object ?? + Single as object ?? + Double as object ?? + Decimal as object ?? + null; + + public static NumericOneof Create(object? value) { + if (value is short i16) { + return new(i16); + } + if (value is int i32) { + return new(i32); + } + if (value is long i64) { + return new(i64); + } + if (value is ushort u16) { + return new(u16); + } + if (value is uint u32) { + return new(u32); + } + if (value is ulong u64) { + return new(u64); + } + if (value is float f32) { + return new(f32); + } + if (value is double f64) { + return new(f64); + } + if (value is decimal dec) { + return new(dec); + } + if (value is NumericOneof n) { + return n; + } + throw new ArgumentException( + "Obj is not of a supported numeric type (int, uint, double).", nameof(value)); + } + + public int CompareTo(object? obj) { + if (obj is short i16) { + return -i16.CompareTo(this); + } + if (obj is int i32) { + return -i32.CompareTo(this); + } + if (obj is long i64) { + return -i64.CompareTo(this); + } + if (obj is ushort u16) { + return -u16.CompareTo(this); + } + if (obj is uint u32) { + return -u32.CompareTo(this); + } + if (obj is ulong u64) { + return -u64.CompareTo(this); + } + if (obj is float f32) { + return -f32.CompareTo(this); + } + if (obj is double f64) { + return -f64.CompareTo(this); + } + if (obj is decimal dec) { + return -dec.CompareTo(this); + } + if (obj is NumericOneof n) { + return CompareTo(n.Value); + } + throw new ArgumentException( + "Obj is not of a supported numeric type (int, uint, double, decimal).", nameof(obj)); + } + + public static implicit operator int(NumericOneof w) { + checked { + if (w.UInt16.HasValue) { + return w.UInt16.Value; + } + if (w.UInt32.HasValue) { + return (int)w.UInt32.Value; + } + if (w.UInt64.HasValue) { + return (int)w.UInt64.Value; + } + if (w.Int16.HasValue) { + return w.Int16.Value; + } + if (w.Int32.HasValue) { + return w.Int32.Value; + } + if (w.Int64.HasValue) { + return (int)w.Int64.Value; + } + if (w.Single.HasValue) { + return (int)w.Single.Value; + } + if (w.Double.HasValue) { + return (int)w.Double.Value; + } + if (w.Decimal.HasValue) { + return (int)w.Decimal.Value; + } + return 0; + } + } + + public static implicit operator uint(NumericOneof w) { + checked { + if (w.UInt16.HasValue) { + return w.UInt16.Value; + } + if (w.UInt32.HasValue) { + return w.UInt32.Value; + } + if (w.UInt64.HasValue) { + return (uint)w.UInt64.Value; + } + if (w.Int16.HasValue) { + return (uint)w.Int16.Value; + } + if (w.Int32.HasValue) { + return (uint)w.Int32.Value; + } + if (w.Int64.HasValue) { + return (uint)w.Int64.Value; + } + if (w.Single.HasValue) { + return (uint)w.Single.Value; + } + if (w.Double.HasValue) { + return (uint)w.Double.Value; + } + if (w.Decimal.HasValue) { + return (uint)w.Decimal.Value; + } + return 0; + } + } + + public static implicit operator double(NumericOneof w) { + if (w.UInt16.HasValue) { + return w.UInt16.Value; + } + if (w.UInt32.HasValue) { + return w.UInt32.Value; + } + if (w.UInt64.HasValue) { + return w.UInt64.Value; + } + if (w.Int16.HasValue) { + return w.Int16.Value; + } + if (w.Int32.HasValue) { + return w.Int32.Value; + } + if (w.Int64.HasValue) { + return w.Int64.Value; + } + if (w.Single.HasValue) { + return w.Single.Value; + } + if (w.Double.HasValue) { + return w.Double.Value; + } + if (w.Decimal.HasValue) { + return (double)w.Decimal.Value; + } + return 0; + } + + public static implicit operator short(NumericOneof w) { + checked { + if (w.UInt16.HasValue) { + return (short)w.UInt16.Value; + } + if (w.UInt32.HasValue) { + return (short)w.UInt32.Value; + } + if (w.UInt64.HasValue) { + return (short)w.UInt64.Value; + } + if (w.Int16.HasValue) { + return w.Int16.Value; + } + if (w.Int32.HasValue) { + return (short)w.Int32.Value; + } + if (w.Int64.HasValue) { + return (short)w.Int64.Value; + } + if (w.Single.HasValue) { + return (short)w.Single.Value; + } + if (w.Double.HasValue) { + return (short)w.Double.Value; + } + if (w.Decimal.HasValue) { + return (short)w.Decimal.Value; + } + return 0; + } + } + + public static implicit operator long(NumericOneof w) { + checked { + if (w.UInt16.HasValue) { + return w.UInt16.Value; + } + if (w.UInt32.HasValue) { + return w.UInt32.Value; + } + if (w.UInt64.HasValue) { + return (long)w.UInt64.Value; + } + if (w.Int16.HasValue) { + return w.Int16.Value; + } + if (w.Int32.HasValue) { + return w.Int32.Value; + } + if (w.Int64.HasValue) { + return w.Int64.Value; + } + if (w.Single.HasValue) { + return (long)w.Single.Value; + } + if (w.Double.HasValue) { + return (long)w.Double.Value; + } + if (w.Decimal.HasValue) { + return (long)w.Decimal.Value; + } + return 0; + } + } + + public static implicit operator float(NumericOneof w) { + checked { + if (w.UInt16.HasValue) { + return w.UInt16.Value; + } + if (w.UInt32.HasValue) { + return w.UInt32.Value; + } + if (w.UInt64.HasValue) { + return w.UInt64.Value; + } + if (w.Int16.HasValue) { + return w.Int16.Value; + } + if (w.Int32.HasValue) { + return w.Int32.Value; + } + if (w.Int64.HasValue) { + return w.Int64.Value; + } + if (w.Single.HasValue) { + return w.Single.Value; + } + if (w.Double.HasValue) { + return (float)w.Double.Value; + } + if (w.Decimal.HasValue) { + return (float)w.Decimal.Value; + } + return 0; + } + } + + public static implicit operator decimal(NumericOneof w) { + checked { + if (w.UInt16.HasValue) { + return w.UInt16.Value; + } + if (w.UInt32.HasValue) { + return w.UInt32.Value; + } + if (w.UInt64.HasValue) { + return w.UInt64.Value; + } + if (w.Int16.HasValue) { + return w.Int16.Value; + } + if (w.Int32.HasValue) { + return w.Int32.Value; + } + if (w.Int64.HasValue) { + return w.Int64.Value; + } + if (w.Single.HasValue) { + return (decimal)w.Single.Value; + } + if (w.Double.HasValue) { + return (decimal)w.Double.Value; + } + if (w.Decimal.HasValue) { + return w.Decimal.Value; + } + return 0; + } + } + + public static implicit operator NumericOneof(short value) => new(value); + public static implicit operator NumericOneof(int value) => new(value); + public static implicit operator NumericOneof(long value) => new(value); + public static implicit operator NumericOneof(ushort value) => new(value); + public static implicit operator NumericOneof(uint value) => new(value); + public static implicit operator NumericOneof(ulong value) => new(value); + public static implicit operator NumericOneof(float value) => new(value); + public static implicit operator NumericOneof(double value) => new(value); + public static implicit operator NumericOneof(decimal value) => new(value); +}