Stack.cs 5.3 KB

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