mscorlib(4.0.0.0) API with additions
Stack.cs
1 using System.Diagnostics;
3 using System.Threading;
4 
6 {
10  [DebuggerTypeProxy(typeof(System_StackDebugView<>))]
11  [DebuggerDisplay("Count = {Count}")]
12  [ComVisible(false)]
13  [global::__DynamicallyInvokable]
15  {
17  [Serializable]
18  [global::__DynamicallyInvokable]
20  {
21  private Stack<T> _stack;
22 
23  private int _index;
24 
25  private int _version;
26 
27  private T currentElement;
28 
32  [global::__DynamicallyInvokable]
33  public T Current
34  {
35  [global::__DynamicallyInvokable]
36  get
37  {
38  if (_index == -2)
39  {
40  System.ThrowHelper.ThrowInvalidOperationException(System.ExceptionResource.InvalidOperation_EnumNotStarted);
41  }
42  if (_index == -1)
43  {
44  System.ThrowHelper.ThrowInvalidOperationException(System.ExceptionResource.InvalidOperation_EnumEnded);
45  }
46  return currentElement;
47  }
48  }
49 
53  [global::__DynamicallyInvokable]
54  object IEnumerator.Current
55  {
56  [global::__DynamicallyInvokable]
57  get
58  {
59  if (_index == -2)
60  {
61  System.ThrowHelper.ThrowInvalidOperationException(System.ExceptionResource.InvalidOperation_EnumNotStarted);
62  }
63  if (_index == -1)
64  {
65  System.ThrowHelper.ThrowInvalidOperationException(System.ExceptionResource.InvalidOperation_EnumEnded);
66  }
67  return currentElement;
68  }
69  }
70 
71  internal Enumerator(Stack<T> stack)
72  {
73  _stack = stack;
74  _version = _stack._version;
75  _index = -2;
76  currentElement = default(T);
77  }
78 
80  [global::__DynamicallyInvokable]
81  public void Dispose()
82  {
83  _index = -1;
84  }
85 
90  [global::__DynamicallyInvokable]
91  public bool MoveNext()
92  {
93  if (_version != _stack._version)
94  {
95  System.ThrowHelper.ThrowInvalidOperationException(System.ExceptionResource.InvalidOperation_EnumFailedVersion);
96  }
97  bool flag;
98  if (_index == -2)
99  {
100  _index = _stack._size - 1;
101  flag = (_index >= 0);
102  if (flag)
103  {
104  currentElement = _stack._array[_index];
105  }
106  return flag;
107  }
108  if (_index == -1)
109  {
110  return false;
111  }
112  flag = (--_index >= 0);
113  if (flag)
114  {
115  currentElement = _stack._array[_index];
116  }
117  else
118  {
119  currentElement = default(T);
120  }
121  return flag;
122  }
123 
126  [global::__DynamicallyInvokable]
128  {
129  if (_version != _stack._version)
130  {
131  System.ThrowHelper.ThrowInvalidOperationException(System.ExceptionResource.InvalidOperation_EnumFailedVersion);
132  }
133  _index = -2;
134  currentElement = default(T);
135  }
136  }
137 
138  private T[] _array;
139 
140  private int _size;
141 
142  private int _version;
143 
144  [NonSerialized]
145  private object _syncRoot;
146 
147  private const int _defaultCapacity = 4;
148 
149  private static T[] _emptyArray = new T[0];
150 
153  [global::__DynamicallyInvokable]
154  public int Count
155  {
156  [global::__DynamicallyInvokable]
157  get
158  {
159  return _size;
160  }
161  }
162 
166  [global::__DynamicallyInvokable]
167  bool ICollection.IsSynchronized
168  {
169  [global::__DynamicallyInvokable]
170  get
171  {
172  return false;
173  }
174  }
175 
178  [global::__DynamicallyInvokable]
179  object ICollection.SyncRoot
180  {
181  [global::__DynamicallyInvokable]
182  get
183  {
184  if (_syncRoot == null)
185  {
186  Interlocked.CompareExchange<object>(ref _syncRoot, new object(), (object)null);
187  }
188  return _syncRoot;
189  }
190  }
191 
193  [global::__DynamicallyInvokable]
194  public Stack()
195  {
196  _array = _emptyArray;
197  _size = 0;
198  _version = 0;
199  }
200 
205  [global::__DynamicallyInvokable]
206  public Stack(int capacity)
207  {
208  if (capacity < 0)
209  {
210  System.ThrowHelper.ThrowArgumentOutOfRangeException(System.ExceptionArgument.capacity, System.ExceptionResource.ArgumentOutOfRange_NeedNonNegNumRequired);
211  }
212  _array = new T[capacity];
213  _size = 0;
214  _version = 0;
215  }
216 
221  [global::__DynamicallyInvokable]
222  public Stack(IEnumerable<T> collection)
223  {
224  if (collection == null)
225  {
226  System.ThrowHelper.ThrowArgumentNullException(System.ExceptionArgument.collection);
227  }
228  ICollection<T> collection2 = collection as ICollection<T>;
229  if (collection2 != null)
230  {
231  int count = collection2.Count;
232  _array = new T[count];
233  collection2.CopyTo(_array, 0);
234  _size = count;
235  }
236  else
237  {
238  _size = 0;
239  _array = new T[4];
240  foreach (T item in collection)
241  {
242  Push(item);
243  }
244  }
245  }
246 
248  [global::__DynamicallyInvokable]
249  public void Clear()
250  {
251  Array.Clear(_array, 0, _size);
252  _size = 0;
253  _version++;
254  }
255 
260  [global::__DynamicallyInvokable]
261  public bool Contains(T item)
262  {
263  int num = _size;
265  while (num-- > 0)
266  {
267  if (item == null)
268  {
269  if (_array[num] == null)
270  {
271  return true;
272  }
273  }
274  else if (_array[num] != null && @default.Equals(_array[num], item))
275  {
276  return true;
277  }
278  }
279  return false;
280  }
281 
290  [global::__DynamicallyInvokable]
291  public void CopyTo(T[] array, int arrayIndex)
292  {
293  if (array == null)
294  {
295  System.ThrowHelper.ThrowArgumentNullException(System.ExceptionArgument.array);
296  }
297  if (arrayIndex < 0 || arrayIndex > array.Length)
298  {
299  System.ThrowHelper.ThrowArgumentOutOfRangeException(System.ExceptionArgument.arrayIndex, System.ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
300  }
301  if (array.Length - arrayIndex < _size)
302  {
303  System.ThrowHelper.ThrowArgumentException(System.ExceptionResource.Argument_InvalidOffLen);
304  }
305  Array.Copy(_array, 0, array, arrayIndex, _size);
306  Array.Reverse(array, arrayIndex, _size);
307  }
308 
319  [global::__DynamicallyInvokable]
320  void ICollection.CopyTo(Array array, int arrayIndex)
321  {
322  if (array == null)
323  {
324  System.ThrowHelper.ThrowArgumentNullException(System.ExceptionArgument.array);
325  }
326  if (array.Rank != 1)
327  {
328  System.ThrowHelper.ThrowArgumentException(System.ExceptionResource.Arg_RankMultiDimNotSupported);
329  }
330  if (array.GetLowerBound(0) != 0)
331  {
332  System.ThrowHelper.ThrowArgumentException(System.ExceptionResource.Arg_NonZeroLowerBound);
333  }
334  if (arrayIndex < 0 || arrayIndex > array.Length)
335  {
336  System.ThrowHelper.ThrowArgumentOutOfRangeException(System.ExceptionArgument.arrayIndex, System.ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
337  }
338  if (array.Length - arrayIndex < _size)
339  {
340  System.ThrowHelper.ThrowArgumentException(System.ExceptionResource.Argument_InvalidOffLen);
341  }
342  try
343  {
344  Array.Copy(_array, 0, array, arrayIndex, _size);
345  Array.Reverse(array, arrayIndex, _size);
346  }
347  catch (ArrayTypeMismatchException)
348  {
349  System.ThrowHelper.ThrowArgumentException(System.ExceptionResource.Argument_InvalidArrayType);
350  }
351  }
352 
355  [global::__DynamicallyInvokable]
356  public Enumerator GetEnumerator()
357  {
358  return new Enumerator(this);
359  }
360 
363  [global::__DynamicallyInvokable]
365  {
366  return new Enumerator(this);
367  }
368 
371  [global::__DynamicallyInvokable]
373  {
374  return new Enumerator(this);
375  }
376 
378  [global::__DynamicallyInvokable]
379  public void TrimExcess()
380  {
381  int num = (int)((double)_array.Length * 0.9);
382  if (_size < num)
383  {
384  T[] array = new T[_size];
385  Array.Copy(_array, 0, array, 0, _size);
386  _array = array;
387  _version++;
388  }
389  }
390 
394  [global::__DynamicallyInvokable]
395  public T Peek()
396  {
397  if (_size == 0)
398  {
399  System.ThrowHelper.ThrowInvalidOperationException(System.ExceptionResource.InvalidOperation_EmptyStack);
400  }
401  return _array[_size - 1];
402  }
403 
407  [global::__DynamicallyInvokable]
408  public T Pop()
409  {
410  if (_size == 0)
411  {
412  System.ThrowHelper.ThrowInvalidOperationException(System.ExceptionResource.InvalidOperation_EmptyStack);
413  }
414  _version++;
415  T result = _array[--_size];
416  _array[_size] = default(T);
417  return result;
418  }
419 
422  [global::__DynamicallyInvokable]
423  public void Push(T item)
424  {
425  if (_size == _array.Length)
426  {
427  T[] array = new T[(_array.Length == 0) ? 4 : (2 * _array.Length)];
428  Array.Copy(_array, 0, array, 0, _size);
429  _array = array;
430  }
431  _array[_size++] = item;
432  _version++;
433  }
434 
437  [global::__DynamicallyInvokable]
438  public T[] ToArray()
439  {
440  T[] array = new T[_size];
441  for (int i = 0; i < _size; i++)
442  {
443  array[i] = _array[_size - i - 1];
444  }
445  return array;
446  }
447  }
448 }
int Count
Gets the number of elements contained in the T:System.Collections.Generic.Stack`1.
Definition: Stack.cs:155
void CopyTo(T[] array, int arrayIndex)
Copies the T:System.Collections.Generic.Stack`1 to an existing one-dimensional T:System....
Definition: Stack.cs:291
Provides a base class for implementations of the T:System.Collections.Generic.IEqualityComparer`1 gen...
T Current
Gets the element at the current position of the enumerator.
Definition: Stack.cs:34
void Reset()
Sets the enumerator to its initial position, which is before the first element in the collection.
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.
bool Contains(T item)
Determines whether an element is in the T:System.Collections.Generic.Stack`1.
Definition: Stack.cs:261
Provides a mechanism for releasing unmanaged resources.To browse the .NET Framework source code for t...
Definition: IDisposable.cs:8
static void Reverse(Array array)
Reverses the sequence of the elements in the entire one-dimensional T:System.Array.
Definition: Array.cs:3171
Definition: __Canon.cs:3
new IEnumerator< T > GetEnumerator()
Returns an enumerator that iterates through the collection.
Stack(int capacity)
Initializes a new instance of the T:System.Collections.Generic.Stack`1 class that is empty and has th...
Definition: Stack.cs:206
Exposes the enumerator, which supports a simple iteration over a collection of a specified type....
Definition: IEnumerable.cs:9
Represents a strongly-typed, read-only collection of elements.
Stack()
Initializes a new instance of the T:System.Collections.Generic.Stack`1 class that is empty and has th...
Definition: Stack.cs:194
void CopyTo(T[] array, int arrayIndex)
Copies the elements of the T:System.Collections.Generic.ICollection`1 to an T:System....
static EqualityComparer< T > Default
Returns a default equality comparer for the type specified by the generic argument.
Supports a simple iteration over a generic collection.
Definition: IEnumerator.cs:6
Defines methods to manipulate generic collections.
Definition: ICollection.cs:9
Enumerates the elements of a T:System.Collections.Generic.Stack`1.
Definition: Stack.cs:19
void Clear()
Removes all objects from the T:System.Collections.Generic.Stack`1.
Definition: Stack.cs:249
Exposes an enumerator, which supports a simple iteration over a non-generic collection....
Definition: IEnumerable.cs:9
T Peek()
Returns the object at the top of the T:System.Collections.Generic.Stack`1 without removing it.
Definition: Stack.cs:395
T Pop()
Removes and returns the object at the top of the T:System.Collections.Generic.Stack`1.
Definition: Stack.cs:408
bool MoveNext()
Advances the enumerator to the next element of the T:System.Collections.Generic.Stack`1.
Definition: Stack.cs:91
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.
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition: Array.cs:17
new T Current
Gets the element in the collection at the current position of the enumerator.
Definition: IEnumerator.cs:12
void Push(T item)
Inserts an object at the top of the T:System.Collections.Generic.Stack`1.
Definition: Stack.cs:423
T [] ToArray()
Copies the T:System.Collections.Generic.Stack`1 to a new array.
Definition: Stack.cs:438
IEnumerator GetEnumerator()
Returns an enumerator that iterates through a collection.
Represents a variable size last-in-first-out (LIFO) collection of instances of the same specified typ...
Definition: Stack.cs:14
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
Stack(IEnumerable< T > collection)
Initializes a new instance of the T:System.Collections.Generic.Stack`1 class that contains elements c...
Definition: Stack.cs:222
void TrimExcess()
Sets the capacity to the actual number of elements in the T:System.Collections.Generic....
Definition: Stack.cs:379
Specifies that the class can be serialized.
int Count
Gets the number of elements contained in the T:System.Collections.ICollection.
Definition: ICollection.cs:14
void CopyTo(Array array, int index)
Copies the elements of the T:System.Collections.ICollection to an T:System.Array, starting at a parti...
Provides atomic operations for variables that are shared by multiple threads.
Definition: Interlocked.cs:10
void Dispose()
Releases all resources used by the T:System.Collections.Generic.Stack`1.Enumerator.
Definition: Stack.cs:81
Supports a simple iteration over a non-generic collection.
Definition: IEnumerator.cs:9
Enumerator GetEnumerator()
Returns an enumerator for the T:System.Collections.Generic.Stack`1.
Definition: Stack.cs:356