C#动态数组原理

作者:追风剑情 发布于:2014-5-23 14:38 分类:C#

以下代码摘自NGUI 2.7

  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. /// <summary>
  5. /// 这是改进版的System.Collections.Generic.List
  6. /// </summary>
  7.  
  8. public class BetterList<T>
  9. {
  10. /// <summary>
  11. /// 缓冲区
  12. /// </summary>
  13.  
  14. public T[] buffer;
  15.  
  16. /// <summary>
  17. /// 缓冲区大小
  18. /// </summary>
  19.  
  20. public int size = 0;
  21.  
  22. /// <summary>
  23. /// 数组迭代器
  24. /// </summary>
  25.  
  26. public IEnumerator<T> GetEnumerator ()
  27. {
  28. if (buffer != null)
  29. {
  30. for (int i = 0; i < size; ++i)
  31. {
  32. yield return buffer[i];
  33. }
  34. }
  35. }
  36. /// <summary>
  37. /// 索引器
  38. /// </summary>
  39. public T this[int i]
  40. {
  41. get { return buffer[i]; }
  42. set { buffer[i] = value; }
  43. }
  44.  
  45. /// <summary>
  46. /// 动态增加数组长度
  47. /// </summary>
  48.  
  49. void AllocateMore ()
  50. {
  51. T[] newList = (buffer != null) ? new T[Math.Max(buffer.Length << 1, 32)] : new T[32];
  52. if (buffer != null && size > 0) buffer.CopyTo(newList, 0);
  53. buffer = newList;
  54. }
  55.  
  56. /// <summary>
  57. /// 去除不必要的内存
  58. /// </summary>
  59.  
  60. void Trim ()
  61. {
  62. if (size > 0)
  63. {
  64. if (size < buffer.Length)
  65. {
  66. T[] newList = new T[size];
  67. for (int i = 0; i < size; ++i) newList[i] = buffer[i];
  68. buffer = newList;
  69. }
  70. }
  71. else buffer = null;
  72. }
  73.  
  74. /// <summary>
  75. /// 清除数组中的项
  76. /// </summary>
  77.  
  78. public void Clear () { size = 0; }
  79.  
  80. /// <summary>
  81. /// 清除数组中的项,并释放内存
  82. /// </summary>
  83.  
  84. public void Release () { size = 0; buffer = null; }
  85.  
  86. /// <summary>
  87. /// 添加项
  88. /// </summary>
  89.  
  90. public void Add (T item)
  91. {
  92. if (buffer == null || size == buffer.Length) AllocateMore();
  93. buffer[size++] = item;
  94. }
  95.  
  96. /// <summary>
  97. /// 在数组的指定位置插入一项
  98. /// </summary>
  99.  
  100. public void Insert (int index, T item)
  101. {
  102. if (buffer == null || size == buffer.Length) AllocateMore();
  103.  
  104. if (index < size)
  105. {
  106. for (int i = size; i > index; --i) buffer[i] = buffer[i - 1];
  107. buffer[index] = item;
  108. ++size;
  109. }
  110. else Add(item);
  111. }
  112.  
  113. /// <summary>
  114. /// 检查数组中是否存在指定项 true:存在 false:不存在
  115. /// </summary>
  116.  
  117. public bool Contains (T item)
  118. {
  119. if (buffer == null) return false;
  120. for (int i = 0; i < size; ++i)
  121. {
  122. if (buffer[i] == null)
  123. continue;
  124. if (buffer[i].Equals(item))
  125. return true;
  126. }
  127. return false;
  128. }
  129.  
  130. /// <summary>
  131. /// 移除指定项
  132. /// </summary>
  133.  
  134. public bool Remove (T item)
  135. {
  136. if (buffer != null)
  137. {
  138. EqualityComparer<T> comp = EqualityComparer<T>.Default;
  139.  
  140. for (int i = 0; i < size; ++i)
  141. {
  142. if (comp.Equals(buffer[i], item))
  143. {
  144. --size;
  145. buffer[i] = default(T);
  146. for (int b = i; b < size; ++b) buffer[b] = buffer[b + 1];
  147. return true;
  148. }
  149. }
  150. }
  151. return false;
  152. }
  153.  
  154. /// <summary>
  155. /// 移除指定索引的项
  156. /// </summary>
  157.  
  158. public void RemoveAt (int index)
  159. {
  160. if (buffer != null && index < size)
  161. {
  162. --size;
  163. buffer[index] = default(T);
  164. for (int b = index; b < size; ++b) buffer[b] = buffer[b + 1];
  165. }
  166. }
  167.  
  168. /// <summary>
  169. /// 移除数组中最后一项
  170. /// </summary>
  171.  
  172. public T Pop ()
  173. {
  174. if (buffer != null && size != 0)
  175. {
  176. T val = buffer[--size];
  177. buffer[size] = default(T);
  178. return val;
  179. }
  180. return default(T);
  181. }
  182.  
  183. /// <summary>
  184. /// 返回一个长度为size的数组
  185. /// </summary>
  186.  
  187. public T[] ToArray () { Trim(); return buffer; }
  188.  
  189. /// <summary>
  190. /// 排序方法.
  191. /// </summary>
  192.  
  193. public void Sort (System.Comparison<T> comparer)
  194. {
  195. bool changed = true;
  196.  
  197. while (changed)
  198. {
  199. changed = false;
  200.  
  201. for (int i = 1; i < size; ++i)
  202. {
  203. if (comparer.Invoke(buffer[i - 1], buffer[i]) > 0)
  204. {
  205. T temp = buffer[i];
  206. buffer[i] = buffer[i - 1];
  207. buffer[i - 1] = temp;
  208. changed = true;
  209. }
  210. }
  211. }
  212. }
  213. }

 

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号