Collection.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. //
  2. // System.Collections.Generic.Collection
  3. //
  4. // Author:
  5. // Carlos Alberto Cortez ([email protected])
  6. //
  7. // (C) 2004 Carlos Alberto Cortez
  8. //
  9. //
  10. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. #if NET_2_0
  32. using System;
  33. using System.Collections;
  34. using System.Collections.Generic;
  35. using System.Runtime.InteropServices;
  36. namespace System.Collections.Generic
  37. {
  38. [CLSCompliant(false)]
  39. [ComVisible(false)]
  40. public class Collection <T> : ICollection <T>, IEnumerable <T>, IList <T>,
  41. ICollection, IEnumerable, IList
  42. {
  43. const int defaultLength = 16;
  44. T [] contents;
  45. int count;
  46. int modcount;
  47. public Collection ()
  48. {
  49. contents = new T [defaultLength];
  50. }
  51. public Collection (List <T> list) : this (list, list.Count * 2)
  52. {
  53. }
  54. internal Collection (IList <T> list, int length)
  55. {
  56. if (list == null)
  57. throw new ArgumentNullException ("list");
  58. contents = new T [length];
  59. list.CopyTo (contents, 0);
  60. count = list.Count;
  61. }
  62. public void Add (T item)
  63. {
  64. if (count >= contents.Length)
  65. Array.Resize<T> (ref contents, contents.Length * 2);
  66. InsertItem (count, item);
  67. }
  68. public void Clear ()
  69. {
  70. ClearItems ();
  71. }
  72. protected virtual void ClearItems ()
  73. {
  74. modcount++;
  75. if (count <= 0)
  76. return;
  77. Array.Clear (contents, 0, count);
  78. count = 0;
  79. }
  80. public bool Contains (T item)
  81. {
  82. return IndexOf (item) >= 0;
  83. }
  84. public void CopyTo (T [] array, int index)
  85. {
  86. if (array == null)
  87. throw new ArgumentNullException ("array");
  88. if (index < 0)
  89. throw new ArgumentOutOfRangeException ("index");
  90. if (count < 0)
  91. return;
  92. Array.Copy (contents, 0, array, index, count);
  93. }
  94. public IEnumerator<T> GetEnumerator ()
  95. {
  96. return new Enumerator (this);
  97. }
  98. void ICollection.CopyTo (Array array, int index)
  99. {
  100. if (array == null)
  101. throw new ArgumentNullException ("array");
  102. if (array.Rank > 1)
  103. throw new ArgumentException ("Only single dimension arrays are supported.","array");
  104. CopyTo ((T []) array, index);
  105. }
  106. IEnumerator IEnumerable.GetEnumerator ()
  107. {
  108. return new Enumerator (this);
  109. }
  110. int IList.Add (object value)
  111. {
  112. int pos = count;
  113. CheckType (value);
  114. Add ((T) value);
  115. return pos;
  116. }
  117. bool IList.Contains (object value)
  118. {
  119. CheckType (value);
  120. return Contains ((T) value);
  121. }
  122. int IList.IndexOf (object value)
  123. {
  124. CheckType (value);
  125. return IndexOf ((T) value);
  126. }
  127. void IList.Insert (int index, object value)
  128. {
  129. CheckType (value);
  130. Insert (index, (T) value);
  131. }
  132. void IList.Remove (object value)
  133. {
  134. CheckType (value);
  135. Remove ((T) value);
  136. }
  137. public int IndexOf (T item)
  138. {
  139. if (item == null)
  140. for (int i = 0; i < count; i++) {
  141. if (contents [i] == null)
  142. return i;
  143. }
  144. else
  145. for (int i = 0; i < count; i++) {
  146. if (item.Equals (contents [i]))
  147. return i;
  148. }
  149. return -1;
  150. }
  151. public void Insert (int index, T item)
  152. {
  153. if (index < 0 || index > count)
  154. throw new ArgumentOutOfRangeException ("index");
  155. if (count >= contents.Length)
  156. Array.Resize<T> (ref contents, contents.Length * 2);
  157. InsertItem (index, item);
  158. }
  159. protected virtual void InsertItem (int index, T item)
  160. {
  161. modcount++;
  162. //
  163. // Shift them by 1
  164. //
  165. int rest = count - index;
  166. if (rest > 0)
  167. Array.Copy (contents, index, contents, index + 1, rest);
  168. contents [index] = item;
  169. count++;
  170. }
  171. public bool Remove (T item)
  172. {
  173. int index = IndexOf (item);
  174. if (index > -1) {
  175. RemoveItem (index);
  176. return true;
  177. }
  178. return false;
  179. }
  180. public void RemoveAt (int index)
  181. {
  182. if (index < 0 || index >= count)
  183. throw new ArgumentOutOfRangeException ("index");
  184. RemoveItem (index);
  185. }
  186. protected virtual void RemoveItem (int index)
  187. {
  188. modcount++;
  189. int rest = count - index - 1;
  190. if (rest > 0)
  191. Array.Copy (contents, index + 1, contents, index, rest);
  192. //
  193. // Clear the last element
  194. //
  195. Array.Clear (contents, --count, 1);
  196. }
  197. protected virtual void SetItem (int index, T item)
  198. {
  199. modcount++;
  200. contents [index] = item;
  201. }
  202. public int Count {
  203. get {
  204. return count;
  205. }
  206. }
  207. public T this [int index] {
  208. get {
  209. if (index < 0 || index >= count)
  210. throw new ArgumentOutOfRangeException ("index");
  211. return contents [index];
  212. }
  213. set {
  214. if (index < 0 || index >= count)
  215. throw new ArgumentOutOfRangeException ("index");
  216. SetItem (index, value);
  217. }
  218. }
  219. object IList.this [int index] {
  220. get {
  221. return this [index];
  222. }
  223. set {
  224. CheckType(value);
  225. this [index] = (T) value;
  226. }
  227. }
  228. //
  229. // We could try to make List.contents internal,
  230. // avoiding the box and unbox when copying
  231. //
  232. protected internal List <T> Items {
  233. get {
  234. return new List <T> (this);
  235. }
  236. }
  237. public bool IsFixedSize {
  238. get {
  239. return false;
  240. }
  241. }
  242. public bool IsReadOnly {
  243. get {
  244. return false;
  245. }
  246. }
  247. public bool IsSynchronized {
  248. get {
  249. return false;
  250. }
  251. }
  252. public object SyncRoot {
  253. get {
  254. return this;
  255. }
  256. }
  257. private void CheckType (object value)
  258. {
  259. if (!(value is T)) {
  260. string valtype = value.GetType ().ToString ();
  261. throw new ArgumentException (
  262. String.Format ("A value of type {0} can't be used in this generic collection.", valtype),
  263. "value");
  264. }
  265. }
  266. //
  267. // Will we use this iterator until iterators support is done?
  268. //
  269. private struct Enumerator : IEnumerator <T>, IEnumerator
  270. {
  271. private Collection <T> collection;
  272. private int current;
  273. private int modcount;
  274. public Enumerator (Collection <T> collection)
  275. {
  276. this.collection = collection;
  277. modcount = collection.modcount;
  278. current = -1;
  279. }
  280. public void Dispose()
  281. {
  282. modcount = -1;
  283. }
  284. object IEnumerator.Current {
  285. get {
  286. return Current;
  287. }
  288. }
  289. void IEnumerator.Reset ()
  290. {
  291. if (modcount != collection.modcount)
  292. throw new InvalidOperationException
  293. ("Collection was modified while enumerating.");
  294. current = -1;
  295. }
  296. public bool MoveNext ()
  297. {
  298. if (modcount != collection.modcount)
  299. throw new InvalidOperationException
  300. ("Collection was modified while enumerating.");
  301. current++;
  302. return current < collection.count;
  303. }
  304. public T Current {
  305. get {
  306. if (current < 0 || current >= collection.count)
  307. throw new InvalidOperationException
  308. ("Collection was modified while enumerating.");
  309. return collection.contents [current];
  310. }
  311. }
  312. }
  313. }
  314. }
  315. #endif