Stack.cs 6.4 KB

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