Files
QuanTAlib/lib/core/tvalue/tvalue.cs
T

58 lines
1.8 KiB
C#
Raw Normal View History

2024-10-27 16:11:08 -07:00
using System.Runtime.CompilerServices;
2024-09-22 17:31:24 -07:00
namespace QuanTAlib;
2025-11-25 20:40:46 -08:00
/// <summary>
/// A lightweight struct representing a time-value pair.
/// Pure data type: 16 bytes (long + double).
/// </summary>
2024-10-27 16:11:08 -07:00
[SkipLocalsInit]
2025-11-25 20:40:46 -08:00
public readonly struct TValue : IEquatable<TValue>
2024-09-22 17:31:24 -07:00
{
2025-11-25 20:40:46 -08:00
/// <summary>
/// Time in ticks (UTC).
/// </summary>
public readonly long Time;
/// <summary>
/// The value.
/// </summary>
public readonly double Value;
/// <summary>
/// Convenience property to get DateTime from Ticks.
/// </summary>
public DateTime AsDateTime => new(Time, DateTimeKind.Utc);
2024-09-22 17:31:24 -07:00
2024-10-27 16:11:08 -07:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2025-11-25 20:40:46 -08:00
public TValue(long time, double value)
{
Time = time;
Value = value;
}
2024-09-22 17:31:24 -07:00
2024-10-27 16:11:08 -07:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2025-11-25 20:40:46 -08:00
public TValue(DateTime time, double value)
{
Time = time.Ticks;
Value = value;
}
2024-10-27 16:11:08 -07:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator double(TValue tv) => tv.Value;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2025-11-25 20:40:46 -08:00
public static implicit operator DateTime(TValue tv) => new(tv.Time, DateTimeKind.Utc);
2024-10-27 16:11:08 -07:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2025-11-25 20:40:46 -08:00
public override string ToString() => $"[{AsDateTime:yyyy-MM-dd HH:mm:ss}, {Value:F2}]";
2024-10-27 16:11:08 -07:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2025-11-25 20:40:46 -08:00
public bool Equals(TValue other) => Time == other.Time && Value == other.Value;
public override bool Equals(object? obj) => obj is TValue other && Equals(other);
public override int GetHashCode() => HashCode.Combine(Time, Value);
public static bool operator ==(TValue left, TValue right) => left.Equals(right);
public static bool operator !=(TValue left, TValue right) => !left.Equals(right);
2024-09-22 17:31:24 -07:00
}