Stack.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. //
  2. // System.Collections.Stack
  3. //
  4. // Author:
  5. // Garrett Rooney ([email protected])
  6. //
  7. // (C) 2001 Garrett Rooney
  8. //
  9. namespace System.Collections {
  10. [Serializable]
  11. public class Stack : ICollection, IEnumerable, ICloneable {
  12. // properties
  13. private object[] contents;
  14. private int current = -1;
  15. private int count = 0;
  16. private int capacity;
  17. private int modCount = 0;
  18. private void Resize(int ncapacity) {
  19. ncapacity = Math.Max (ncapacity, 16);
  20. object[] ncontents = new object[ncapacity];
  21. Array.Copy(contents, ncontents, count);
  22. capacity = ncapacity;
  23. contents = ncontents;
  24. }
  25. public Stack () : this (16) {}
  26. public Stack(ICollection col) : this (col == null ? 16 : col.Count) {
  27. if (col == null)
  28. throw new ArgumentNullException("col");
  29. current = col.Count - 1;
  30. count = col.Count;
  31. col.CopyTo(contents, 0);
  32. }
  33. public Stack (int initialCapacity) {
  34. if (initialCapacity < 0)
  35. throw new ArgumentOutOfRangeException ("initialCapacity");
  36. capacity = Math.Max (initialCapacity, 16);
  37. contents = new object[capacity];
  38. }
  39. [Serializable]
  40. private class SyncStack : Stack {
  41. Stack stack;
  42. internal SyncStack(Stack s) {
  43. stack = s;
  44. }
  45. public override int Count {
  46. get {
  47. lock (stack) {
  48. return stack.Count;
  49. }
  50. }
  51. }
  52. /*
  53. public override bool IsReadOnly {
  54. get {
  55. lock (stack) {
  56. return stack.IsReadOnly;
  57. }
  58. }
  59. }
  60. */
  61. public override bool IsSynchronized {
  62. get { return true; }
  63. }
  64. public override object SyncRoot {
  65. get { return stack.SyncRoot; }
  66. }
  67. public override void Clear() {
  68. lock(stack) { stack.Clear(); }
  69. }
  70. public override object Clone() {
  71. lock (stack) {
  72. return Stack.Synchronized((Stack)stack.Clone());
  73. }
  74. }
  75. public override bool Contains(object obj) {
  76. lock (stack) { return stack.Contains(obj); }
  77. }
  78. public override void CopyTo(Array array, int index) {
  79. lock (stack) { stack.CopyTo(array, index); }
  80. }
  81. public override IEnumerator GetEnumerator() {
  82. lock (stack) {
  83. return new Enumerator(stack);
  84. }
  85. }
  86. public override object Peek() {
  87. lock (stack) { return stack.Peek(); }
  88. }
  89. public override object Pop() {
  90. lock (stack) { return stack.Pop(); }
  91. }
  92. public override void Push(object obj) {
  93. lock (stack) { stack.Push(obj); }
  94. }
  95. public override object[] ToArray() {
  96. lock (stack) { return stack.ToArray(); }
  97. }
  98. }
  99. public static Stack Synchronized(Stack s) {
  100. if (s == null) {
  101. throw new ArgumentNullException();
  102. }
  103. return new SyncStack(s);
  104. }
  105. public virtual int Count {
  106. get { return count; }
  107. }
  108. /*
  109. public virtual bool IsReadOnly {
  110. get { return false; }
  111. }
  112. */
  113. public virtual bool IsSynchronized {
  114. get { return false; }
  115. }
  116. public virtual object SyncRoot {
  117. get { return this; }
  118. }
  119. public virtual void Clear() {
  120. modCount++;
  121. for (int i = 0; i < count; i++) {
  122. contents[i] = null;
  123. }
  124. count = 0;
  125. current = -1;
  126. }
  127. public virtual object Clone() {
  128. Stack stack = new Stack (contents);
  129. stack.current = current;
  130. stack.count = count;
  131. return stack;
  132. }
  133. public virtual bool Contains(object obj) {
  134. if (count == 0)
  135. return false;
  136. if (obj == null) {
  137. for (int i = 0; i < count; i++) {
  138. if (contents[i] == null)
  139. return true;
  140. }
  141. } else {
  142. for (int i = 0; i < count; i++) {
  143. if (contents[i].Equals(obj))
  144. return true;
  145. }
  146. }
  147. return false;
  148. }
  149. public virtual void CopyTo (Array array, int index) {
  150. if (array == null) {
  151. throw new ArgumentNullException("array");
  152. }
  153. if (index < 0) {
  154. throw new ArgumentOutOfRangeException("index");
  155. }
  156. if (array.Rank > 1 ||
  157. index >= array.Length ||
  158. count > array.Length - index) {
  159. throw new ArgumentException();
  160. }
  161. for (int i = current; i != -1; i--) {
  162. array.SetValue(contents[i],
  163. count - (i + 1) + index);
  164. }
  165. }
  166. private class Enumerator : IEnumerator, ICloneable {
  167. const int EOF = -1;
  168. const int BOF = -2;
  169. Stack stack;
  170. private int modCount;
  171. private int current;
  172. internal Enumerator(Stack s) {
  173. stack = s;
  174. modCount = s.modCount;
  175. current = BOF;
  176. }
  177. public object Clone ()
  178. {
  179. return MemberwiseClone ();
  180. }
  181. public virtual object Current {
  182. get {
  183. if (modCount != stack.modCount
  184. || current == BOF
  185. || current == EOF
  186. || current > stack.count)
  187. throw new InvalidOperationException();
  188. return stack.contents[current];
  189. }
  190. }
  191. public virtual bool MoveNext() {
  192. if (modCount != stack.modCount)
  193. throw new InvalidOperationException();
  194. switch (current) {
  195. case BOF:
  196. current = stack.current;
  197. return current != -1;
  198. case EOF:
  199. return false;
  200. default:
  201. current--;
  202. return current != -1;
  203. }
  204. }
  205. public virtual void Reset() {
  206. if (modCount != stack.modCount) {
  207. throw new InvalidOperationException();
  208. }
  209. current = BOF;
  210. }
  211. }
  212. public virtual IEnumerator GetEnumerator() {
  213. return new Enumerator(this);
  214. }
  215. public virtual object Peek() {
  216. if (current == -1) {
  217. throw new InvalidOperationException();
  218. } else {
  219. return contents[current];
  220. }
  221. }
  222. public virtual object Pop() {
  223. if (current == -1) {
  224. throw new InvalidOperationException();
  225. } else {
  226. modCount++;
  227. object ret = contents[current];
  228. contents [current] = null;
  229. count--;
  230. current--;
  231. // if we're down to capacity/4, go back to a
  232. // lower array size. this should keep us from
  233. // sucking down huge amounts of memory when
  234. // putting large numbers of items in the Stack.
  235. // if we're lower than 16, don't bother, since
  236. // it will be more trouble than it's worth.
  237. if (count <= (capacity/4) && count > 16) {
  238. Resize(capacity/2);
  239. }
  240. return ret;
  241. }
  242. }
  243. public virtual void Push(Object o) {
  244. modCount++;
  245. if (capacity == count) {
  246. Resize(capacity * 2);
  247. }
  248. count++;
  249. current++;
  250. contents[current] = o;
  251. }
  252. public virtual object[] ToArray() {
  253. object[] ret = new object[count];
  254. Array.Copy(contents, ret, count);
  255. // ret needs to be in LIFO order
  256. Array.Reverse(ret);
  257. return ret;
  258. }
  259. }
  260. }