Stack.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. // -*- Mode: C; tab-width: 8; c-basic-offset: 8 -*-
  2. //
  3. // System.Collections.Stack
  4. //
  5. // Author:
  6. // Garrett Rooney ([email protected])
  7. //
  8. // (C) 2001 Garrett Rooney
  9. //
  10. namespace System.Collections {
  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 = 16;
  17. private bool readOnly = false;
  18. private bool synchronized = false;
  19. private void Resize(int ncapacity) {
  20. object[] ncontents = new object[ncapacity];
  21. for (int i = 0; i < capacity; i++) {
  22. ncontents[i] = contents[i];
  23. }
  24. capacity = ncapacity;
  25. contents = ncontents;
  26. }
  27. public Stack() {
  28. contents = new object[capacity];
  29. }
  30. public Stack(ICollection collection) {
  31. capacity = collection.Count;
  32. contents = new object[capacity];
  33. current = capacity - 1;
  34. count = capacity;
  35. int i = 0;
  36. foreach (object o in collection) {
  37. contents[i++] = o;
  38. }
  39. }
  40. public Stack(int c) {
  41. capacity = c;
  42. contents = new object[capacity];
  43. }
  44. // The Synchronized version of Stack uses lock(this) to make
  45. // it thread safe. This should be ok, even though we wrap an
  46. // Array, which is a Collection, since there is no way for the
  47. // outside world to get a reference to the Array. If I'm
  48. // wrong about this, then we should change lock(this) to
  49. // lock(contents.SyncRoot).
  50. private class SyncStack : Stack {
  51. public SyncStack(Stack s) {
  52. contents = s.contents;
  53. current = s.current;
  54. count = s.count;
  55. capacity = s.capacity;
  56. readOnly = s.readOnly;
  57. synchronized = true;
  58. }
  59. public override int Count {
  60. get { lock(this) { return count; } }
  61. }
  62. public override bool IsReadOnly {
  63. get { lock(this) { return readOnly; } }
  64. }
  65. public override bool IsSynchronized {
  66. get { lock(this) { return synchronized; } }
  67. }
  68. public override object SyncRoot {
  69. get { lock(this) { return this; } }
  70. }
  71. public override void Clear() {
  72. lock(this) { base.Clear(); }
  73. }
  74. public override object Clone() {
  75. lock (this) { return base.Clone(); }
  76. }
  77. public override bool Contains(object obj) {
  78. lock (this) { return base.Contains(obj); }
  79. }
  80. public override void CopyTo(Array array, int index) {
  81. lock (this) { base.CopyTo(array, index); }
  82. }
  83. // As noted above, this uses lock(this), and if that
  84. // turns out to be unsafe, it should be changed to
  85. // lock(contents.SyncRoot).
  86. private class SyncEnumerator : Enumerator {
  87. internal SyncEnumerator(Stack s) : base(s) {}
  88. public override object Current {
  89. get {
  90. lock (this) {
  91. return base.Current;
  92. }
  93. }
  94. }
  95. public override bool MoveNext() {
  96. lock (this) { return base.MoveNext(); }
  97. }
  98. public override void Reset() {
  99. lock (this) { base.Reset(); }
  100. }
  101. }
  102. public override IEnumerator GetEnumerator() {
  103. lock (this) {
  104. return new SyncEnumerator(this);
  105. }
  106. }
  107. public override object Peek() {
  108. lock (this) { return base.Peek(); }
  109. }
  110. public override object Pop() {
  111. lock (this) { return base.Pop(); }
  112. }
  113. public override void Push(object obj) {
  114. lock (this) { base.Push(obj); }
  115. }
  116. public override object[] ToArray() {
  117. lock (this) { return base.ToArray(); }
  118. }
  119. }
  120. public static Stack Syncronized(Stack s) {
  121. if (s == null) {
  122. throw new ArgumentNullException();
  123. }
  124. return new SyncStack(s);
  125. }
  126. public virtual int Count {
  127. get { return count; }
  128. }
  129. public virtual bool IsReadOnly {
  130. get { return readOnly; }
  131. }
  132. public virtual bool IsSynchronized {
  133. get { return synchronized; }
  134. }
  135. // If using this for the SyncRoot is unsafe, we should use
  136. // contents.SyncRoot instead. I think this is ok though.
  137. public virtual object SyncRoot {
  138. get { return this; }
  139. }
  140. public virtual void Clear() {
  141. for (int i = 0; i < count; i++) {
  142. contents[i] = null;
  143. }
  144. count = 0;
  145. current = -1;
  146. }
  147. public virtual object Clone() {
  148. Stack stack;
  149. if (synchronized == false) {
  150. stack = new Stack();
  151. stack.current = current;
  152. stack.contents = contents;
  153. stack.count = count;
  154. stack.capacity = capacity;
  155. stack.readOnly = readOnly;
  156. stack.synchronized = synchronized;
  157. } else {
  158. stack = new SyncStack(this);
  159. }
  160. return stack;
  161. }
  162. public virtual bool Contains(object obj) {
  163. if (count == 0)
  164. return false;
  165. for (int i = 0; i < count; i++) {
  166. if (contents[i].Equals(obj))
  167. return true;
  168. }
  169. return false;
  170. }
  171. public virtual void CopyTo (Array array, int index) {
  172. if (array == null) {
  173. throw new ArgumentNullException();
  174. }
  175. if (index < 0) {
  176. throw new ArgumentOutOfRangeException();
  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. // I made several methods of this class virtual, so that they
  189. // could be overriden by a thread safe version of the
  190. // Enumerator for use by SyncStack. I don't know if MS does
  191. // that in their implimentation, but it seemed like one should
  192. // reasonably be able to expect a thread safe Collection to
  193. // return a thread safe Enumerator. If this is a problem, it
  194. // could be ripped out. Realistically speaking, I doubt if
  195. // many people would ever notice if the Enumerator was thread
  196. // safe, as I cannot concieve of a situation where an
  197. // Enumerator would be accessed by more than one thread.
  198. private class Enumerator : IEnumerator {
  199. private Array contents;
  200. private int current;
  201. private int count;
  202. private int begin;
  203. internal Enumerator(Stack s) {
  204. // this is odd. it seems that you need to
  205. // start one further ahead than current, since
  206. // MoveNext() gets called first when using an
  207. // Enumeration...
  208. begin = s.current + 1;
  209. current = begin;
  210. count = s.count;
  211. contents = (Array) s.contents.Clone();
  212. }
  213. public virtual object Current {
  214. get {
  215. if (current == -1 || current > count)
  216. throw new InvalidOperationException();
  217. return contents.GetValue(current);
  218. }
  219. }
  220. public virtual bool MoveNext() {
  221. if (current == -1) {
  222. throw new InvalidOperationException();
  223. }
  224. current--;
  225. if (current == -1) {
  226. return false;
  227. } else {
  228. return true;
  229. }
  230. }
  231. public virtual void Reset() {
  232. // start one ahead of stack.current, so the
  233. // first MoveNext() will put us at the top
  234. current = begin;
  235. }
  236. }
  237. public virtual IEnumerator GetEnumerator() {
  238. return new Enumerator(this);
  239. }
  240. public virtual object Peek() {
  241. if (current == -1) {
  242. throw new InvalidOperationException();
  243. } else {
  244. return contents[current];
  245. }
  246. }
  247. public virtual object Pop() {
  248. if (current == -1) {
  249. throw new InvalidOperationException();
  250. } else {
  251. object ret = contents[current];
  252. count--;
  253. current--;
  254. return ret;
  255. }
  256. }
  257. // FIXME: We should probably be a bit smarter about our
  258. // resizing. After a certain point, doubling isn't that smart.
  259. // We just need to find out what that point is...
  260. public virtual void Push(Object o) {
  261. if (capacity == count) {
  262. Resize(capacity * 2);
  263. }
  264. count++;
  265. current++;
  266. contents[current] = o;
  267. }
  268. public virtual object[] ToArray() {
  269. object[] ret = new object[count];
  270. Array.Copy(contents, ret, count);
  271. // ret needs to be in LIFO order
  272. Array.Reverse(ret);
  273. return ret;
  274. }
  275. }
  276. }