Stack.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 = new Stack (contents);
  128. stack.current = current;
  129. return stack;
  130. }
  131. public virtual bool Contains(object obj) {
  132. if (count == 0)
  133. return false;
  134. if (obj == null) {
  135. for (int i = 0; i < count; i++) {
  136. if (contents[i] == null)
  137. return true;
  138. }
  139. } else {
  140. for (int i = 0; i < count; i++) {
  141. if (contents[i].Equals(obj))
  142. return true;
  143. }
  144. }
  145. return false;
  146. }
  147. public virtual void CopyTo (Array array, int index) {
  148. if (array == null) {
  149. throw new ArgumentNullException();
  150. }
  151. if (index < 0) {
  152. throw new ArgumentOutOfRangeException();
  153. }
  154. if (array.Rank > 1 ||
  155. index >= array.Length ||
  156. count > array.Length - index) {
  157. throw new ArgumentException();
  158. }
  159. for (int i = current; i != -1; i--) {
  160. array.SetValue(contents[i],
  161. count - (i + 1) + index);
  162. }
  163. }
  164. private class Enumerator : IEnumerator {
  165. Stack stack;
  166. private int modCount;
  167. private int current;
  168. internal Enumerator(Stack s) {
  169. // this is odd. it seems that you need to
  170. // start one further ahead than current, since
  171. // MoveNext() gets called first when using an
  172. // Enumeration...
  173. stack = s;
  174. modCount = s.modCount;
  175. current = s.current + 1;
  176. }
  177. public virtual object Current {
  178. get {
  179. if (modCount != stack.modCount
  180. || current == -1
  181. || current > stack.count)
  182. throw new InvalidOperationException();
  183. return stack.contents[current];
  184. }
  185. }
  186. public virtual bool MoveNext() {
  187. if (modCount != stack.modCount
  188. || current == -1) {
  189. throw new InvalidOperationException();
  190. }
  191. current--;
  192. if (current == -1) {
  193. return false;
  194. } else {
  195. return true;
  196. }
  197. }
  198. public virtual void Reset() {
  199. if (modCount != stack.modCount) {
  200. throw new InvalidOperationException();
  201. }
  202. // start one ahead of stack.current, so the
  203. // first MoveNext() will put us at the top
  204. current = stack.current + 1;
  205. }
  206. }
  207. public virtual IEnumerator GetEnumerator() {
  208. return new Enumerator(this);
  209. }
  210. public virtual object Peek() {
  211. if (current == -1) {
  212. throw new InvalidOperationException();
  213. } else {
  214. return contents[current];
  215. }
  216. }
  217. public virtual object Pop() {
  218. if (current == -1) {
  219. throw new InvalidOperationException();
  220. } else {
  221. modCount++;
  222. object ret = contents[current];
  223. contents [current] = null;
  224. count--;
  225. current--;
  226. // if we're down to capacity/4, go back to a
  227. // lower array size. this should keep us from
  228. // sucking down huge amounts of memory when
  229. // putting large numbers of items in the Stack.
  230. // if we're lower than 16, don't bother, since
  231. // it will be more trouble than it's worth.
  232. if (count <= (capacity/4) && count > 16) {
  233. Resize(capacity/2);
  234. }
  235. return ret;
  236. }
  237. }
  238. public virtual void Push(Object o) {
  239. modCount++;
  240. if (capacity == count) {
  241. Resize(capacity * 2);
  242. }
  243. count++;
  244. current++;
  245. contents[current] = o;
  246. }
  247. public virtual object[] ToArray() {
  248. object[] ret = new object[count];
  249. Array.Copy(contents, ret, count);
  250. // ret needs to be in LIFO order
  251. Array.Reverse(ret);
  252. return ret;
  253. }
  254. }
  255. }