Stack.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. //
  2. // System.Collections.Generic.Stack
  3. //
  4. // Authors:
  5. // Martin Baulig ([email protected])
  6. // Ben Maurer ([email protected])
  7. //
  8. // (C) 2003, 2004 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>, ICollection, IEnumerable {
  40. T [] data;
  41. int size;
  42. int ver;
  43. public Stack ()
  44. {
  45. }
  46. public Stack (int count)
  47. {
  48. if (count < 0)
  49. throw new ArgumentOutOfRangeException ("count");
  50. data = new T [count];
  51. }
  52. public Stack (IEnumerable <T> collection)
  53. {
  54. if (collection == null)
  55. throw new ArgumentNullException ("collection");
  56. ICollection <T> col = collection as ICollection <T>;
  57. if (col != null) {
  58. size = col.Count;
  59. data = new T [size];
  60. col.CopyTo (data, 0);
  61. } else {
  62. foreach (T t in collection)
  63. Push (t);
  64. }
  65. }
  66. public void Clear ()
  67. {
  68. if (data != null)
  69. Array.Clear (data, 0, data.Length);
  70. size = 0;
  71. ver ++;
  72. }
  73. public bool Contains (T t)
  74. {
  75. return data != null && Array.IndexOf (data, t, 0, size) != -1;
  76. }
  77. public void CopyTo (T [] dest, int idx)
  78. {
  79. // this gets copied in the order that it is poped
  80. if (data != null) {
  81. Array.Copy (data, 0, dest, idx, size);
  82. Array.Reverse (dest, idx, size);
  83. }
  84. }
  85. public T Peek ()
  86. {
  87. if (size == 0)
  88. throw new InvalidOperationException ();
  89. ver ++;
  90. return data [size - 1];
  91. }
  92. public T Pop ()
  93. {
  94. if (size == 0)
  95. throw new InvalidOperationException ();
  96. ver ++;
  97. return data [-- size];
  98. }
  99. public void Push (T t)
  100. {
  101. if (size == 0 || size == data.Length)
  102. Array.Resize <T> (ref data, size == 0 ? 10 : 2 * size);
  103. ver ++;
  104. data [size++] = t;
  105. }
  106. public T [] ToArray ()
  107. {
  108. T [] copy = new T [size];
  109. CopyTo (copy, 0);
  110. return copy;
  111. }
  112. public void TrimToSize ()
  113. {
  114. // for some broken reason, msft increments the version here
  115. ver ++;
  116. if (size == 0)
  117. data = null;
  118. else
  119. Array.Resize <T> (ref data, size);
  120. }
  121. public int Count {
  122. get { return size; }
  123. }
  124. bool ICollection <T>.IsReadOnly {
  125. get { return false; }
  126. }
  127. bool ICollection.IsSynchronized {
  128. get { return false; }
  129. }
  130. object ICollection.SyncRoot {
  131. get { return this; }
  132. }
  133. void ICollection <T>.Add (T t)
  134. {
  135. Push (t);
  136. }
  137. bool ICollection <T>.Remove (T t)
  138. {
  139. throw new InvalidOperationException ("");
  140. }
  141. void ICollection.CopyTo (Array dest, int idx)
  142. {
  143. try {
  144. if (data != null) {
  145. data.CopyTo (dest, idx);
  146. Array.Reverse (dest, idx, size);
  147. }
  148. } catch (ArrayTypeMismatchException) {
  149. throw new ArgumentException ();
  150. }
  151. }
  152. public Enumerator <T> GetEnumerator ()
  153. {
  154. return new Enumerator <T> (this);
  155. }
  156. IEnumerator <T> IEnumerable<T>.GetEnumerator ()
  157. {
  158. return GetEnumerator ();
  159. }
  160. IEnumerator IEnumerable.GetEnumerator ()
  161. {
  162. return GetEnumerator ();
  163. }
  164. public struct Enumerator <T> : IEnumerator <T>, IEnumerator, IDisposable {
  165. const int NOT_STARTED = -2;
  166. // this MUST be -1, because we depend on it in move next.
  167. // we just decr the size, so, 0 - 1 == FINISHED
  168. const int FINISHED = -1;
  169. Stack <T> parent;
  170. int idx;
  171. int ver;
  172. internal Enumerator (Stack <T> t)
  173. {
  174. parent = t;
  175. idx = NOT_STARTED;
  176. ver = t.ver;
  177. }
  178. // for some fucked up reason, MSFT added a useless dispose to this class
  179. // It means that in foreach, we must still do a try/finally. Broken, very
  180. // broken.
  181. public void Dispose ()
  182. {
  183. idx = NOT_STARTED;
  184. }
  185. public bool MoveNext ()
  186. {
  187. if (ver != parent.ver)
  188. throw new InvalidOperationException ();
  189. if (idx == -2)
  190. idx = parent.size;
  191. return idx != FINISHED && -- idx != FINISHED;
  192. }
  193. public T Current {
  194. get {
  195. if (idx < 0)
  196. throw new InvalidOperationException ();
  197. return parent.data [idx];
  198. }
  199. }
  200. void IEnumerator.Reset ()
  201. {
  202. if (ver != parent.ver)
  203. throw new InvalidOperationException ();
  204. idx = NOT_STARTED;
  205. }
  206. object IEnumerator.Current {
  207. get { return Current; }
  208. }
  209. }
  210. }
  211. }
  212. #endif