ValueRange.cs 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. namespace MeloongCore;
  2. /// <summary>
  3. /// 表示一个由可选下界和可选上界组成的值范围。
  4. /// </summary>
  5. public sealed class ValueRange<T>(
  6. T? lower, T? upper,
  7. bool isLowerInclusive, bool isUpperInclusive,
  8. bool hasLower, bool hasUpper
  9. ) where T : notnull, IComparable<T> {
  10. public T? Lower { get; private set; } = hasLower && lower is null ? throw new ArgumentNullException(nameof(lower)) : lower;
  11. public T? Upper { get; private set; } = hasUpper && upper is null ? throw new ArgumentNullException(nameof(upper)) : upper;
  12. public bool HasLower { get; private set; } = hasLower;
  13. public bool HasUpper { get; private set; } = hasUpper;
  14. public void ClearLower() { Lower = default; HasLower = false; }
  15. public void ClearUpper() { Upper = default; HasUpper = false; }
  16. public void SetLower(T lower, bool isInclusive) {
  17. Lower = lower;
  18. HasLower = true;
  19. IsLowerInclusive = isInclusive;
  20. }
  21. public void SetUpper(T upper, bool isInclusive) {
  22. Upper = upper;
  23. HasUpper = true;
  24. IsUpperInclusive = isInclusive;
  25. }
  26. /// <summary> 下界值是否为闭区间。 </summary>
  27. public bool IsLowerInclusive = isLowerInclusive;
  28. /// <summary> 上界值是否为闭区间。 </summary>
  29. public bool IsUpperInclusive = isUpperInclusive;
  30. #region 工厂方法
  31. /// <summary> 创建等同于 <c>[lower, upper]</c> 的闭区间。 </summary>
  32. public static ValueRange<T> Closed(T lower, T upper) => new(lower, upper, true, true, true, true);
  33. /// <summary> 创建形如 <c>[lower, upper)</c> 的左闭右开区间。 </summary>
  34. public static ValueRange<T> ClosedOpen(T lower, T upper) => new(lower, upper, true, false, true, true);
  35. /// <summary> 创建形如 <c>(lower, upper]</c> 的左开右闭区间。 </summary>
  36. public static ValueRange<T> OpenClosed(T lower, T upper) => new(lower, upper, false, true, true, true);
  37. /// <summary> 创建形如 <c>(lower, upper)</c> 的开区间。 </summary>
  38. public static ValueRange<T> Open(T lower, T upper) => new(lower, upper, false, false, true, true);
  39. /// <summary> 创建形如 <c>[value, value]</c> 的单值闭区间。 </summary>
  40. public static ValueRange<T> Exactly(T value) => new(value, value, true, true, true, true);
  41. /// <summary> 创建形如 <c>[lower, +∞)</c> 的范围。 </summary>
  42. public static ValueRange<T> AtLeast(T lower) => new(lower, default, true, true, true, false);
  43. /// <summary> 创建形如 <c>(lower, +∞)</c> 的范围。 </summary>
  44. public static ValueRange<T> GreaterThan(T lower) => new(lower, default, false, true, true, false);
  45. /// <summary> 创建形如 <c>(-∞, upper]</c> 的范围。 </summary>
  46. public static ValueRange<T> AtMost(T upper) => new(default, upper, true, true, false, true);
  47. /// <summary> 创建形如 <c>(-∞, upper)</c> 的范围。 </summary>
  48. public static ValueRange<T> LessThan(T upper) => new(default, upper, true, false, false, true);
  49. /// <summary> 创建形如 <c>(-∞, +∞)</c> 的无限范围。 </summary>
  50. public static ValueRange<T> All() => new(default, default, true, true, false, false);
  51. #endregion
  52. /// <summary>
  53. /// 判断指定值是否位于当前范围内。
  54. /// </summary>
  55. public bool Contains(T value) {
  56. if (HasLower) {
  57. var compareLower = value.CompareTo(Lower!);
  58. if (compareLower < 0) return false;
  59. if (compareLower == 0 && !IsLowerInclusive) return false;
  60. }
  61. if (HasUpper) {
  62. var compareUpper = value.CompareTo(Upper!);
  63. if (compareUpper > 0) return false;
  64. if (compareUpper == 0 && !IsUpperInclusive) return false;
  65. }
  66. return true;
  67. }
  68. public override string ToString()
  69. => $"{(HasLower && IsLowerInclusive ? "[" : "(")}{(HasLower ? Lower!.ToString() : "-∞")}, {(HasUpper ? Upper!.ToString() : "+∞")}{(HasUpper && IsUpperInclusive ? "]" : ")")}";
  70. }