ByteRange.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. namespace MatrixIO.IO
  3. {
  4. public struct ByteRange
  5. {
  6. private long _Start;
  7. public long Start { get { return _Start; } }
  8. private long _End;
  9. public long End { get { return _End; } }
  10. private long _Length;
  11. public long Length { get { return _Length; } }
  12. public ByteRange(long start, long end)
  13. {
  14. if (start < 0 || end < 0) throw new FormatException("ByteRange does not support negative Start and End values.");
  15. if (start > end)
  16. {
  17. _Start = end;
  18. _End = start;
  19. }
  20. else
  21. {
  22. _Start = start;
  23. _End = end;
  24. }
  25. _Length = _End - _Start;
  26. }
  27. public bool Contains(long offset)
  28. {
  29. if (offset >= Start && offset <= End) return true;
  30. else return false;
  31. }
  32. public bool Contains(ByteRange range)
  33. {
  34. if (range.Start >= Start && range.End <= End) return true;
  35. return false;
  36. }
  37. public bool Contains(long start, long end)
  38. {
  39. if (start >= Start && end <= End) return true;
  40. else return false;
  41. }
  42. public bool Overlaps(ByteRange range)
  43. {
  44. if (Contains(range.Start) || Contains(range.End)) return true;
  45. else return false;
  46. }
  47. public bool Overlaps(long start, long end)
  48. {
  49. if (Contains(start) || Contains(end)) return true;
  50. else return false;
  51. }
  52. #region Operator Overloads
  53. public static bool operator <(ByteRange a, ByteRange b)
  54. {
  55. if (a.Start > b.Start && a.End <= b.End) return true;
  56. else if (a.Start >= b.Start && a.End < b.End) return true;
  57. else return false;
  58. }
  59. public static bool operator >(ByteRange a, ByteRange b)
  60. {
  61. if (a.Start < b.Start && a.End >= b.End) return true;
  62. else if (a.Start <= b.Start && a.End > b.End) return true;
  63. else return false;
  64. }
  65. public static bool operator ==(ByteRange a, ByteRange b)
  66. {
  67. return a.Equals(b);
  68. }
  69. public static bool operator !=(ByteRange a, ByteRange b)
  70. {
  71. return !(a == b);
  72. }
  73. public static bool operator >=(ByteRange a, ByteRange b)
  74. {
  75. if (a == b) return true;
  76. else if (a > b) return true;
  77. else return false;
  78. }
  79. public static bool operator <=(ByteRange a, ByteRange b)
  80. {
  81. if (a == b) return true;
  82. else if (a < b) return true;
  83. else return false;
  84. }
  85. public override bool Equals(object obj)
  86. {
  87. return base.Equals(obj) && ((ByteRange)obj).Start == Start && ((ByteRange)obj).End == End;
  88. }
  89. private const long TOP_MASK = unchecked((long)0xFFFFFFFF00000000);
  90. private const long BOTTOM_MASK = 0x00000000FFFFFFFF;
  91. public override int GetHashCode()
  92. {
  93. int hashcode = (int)((Start & TOP_MASK) >> 32);
  94. hashcode ^= (int)(Start & BOTTOM_MASK);
  95. hashcode ^= (int)((End & TOP_MASK) >> 32);
  96. hashcode ^= (int)(End & BOTTOM_MASK);
  97. return hashcode;
  98. }
  99. #endregion
  100. public static readonly ByteRange MaxValue = new ByteRange(0, long.MaxValue);
  101. public static readonly ByteRange MinValue = new ByteRange(0, 0);
  102. }
  103. }