mscorlib(4.0.0.0) API with additions
Stack.cs
1 using System.Diagnostics;
4 using System.Threading;
5 
6 namespace System.Collections
7 {
10  [DebuggerTypeProxy(typeof(StackDebugView))]
11  [DebuggerDisplay("Count = {Count}")]
12  [ComVisible(true)]
14  {
15  [Serializable]
16  private class SyncStack : Stack
17  {
18  private Stack _s;
19 
20  private object _root;
21 
22  public override bool IsSynchronized => true;
23 
24  public override object SyncRoot => _root;
25 
26  public override int Count
27  {
28  get
29  {
30  lock (_root)
31  {
32  return _s.Count;
33  }
34  }
35  }
36 
37  internal SyncStack(Stack stack)
38  {
39  _s = stack;
40  _root = stack.SyncRoot;
41  }
42 
43  public override bool Contains(object obj)
44  {
45  lock (_root)
46  {
47  return _s.Contains(obj);
48  }
49  }
50 
51  public override object Clone()
52  {
53  lock (_root)
54  {
55  return new SyncStack((Stack)_s.Clone());
56  }
57  }
58 
59  public override void Clear()
60  {
61  lock (_root)
62  {
63  _s.Clear();
64  }
65  }
66 
67  public override void CopyTo(Array array, int arrayIndex)
68  {
69  lock (_root)
70  {
71  _s.CopyTo(array, arrayIndex);
72  }
73  }
74 
75  public override void Push(object value)
76  {
77  lock (_root)
78  {
79  _s.Push(value);
80  }
81  }
82 
83  public override object Pop()
84  {
85  lock (_root)
86  {
87  return _s.Pop();
88  }
89  }
90 
91  public override IEnumerator GetEnumerator()
92  {
93  lock (_root)
94  {
95  return _s.GetEnumerator();
96  }
97  }
98 
99  public override object Peek()
100  {
101  lock (_root)
102  {
103  return _s.Peek();
104  }
105  }
106 
107  public override object[] ToArray()
108  {
109  lock (_root)
110  {
111  return _s.ToArray();
112  }
113  }
114  }
115 
116  [Serializable]
117  private class StackEnumerator : IEnumerator, ICloneable
118  {
119  private Stack _stack;
120 
121  private int _index;
122 
123  private int _version;
124 
125  private object currentElement;
126 
127  public virtual object Current
128  {
129  get
130  {
131  if (_index == -2)
132  {
133  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumNotStarted"));
134  }
135  if (_index == -1)
136  {
137  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumEnded"));
138  }
139  return currentElement;
140  }
141  }
142 
143  internal StackEnumerator(Stack stack)
144  {
145  _stack = stack;
146  _version = _stack._version;
147  _index = -2;
148  currentElement = null;
149  }
150 
151  public object Clone()
152  {
153  return MemberwiseClone();
154  }
155 
156  public virtual bool MoveNext()
157  {
158  if (_version != _stack._version)
159  {
160  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumFailedVersion"));
161  }
162  bool flag;
163  if (_index == -2)
164  {
165  _index = _stack._size - 1;
166  flag = (_index >= 0);
167  if (flag)
168  {
169  currentElement = _stack._array[_index];
170  }
171  return flag;
172  }
173  if (_index == -1)
174  {
175  return false;
176  }
177  flag = (--_index >= 0);
178  if (flag)
179  {
180  currentElement = _stack._array[_index];
181  }
182  else
183  {
184  currentElement = null;
185  }
186  return flag;
187  }
188 
189  public virtual void Reset()
190  {
191  if (_version != _stack._version)
192  {
193  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EnumFailedVersion"));
194  }
195  _index = -2;
196  currentElement = null;
197  }
198  }
199 
200  internal class StackDebugView
201  {
202  private Stack stack;
203 
204  [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
205  public object[] Items
206  {
207  get
208  {
209  return stack.ToArray();
210  }
211  }
212 
213  public StackDebugView(Stack stack)
214  {
215  if (stack == null)
216  {
217  throw new ArgumentNullException("stack");
218  }
219  this.stack = stack;
220  }
221  }
222 
223  private object[] _array;
224 
225  private int _size;
226 
227  private int _version;
228 
229  [NonSerialized]
230  private object _syncRoot;
231 
232  private const int _defaultCapacity = 10;
233 
236  public virtual int Count => _size;
237 
241  public virtual bool IsSynchronized => false;
242 
245  public virtual object SyncRoot
246  {
247  get
248  {
249  if (_syncRoot == null)
250  {
251  Interlocked.CompareExchange<object>(ref _syncRoot, new object(), (object)null);
252  }
253  return _syncRoot;
254  }
255  }
256 
258  public Stack()
259  {
260  _array = new object[10];
261  _size = 0;
262  _version = 0;
263  }
264 
269  public Stack(int initialCapacity)
270  {
271  if (initialCapacity < 0)
272  {
273  throw new ArgumentOutOfRangeException("initialCapacity", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
274  }
275  if (initialCapacity < 10)
276  {
277  initialCapacity = 10;
278  }
279  _array = new object[initialCapacity];
280  _size = 0;
281  _version = 0;
282  }
283 
288  public Stack(ICollection col)
289  : this(col?.Count ?? 32)
290  {
291  if (col == null)
292  {
293  throw new ArgumentNullException("col");
294  }
295  IEnumerator enumerator = col.GetEnumerator();
296  while (enumerator.MoveNext())
297  {
298  Push(enumerator.Current);
299  }
300  }
301 
303  public virtual void Clear()
304  {
305  Array.Clear(_array, 0, _size);
306  _size = 0;
307  _version++;
308  }
309 
312  public virtual object Clone()
313  {
314  Stack stack = new Stack(_size);
315  stack._size = _size;
316  Array.Copy(_array, 0, stack._array, 0, _size);
317  stack._version = _version;
318  return stack;
319  }
320 
325  public virtual bool Contains(object obj)
326  {
327  int num = _size;
328  while (num-- > 0)
329  {
330  if (obj == null)
331  {
332  if (_array[num] == null)
333  {
334  return true;
335  }
336  }
337  else if (_array[num] != null && _array[num].Equals(obj))
338  {
339  return true;
340  }
341  }
342  return false;
343  }
344 
355  public virtual void CopyTo(Array array, int index)
356  {
357  if (array == null)
358  {
359  throw new ArgumentNullException("array");
360  }
361  if (array.Rank != 1)
362  {
363  throw new ArgumentException(Environment.GetResourceString("Arg_RankMultiDimNotSupported"));
364  }
365  if (index < 0)
366  {
367  throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
368  }
369  if (array.Length - index < _size)
370  {
371  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
372  }
373  int i = 0;
374  if (array is object[])
375  {
376  object[] array2 = (object[])array;
377  for (; i < _size; i++)
378  {
379  array2[i + index] = _array[_size - i - 1];
380  }
381  }
382  else
383  {
384  for (; i < _size; i++)
385  {
386  array.SetValue(_array[_size - i - 1], i + index);
387  }
388  }
389  }
390 
393  public virtual IEnumerator GetEnumerator()
394  {
395  return new StackEnumerator(this);
396  }
397 
401  public virtual object Peek()
402  {
403  if (_size == 0)
404  {
405  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EmptyStack"));
406  }
407  return _array[_size - 1];
408  }
409 
413  public virtual object Pop()
414  {
415  if (_size == 0)
416  {
417  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_EmptyStack"));
418  }
419  _version++;
420  object result = _array[--_size];
421  _array[_size] = null;
422  return result;
423  }
424 
427  public virtual void Push(object obj)
428  {
429  if (_size == _array.Length)
430  {
431  object[] array = new object[2 * _array.Length];
432  Array.Copy(_array, 0, array, 0, _size);
433  _array = array;
434  }
435  _array[_size++] = obj;
436  _version++;
437  }
438 
444  [HostProtection(SecurityAction.LinkDemand, Synchronization = true)]
445  public static Stack Synchronized(Stack stack)
446  {
447  if (stack == null)
448  {
449  throw new ArgumentNullException("stack");
450  }
451  return new SyncStack(stack);
452  }
453 
456  public virtual object[] ToArray()
457  {
458  object[] array = new object[_size];
459  for (int i = 0; i < _size; i++)
460  {
461  array[i] = _array[_size - i - 1];
462  }
463  return array;
464  }
465  }
466 }
virtual object SyncRoot
Gets an object that can be used to synchronize access to the T:System.Collections....
Definition: Stack.cs:246
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
virtual bool IsSynchronized
Gets a value indicating whether access to the T:System.Collections.Stack is synchronized (thread safe...
Definition: Stack.cs:241
bool MoveNext()
Advances the enumerator to the next element of the collection.
Represents a simple last-in-first-out (LIFO) non-generic collection of objects.
Definition: Stack.cs:13
virtual void CopyTo(Array array, int index)
Copies the T:System.Collections.Stack to an existing one-dimensional T:System.Array,...
Definition: Stack.cs:355
static void Clear(Array array, int index, int length)
Sets a range of elements in an array to the default value of each element type.
virtual object Clone()
Creates a shallow copy of the T:System.Collections.Stack.
Definition: Stack.cs:312
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
virtual IEnumerator GetEnumerator()
Returns an T:System.Collections.IEnumerator for the T:System.Collections.Stack.
Definition: Stack.cs:393
static Stack Synchronized(Stack stack)
Returns a synchronized (thread safe) wrapper for the T:System.Collections.Stack.
Definition: Stack.cs:445
virtual object [] ToArray()
Copies the T:System.Collections.Stack to a new array.
Definition: Stack.cs:456
Stack(int initialCapacity)
Initializes a new instance of the T:System.Collections.Stack class that is empty and has the specifie...
Definition: Stack.cs:269
SecurityAction
Specifies the security actions that can be performed using declarative security.
Exposes an enumerator, which supports a simple iteration over a non-generic collection....
Definition: IEnumerable.cs:9
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
virtual object Peek()
Returns the object at the top of the T:System.Collections.Stack without removing it.
Definition: Stack.cs:401
static int CompareExchange(ref int location1, int value, int comparand)
Compares two 32-bit signed integers for equality and, if they are equal, replaces the first value.
object Current
Gets the element in the collection at the current position of the enumerator.
Definition: IEnumerator.cs:15
Supports cloning, which creates a new instance of a class with the same value as an existing instance...
Definition: ICloneable.cs:7
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition: Array.cs:17
virtual object Pop()
Removes and returns the object at the top of the T:System.Collections.Stack.
Definition: Stack.cs:413
IEnumerator GetEnumerator()
Returns an enumerator that iterates through a collection.
The exception that is thrown when one of the arguments provided to a method is not valid.
Stack()
Initializes a new instance of the T:System.Collections.Stack class that is empty and has the default ...
Definition: Stack.cs:258
static void Copy(Array sourceArray, Array destinationArray, int length)
Copies a range of elements from an T:System.Array starting at the first element and pastes them into ...
Definition: Array.cs:1275
virtual void Clear()
Removes all objects from the T:System.Collections.Stack.
Definition: Stack.cs:303
Specifies that the class can be serialized.
virtual int Count
Gets the number of elements contained in the T:System.Collections.Stack.
Definition: Stack.cs:236
The exception that is thrown when a method call is invalid for the object's current state.
virtual void Push(object obj)
Inserts an object at the top of the T:System.Collections.Stack.
Definition: Stack.cs:427
Defines size, enumerators, and synchronization methods for all nongeneric collections.
Definition: ICollection.cs:8
DebuggerBrowsableState
Provides display instructions for the debugger.
Provides atomic operations for variables that are shared by multiple threads.
Definition: Interlocked.cs:10
Supports a simple iteration over a non-generic collection.
Definition: IEnumerator.cs:9
virtual bool Contains(object obj)
Determines whether an element is in the T:System.Collections.Stack.
Definition: Stack.cs:325