SRList`1.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using UnityEngine;
  6. namespace SRF
  7. {
  8. [Serializable]
  9. public class SRList<T> : IEnumerable<T>, IList<T>, ICollection<T>, IEnumerable, ISerializationCallbackReceiver
  10. {
  11. public SRList()
  12. {
  13. }
  14. public SRList(int capacity)
  15. {
  16. this.Buffer = new T[capacity];
  17. }
  18. public SRList(IEnumerable<T> source)
  19. {
  20. this.AddRange(source);
  21. }
  22. public T[] Buffer
  23. {
  24. get
  25. {
  26. return this._buffer;
  27. }
  28. private set
  29. {
  30. this._buffer = value;
  31. }
  32. }
  33. private EqualityComparer<T> EqualityComparer
  34. {
  35. get
  36. {
  37. if (this._equalityComparer == null)
  38. {
  39. this._equalityComparer = EqualityComparer<T>.Default;
  40. }
  41. return this._equalityComparer;
  42. }
  43. }
  44. public int Count
  45. {
  46. get
  47. {
  48. return this._count;
  49. }
  50. private set
  51. {
  52. this._count = value;
  53. }
  54. }
  55. public IEnumerator<T> GetEnumerator()
  56. {
  57. if (this.Buffer != null)
  58. {
  59. for (int i = 0; i < this.Count; i++)
  60. {
  61. yield return this.Buffer[i];
  62. }
  63. }
  64. yield break;
  65. }
  66. IEnumerator IEnumerable.GetEnumerator()
  67. {
  68. return this.GetEnumerator();
  69. }
  70. public void Add(T item)
  71. {
  72. if (this.Buffer == null || this.Count == this.Buffer.Length)
  73. {
  74. this.Expand();
  75. }
  76. this.Buffer[this.Count++] = item;
  77. }
  78. public void Clear()
  79. {
  80. this.Count = 0;
  81. }
  82. public bool Contains(T item)
  83. {
  84. if (this.Buffer == null)
  85. {
  86. return false;
  87. }
  88. for (int i = 0; i < this.Count; i++)
  89. {
  90. if (this.EqualityComparer.Equals(this.Buffer[i], item))
  91. {
  92. return true;
  93. }
  94. }
  95. return false;
  96. }
  97. public void CopyTo(T[] array, int arrayIndex)
  98. {
  99. this.Trim();
  100. this.Buffer.CopyTo(array, arrayIndex);
  101. }
  102. public bool Remove(T item)
  103. {
  104. if (this.Buffer == null)
  105. {
  106. return false;
  107. }
  108. int num = this.IndexOf(item);
  109. if (num < 0)
  110. {
  111. return false;
  112. }
  113. this.RemoveAt(num);
  114. return true;
  115. }
  116. public bool IsReadOnly
  117. {
  118. get
  119. {
  120. return false;
  121. }
  122. }
  123. public int IndexOf(T item)
  124. {
  125. if (this.Buffer == null)
  126. {
  127. return -1;
  128. }
  129. for (int i = 0; i < this.Count; i++)
  130. {
  131. if (this.EqualityComparer.Equals(this.Buffer[i], item))
  132. {
  133. return i;
  134. }
  135. }
  136. return -1;
  137. }
  138. public void Insert(int index, T item)
  139. {
  140. if (this.Buffer == null || this.Count == this.Buffer.Length)
  141. {
  142. this.Expand();
  143. }
  144. if (index < this.Count)
  145. {
  146. for (int i = this.Count; i > index; i--)
  147. {
  148. this.Buffer[i] = this.Buffer[i - 1];
  149. }
  150. this.Buffer[index] = item;
  151. this.Count++;
  152. }
  153. else
  154. {
  155. this.Add(item);
  156. }
  157. }
  158. public void RemoveAt(int index)
  159. {
  160. if (this.Buffer != null && index < this.Count)
  161. {
  162. this.Count--;
  163. this.Buffer[index] = default(T);
  164. for (int i = index; i < this.Count; i++)
  165. {
  166. this.Buffer[i] = this.Buffer[i + 1];
  167. }
  168. }
  169. }
  170. public T this[int index]
  171. {
  172. get
  173. {
  174. if (this.Buffer == null)
  175. {
  176. throw new IndexOutOfRangeException();
  177. }
  178. return this.Buffer[index];
  179. }
  180. set
  181. {
  182. if (this.Buffer == null)
  183. {
  184. throw new IndexOutOfRangeException();
  185. }
  186. this.Buffer[index] = value;
  187. }
  188. }
  189. public void OnBeforeSerialize()
  190. {
  191. UnityEngine.Debug.Log("[OnBeforeSerialize] Count: {0}".Fmt(new object[]
  192. {
  193. this._count
  194. }));
  195. this.Clean();
  196. }
  197. public void OnAfterDeserialize()
  198. {
  199. UnityEngine.Debug.Log("[OnAfterDeserialize] Count: {0}".Fmt(new object[]
  200. {
  201. this._count
  202. }));
  203. }
  204. public void AddRange(IEnumerable<T> range)
  205. {
  206. foreach (T item in range)
  207. {
  208. this.Add(item);
  209. }
  210. }
  211. public void Clear(bool clean)
  212. {
  213. this.Clear();
  214. if (!clean)
  215. {
  216. return;
  217. }
  218. this.Clean();
  219. }
  220. public void Clean()
  221. {
  222. if (this.Buffer == null)
  223. {
  224. return;
  225. }
  226. for (int i = this.Count; i < this._buffer.Length; i++)
  227. {
  228. this._buffer[i] = default(T);
  229. }
  230. }
  231. public ReadOnlyCollection<T> AsReadOnly()
  232. {
  233. if (this._readOnlyWrapper == null)
  234. {
  235. this._readOnlyWrapper = new ReadOnlyCollection<T>(this);
  236. }
  237. return this._readOnlyWrapper;
  238. }
  239. private void Expand()
  240. {
  241. T[] array = (this.Buffer == null) ? new T[32] : new T[Mathf.Max(this.Buffer.Length << 1, 32)];
  242. if (this.Buffer != null && this.Count > 0)
  243. {
  244. this.Buffer.CopyTo(array, 0);
  245. }
  246. this.Buffer = array;
  247. }
  248. public void Trim()
  249. {
  250. if (this.Count > 0)
  251. {
  252. if (this.Count >= this.Buffer.Length)
  253. {
  254. return;
  255. }
  256. T[] array = new T[this.Count];
  257. for (int i = 0; i < this.Count; i++)
  258. {
  259. array[i] = this.Buffer[i];
  260. }
  261. this.Buffer = array;
  262. }
  263. else
  264. {
  265. this.Buffer = new T[0];
  266. }
  267. }
  268. public void Sort(Comparison<T> comparer)
  269. {
  270. bool flag = true;
  271. while (flag)
  272. {
  273. flag = false;
  274. for (int i = 1; i < this.Count; i++)
  275. {
  276. if (comparer(this.Buffer[i - 1], this.Buffer[i]) > 0)
  277. {
  278. T t = this.Buffer[i];
  279. this.Buffer[i] = this.Buffer[i - 1];
  280. this.Buffer[i - 1] = t;
  281. flag = true;
  282. }
  283. }
  284. }
  285. }
  286. [SerializeField]
  287. private T[] _buffer;
  288. [SerializeField]
  289. private int _count;
  290. private EqualityComparer<T> _equalityComparer;
  291. private ReadOnlyCollection<T> _readOnlyWrapper;
  292. }
  293. }