Stack.cs 6.2 KB

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