Stack.cs 7.6 KB

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