Stack.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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> : 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. [MonoTODO]
  112. public void TrimExcess ()
  113. {
  114. throw new NotImplementedException ();
  115. }
  116. public int Count {
  117. get { return size; }
  118. }
  119. bool ICollection.IsSynchronized {
  120. get { return false; }
  121. }
  122. object ICollection.SyncRoot {
  123. get { return this; }
  124. }
  125. void ICollection.CopyTo (Array dest, int idx)
  126. {
  127. try {
  128. if (data != null) {
  129. data.CopyTo (dest, idx);
  130. Array.Reverse (dest, idx, size);
  131. }
  132. } catch (ArrayTypeMismatchException) {
  133. throw new ArgumentException ();
  134. }
  135. }
  136. public Enumerator GetEnumerator ()
  137. {
  138. return new Enumerator (this);
  139. }
  140. IEnumerator <T> IEnumerable<T>.GetEnumerator ()
  141. {
  142. return GetEnumerator ();
  143. }
  144. IEnumerator IEnumerable.GetEnumerator ()
  145. {
  146. return GetEnumerator ();
  147. }
  148. public struct Enumerator : IEnumerator <T>, IEnumerator, IDisposable {
  149. const int NOT_STARTED = -2;
  150. // this MUST be -1, because we depend on it in move next.
  151. // we just decr the size, so, 0 - 1 == FINISHED
  152. const int FINISHED = -1;
  153. Stack <T> parent;
  154. int idx;
  155. int ver;
  156. internal Enumerator (Stack <T> t)
  157. {
  158. parent = t;
  159. idx = NOT_STARTED;
  160. ver = t.ver;
  161. }
  162. // for some fucked up reason, MSFT added a useless dispose to this class
  163. // It means that in foreach, we must still do a try/finally. Broken, very
  164. // broken.
  165. public void Dispose ()
  166. {
  167. idx = NOT_STARTED;
  168. }
  169. public bool MoveNext ()
  170. {
  171. if (ver != parent.ver)
  172. throw new InvalidOperationException ();
  173. if (idx == -2)
  174. idx = parent.size;
  175. return idx != FINISHED && -- idx != FINISHED;
  176. }
  177. public T Current {
  178. get {
  179. if (idx < 0)
  180. throw new InvalidOperationException ();
  181. return parent.data [idx];
  182. }
  183. }
  184. void IEnumerator.Reset ()
  185. {
  186. if (ver != parent.ver)
  187. throw new InvalidOperationException ();
  188. idx = NOT_STARTED;
  189. }
  190. object IEnumerator.Current {
  191. get { return Current; }
  192. }
  193. }
  194. }
  195. }
  196. #endif