RangeList.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Ryujinx.Graphics.Gpu.Memory
  4. {
  5. class RangeList<T> where T : IRange<T>
  6. {
  7. private const int ArrayGrowthSize = 32;
  8. private List<T> _items;
  9. public RangeList()
  10. {
  11. _items = new List<T>();
  12. }
  13. public void Add(T item)
  14. {
  15. int index = BinarySearch(item.Address);
  16. if (index < 0)
  17. {
  18. index = ~index;
  19. }
  20. _items.Insert(index, item);
  21. }
  22. public bool Remove(T item)
  23. {
  24. int index = BinarySearch(item.Address);
  25. if (index >= 0)
  26. {
  27. while (index > 0 && _items[index - 1].Address == item.Address)
  28. {
  29. index--;
  30. }
  31. while (index < _items.Count)
  32. {
  33. if (_items[index].Equals(item))
  34. {
  35. _items.RemoveAt(index);
  36. return true;
  37. }
  38. if (_items[index].Address > item.Address)
  39. {
  40. break;
  41. }
  42. index++;
  43. }
  44. }
  45. return false;
  46. }
  47. public T FindFirstOverlap(T item)
  48. {
  49. return FindFirstOverlap(item.Address, item.Size);
  50. }
  51. public T FindFirstOverlap(ulong address, ulong size)
  52. {
  53. int index = BinarySearch(address, size);
  54. if (index < 0)
  55. {
  56. return default(T);
  57. }
  58. return _items[index];
  59. }
  60. public int FindOverlaps(T item, ref T[] output)
  61. {
  62. return FindOverlaps(item.Address, item.Size, ref output);
  63. }
  64. public int FindOverlaps(ulong address, ulong size, ref T[] output)
  65. {
  66. int outputIndex = 0;
  67. ulong endAddress = address + size;
  68. lock (_items)
  69. {
  70. foreach (T item in _items)
  71. {
  72. if (item.Address >= endAddress)
  73. {
  74. break;
  75. }
  76. if (item.OverlapsWith(address, size))
  77. {
  78. if (outputIndex == output.Length)
  79. {
  80. Array.Resize(ref output, outputIndex + ArrayGrowthSize);
  81. }
  82. output[outputIndex++] = item;
  83. }
  84. }
  85. }
  86. return outputIndex;
  87. }
  88. public int FindOverlapsNonOverlapping(T item, ref T[] output)
  89. {
  90. return FindOverlapsNonOverlapping(item.Address, item.Size, ref output);
  91. }
  92. public int FindOverlapsNonOverlapping(ulong address, ulong size, ref T[] output)
  93. {
  94. // This is a bit faster than FindOverlaps, but only works
  95. // when none of the items on the list overlaps with each other.
  96. int outputIndex = 0;
  97. ulong endAddress = address + size;
  98. int index = BinarySearch(address, size);
  99. if (index >= 0)
  100. {
  101. while (index > 0 && _items[index - 1].OverlapsWith(address, size))
  102. {
  103. index--;
  104. }
  105. do
  106. {
  107. if (outputIndex == output.Length)
  108. {
  109. Array.Resize(ref output, outputIndex + ArrayGrowthSize);
  110. }
  111. output[outputIndex++] = _items[index++];
  112. }
  113. while (index < _items.Count && _items[index].OverlapsWith(address, size));
  114. }
  115. return outputIndex;
  116. }
  117. public int FindOverlaps(ulong address, ref T[] output)
  118. {
  119. int index = BinarySearch(address);
  120. int outputIndex = 0;
  121. if (index >= 0)
  122. {
  123. while (index > 0 && _items[index - 1].Address == address)
  124. {
  125. index--;
  126. }
  127. while (index < _items.Count)
  128. {
  129. T overlap = _items[index++];
  130. if (overlap.Address != address)
  131. {
  132. break;
  133. }
  134. if (outputIndex == output.Length)
  135. {
  136. Array.Resize(ref output, outputIndex + ArrayGrowthSize);
  137. }
  138. output[outputIndex++] = overlap;
  139. }
  140. }
  141. return outputIndex;
  142. }
  143. private int BinarySearch(ulong address)
  144. {
  145. int left = 0;
  146. int right = _items.Count - 1;
  147. while (left <= right)
  148. {
  149. int range = right - left;
  150. int middle = left + (range >> 1);
  151. T item = _items[middle];
  152. if (item.Address == address)
  153. {
  154. return middle;
  155. }
  156. if (address < item.Address)
  157. {
  158. right = middle - 1;
  159. }
  160. else
  161. {
  162. left = middle + 1;
  163. }
  164. }
  165. return ~left;
  166. }
  167. private int BinarySearch(ulong address, ulong size)
  168. {
  169. int left = 0;
  170. int right = _items.Count - 1;
  171. while (left <= right)
  172. {
  173. int range = right - left;
  174. int middle = left + (range >> 1);
  175. T item = _items[middle];
  176. if (item.OverlapsWith(address, size))
  177. {
  178. return middle;
  179. }
  180. if (address < item.Address)
  181. {
  182. right = middle - 1;
  183. }
  184. else
  185. {
  186. left = middle + 1;
  187. }
  188. }
  189. return ~left;
  190. }
  191. }
  192. }