diff --git a/src/client/spark/core/Utility/UniversalNumericComparer.cs b/src/client/spark/core/Utility/UniversalNumericComparer.cs
deleted file mode 100644
index 9896b44de..000000000
--- a/src/client/spark/core/Utility/UniversalNumericComparer.cs
+++ /dev/null
@@ -1,106 +0,0 @@
-using System;
-
-namespace bts.utils {
- ///
- /// An IComparable implementation that supports cross type comparisons of any combination of int, uint, double, and decimal.
- ///
- readonly struct UniversalNumericComparer : IComparable {
- readonly int? _int;
- readonly uint? _uint;
- readonly double? _double;
- readonly decimal? _decimal;
-
- public UniversalNumericComparer(int i) {
- _int = i;
- _uint = null;
- _double = null;
- _decimal = null;
- }
-
- public UniversalNumericComparer(double d) {
- _int = null;
- _uint = null;
- _double = d;
- _decimal = null;
- }
-
- public UniversalNumericComparer(uint u) {
- _int = null;
- _uint = u;
- _double = null;
- _decimal = null;
- }
-
- public UniversalNumericComparer(decimal d) {
- _int = null;
- _uint = null;
- _double = null;
- _decimal = d;
- }
-
- public int CompareTo(object obj) {
- if (obj is int i) {
- return -(i.CompareTo(this));
- } else if (obj is uint u) {
- return -(u.CompareTo(this));
- } else if (obj is double d)
- return -(d.CompareTo(this));
- else if (obj is decimal dec)
- return -(dec.CompareTo(this));
- throw new ArgumentException("Obj is not of a supported numeric type (int, uint, double).", nameof(obj));
- }
-
- public static implicit operator int(UniversalNumericComparer w) {
- if (w._int.HasValue)
- return w._int.Value;
- else if (w._double.HasValue)
- return (int)w._double.Value;
- else if (w._uint.HasValue)
- return (int)w._uint.Value;
- else if (w._decimal.HasValue)
- return (int)w._decimal.Value;
- return 0;
- }
-
- public static implicit operator uint(UniversalNumericComparer w) {
- if (w._uint.HasValue)
- return w._uint.Value;
- else if (w._int.HasValue)
- return (uint)w._int.Value;
- else if (w._double.HasValue)
- return (uint)w._double.Value;
- else if (w._decimal.HasValue)
- return (uint)w._decimal.Value;
- return 0u;
- }
-
- public static implicit operator double(UniversalNumericComparer w) {
- if (w._double.HasValue)
- return w._double.Value;
- else if (w._int.HasValue)
- return w._int.Value;
- else if (w._uint.HasValue)
- return w._uint.Value;
- else if (w._decimal.HasValue)
- return (double)w._decimal.Value;
- return 0.0;
- }
-
- public static implicit operator decimal(UniversalNumericComparer w) {
- if (w._double.HasValue)
- return (decimal)w._double.Value;
- else if (w._int.HasValue)
- return w._int.Value;
- else if (w._uint.HasValue)
- return w._uint.Value;
- else if (w._decimal.HasValue)
- return w._decimal.Value;
- return 0;
- }
-
- public static implicit operator UniversalNumericComparer(int i) => new UniversalNumericComparer(i);
- public static implicit operator UniversalNumericComparer(uint u) => new UniversalNumericComparer(u);
- public static implicit operator UniversalNumericComparer(double d) => new UniversalNumericComparer(d);
- public static implicit operator UniversalNumericComparer(decimal d) => new UniversalNumericComparer(d);
- }
-}
diff --git a/src/client/spark/view/Converters/Numeric/NumericSignBrushConverter.cs b/src/client/spark/view/Converters/Numeric/NumericSignBrushConverter.cs
index bb22df2f7..fec25d68c 100644
--- a/src/client/spark/view/Converters/Numeric/NumericSignBrushConverter.cs
+++ b/src/client/spark/view/Converters/Numeric/NumericSignBrushConverter.cs
@@ -1,5 +1,7 @@
using bts.utils;
+using Spark.Core.Math;
+
using System;
using System.Globalization;
using System.Windows.Data;
@@ -55,7 +57,7 @@ namespace mbts.ui.converters {
_brushes.error.Clone().with(value).frozen());
}
}
- public IComparable Zero { get; set; } = new UniversalNumericComparer(0.0);
+ public IComparable Zero { get; set; } = new NumericOneof(0.0);
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
var comparable = value as IComparable;
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);
+}