Stack.cs 5.3 KB

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