Stack.cs 7.7 KB

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