Stack.cs 6.3 KB

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