Add c# Variant class, modeled after std::variant in c++
This commit is contained in:
1 parent
e534449638
commit
20bb42c8b0
3 files changed
+673
No files matched your search
@@ -0,0 +1,303 @@
|
||||
#nullable enable
|
||||
namespace Spark.Core;
|
||||
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
/// <summary>
|
||||
/// A c# equivalent to std::any in c++. It holds a value for an arbitrary primitive with minimal
|
||||
/// memory footprint (16 bytes). For best performance when reading values, use the 'VFoo' fields.
|
||||
/// However, there is a drawback: fields don't support implicit conversions (e.g., int32 -> int64).
|
||||
/// If implicit conversion is needed, use the 'GetFoo' methods.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Explicit)]
|
||||
public readonly struct PrimitiveVariant : IEquatable<PrimitiveVariant> {
|
||||
[FieldOffset(0)]
|
||||
public readonly TypeIndex Index;
|
||||
|
||||
[FieldOffset(1)]
|
||||
public readonly int VInt32;
|
||||
|
||||
[FieldOffset(1)]
|
||||
public readonly long VInt64;
|
||||
|
||||
[FieldOffset(1)]
|
||||
public readonly uint VUInt32;
|
||||
|
||||
[FieldOffset(1)]
|
||||
public readonly ulong VUInt64;
|
||||
|
||||
[FieldOffset(1)]
|
||||
public readonly float VFloat32;
|
||||
|
||||
[FieldOffset(1)]
|
||||
public readonly double VFloat64;
|
||||
|
||||
[FieldOffset(1)]
|
||||
public readonly DateTime VDateTime;
|
||||
|
||||
[FieldOffset(1)]
|
||||
public readonly TimeSpan VTimeSpan;
|
||||
|
||||
public PrimitiveVariant(int value) {
|
||||
VInt32 = value;
|
||||
Index = TypeIndex.Int32;
|
||||
}
|
||||
|
||||
public PrimitiveVariant(long value) {
|
||||
VInt64 = value;
|
||||
Index = TypeIndex.Int64;
|
||||
}
|
||||
|
||||
public PrimitiveVariant(uint value) {
|
||||
VUInt32 = value;
|
||||
Index = TypeIndex.UInt32;
|
||||
}
|
||||
|
||||
public PrimitiveVariant(ulong value) {
|
||||
VUInt64 = value;
|
||||
Index = TypeIndex.UInt64;
|
||||
}
|
||||
|
||||
public PrimitiveVariant(float value) {
|
||||
VFloat32 = value;
|
||||
Index = TypeIndex.Float32;
|
||||
}
|
||||
|
||||
public PrimitiveVariant(double value) {
|
||||
VFloat64 = value;
|
||||
Index = TypeIndex.Float64;
|
||||
}
|
||||
|
||||
public PrimitiveVariant(DateTime value) {
|
||||
VDateTime = value;
|
||||
Index = TypeIndex.DateTime;
|
||||
}
|
||||
|
||||
public PrimitiveVariant(TimeSpan value) {
|
||||
VTimeSpan = value;
|
||||
Index = TypeIndex.TimeSpan;
|
||||
}
|
||||
|
||||
public int GetInt32() =>
|
||||
Index switch {
|
||||
TypeIndex.Int32 => VInt32,
|
||||
_ => throw new InvalidOperationException("Variant does not contain Int32.")
|
||||
};
|
||||
|
||||
public long GetInt64() =>
|
||||
Index switch {
|
||||
TypeIndex.Int32 => VInt32,
|
||||
TypeIndex.UInt32 => VUInt32,
|
||||
TypeIndex.Int64 => VInt64,
|
||||
_ => throw new InvalidOperationException("Variant does not contain Int64.")
|
||||
};
|
||||
|
||||
public uint GetUInt32() =>
|
||||
Index switch {
|
||||
TypeIndex.UInt32 => VUInt32,
|
||||
_ => throw new InvalidOperationException("Variant does not contain UInt32.")
|
||||
};
|
||||
|
||||
public ulong GetUInt64() =>
|
||||
Index switch {
|
||||
TypeIndex.UInt32 => VUInt32,
|
||||
TypeIndex.UInt64 => VUInt64,
|
||||
_ => throw new InvalidOperationException("Variant does not contain UInt64.")
|
||||
};
|
||||
|
||||
public float GetFloat32() =>
|
||||
Index switch {
|
||||
TypeIndex.Float32 => VFloat32,
|
||||
_ => throw new InvalidOperationException("Variant does not contain Float32.")
|
||||
};
|
||||
|
||||
public double GetFloat64() =>
|
||||
Index switch {
|
||||
TypeIndex.Int32 => VInt32,
|
||||
TypeIndex.UInt32 => VUInt32,
|
||||
TypeIndex.Float32 => VFloat32,
|
||||
TypeIndex.Float64 => VFloat64,
|
||||
_ => throw new InvalidOperationException("Variant does not contain Float64.")
|
||||
};
|
||||
|
||||
public DateTime GetDateTime() =>
|
||||
Index switch {
|
||||
TypeIndex.DateTime => VDateTime,
|
||||
_ => throw new InvalidOperationException("Variant does not contain DateTime.")
|
||||
};
|
||||
|
||||
public TimeSpan GetTimeSpan() =>
|
||||
Index switch {
|
||||
TypeIndex.TimeSpan => VTimeSpan,
|
||||
_ => throw new InvalidOperationException("Variant does not contain TimeSpan.")
|
||||
};
|
||||
|
||||
public readonly bool IsInt32 => Index == TypeIndex.Int32;
|
||||
public readonly bool IsInt64 => Index == TypeIndex.Int64;
|
||||
public readonly bool IsUInt32 => Index == TypeIndex.UInt32;
|
||||
public readonly bool IsUInt64 => Index == TypeIndex.UInt64;
|
||||
public readonly bool IsFloat32 => Index == TypeIndex.Float32;
|
||||
public readonly bool IsFloat64 => Index == TypeIndex.Float64;
|
||||
public readonly bool IsDateTime => Index == TypeIndex.DateTime;
|
||||
public readonly bool IsTimeSpan => Index == TypeIndex.TimeSpan;
|
||||
|
||||
public bool Equals(PrimitiveVariant other) {
|
||||
if (Index != other.Index) {
|
||||
return false;
|
||||
}
|
||||
return Index switch {
|
||||
TypeIndex.Int32 => VInt32 == other.VInt32,
|
||||
TypeIndex.Int64 => VInt64 == other.VInt64,
|
||||
TypeIndex.UInt32 => VUInt32 == other.VUInt32,
|
||||
TypeIndex.UInt64 => VUInt64 == other.VUInt64,
|
||||
TypeIndex.Float32 => VFloat32.Equals(other.VFloat32),
|
||||
TypeIndex.Float64 => VFloat64.Equals(other.VFloat64),
|
||||
TypeIndex.DateTime => VDateTime == other.VDateTime,
|
||||
_ => true,
|
||||
};
|
||||
}
|
||||
|
||||
public override bool Equals([NotNullWhen(true)] object? obj) =>
|
||||
obj is PrimitiveVariant other && Equals(other);
|
||||
|
||||
public override int GetHashCode() {
|
||||
int hash = Index switch {
|
||||
TypeIndex.Int32 => VInt32.GetHashCode(),
|
||||
TypeIndex.Int64 => VInt64.GetHashCode(),
|
||||
TypeIndex.UInt32 => VUInt32.GetHashCode(),
|
||||
TypeIndex.UInt64 => VUInt64.GetHashCode(),
|
||||
TypeIndex.Float32 => VFloat32.GetHashCode(),
|
||||
TypeIndex.Float64 => VFloat64.GetHashCode(),
|
||||
TypeIndex.DateTime => VDateTime.GetHashCode(),
|
||||
TypeIndex.TimeSpan => VTimeSpan.GetHashCode(),
|
||||
_ => 0,
|
||||
};
|
||||
return HashCode.Combine(Index, hash);
|
||||
}
|
||||
|
||||
public override string ToString() =>
|
||||
Index switch {
|
||||
TypeIndex.Int32 => VInt32.ToString(),
|
||||
TypeIndex.Int64 => VInt64.ToString(),
|
||||
TypeIndex.UInt32 => VUInt32.ToString(),
|
||||
TypeIndex.UInt64 => VUInt64.ToString(),
|
||||
TypeIndex.Float32 => VFloat32.ToString(),
|
||||
TypeIndex.Float64 => VFloat64.ToString(),
|
||||
TypeIndex.DateTime => VDateTime.ToString(),
|
||||
TypeIndex.TimeSpan => VTimeSpan.ToString(),
|
||||
_ => "Undefined",
|
||||
};
|
||||
|
||||
public enum TypeIndex : byte {
|
||||
None,
|
||||
Int32,
|
||||
Int64,
|
||||
UInt32,
|
||||
UInt64,
|
||||
Float32,
|
||||
Float64,
|
||||
DateTime,
|
||||
TimeSpan
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////
|
||||
// Implicit conversions
|
||||
//////////////////////////////////////////////////
|
||||
public static implicit operator int(PrimitiveVariant v) => v.GetInt32();
|
||||
public static implicit operator long(PrimitiveVariant v) => v.GetInt64();
|
||||
public static implicit operator uint(PrimitiveVariant v) => v.GetUInt32();
|
||||
public static implicit operator ulong(PrimitiveVariant v) => v.GetUInt64();
|
||||
public static implicit operator float(PrimitiveVariant v) => v.GetFloat32();
|
||||
public static implicit operator double(PrimitiveVariant v) => v.GetFloat64();
|
||||
public static implicit operator DateTime(PrimitiveVariant v) => v.GetDateTime();
|
||||
public static implicit operator TimeSpan(PrimitiveVariant v) => v.GetTimeSpan();
|
||||
|
||||
public static implicit operator PrimitiveVariant(int i) => new(i);
|
||||
public static implicit operator PrimitiveVariant(long l) => new(l);
|
||||
public static implicit operator PrimitiveVariant(uint ui) => new(ui);
|
||||
public static implicit operator PrimitiveVariant(ulong ul) => new(ul);
|
||||
public static implicit operator PrimitiveVariant(float f) => new(f);
|
||||
public static implicit operator PrimitiveVariant(double d) => new(d);
|
||||
public static implicit operator PrimitiveVariant(DateTime dt) => new(dt);
|
||||
public static implicit operator PrimitiveVariant(TimeSpan ts) => new(ts);
|
||||
|
||||
//////////////////////////////////////////////////
|
||||
// Comparison operators
|
||||
//////////////////////////////////////////////////
|
||||
public static bool operator ==(PrimitiveVariant left, PrimitiveVariant right) => left.Equals(right);
|
||||
public static bool operator !=(PrimitiveVariant left, PrimitiveVariant right) => !left.Equals(right);
|
||||
|
||||
public static bool operator ==(PrimitiveVariant left, int right) => left.GetInt32() == right;
|
||||
public static bool operator !=(PrimitiveVariant left, int right) => left.GetInt32() != right;
|
||||
public static bool operator ==(int left, PrimitiveVariant right) => left == right.GetInt32();
|
||||
public static bool operator !=(int left, PrimitiveVariant right) => left != right.GetInt32();
|
||||
public static bool operator >(PrimitiveVariant left, int right) => left.GetInt32() > right;
|
||||
public static bool operator <(PrimitiveVariant left, int right) => left.GetInt32() < right;
|
||||
public static bool operator >(int left, PrimitiveVariant right) => left > right.GetInt32();
|
||||
public static bool operator <(int left, PrimitiveVariant right) => left < right.GetInt32();
|
||||
|
||||
public static bool operator ==(PrimitiveVariant left, long right) => left.GetInt64() == right;
|
||||
public static bool operator !=(PrimitiveVariant left, long right) => left.GetInt64() != right;
|
||||
public static bool operator ==(long left, PrimitiveVariant right) => left == right.GetInt64();
|
||||
public static bool operator !=(long left, PrimitiveVariant right) => left != right.GetInt64();
|
||||
public static bool operator >(PrimitiveVariant left, long right) => left.GetInt64() > right;
|
||||
public static bool operator <(PrimitiveVariant left, long right) => left.GetInt64() < right;
|
||||
public static bool operator >(long left, PrimitiveVariant right) => left > right.GetInt64();
|
||||
public static bool operator <(long left, PrimitiveVariant right) => left < right.GetInt64();
|
||||
|
||||
public static bool operator ==(PrimitiveVariant left, uint right) => left.GetUInt32() == right;
|
||||
public static bool operator !=(PrimitiveVariant left, uint right) => left.GetUInt32() != right;
|
||||
public static bool operator ==(uint left, PrimitiveVariant right) => left == right.GetUInt32();
|
||||
public static bool operator !=(uint left, PrimitiveVariant right) => left != right.GetUInt32();
|
||||
public static bool operator >(PrimitiveVariant left, uint right) => left.GetUInt32() > right;
|
||||
public static bool operator <(PrimitiveVariant left, uint right) => left.GetUInt32() < right;
|
||||
public static bool operator >(uint left, PrimitiveVariant right) => left > right.GetUInt32();
|
||||
public static bool operator <(uint left, PrimitiveVariant right) => left < right.GetUInt32();
|
||||
|
||||
public static bool operator ==(PrimitiveVariant left, ulong right) => left.GetUInt64() == right;
|
||||
public static bool operator !=(PrimitiveVariant left, ulong right) => left.GetUInt64() != right;
|
||||
public static bool operator ==(ulong left, PrimitiveVariant right) => left == right.GetUInt64();
|
||||
public static bool operator !=(ulong left, PrimitiveVariant right) => left != right.GetUInt64();
|
||||
public static bool operator >(PrimitiveVariant left, ulong right) => left.GetUInt64() > right;
|
||||
public static bool operator <(PrimitiveVariant left, ulong right) => left.GetUInt64() < right;
|
||||
public static bool operator >(ulong left, PrimitiveVariant right) => left > right.GetUInt64();
|
||||
public static bool operator <(ulong left, PrimitiveVariant right) => left < right.GetUInt64();
|
||||
|
||||
public static bool operator ==(PrimitiveVariant left, float right) => left.GetFloat32() == right;
|
||||
public static bool operator !=(PrimitiveVariant left, float right) => left.GetFloat32() != right;
|
||||
public static bool operator ==(float left, PrimitiveVariant right) => left == right.GetFloat32();
|
||||
public static bool operator !=(float left, PrimitiveVariant right) => left != right.GetFloat32();
|
||||
public static bool operator >(PrimitiveVariant left, float right) => left.GetFloat32() > right;
|
||||
public static bool operator <(PrimitiveVariant left, float right) => left.GetFloat32() < right;
|
||||
public static bool operator >(float left, PrimitiveVariant right) => left > right.GetFloat32();
|
||||
public static bool operator <(float left, PrimitiveVariant right) => left < right.GetFloat32();
|
||||
|
||||
public static bool operator ==(PrimitiveVariant left, double right) => left.GetFloat64() == right;
|
||||
public static bool operator !=(PrimitiveVariant left, double right) => left.GetFloat64() != right;
|
||||
public static bool operator ==(double left, PrimitiveVariant right) => left == right.GetFloat64();
|
||||
public static bool operator !=(double left, PrimitiveVariant right) => left != right.GetFloat64();
|
||||
public static bool operator >(PrimitiveVariant left, double right) => left.GetFloat64() > right;
|
||||
public static bool operator <(PrimitiveVariant left, double right) => left.GetFloat64() < right;
|
||||
public static bool operator >(double left, PrimitiveVariant right) => left > right.GetFloat64();
|
||||
public static bool operator <(double left, PrimitiveVariant right) => left < right.GetFloat64();
|
||||
|
||||
public static bool operator ==(PrimitiveVariant left, DateTime right) => left.GetDateTime() == right;
|
||||
public static bool operator !=(PrimitiveVariant left, DateTime right) => left.GetDateTime() != right;
|
||||
public static bool operator ==(DateTime left, PrimitiveVariant right) => left == right.GetDateTime();
|
||||
public static bool operator !=(DateTime left, PrimitiveVariant right) => left != right.GetDateTime();
|
||||
public static bool operator >(PrimitiveVariant left, DateTime right) => left.GetDateTime() > right;
|
||||
public static bool operator <(PrimitiveVariant left, DateTime right) => left.GetDateTime() < right;
|
||||
public static bool operator >(DateTime left, PrimitiveVariant right) => left > right.GetDateTime();
|
||||
public static bool operator <(DateTime left, PrimitiveVariant right) => left < right.GetDateTime();
|
||||
|
||||
public static bool operator ==(PrimitiveVariant left, TimeSpan right) => left.GetTimeSpan() == right;
|
||||
public static bool operator !=(PrimitiveVariant left, TimeSpan right) => left.GetTimeSpan() != right;
|
||||
public static bool operator ==(TimeSpan left, PrimitiveVariant right) => left == right.GetTimeSpan();
|
||||
public static bool operator !=(TimeSpan left, PrimitiveVariant right) => left != right.GetTimeSpan();
|
||||
public static bool operator >(PrimitiveVariant left, TimeSpan right) => left.GetTimeSpan() > right;
|
||||
public static bool operator <(PrimitiveVariant left, TimeSpan right) => left.GetTimeSpan() < right;
|
||||
public static bool operator >(TimeSpan left, PrimitiveVariant right) => left > right.GetTimeSpan();
|
||||
public static bool operator <(TimeSpan left, PrimitiveVariant right) => left < right.GetTimeSpan();
|
||||
}
|
||||
@@ -0,0 +1,191 @@
|
||||
namespace Spark.Core;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
/// <summary>
|
||||
/// A c# equivalent to std::any in c++ that also supports strings. It holds a value for an arbitrary
|
||||
/// primitive or string with minimal memory footprint (32 bytes). For best performance when reading
|
||||
/// values, use the 'VFoo' fields. However, there is a drawback: fields don't support implicit
|
||||
/// conversions (e.g., int32 -> int64). If implicit conversion is needed, use the 'GetFoo' methods.
|
||||
/// </summary>
|
||||
public readonly struct Variant : IEquatable<Variant> {
|
||||
public readonly TypeIndex Index;
|
||||
|
||||
readonly PrimitiveVariant vPrimitive;
|
||||
readonly string vString;
|
||||
|
||||
public Variant(PrimitiveVariant any) {
|
||||
vPrimitive = any;
|
||||
Index = (TypeIndex)any.Index;
|
||||
}
|
||||
|
||||
public Variant(string value) {
|
||||
vString = value;
|
||||
Index = TypeIndex.String;
|
||||
}
|
||||
|
||||
public readonly int VInt32 => vPrimitive.VInt32;
|
||||
public readonly long VInt64 => vPrimitive.VInt64;
|
||||
public readonly uint VUInt32 => vPrimitive.VUInt32;
|
||||
public readonly ulong VUInt64 => vPrimitive.VUInt64;
|
||||
public readonly float VFloat32 => vPrimitive.VFloat32;
|
||||
public readonly double VFloat64 => vPrimitive.VFloat64;
|
||||
public readonly DateTime VDateTime => vPrimitive.VDateTime;
|
||||
public readonly TimeSpan VTimeSpan => vPrimitive.VTimeSpan;
|
||||
|
||||
public int GetInt32() => vPrimitive.GetInt32();
|
||||
public long GetInt64() => vPrimitive.GetInt64();
|
||||
public uint GetUInt32() => vPrimitive.GetUInt32();
|
||||
public ulong GetUInt64() => vPrimitive.GetUInt64();
|
||||
public float GetFloat32() => vPrimitive.GetFloat32();
|
||||
public double GetFloat64() => vPrimitive.GetFloat64();
|
||||
public DateTime GetDateTime() => vPrimitive.GetDateTime();
|
||||
public TimeSpan GetTimeSpan() => vPrimitive.GetTimeSpan();
|
||||
|
||||
public string GetString() =>
|
||||
Index switch {
|
||||
TypeIndex.String => vString,
|
||||
_ => throw new InvalidOperationException("Variant does not contain String.")
|
||||
};
|
||||
|
||||
public bool Equals(Variant other) {
|
||||
if (Index != other.Index) {
|
||||
return false;
|
||||
}
|
||||
return Index switch {
|
||||
TypeIndex.String => EqualityComparer<string>.Default.Equals(vString, other.vString),
|
||||
_ => vPrimitive.Equals(other.vPrimitive),
|
||||
};
|
||||
}
|
||||
|
||||
public override bool Equals([NotNullWhen(true)] object obj) {
|
||||
return base.Equals(obj);
|
||||
}
|
||||
|
||||
public override int GetHashCode() =>
|
||||
Index switch {
|
||||
TypeIndex.String => HashCode.Combine(Index, vString?.GetHashCode() ?? 0),
|
||||
_ => vPrimitive.GetHashCode(),
|
||||
};
|
||||
|
||||
public enum TypeIndex : byte {
|
||||
None = PrimitiveVariant.TypeIndex.None,
|
||||
Int32 = PrimitiveVariant.TypeIndex.Int32,
|
||||
Int64 = PrimitiveVariant.TypeIndex.Int64,
|
||||
UInt32 = PrimitiveVariant.TypeIndex.UInt32,
|
||||
UInt64 = PrimitiveVariant.TypeIndex.UInt64,
|
||||
Float32 = PrimitiveVariant.TypeIndex.Float32,
|
||||
Float64 = PrimitiveVariant.TypeIndex.Float64,
|
||||
DateTime = PrimitiveVariant.TypeIndex.DateTime,
|
||||
TimeSpan = PrimitiveVariant.TypeIndex.TimeSpan,
|
||||
String,
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////
|
||||
// Implicit conversions
|
||||
//////////////////////////////////////////////////
|
||||
public static implicit operator int(Variant v) => v.GetInt32();
|
||||
public static implicit operator long(Variant v) => v.GetInt64();
|
||||
public static implicit operator uint(Variant v) => v.GetUInt32();
|
||||
public static implicit operator ulong(Variant v) => v.GetUInt64();
|
||||
public static implicit operator float(Variant v) => v.GetFloat32();
|
||||
public static implicit operator double(Variant v) => v.GetFloat64();
|
||||
public static implicit operator DateTime(Variant v) => v.GetDateTime();
|
||||
public static implicit operator TimeSpan(Variant v) => v.GetTimeSpan();
|
||||
public static implicit operator string(Variant v) => v.GetString();
|
||||
|
||||
public static implicit operator Variant(int i) => new(i);
|
||||
public static implicit operator Variant(long l) => new(l);
|
||||
public static implicit operator Variant(uint ui) => new(ui);
|
||||
public static implicit operator Variant(ulong ul) => new(ul);
|
||||
public static implicit operator Variant(float f) => new(f);
|
||||
public static implicit operator Variant(double d) => new(d);
|
||||
public static implicit operator Variant(DateTime dt) => new(dt);
|
||||
public static implicit operator Variant(TimeSpan ts) => new(ts);
|
||||
public static implicit operator Variant(string s) => new(s);
|
||||
|
||||
//////////////////////////////////////////////////
|
||||
// Comparison operators
|
||||
//////////////////////////////////////////////////
|
||||
public static bool operator ==(Variant left, Variant right) => left.Equals(right);
|
||||
public static bool operator !=(Variant left, Variant right) => !left.Equals(right);
|
||||
|
||||
public static bool operator ==(Variant left, int right) => left.GetInt32() == right;
|
||||
public static bool operator !=(Variant left, int right) => left.GetInt32() != right;
|
||||
public static bool operator ==(int left, Variant right) => left == right.GetInt32();
|
||||
public static bool operator !=(int left, Variant right) => left != right.GetInt32();
|
||||
public static bool operator >(Variant left, int right) => left.GetInt32() > right;
|
||||
public static bool operator <(Variant left, int right) => left.GetInt32() < right;
|
||||
public static bool operator >(int left, Variant right) => left > right.GetInt32();
|
||||
public static bool operator <(int left, Variant right) => left < right.GetInt32();
|
||||
|
||||
public static bool operator ==(Variant left, long right) => left.GetInt64() == right;
|
||||
public static bool operator !=(Variant left, long right) => left.GetInt64() != right;
|
||||
public static bool operator ==(long left, Variant right) => left == right.GetInt64();
|
||||
public static bool operator !=(long left, Variant right) => left != right.GetInt64();
|
||||
public static bool operator >(Variant left, long right) => left.GetInt64() > right;
|
||||
public static bool operator <(Variant left, long right) => left.GetInt64() < right;
|
||||
public static bool operator >(long left, Variant right) => left > right.GetInt64();
|
||||
public static bool operator <(long left, Variant right) => left < right.GetInt64();
|
||||
|
||||
public static bool operator ==(Variant left, uint right) => left.GetUInt32() == right;
|
||||
public static bool operator !=(Variant left, uint right) => left.GetUInt32() != right;
|
||||
public static bool operator ==(uint left, Variant right) => left == right.GetUInt32();
|
||||
public static bool operator !=(uint left, Variant right) => left != right.GetUInt32();
|
||||
public static bool operator >(Variant left, uint right) => left.GetUInt32() > right;
|
||||
public static bool operator <(Variant left, uint right) => left.GetUInt32() < right;
|
||||
public static bool operator >(uint left, Variant right) => left > right.GetUInt32();
|
||||
public static bool operator <(uint left, Variant right) => left < right.GetUInt32();
|
||||
|
||||
public static bool operator ==(Variant left, ulong right) => left.GetUInt64() == right;
|
||||
public static bool operator !=(Variant left, ulong right) => left.GetUInt64() != right;
|
||||
public static bool operator ==(ulong left, Variant right) => left == right.GetUInt64();
|
||||
public static bool operator !=(ulong left, Variant right) => left != right.GetUInt64();
|
||||
public static bool operator >(Variant left, ulong right) => left.GetUInt64() > right;
|
||||
public static bool operator <(Variant left, ulong right) => left.GetUInt64() < right;
|
||||
public static bool operator >(ulong left, Variant right) => left > right.GetUInt64();
|
||||
public static bool operator <(ulong left, Variant right) => left < right.GetUInt64();
|
||||
|
||||
public static bool operator ==(Variant left, float right) => left.GetFloat32() == right;
|
||||
public static bool operator !=(Variant left, float right) => left.GetFloat32() != right;
|
||||
public static bool operator ==(float left, Variant right) => left == right.GetFloat32();
|
||||
public static bool operator !=(float left, Variant right) => left != right.GetFloat32();
|
||||
public static bool operator >(Variant left, float right) => left.GetFloat32() > right;
|
||||
public static bool operator <(Variant left, float right) => left.GetFloat32() < right;
|
||||
public static bool operator >(float left, Variant right) => left > right.GetFloat32();
|
||||
public static bool operator <(float left, Variant right) => left < right.GetFloat32();
|
||||
|
||||
public static bool operator ==(Variant left, double right) => left.GetFloat64() == right;
|
||||
public static bool operator !=(Variant left, double right) => left.GetFloat64() != right;
|
||||
public static bool operator ==(double left, Variant right) => left == right.GetFloat64();
|
||||
public static bool operator !=(double left, Variant right) => left != right.GetFloat64();
|
||||
public static bool operator >(Variant left, double right) => left.GetFloat64() > right;
|
||||
public static bool operator <(Variant left, double right) => left.GetFloat64() < right;
|
||||
public static bool operator >(double left, Variant right) => left > right.GetFloat64();
|
||||
public static bool operator <(double left, Variant right) => left < right.GetFloat64();
|
||||
|
||||
public static bool operator ==(Variant left, DateTime right) => left.GetDateTime() == right;
|
||||
public static bool operator !=(Variant left, DateTime right) => left.GetDateTime() != right;
|
||||
public static bool operator ==(DateTime left, Variant right) => left == right.GetDateTime();
|
||||
public static bool operator !=(DateTime left, Variant right) => left != right.GetDateTime();
|
||||
public static bool operator >(Variant left, DateTime right) => left.GetDateTime() > right;
|
||||
public static bool operator <(Variant left, DateTime right) => left.GetDateTime() < right;
|
||||
public static bool operator >(DateTime left, Variant right) => left > right.GetDateTime();
|
||||
public static bool operator <(DateTime left, Variant right) => left < right.GetDateTime();
|
||||
|
||||
public static bool operator ==(Variant left, TimeSpan right) => left.GetTimeSpan() == right;
|
||||
public static bool operator !=(Variant left, TimeSpan right) => left.GetTimeSpan() != right;
|
||||
public static bool operator ==(TimeSpan left, Variant right) => left == right.GetTimeSpan();
|
||||
public static bool operator !=(TimeSpan left, Variant right) => left != right.GetTimeSpan();
|
||||
public static bool operator >(Variant left, TimeSpan right) => left.GetTimeSpan() > right;
|
||||
public static bool operator <(Variant left, TimeSpan right) => left.GetTimeSpan() < right;
|
||||
public static bool operator >(TimeSpan left, Variant right) => left > right.GetTimeSpan();
|
||||
public static bool operator <(TimeSpan left, Variant right) => left < right.GetTimeSpan();
|
||||
|
||||
public static bool operator ==(Variant left, string right) => left.GetString() == right;
|
||||
public static bool operator !=(Variant left, string right) => left.GetString() != right;
|
||||
public static bool operator ==(string left, Variant right) => left == right.GetString();
|
||||
public static bool operator !=(string left, Variant right) => left != right.GetString();
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
namespace bts.tests;
|
||||
|
||||
using bts.test;
|
||||
|
||||
using Spark.Core;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
[TestGroup]
|
||||
class VariantTests {
|
||||
[Test]
|
||||
public void TestPrimitiveVariant() {
|
||||
int intv = 4;
|
||||
double dblv = 5.75;
|
||||
DateTime datev = new DateTime(2025, 5, 5);
|
||||
|
||||
var pvint = new PrimitiveVariant(intv);
|
||||
var pvdbl = new PrimitiveVariant(dblv);
|
||||
var pvdate = new PrimitiveVariant(datev);
|
||||
|
||||
Assert.That(pvint == intv);
|
||||
Assert.That(pvdbl == dblv);
|
||||
Assert.That(pvdate == datev);
|
||||
Assert.That(intv == pvint);
|
||||
Assert.That(dblv == pvdbl);
|
||||
Assert.That(datev == pvdate);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestVariant() {
|
||||
int intv = 4;
|
||||
double dblv = 5.75;
|
||||
DateTime datev = new DateTime(2025, 5, 5);
|
||||
string strv = "abc";
|
||||
|
||||
var vint = new Variant(intv);
|
||||
var vdbl = new Variant(dblv);
|
||||
var vdate = new Variant(datev);
|
||||
var vstr = new Variant(strv);
|
||||
|
||||
Assert.That(vint == intv);
|
||||
Assert.That(vdbl == dblv);
|
||||
Assert.That(vdate == datev);
|
||||
Assert.That(vstr == strv);
|
||||
Assert.That(intv == vint);
|
||||
Assert.That(dblv == vdbl);
|
||||
Assert.That(datev == vdate);
|
||||
Assert.That(strv == vstr);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestMemoryFootprint() {
|
||||
// Test that PrimitiveVariant is 16 bytes, regardless of the underlying type.
|
||||
var pvs = new PrimitiveVariant[] {
|
||||
4, 4L, 4U, 4UL, 4F, 4D, new DateTime(2025, 5, 5),
|
||||
};
|
||||
Assert.That(pvs.All(pv => Marshal.SizeOf(pv) == 16));
|
||||
// Test that Variant is 32 bytes, regardless of the underlying type.
|
||||
// Note: the size of a struct that allocates memory for each underlying type
|
||||
// is 64 bytes. An example of such a struct is below:
|
||||
// struct FullyAllocatedVariant {
|
||||
// int vInt32;
|
||||
// long vInt64;
|
||||
// uint vUInt32;
|
||||
// ulong vUInt64;
|
||||
// float vFloat32;
|
||||
// double vFloat64;
|
||||
// DateTime vDateTime;
|
||||
// string vString;
|
||||
// }
|
||||
var vs = new Variant[] {
|
||||
4, 4L, 4U, 4UL, 4F, 4D, new DateTime(2025, 5, 5),
|
||||
};
|
||||
Assert.That(vs.All(v => Marshal.SizeOf(v) == 32));
|
||||
}
|
||||
|
||||
// [Test]
|
||||
public void TestPerformance() {
|
||||
// This isn't really a test (makes no asserts). It's not even a "proper" benchmark test,
|
||||
// since the environment in which the test runs is irregular. It's a quick/cheap sanity
|
||||
// check for performance.
|
||||
const int NUM_TESTS = 1024;
|
||||
const int SAMPLE_SIZE = 32768;
|
||||
var nums = new int[SAMPLE_SIZE];
|
||||
var pvNums = new PrimitiveVariant[SAMPLE_SIZE];
|
||||
var vNums = new Variant[SAMPLE_SIZE];
|
||||
var oNums = new object[SAMPLE_SIZE];
|
||||
var timer = new Stopwatch();
|
||||
var results = new TimeSpan[NUM_TESTS];
|
||||
var pvResults = new TimeSpan[NUM_TESTS];
|
||||
var vResults = new TimeSpan[NUM_TESTS];
|
||||
var oResults = new TimeSpan[NUM_TESTS];
|
||||
var rand = new Random();
|
||||
for (int i = 0; i < NUM_TESTS; i++) {
|
||||
for (int j = 0; j < nums.Length; j++) {
|
||||
nums[j] = rand.Next(SAMPLE_SIZE * 4);
|
||||
pvNums[j] = nums[j];
|
||||
vNums[j] = nums[j];
|
||||
oNums[j] = nums[j];
|
||||
}
|
||||
// Regular ints
|
||||
timer.Restart();
|
||||
computeSum(nums);
|
||||
timer.Stop();
|
||||
results[i] = timer.Elapsed;
|
||||
// PrimitiveVariant ints
|
||||
timer.Restart();
|
||||
computeSum(pvNums);
|
||||
timer.Stop();
|
||||
pvResults[i] = timer.Elapsed;
|
||||
// Variant ints
|
||||
timer.Restart();
|
||||
computeSum(vNums);
|
||||
timer.Stop();
|
||||
vResults[i] = timer.Elapsed;
|
||||
// Object ints
|
||||
timer.Restart();
|
||||
computeSum(oNums, oNums.Length - 1);
|
||||
timer.Stop();
|
||||
oResults[i] = timer.Elapsed;
|
||||
}
|
||||
Console.WriteLine($"Baseline : {computeSum(results)}");
|
||||
Console.WriteLine($"Baseline longest : {results.Max()}");
|
||||
Console.WriteLine($"Baseline shortest : {results.Min()}");
|
||||
Console.WriteLine($"PrimitiveVariant : {computeSum(pvResults)}");
|
||||
Console.WriteLine($"PrimitiveVariant longest : {pvResults.Max()}");
|
||||
Console.WriteLine($"PrimitiveVariant shortest : {pvResults.Min()}");
|
||||
Console.WriteLine($"Variant : {computeSum(vResults)}");
|
||||
Console.WriteLine($"Variant longest : {vResults.Max()}");
|
||||
Console.WriteLine($"Variant shortest : {vResults.Min()}");
|
||||
Console.WriteLine($"Object : {computeSum(oResults)}");
|
||||
Console.WriteLine($"Object longest : {oResults.Max()}");
|
||||
Console.WriteLine($"Object shortest : {oResults.Min()}");
|
||||
}
|
||||
|
||||
static TimeSpan computeSum(TimeSpan[] nums) {
|
||||
TimeSpan sum = TimeSpan.Zero;
|
||||
for (int i = 0; i < nums.Length; i++) {
|
||||
sum += nums[i];
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
static int computeSum(int[] nums) {
|
||||
int sum = 0;
|
||||
for (int i = 0; i < nums.Length; i++) {
|
||||
sum += nums[i];
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
static int computeSum(PrimitiveVariant[] nums) {
|
||||
int sum = 0;
|
||||
for (int i = 0; i < nums.Length; i++) {
|
||||
sum += nums[i].VInt32;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
static int computeSum(Variant[] nums) {
|
||||
int sum = 0;
|
||||
for (int i = 0; i < nums.Length; i++) {
|
||||
sum += nums[i].VInt32;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
static int computeSum(object[] nums, int length) {
|
||||
int sum = 0;
|
||||
for (int i = 0; i < length; i++) {
|
||||
sum += (int)nums[i];
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user