BetterList.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using UnityEngine;
  5. public class BetterList<T>
  6. {
  7. public IEnumerator<T> GetEnumerator()
  8. {
  9. if (this.buffer != null)
  10. {
  11. for (int i = 0; i < this.size; i++)
  12. {
  13. yield return this.buffer[i];
  14. }
  15. }
  16. yield break;
  17. }
  18. [DebuggerHidden]
  19. public T this[int i]
  20. {
  21. get
  22. {
  23. return this.buffer[i];
  24. }
  25. set
  26. {
  27. this.buffer[i] = value;
  28. }
  29. }
  30. private void AllocateMore()
  31. {
  32. T[] array = (this.buffer == null) ? new T[32] : new T[Mathf.Max(this.buffer.Length << 1, 32)];
  33. if (this.buffer != null && this.size > 0)
  34. {
  35. this.buffer.CopyTo(array, 0);
  36. }
  37. this.buffer = array;
  38. }
  39. private void Trim()
  40. {
  41. if (this.size > 0)
  42. {
  43. if (this.size < this.buffer.Length)
  44. {
  45. T[] array = new T[this.size];
  46. for (int i = 0; i < this.size; i++)
  47. {
  48. array[i] = this.buffer[i];
  49. }
  50. this.buffer = array;
  51. }
  52. }
  53. else
  54. {
  55. this.buffer = null;
  56. }
  57. }
  58. public void Clear()
  59. {
  60. this.size = 0;
  61. }
  62. public void Release()
  63. {
  64. this.size = 0;
  65. this.buffer = null;
  66. }
  67. public void Add(T item)
  68. {
  69. if (this.buffer == null || this.size == this.buffer.Length)
  70. {
  71. this.AllocateMore();
  72. }
  73. this.buffer[this.size++] = item;
  74. }
  75. public void Insert(int index, T item)
  76. {
  77. if (this.buffer == null || this.size == this.buffer.Length)
  78. {
  79. this.AllocateMore();
  80. }
  81. if (index > -1 && index < this.size)
  82. {
  83. for (int i = this.size; i > index; i--)
  84. {
  85. this.buffer[i] = this.buffer[i - 1];
  86. }
  87. this.buffer[index] = item;
  88. this.size++;
  89. }
  90. else
  91. {
  92. this.Add(item);
  93. }
  94. }
  95. public bool Contains(T item)
  96. {
  97. if (this.buffer == null)
  98. {
  99. return false;
  100. }
  101. for (int i = 0; i < this.size; i++)
  102. {
  103. if (this.buffer[i].Equals(item))
  104. {
  105. return true;
  106. }
  107. }
  108. return false;
  109. }
  110. public int IndexOf(T item)
  111. {
  112. if (this.buffer == null)
  113. {
  114. return -1;
  115. }
  116. for (int i = 0; i < this.size; i++)
  117. {
  118. if (this.buffer[i].Equals(item))
  119. {
  120. return i;
  121. }
  122. }
  123. return -1;
  124. }
  125. public bool Remove(T item)
  126. {
  127. if (this.buffer != null)
  128. {
  129. EqualityComparer<T> @default = EqualityComparer<T>.Default;
  130. for (int i = 0; i < this.size; i++)
  131. {
  132. if (@default.Equals(this.buffer[i], item))
  133. {
  134. this.size--;
  135. this.buffer[i] = default(T);
  136. for (int j = i; j < this.size; j++)
  137. {
  138. this.buffer[j] = this.buffer[j + 1];
  139. }
  140. this.buffer[this.size] = default(T);
  141. return true;
  142. }
  143. }
  144. }
  145. return false;
  146. }
  147. public void RemoveAt(int index)
  148. {
  149. if (this.buffer != null && index > -1 && index < this.size)
  150. {
  151. this.size--;
  152. this.buffer[index] = default(T);
  153. for (int i = index; i < this.size; i++)
  154. {
  155. this.buffer[i] = this.buffer[i + 1];
  156. }
  157. this.buffer[this.size] = default(T);
  158. }
  159. }
  160. public T Pop()
  161. {
  162. if (this.buffer != null && this.size != 0)
  163. {
  164. T result = this.buffer[--this.size];
  165. this.buffer[this.size] = default(T);
  166. return result;
  167. }
  168. return default(T);
  169. }
  170. public T[] ToArray()
  171. {
  172. this.Trim();
  173. return this.buffer;
  174. }
  175. [DebuggerHidden]
  176. [DebuggerStepThrough]
  177. public void Sort(BetterList<T>.CompareFunc comparer)
  178. {
  179. int num = 0;
  180. int num2 = this.size - 1;
  181. bool flag = true;
  182. while (flag)
  183. {
  184. flag = false;
  185. for (int i = num; i < num2; i++)
  186. {
  187. if (comparer(this.buffer[i], this.buffer[i + 1]) > 0)
  188. {
  189. T t = this.buffer[i];
  190. this.buffer[i] = this.buffer[i + 1];
  191. this.buffer[i + 1] = t;
  192. flag = true;
  193. }
  194. else if (!flag)
  195. {
  196. num = ((i != 0) ? (i - 1) : 0);
  197. }
  198. }
  199. }
  200. }
  201. public T[] buffer;
  202. public int size;
  203. public delegate int CompareFunc(T left, T right);
  204. }