Stack.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. // -*- Mode: C; tab-width: 8; c-basic-offset: 8 -*-
  2. //
  3. // System.Collections.Stack
  4. //
  5. // Author:
  6. // Garrett Rooney ([email protected])
  7. //
  8. // (C) 2001 Garrett Rooney
  9. //
  10. namespace System.Collections {
  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. private class SyncStack : Stack {
  39. Stack stack;
  40. public SyncStack(Stack s) {
  41. stack = s;
  42. }
  43. public override int Count {
  44. get {
  45. lock (stack) {
  46. return stack.Count;
  47. }
  48. }
  49. }
  50. public override bool IsReadOnly {
  51. get { return false; }
  52. }
  53. public override bool IsSynchronized {
  54. get { return true; }
  55. }
  56. public override object SyncRoot {
  57. get { return stack.SyncRoot; }
  58. }
  59. public override void Clear() {
  60. lock(stack) { stack.Clear(); }
  61. }
  62. public override object Clone() {
  63. lock (stack) {
  64. return Stack.Synchronized((Stack)stack.Clone());
  65. }
  66. }
  67. public override bool Contains(object obj) {
  68. lock (stack) { return stack.Contains(obj); }
  69. }
  70. public override void CopyTo(Array array, int index) {
  71. lock (stack) { stack.CopyTo(array, index); }
  72. }
  73. public override IEnumerator GetEnumerator() {
  74. lock (stack) {
  75. return new Enumerator(stack);
  76. }
  77. }
  78. public override object Peek() {
  79. lock (stack) { return stack.Peek(); }
  80. }
  81. public override object Pop() {
  82. lock (stack) { return stack.Pop(); }
  83. }
  84. public override void Push(object obj) {
  85. lock (stack) { stack.Push(obj); }
  86. }
  87. public override object[] ToArray() {
  88. lock (stack) { return stack.ToArray(); }
  89. }
  90. }
  91. public static Stack Synchronized(Stack s) {
  92. if (s == null) {
  93. throw new ArgumentNullException();
  94. }
  95. return new SyncStack(s);
  96. }
  97. public virtual int Count {
  98. get { return count; }
  99. }
  100. public virtual bool IsReadOnly {
  101. get { return false; }
  102. }
  103. public virtual bool IsSynchronized {
  104. get { return false; }
  105. }
  106. public virtual object SyncRoot {
  107. get { return this; }
  108. }
  109. public virtual void Clear() {
  110. modCount++;
  111. for (int i = 0; i < count; i++) {
  112. contents[i] = null;
  113. }
  114. count = 0;
  115. current = -1;
  116. }
  117. public virtual object Clone() {
  118. Stack stack;
  119. if (IsSynchronized) {
  120. stack = new Stack();
  121. stack.current = current;
  122. stack.contents = contents;
  123. stack.count = count;
  124. stack.capacity = capacity;
  125. } else {
  126. stack = new SyncStack(this);
  127. }
  128. return stack;
  129. }
  130. public virtual bool Contains(object obj) {
  131. if (count == 0)
  132. return false;
  133. for (int i = 0; i < count; i++) {
  134. if (contents[i].Equals(obj))
  135. return true;
  136. }
  137. return false;
  138. }
  139. public virtual void CopyTo (Array array, int index) {
  140. if (array == null) {
  141. throw new ArgumentNullException();
  142. }
  143. if (index < 0) {
  144. throw new ArgumentOutOfRangeException();
  145. }
  146. if (array.Rank > 1 ||
  147. index >= array.Length ||
  148. count > array.Length - index) {
  149. throw new ArgumentException();
  150. }
  151. for (int i = current; i != -1; i--) {
  152. array.SetValue(contents[i],
  153. count - (i + 1) + index);
  154. }
  155. }
  156. private class Enumerator : IEnumerator {
  157. Stack stack;
  158. private int modCount;
  159. private int current;
  160. internal Enumerator(Stack s) {
  161. // this is odd. it seems that you need to
  162. // start one further ahead than current, since
  163. // MoveNext() gets called first when using an
  164. // Enumeration...
  165. stack = s;
  166. modCount = s.modCount;
  167. current = s.current + 1;
  168. }
  169. public virtual object Current {
  170. get {
  171. if (modCount != stack.modCount
  172. || current == -1
  173. || current > stack.count)
  174. throw new InvalidOperationException();
  175. return stack.contents[current];
  176. }
  177. }
  178. public virtual bool MoveNext() {
  179. if (modCount != stack.modCount
  180. || current == -1) {
  181. throw new InvalidOperationException();
  182. }
  183. current--;
  184. if (current == -1) {
  185. return false;
  186. } else {
  187. return true;
  188. }
  189. }
  190. public virtual void Reset() {
  191. if (modCount != stack.modCount) {
  192. throw new InvalidOperationException();
  193. }
  194. // start one ahead of stack.current, so the
  195. // first MoveNext() will put us at the top
  196. current = stack.current + 1;
  197. }
  198. }
  199. public virtual IEnumerator GetEnumerator() {
  200. return new Enumerator(this);
  201. }
  202. public virtual object Peek() {
  203. if (current == -1) {
  204. throw new InvalidOperationException();
  205. } else {
  206. return contents[current];
  207. }
  208. }
  209. public virtual object Pop() {
  210. if (current == -1) {
  211. throw new InvalidOperationException();
  212. } else {
  213. modCount++;
  214. object ret = contents[current];
  215. count--;
  216. current--;
  217. return ret;
  218. }
  219. }
  220. // FIXME: We should probably be a bit smarter about our
  221. // resizing. After a certain point, doubling isn't that smart.
  222. // We just need to find out what that point is...
  223. public virtual void Push(Object o) {
  224. modCount++;
  225. if (capacity == count) {
  226. Resize(capacity * 2);
  227. }
  228. count++;
  229. current++;
  230. contents[current] = o;
  231. }
  232. public virtual object[] ToArray() {
  233. object[] ret = new object[count];
  234. Array.Copy(contents, ret, count);
  235. // ret needs to be in LIFO order
  236. Array.Reverse(ret);
  237. return ret;
  238. }
  239. }
  240. }