PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/corlib/System.Collections.Generic/List.cs

https://bitbucket.org/cosi2/dotnetanywhere-wb
C# | 273 lines | 225 code | 48 blank | 0 comment | 21 complexity | 923f2938fab538099b6c776a538d061b MD5 | raw file
  1. using System;
  2. #if LOCALTEST
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. namespace System_.Collections.Generic {
  6. #else
  7. namespace System.Collections.Generic {
  8. #endif
  9. public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable {
  10. public struct Enumerator : IEnumerator<T>, IDisposable {
  11. private List<T> list;
  12. private int index;
  13. internal Enumerator(List<T> list) {
  14. this.list = list;
  15. this.index = -1;
  16. }
  17. public T Current {
  18. get {
  19. return this.list[this.index];
  20. }
  21. }
  22. public void Dispose() {
  23. }
  24. object IEnumerator.Current {
  25. get {
  26. return this.list[this.index];
  27. }
  28. }
  29. public bool MoveNext() {
  30. this.index++;
  31. return (this.index < this.list.Count);
  32. }
  33. public void Reset() {
  34. this.index = -1;
  35. }
  36. }
  37. private const int defaultCapacity = 4;
  38. private T[] items;
  39. private int size;
  40. public List() : this(defaultCapacity) { }
  41. public List(int capacity) {
  42. if (capacity < 0) {
  43. throw new ArgumentOutOfRangeException("capacity");
  44. }
  45. this.items = new T[capacity];
  46. this.size = 0;
  47. }
  48. public List(IEnumerable<T> collection) {
  49. ICollection<T> iCol = collection as ICollection<T>;
  50. if (iCol != null) {
  51. this.size = iCol.Count;
  52. this.items = new T[this.size];
  53. iCol.CopyTo(this.items, 0);
  54. } else {
  55. this.items = new T[defaultCapacity];
  56. this.size = 0;
  57. foreach (T item in collection) {
  58. this.Add(item);
  59. }
  60. }
  61. }
  62. private void EnsureSpace(int space) {
  63. if (this.size + space > this.items.Length) {
  64. Array.Resize<T>(ref this.items, Math.Max(this.items.Length << 1, 4));
  65. }
  66. }
  67. private void Shift(int index, int count) {
  68. if (count > 0) {
  69. this.EnsureSpace(count);
  70. for (int i = this.size - 1; i >= index; i--) {
  71. this.items[i + count] = this.items[i];
  72. }
  73. } else {
  74. for (int i = index; i < this.size + count; i++) {
  75. this.items[i] = this.items[i - count];
  76. }
  77. }
  78. this.size += count;
  79. }
  80. public void Add(T item) {
  81. this.EnsureSpace(1);
  82. this.items[this.size++] = item;
  83. }
  84. public int Count {
  85. get {
  86. return this.size;
  87. }
  88. }
  89. public int Capacity {
  90. get {
  91. return this.items.Length;
  92. }
  93. set {
  94. throw new NotImplementedException();
  95. }
  96. }
  97. public T this[int index] {
  98. get {
  99. if (index >= this.size || index < 0) {
  100. throw new ArgumentOutOfRangeException("index");
  101. }
  102. return this.items[index];
  103. }
  104. set {
  105. if (index >= this.size || index < 0) {
  106. throw new ArgumentOutOfRangeException("index");
  107. }
  108. this.items[index] = value;
  109. }
  110. }
  111. public Enumerator GetEnumerator() {
  112. return new Enumerator(this);
  113. }
  114. public int IndexOf(T item, int start, int count) {
  115. return Array.IndexOf<T>(this.items, item, start, count);
  116. }
  117. public int IndexOf(T item, int start) {
  118. return this.IndexOf(item, start, this.size - start);
  119. }
  120. public void InsertRange(int index, IEnumerable<T> collection) {
  121. if (collection == null) {
  122. throw new ArgumentNullException("collection");
  123. }
  124. if (index < 0 || index > this.size) {
  125. throw new ArgumentOutOfRangeException("index");
  126. }
  127. List<T> toInsert = new List<T>(collection);
  128. this.Shift(index, toInsert.Count);
  129. for (int i = 0; i < toInsert.Count; i++) {
  130. this.items[index + i] = toInsert[i];
  131. }
  132. }
  133. public T[] ToArray() {
  134. T[] array = new T[this.size];
  135. Array.Copy(this.items, array, this.size);
  136. return array;
  137. }
  138. #region Interface Members
  139. public int IndexOf(T item) {
  140. return this.IndexOf(item, 0, size);
  141. }
  142. public void Insert(int index, T item) {
  143. if (index < 0 || index > this.size) {
  144. throw new ArgumentOutOfRangeException("index");
  145. }
  146. this.Shift(index, 1);
  147. this.items[index] = item;
  148. }
  149. public void RemoveAt(int index) {
  150. this.Shift(index, -1);
  151. }
  152. public bool IsReadOnly {
  153. get {
  154. return false;
  155. }
  156. }
  157. public void Clear() {
  158. Array.Clear(this.items, 0, this.items.Length);
  159. this.size = 0;
  160. }
  161. public bool Contains(T item) {
  162. return Array.IndexOf(this.items, item) >= 0;
  163. }
  164. public void CopyTo(T[] array, int arrayIndex) {
  165. Array.Copy(this.items, 0, (Array)array, arrayIndex, this.size);
  166. }
  167. public bool Remove(T item) {
  168. int idx = Array.IndexOf(this.items, item);
  169. if (idx >= 0) {
  170. this.RemoveAt(idx);
  171. return true;
  172. }
  173. return false;
  174. }
  175. IEnumerator<T> IEnumerable<T>.GetEnumerator() {
  176. return new Enumerator(this);
  177. }
  178. IEnumerator IEnumerable.GetEnumerator() {
  179. return new Enumerator(this);
  180. }
  181. public bool IsFixedSize {
  182. get {
  183. return false;
  184. }
  185. }
  186. object IList.this[int index] {
  187. get {
  188. return this[index];
  189. }
  190. set {
  191. this[index] = (T)value;
  192. }
  193. }
  194. public int Add(object value) {
  195. this.Add((T)value);
  196. return this.items.Length - 1;
  197. }
  198. public bool Contains(object value) {
  199. return this.Contains((T)value);
  200. }
  201. public int IndexOf(object value) {
  202. return this.IndexOf((T)value);
  203. }
  204. public void Insert(int index, object value) {
  205. this.Insert(index, (T)value);
  206. }
  207. public void Remove(object value) {
  208. this.Remove((T)value);
  209. }
  210. public bool IsSynchronized {
  211. get {
  212. return false;
  213. }
  214. }
  215. public object SyncRoot {
  216. get {
  217. return this;
  218. }
  219. }
  220. public void CopyTo(Array array, int index) {
  221. Array.Copy(this.items, 0, array, index, this.size);
  222. }
  223. #endregion
  224. }
  225. }