2
0

LinkedList.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. //
  2. // System.Collections.Generic.LinkedListNode
  3. //
  4. // Author:
  5. // David Waite
  6. //
  7. // (C) 2005 David Waite ([email protected])
  8. //
  9. //
  10. // Copyright (C) 2005 David Waite
  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.Runtime.InteropServices;
  34. using System.Runtime.Serialization;
  35. using System.Security.Permissions;
  36. namespace System.Collections.Generic
  37. {
  38. [Serializable, ComVisible (false)]
  39. public class LinkedList <T> : ICollection <T>, ICollection, ISerializable, IDeserializationCallback
  40. {
  41. const string DataArrayKey = "DataArray";
  42. const string VersionKey = "version";
  43. uint count, version;
  44. object syncRoot;
  45. // Internally a circular list - first.back == last
  46. internal LinkedListNode <T> first;
  47. internal SerializationInfo si;
  48. public LinkedList ()
  49. {
  50. syncRoot = new object ();
  51. first = null;
  52. count = version = 0;
  53. }
  54. public LinkedList (IEnumerable <T> collection) : this ()
  55. {
  56. foreach (T item in collection)
  57. AddLast (item);
  58. }
  59. protected LinkedList (SerializationInfo info, StreamingContext context) : this ()
  60. {
  61. si = info;
  62. syncRoot = new object ();
  63. }
  64. void VerifyReferencedNode (LinkedListNode <T> node)
  65. {
  66. if (node == null)
  67. throw new ArgumentNullException ("node");
  68. if (node.List != this)
  69. throw new InvalidOperationException ();
  70. }
  71. static void VerifyBlankNode (LinkedListNode <T> newNode)
  72. {
  73. if (newNode == null)
  74. throw new ArgumentNullException ("newNode");
  75. if (newNode.List != null)
  76. throw new InvalidOperationException ();
  77. }
  78. public LinkedListNode <T> AddAfter (LinkedListNode <T> node, T value)
  79. {
  80. VerifyReferencedNode (node);
  81. LinkedListNode <T> newNode = new LinkedListNode <T> (this, value, node, node.forward);
  82. count++;
  83. version++;
  84. return newNode;
  85. }
  86. public void AddAfter (LinkedListNode <T> node, LinkedListNode <T> newNode)
  87. {
  88. VerifyReferencedNode (node);
  89. VerifyBlankNode (newNode);
  90. newNode.InsertBetween (node, node.forward, this);
  91. count++;
  92. version++;
  93. }
  94. public LinkedListNode <T> AddBefore (LinkedListNode <T> node, T value)
  95. {
  96. VerifyReferencedNode (node);
  97. LinkedListNode <T> newNode = new LinkedListNode <T> (this, value, node.back, node);
  98. count++;
  99. version++;
  100. if (node == first)
  101. first = newNode;
  102. return newNode;
  103. }
  104. public void AddBefore (LinkedListNode <T> node, LinkedListNode <T> newNode)
  105. {
  106. VerifyReferencedNode (node);
  107. VerifyBlankNode (newNode);
  108. newNode.InsertBetween (node.back, node, this);
  109. count++;
  110. version++;
  111. if (node == first)
  112. first = newNode;
  113. }
  114. public void AddFirst (LinkedListNode <T> node)
  115. {
  116. VerifyBlankNode (node);
  117. if (first == null)
  118. node.SelfReference (this);
  119. else
  120. node.InsertBetween (first.back, first, this);
  121. count++;
  122. version++;
  123. first = node;
  124. }
  125. public LinkedListNode <T> AddFirst (T value)
  126. {
  127. LinkedListNode <T> newNode;
  128. if (first == null)
  129. newNode = new LinkedListNode <T> (this, value);
  130. else
  131. newNode = new LinkedListNode <T> (this, value, first.back, first);
  132. count++;
  133. version++;
  134. first = newNode;
  135. return newNode;
  136. }
  137. public LinkedListNode <T> AddLast (T value)
  138. {
  139. LinkedListNode <T> newNode;
  140. if (first == null)
  141. {
  142. newNode = new LinkedListNode <T> (this, value);
  143. first = newNode;
  144. }
  145. else
  146. newNode = new LinkedListNode <T> (this, value, first.back, first);
  147. count++;
  148. version++;
  149. return newNode;
  150. }
  151. public void AddLast (LinkedListNode <T> node)
  152. {
  153. VerifyBlankNode (node);
  154. if (first == null)
  155. {
  156. node.SelfReference (this);
  157. first = node;
  158. }
  159. else
  160. node.InsertBetween (first.back, first, this);
  161. count++;
  162. version++;
  163. }
  164. public void Clear ()
  165. {
  166. count = 0;
  167. first = null;
  168. version++;
  169. }
  170. public bool Contains (T value)
  171. {
  172. LinkedListNode <T> node = first;
  173. if (node == null)
  174. return false;
  175. do
  176. {
  177. if (value.Equals (node.Value))
  178. return true;
  179. node = node.forward;
  180. }
  181. while (node != first);
  182. return false;
  183. }
  184. public void CopyTo (T [] array, int index)
  185. {
  186. if (array == null)
  187. throw new ArgumentNullException ("array");
  188. if ( (uint) index < (uint) array.GetLowerBound (0))
  189. throw new ArgumentOutOfRangeException ("index");
  190. if (array.Rank != 1)
  191. throw new ArgumentException ("array", "Array is multidimensional");
  192. if (array.Length - index + array.GetLowerBound (0) < count)
  193. throw new ArgumentException ("number of items exceeds capacity");
  194. LinkedListNode <T> node = first;
  195. if (first == null)
  196. return;
  197. do
  198. {
  199. array [index] = node.Value;
  200. index++;
  201. node = node.forward;
  202. }
  203. while (node != first);
  204. }
  205. public LinkedListNode <T> Find (T value)
  206. {
  207. LinkedListNode <T> node = first;
  208. if (node == null)
  209. return null;
  210. do
  211. {
  212. if ( (value == null && node.Value == null) || value.Equals (node.Value))
  213. return node;
  214. node = node.forward;
  215. }
  216. while (node != first);
  217. return null;
  218. }
  219. public LinkedListNode <T> FindLast (T value)
  220. {
  221. LinkedListNode <T> node = first;
  222. if (node == null)
  223. return null;
  224. do
  225. {
  226. node = node.back;
  227. if (value.Equals (node.Value))
  228. return node;
  229. }
  230. while (node != first);
  231. return null;
  232. }
  233. public Enumerator GetEnumerator ()
  234. {
  235. return new Enumerator (this);
  236. }
  237. [SecurityPermission (SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.SerializationFormatter)]
  238. public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
  239. {
  240. T [] data = new T [count];
  241. CopyTo (data, 0);
  242. info.AddValue (DataArrayKey, data, typeof (T []));
  243. info.AddValue (VersionKey, version);
  244. }
  245. public void OnDeserialization (object sender)
  246. {
  247. if (si != null)
  248. {
  249. T [] data = (T []) si.GetValue (DataArrayKey, typeof (T []));
  250. if (data != null)
  251. foreach (T item in data)
  252. AddLast (item);
  253. version = si.GetUInt32 (VersionKey);
  254. si = null;
  255. }
  256. }
  257. public bool Remove (T value)
  258. {
  259. LinkedListNode <T> node = Find (value);
  260. if (node == null)
  261. return false;
  262. Remove (node);
  263. return true;
  264. }
  265. public void Remove (LinkedListNode <T> node)
  266. {
  267. VerifyReferencedNode (node);
  268. count--;
  269. if (count == 0)
  270. first = null;
  271. if (node == first)
  272. first = first.forward;
  273. version++;
  274. node.Detach ();
  275. }
  276. public void RemoveFirst ()
  277. {
  278. if (first != null)
  279. Remove (first);
  280. }
  281. public void RemoveLast ()
  282. {
  283. if (first != null)
  284. Remove (first.back);
  285. }
  286. void ICollection <T>.Add (T value)
  287. {
  288. AddLast (value);
  289. }
  290. void ICollection.CopyTo (Array array, int index)
  291. {
  292. T [] Tarray = array as T [];
  293. if (Tarray == null)
  294. throw new ArgumentException ("array");
  295. CopyTo (Tarray, index);
  296. }
  297. IEnumerator <T> IEnumerable <T>.GetEnumerator ()
  298. {
  299. return GetEnumerator ();
  300. }
  301. IEnumerator IEnumerable.GetEnumerator ()
  302. {
  303. return GetEnumerator ();
  304. }
  305. public int Count {
  306. get { return (int) count; }
  307. }
  308. public LinkedListNode <T> First {
  309. get { return first; }
  310. }
  311. public LinkedListNode <T> Last {
  312. get { return (first != null) ? first.back : null; }
  313. }
  314. bool ICollection <T>.IsReadOnly {
  315. get { return false; }
  316. }
  317. bool ICollection.IsSynchronized {
  318. get { return false; }
  319. }
  320. object ICollection.SyncRoot {
  321. get { return syncRoot; }
  322. }
  323. [Serializable, StructLayout (LayoutKind.Sequential)]
  324. public struct Enumerator : IEnumerator <T>, IDisposable, IEnumerator, ISerializable, IDeserializationCallback
  325. {
  326. const String VersionKey = "version";
  327. const String IndexKey = "index";
  328. const String ListKey = "list";
  329. LinkedList <T> list;
  330. LinkedListNode <T> current;
  331. int index;
  332. uint version;
  333. SerializationInfo si;
  334. internal Enumerator (LinkedList <T> parent)
  335. {
  336. si= null;
  337. this.list = parent;
  338. current = null;
  339. index = -1;
  340. version = parent.version;
  341. }
  342. internal Enumerator (SerializationInfo info, StreamingContext context)
  343. {
  344. si = info;
  345. list = (LinkedList <T>) si.GetValue (ListKey, typeof (LinkedList <T>));
  346. index = si.GetInt32 (IndexKey);
  347. version = si.GetUInt32 (VersionKey);
  348. current = null;
  349. }
  350. public T Current {
  351. get {
  352. if (list == null)
  353. throw new ObjectDisposedException (null);
  354. if (current == null)
  355. throw new InvalidOperationException ();
  356. return current.Value;
  357. }
  358. }
  359. object IEnumerator.Current {
  360. get { return Current; }
  361. }
  362. public bool MoveNext ()
  363. {
  364. if (list == null)
  365. throw new ObjectDisposedException (null);
  366. if (version != list.version)
  367. throw new InvalidOperationException ("list modified");
  368. if (current == null)
  369. current = list.first;
  370. else
  371. {
  372. current = current.forward;
  373. if (current == list.first)
  374. current = null;
  375. }
  376. if (current == null)
  377. {
  378. index = -1;
  379. return false;
  380. }
  381. ++index;
  382. return true;
  383. }
  384. void IEnumerator.Reset ()
  385. {
  386. if (list == null)
  387. throw new ObjectDisposedException (null);
  388. if (version != list.version)
  389. throw new InvalidOperationException ("list modified");
  390. current = null;
  391. index = -1;
  392. }
  393. public void Dispose ()
  394. {
  395. if (list == null)
  396. throw new ObjectDisposedException (null);
  397. current = null;
  398. list = null;
  399. }
  400. [SecurityPermission (SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.SerializationFormatter)]
  401. void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
  402. {
  403. if (list == null)
  404. throw new ObjectDisposedException (null);
  405. info.AddValue (VersionKey, version);
  406. info.AddValue (IndexKey, index);
  407. }
  408. void IDeserializationCallback.OnDeserialization (object sender)
  409. {
  410. if (si == null)
  411. return;
  412. if (list.si != null)
  413. ( (IDeserializationCallback) list).OnDeserialization (this);
  414. si = null;
  415. if (version == list.version && index != -1)
  416. {
  417. LinkedListNode <T> node = list.First;
  418. for (int i = 0; i < index; i++)
  419. node = node.forward;
  420. current = node;
  421. }
  422. }
  423. }
  424. }
  425. }
  426. #endif