Stack.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. //
  2. // System.Collections.Stack
  3. //
  4. // Author:
  5. // Garrett Rooney ([email protected])
  6. //
  7. // (C) 2001 Garrett Rooney
  8. //
  9. //
  10. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. namespace System.Collections {
  32. [Serializable]
  33. public class Stack : ICollection, IEnumerable, ICloneable {
  34. // properties
  35. private object[] contents;
  36. private int current = -1;
  37. private int count = 0;
  38. private int capacity;
  39. private int modCount = 0;
  40. private void Resize(int ncapacity) {
  41. ncapacity = Math.Max (ncapacity, 16);
  42. object[] ncontents = new object[ncapacity];
  43. Array.Copy(contents, ncontents, count);
  44. capacity = ncapacity;
  45. contents = ncontents;
  46. }
  47. public Stack () : this (16) {}
  48. public Stack(ICollection col) : this (col == null ? 16 : col.Count) {
  49. if (col == null)
  50. throw new ArgumentNullException("col");
  51. current = col.Count - 1;
  52. count = col.Count;
  53. col.CopyTo(contents, 0);
  54. }
  55. public Stack (int initialCapacity) {
  56. if (initialCapacity < 0)
  57. throw new ArgumentOutOfRangeException ("initialCapacity");
  58. capacity = Math.Max (initialCapacity, 16);
  59. contents = new object[capacity];
  60. }
  61. [Serializable]
  62. private class SyncStack : Stack {
  63. Stack stack;
  64. internal SyncStack(Stack s) {
  65. stack = s;
  66. }
  67. public override int Count {
  68. get {
  69. lock (stack) {
  70. return stack.Count;
  71. }
  72. }
  73. }
  74. /*
  75. public override bool IsReadOnly {
  76. get {
  77. lock (stack) {
  78. return stack.IsReadOnly;
  79. }
  80. }
  81. }
  82. */
  83. public override bool IsSynchronized {
  84. get { return true; }
  85. }
  86. public override object SyncRoot {
  87. get { return stack.SyncRoot; }
  88. }
  89. public override void Clear() {
  90. lock(stack) { stack.Clear(); }
  91. }
  92. public override object Clone() {
  93. lock (stack) {
  94. return Stack.Synchronized((Stack)stack.Clone());
  95. }
  96. }
  97. public override bool Contains(object obj) {
  98. lock (stack) { return stack.Contains(obj); }
  99. }
  100. public override void CopyTo(Array array, int index) {
  101. lock (stack) { stack.CopyTo(array, index); }
  102. }
  103. public override IEnumerator GetEnumerator() {
  104. lock (stack) {
  105. return new Enumerator(stack);
  106. }
  107. }
  108. public override object Peek() {
  109. lock (stack) { return stack.Peek(); }
  110. }
  111. public override object Pop() {
  112. lock (stack) { return stack.Pop(); }
  113. }
  114. public override void Push(object obj) {
  115. lock (stack) { stack.Push(obj); }
  116. }
  117. public override object[] ToArray() {
  118. lock (stack) { return stack.ToArray(); }
  119. }
  120. }
  121. public static Stack Synchronized(Stack s) {
  122. if (s == null) {
  123. throw new ArgumentNullException();
  124. }
  125. return new SyncStack(s);
  126. }
  127. public virtual int Count {
  128. get { return count; }
  129. }
  130. /*
  131. public virtual bool IsReadOnly {
  132. get { return false; }
  133. }
  134. */
  135. public virtual bool IsSynchronized {
  136. get { return false; }
  137. }
  138. public virtual object SyncRoot {
  139. get { return this; }
  140. }
  141. public virtual void Clear() {
  142. modCount++;
  143. for (int i = 0; i < count; i++) {
  144. contents[i] = null;
  145. }
  146. count = 0;
  147. current = -1;
  148. }
  149. public virtual object Clone() {
  150. Stack stack = new Stack (contents);
  151. stack.current = current;
  152. stack.count = count;
  153. return stack;
  154. }
  155. public virtual bool Contains(object obj) {
  156. if (count == 0)
  157. return false;
  158. if (obj == null) {
  159. for (int i = 0; i < count; i++) {
  160. if (contents[i] == null)
  161. return true;
  162. }
  163. } else {
  164. for (int i = 0; i < count; i++) {
  165. if (contents[i].Equals(obj))
  166. return true;
  167. }
  168. }
  169. return false;
  170. }
  171. public virtual void CopyTo (Array array, int index) {
  172. if (array == null) {
  173. throw new ArgumentNullException("array");
  174. }
  175. if (index < 0) {
  176. throw new ArgumentOutOfRangeException("index");
  177. }
  178. if (array.Rank > 1 ||
  179. index >= array.Length ||
  180. count > array.Length - index) {
  181. throw new ArgumentException();
  182. }
  183. for (int i = current; i != -1; i--) {
  184. array.SetValue(contents[i],
  185. count - (i + 1) + index);
  186. }
  187. }
  188. private class Enumerator : IEnumerator, ICloneable {
  189. const int EOF = -1;
  190. const int BOF = -2;
  191. Stack stack;
  192. private int modCount;
  193. private int current;
  194. internal Enumerator(Stack s) {
  195. stack = s;
  196. modCount = s.modCount;
  197. current = BOF;
  198. }
  199. public object Clone ()
  200. {
  201. return MemberwiseClone ();
  202. }
  203. public virtual object Current {
  204. get {
  205. if (modCount != stack.modCount
  206. || current == BOF
  207. || current == EOF
  208. || current > stack.count)
  209. throw new InvalidOperationException();
  210. return stack.contents[current];
  211. }
  212. }
  213. public virtual bool MoveNext() {
  214. if (modCount != stack.modCount)
  215. throw new InvalidOperationException();
  216. switch (current) {
  217. case BOF:
  218. current = stack.current;
  219. return current != -1;
  220. case EOF:
  221. return false;
  222. default:
  223. current--;
  224. return current != -1;
  225. }
  226. }
  227. public virtual void Reset() {
  228. if (modCount != stack.modCount) {
  229. throw new InvalidOperationException();
  230. }
  231. current = BOF;
  232. }
  233. }
  234. public virtual IEnumerator GetEnumerator() {
  235. return new Enumerator(this);
  236. }
  237. public virtual object Peek() {
  238. if (current == -1) {
  239. throw new InvalidOperationException();
  240. } else {
  241. return contents[current];
  242. }
  243. }
  244. public virtual object Pop() {
  245. if (current == -1) {
  246. throw new InvalidOperationException();
  247. } else {
  248. modCount++;
  249. object ret = contents[current];
  250. contents [current] = null;
  251. count--;
  252. current--;
  253. // if we're down to capacity/4, go back to a
  254. // lower array size. this should keep us from
  255. // sucking down huge amounts of memory when
  256. // putting large numbers of items in the Stack.
  257. // if we're lower than 16, don't bother, since
  258. // it will be more trouble than it's worth.
  259. if (count <= (capacity/4) && count > 16) {
  260. Resize(capacity/2);
  261. }
  262. return ret;
  263. }
  264. }
  265. public virtual void Push(Object o) {
  266. modCount++;
  267. if (capacity == count) {
  268. Resize(capacity * 2);
  269. }
  270. count++;
  271. current++;
  272. contents[current] = o;
  273. }
  274. public virtual object[] ToArray() {
  275. object[] ret = new object[count];
  276. Array.Copy(contents, ret, count);
  277. // ret needs to be in LIFO order
  278. Array.Reverse(ret);
  279. return ret;
  280. }
  281. }
  282. }