List.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. #########
  3. ############
  4. #############
  5. ## ###########
  6. ### ###### #####
  7. ### ####### ####
  8. ### ########## ####
  9. #### ########### ####
  10. #### ########### #####
  11. ##### ### ######## #####
  12. ##### ### ######## ######
  13. ###### ### ########### ######
  14. ###### #### ############## ######
  15. ####### ##################### ######
  16. ####### ###################### ######
  17. ####### ###### ################# ######
  18. ####### ###### ###### ######### ######
  19. ####### ## ###### ###### ######
  20. ####### ###### ##### #####
  21. ###### ##### ##### ####
  22. ##### #### ##### ###
  23. ##### ### ### #
  24. ### ### ###
  25. ## ### ###
  26. __________#_______####_______####______________
  27. 我们的未来没有BUG
  28. * ==============================================================================
  29. * Filename: List.cs
  30. * Created: 2018/7/13 14:29:22
  31. * Author: エル・プサイ・コングリィ
  32. * Purpose:
  33. * ==============================================================================
  34. */
  35. #if UNITY_EDITOR || USE_LUA_PROFILER
  36. using System;
  37. using System.Collections.Generic;
  38. using System.Linq;
  39. using System.Text;
  40. namespace MikuLuaProfiler
  41. {
  42. public class MList<T>
  43. {
  44. private const int DefaultCapacity = 4;
  45. private T[] _items;
  46. private int _size;
  47. private int _version;
  48. public MList(int capacity)
  49. {
  50. if (capacity < 0)
  51. {
  52. throw new ArgumentOutOfRangeException("capacity");
  53. }
  54. this._items = new T[capacity];
  55. }
  56. public int Capacity
  57. {
  58. get
  59. {
  60. return this._items.Length;
  61. }
  62. set
  63. {
  64. if (value < this._size)
  65. {
  66. throw new ArgumentOutOfRangeException();
  67. }
  68. Array.Resize<T>(ref this._items, value);
  69. }
  70. }
  71. public int Count
  72. {
  73. get
  74. {
  75. return this._size;
  76. }
  77. }
  78. public T this[int index]
  79. {
  80. get
  81. {
  82. return this._items[index];
  83. }
  84. set
  85. {
  86. this._items[index] = value;
  87. }
  88. }
  89. public void Clear()
  90. {
  91. this._size = 0;
  92. this._version++;
  93. }
  94. public void Add(T item)
  95. {
  96. if (this._size == this._items.Length)
  97. {
  98. this.GrowIfNeeded(1);
  99. }
  100. this._items[this._size++] = item;
  101. this._version++;
  102. }
  103. private void GrowIfNeeded(int newCount)
  104. {
  105. int num = this._size + newCount;
  106. if (num > this._items.Length)
  107. {
  108. this.Capacity = Math.Max(Math.Max(this.Capacity * 2, 4), num);
  109. }
  110. }
  111. public void RemoveAt(int index)
  112. {
  113. if (index < 0 || index >= this._size)
  114. {
  115. throw new ArgumentOutOfRangeException("index");
  116. }
  117. this.Shift(index, -1);
  118. Array.Clear(this._items, this._size, 1);
  119. this._version++;
  120. }
  121. private void Shift(int start, int delta)
  122. {
  123. if (delta < 0)
  124. {
  125. start -= delta;
  126. }
  127. if (start < this._size)
  128. {
  129. Array.Copy(this._items, start, this._items, start + delta, this._size - start);
  130. }
  131. this._size += delta;
  132. if (delta < 0)
  133. {
  134. Array.Clear(this._items, this._size, -delta);
  135. }
  136. }
  137. }
  138. }
  139. #endif