Stack.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // -*- Mode: csharp; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
  2. //
  3. // System.Collections.Generic.Stack
  4. //
  5. // Author:
  6. // Martin Baulig ([email protected])
  7. //
  8. // (C) 2003 Novell, Inc.
  9. //
  10. //
  11. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. #if NET_2_0
  33. using System;
  34. using System.Runtime.InteropServices;
  35. namespace System.Collections.Generic
  36. {
  37. [CLSCompliant(false)]
  38. [ComVisible(false)]
  39. public class Stack<T> : ICollection<T>, IEnumerable<T>,
  40. ICollection, IEnumerable
  41. {
  42. int count;
  43. protected int modified;
  44. protected Node head;
  45. public Stack ()
  46. { }
  47. public void Clear ()
  48. {
  49. head = null;
  50. count = 0;
  51. modified++;
  52. }
  53. public void Push (T item)
  54. {
  55. head = new Node (head, item);
  56. count++;
  57. modified++;
  58. }
  59. public T Peek ()
  60. {
  61. if (head == null)
  62. throw new ArgumentException ();
  63. return head.Item;
  64. }
  65. public T Pop ()
  66. {
  67. if (head == null)
  68. throw new ArgumentException ();
  69. T retval = head.Item;
  70. head = head.Next;
  71. count--;
  72. modified++;
  73. return retval;
  74. }
  75. public bool Contains (T item)
  76. {
  77. for (Node node = head; node != null; node = node.Next)
  78. if (node.Item == item)
  79. return true;
  80. return false;
  81. }
  82. public virtual void CopyTo (T[] array, int start)
  83. {
  84. // re-ordered to avoid possible integer overflow
  85. if (start >= array.Length - count)
  86. throw new ArgumentException ();
  87. for (Node node = head; node != null; node = node.Next)
  88. array [start++] = node.Item;
  89. }
  90. void ICollection.CopyTo (Array array, int start)
  91. {
  92. // re-ordered to avoid possible integer overflow
  93. if (start >= array.Length - count)
  94. throw new ArgumentException ();
  95. for (Node node = head; node != null; node = node.Next)
  96. array.SetValue (node.Item, start++);
  97. }
  98. public T[] ToArray ()
  99. {
  100. int pos = 0;
  101. T[] retval = new T [count];
  102. for (Node node = head; node != null; node = node.Next)
  103. retval [pos++] = node.Item;
  104. return retval;
  105. }
  106. public void TrimToSize ()
  107. { }
  108. public int Count {
  109. get { return count; }
  110. }
  111. public bool IsSynchronized {
  112. get { return false; }
  113. }
  114. public object SyncRoot {
  115. get { return this; }
  116. }
  117. public IEnumerator<T> GetEnumerator ()
  118. {
  119. return new Enumerator (this);
  120. }
  121. IEnumerator IEnumerable.GetEnumerator ()
  122. {
  123. return new Enumerator (this);
  124. }
  125. protected sealed class Node
  126. {
  127. public readonly T Item;
  128. public readonly Node Next;
  129. public Node (Node next, T item)
  130. {
  131. this.Next = next;
  132. this.Item = item;
  133. }
  134. }
  135. protected class Enumerator : IEnumerator<T>, IEnumerator
  136. {
  137. Stack<T> stack;
  138. int modified;
  139. Node current;
  140. public Enumerator (Stack<T> stack)
  141. {
  142. this.stack = stack;
  143. this.modified = stack.modified;
  144. this.current = stack.head;
  145. }
  146. public T Current {
  147. get {
  148. if (stack.modified != modified)
  149. throw new InvalidOperationException ();
  150. if (current == null)
  151. throw new ArgumentException ();
  152. return current.Item;
  153. }
  154. }
  155. object IEnumerator.Current {
  156. get {
  157. return Current;
  158. }
  159. }
  160. public bool MoveNext ()
  161. {
  162. if (stack.modified != modified)
  163. throw new InvalidOperationException ();
  164. if (current == null)
  165. throw new ArgumentException ();
  166. current = current.Next;
  167. return current != null;
  168. }
  169. public void Reset ()
  170. {
  171. if (stack.modified != modified)
  172. throw new InvalidOperationException ();
  173. current = stack.head;
  174. }
  175. public void Dispose ()
  176. {
  177. modified = -1;
  178. }
  179. }
  180. }
  181. }
  182. #endif