Stack.cs 5.2 KB

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