Stack.cs 7.7 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. [MonoTODO ("Fix serialization compatibility with MS.NET")]
  34. public class Stack : ICollection, IEnumerable, ICloneable {
  35. // properties
  36. private object[] contents;
  37. private int current = -1;
  38. private int count = 0;
  39. private int capacity;
  40. private int modCount = 0;
  41. private void Resize(int ncapacity) {
  42. ncapacity = Math.Max (ncapacity, 16);
  43. object[] ncontents = new object[ncapacity];
  44. Array.Copy(contents, ncontents, count);
  45. capacity = ncapacity;
  46. contents = ncontents;
  47. }
  48. public Stack () : this (16) {}
  49. public Stack(ICollection col) : this (col == null ? 16 : col.Count) {
  50. if (col == null)
  51. throw new ArgumentNullException("col");
  52. current = col.Count - 1;
  53. count = col.Count;
  54. col.CopyTo(contents, 0);
  55. }
  56. public Stack (int initialCapacity) {
  57. if (initialCapacity < 0)
  58. throw new ArgumentOutOfRangeException ("initialCapacity");
  59. capacity = Math.Max (initialCapacity, 16);
  60. contents = new object[capacity];
  61. }
  62. [Serializable]
  63. private class SyncStack : Stack {
  64. Stack stack;
  65. internal SyncStack(Stack s) {
  66. stack = s;
  67. }
  68. public override int Count {
  69. get {
  70. lock (stack) {
  71. return stack.Count;
  72. }
  73. }
  74. }
  75. /*
  76. public override bool IsReadOnly {
  77. get {
  78. lock (stack) {
  79. return stack.IsReadOnly;
  80. }
  81. }
  82. }
  83. */
  84. public override bool IsSynchronized {
  85. get { return true; }
  86. }
  87. public override object SyncRoot {
  88. get { return stack.SyncRoot; }
  89. }
  90. public override void Clear() {
  91. lock(stack) { stack.Clear(); }
  92. }
  93. public override object Clone() {
  94. lock (stack) {
  95. return Stack.Synchronized((Stack)stack.Clone());
  96. }
  97. }
  98. public override bool Contains(object obj) {
  99. lock (stack) { return stack.Contains(obj); }
  100. }
  101. public override void CopyTo(Array array, int index) {
  102. lock (stack) { stack.CopyTo(array, index); }
  103. }
  104. public override IEnumerator GetEnumerator() {
  105. lock (stack) {
  106. return new Enumerator(stack);
  107. }
  108. }
  109. public override object Peek() {
  110. lock (stack) { return stack.Peek(); }
  111. }
  112. public override object Pop() {
  113. lock (stack) { return stack.Pop(); }
  114. }
  115. public override void Push(object obj) {
  116. lock (stack) { stack.Push(obj); }
  117. }
  118. public override object[] ToArray() {
  119. lock (stack) { return stack.ToArray(); }
  120. }
  121. }
  122. public static Stack Synchronized(Stack s) {
  123. if (s == null) {
  124. throw new ArgumentNullException();
  125. }
  126. return new SyncStack(s);
  127. }
  128. public virtual int Count {
  129. get { return count; }
  130. }
  131. /*
  132. public virtual bool IsReadOnly {
  133. get { return false; }
  134. }
  135. */
  136. public virtual bool IsSynchronized {
  137. get { return false; }
  138. }
  139. public virtual object SyncRoot {
  140. get { return this; }
  141. }
  142. public virtual void Clear() {
  143. modCount++;
  144. for (int i = 0; i < count; i++) {
  145. contents[i] = null;
  146. }
  147. count = 0;
  148. current = -1;
  149. }
  150. public virtual object Clone() {
  151. Stack stack = new Stack (contents);
  152. stack.current = current;
  153. stack.count = count;
  154. return stack;
  155. }
  156. public virtual bool Contains(object obj) {
  157. if (count == 0)
  158. return false;
  159. if (obj == null) {
  160. for (int i = 0; i < count; i++) {
  161. if (contents[i] == null)
  162. return true;
  163. }
  164. } else {
  165. for (int i = 0; i < count; i++) {
  166. if (contents[i].Equals(obj))
  167. return true;
  168. }
  169. }
  170. return false;
  171. }
  172. public virtual void CopyTo (Array array, int index) {
  173. if (array == null) {
  174. throw new ArgumentNullException("array");
  175. }
  176. if (index < 0) {
  177. throw new ArgumentOutOfRangeException("index");
  178. }
  179. if (array.Rank > 1 ||
  180. index >= array.Length ||
  181. count > array.Length - index) {
  182. throw new ArgumentException();
  183. }
  184. for (int i = current; i != -1; i--) {
  185. array.SetValue(contents[i],
  186. count - (i + 1) + index);
  187. }
  188. }
  189. private class Enumerator : IEnumerator, ICloneable {
  190. const int EOF = -1;
  191. const int BOF = -2;
  192. Stack stack;
  193. private int modCount;
  194. private int current;
  195. internal Enumerator(Stack s) {
  196. stack = s;
  197. modCount = s.modCount;
  198. current = BOF;
  199. }
  200. public object Clone ()
  201. {
  202. return MemberwiseClone ();
  203. }
  204. public virtual object Current {
  205. get {
  206. if (modCount != stack.modCount
  207. || current == BOF
  208. || current == EOF
  209. || current > stack.count)
  210. throw new InvalidOperationException();
  211. return stack.contents[current];
  212. }
  213. }
  214. public virtual bool MoveNext() {
  215. if (modCount != stack.modCount)
  216. throw new InvalidOperationException();
  217. switch (current) {
  218. case BOF:
  219. current = stack.current;
  220. return current != -1;
  221. case EOF:
  222. return false;
  223. default:
  224. current--;
  225. return current != -1;
  226. }
  227. }
  228. public virtual void Reset() {
  229. if (modCount != stack.modCount) {
  230. throw new InvalidOperationException();
  231. }
  232. current = BOF;
  233. }
  234. }
  235. public virtual IEnumerator GetEnumerator() {
  236. return new Enumerator(this);
  237. }
  238. public virtual object Peek() {
  239. if (current == -1) {
  240. throw new InvalidOperationException();
  241. } else {
  242. return contents[current];
  243. }
  244. }
  245. public virtual object Pop() {
  246. if (current == -1) {
  247. throw new InvalidOperationException();
  248. } else {
  249. modCount++;
  250. object ret = contents[current];
  251. contents [current] = null;
  252. count--;
  253. current--;
  254. // if we're down to capacity/4, go back to a
  255. // lower array size. this should keep us from
  256. // sucking down huge amounts of memory when
  257. // putting large numbers of items in the Stack.
  258. // if we're lower than 16, don't bother, since
  259. // it will be more trouble than it's worth.
  260. if (count <= (capacity/4) && count > 16) {
  261. Resize(capacity/2);
  262. }
  263. return ret;
  264. }
  265. }
  266. public virtual void Push(Object o) {
  267. modCount++;
  268. if (capacity == count) {
  269. Resize(capacity * 2);
  270. }
  271. count++;
  272. current++;
  273. contents[current] = o;
  274. }
  275. public virtual object[] ToArray() {
  276. object[] ret = new object[count];
  277. Array.Copy(contents, ret, count);
  278. // ret needs to be in LIFO order
  279. Array.Reverse(ret);
  280. return ret;
  281. }
  282. }
  283. }