Files
2026-06-04 11:42:34 +02:00

364 lines
15 KiB
C#

using System;
using System.Runtime.InteropServices;
namespace System.Collections.Generic
{
/// <summary>Represents a variable size last-in-first-out (LIFO) collection of instances of the same arbitrary type.</summary>
/// <typeparam name="T">Specifies the type of elements in the stack.</typeparam>
/// <filterpriority>1</filterpriority>
// Token: 0x02000023 RID: 35
[ComVisible(false)]
[Serializable]
public class Stack<T> : ICollection, IEnumerable, IEnumerable<T>
{
/// <summary>Initializes a new instance of the <see cref="T:System.Collections.Generic.Stack`1" /> class that is empty and has the default initial capacity.</summary>
// Token: 0x0600015C RID: 348 RVA: 0x000063A0 File Offset: 0x000045A0
public Stack()
{
}
/// <summary>Initializes a new instance of the <see cref="T:System.Collections.Generic.Stack`1" /> class that is empty and has the specified initial capacity or the default initial capacity, whichever is greater.</summary>
/// <param name="capacity">The initial number of elements that the <see cref="T:System.Collections.Generic.Stack`1" /> can contain.</param>
/// <exception cref="T:System.ArgumentOutOfRangeException">
/// <paramref name="capacity" /> is less than zero.</exception>
// Token: 0x0600015D RID: 349 RVA: 0x000063A8 File Offset: 0x000045A8
public Stack(int count)
{
if (count < 0)
{
throw new ArgumentOutOfRangeException("count");
}
this._array = new T[count];
}
/// <summary>Initializes a new instance of the <see cref="T:System.Collections.Generic.Stack`1" /> class that contains elements copied from the specified collection and has sufficient capacity to accommodate the number of elements copied.</summary>
/// <param name="collection">The collection to copy elements from.</param>
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="collection" /> is null.</exception>
// Token: 0x0600015E RID: 350 RVA: 0x000063DC File Offset: 0x000045DC
public Stack(IEnumerable<T> collection)
{
if (collection == null)
{
throw new ArgumentNullException("collection");
}
ICollection<T> collection2 = collection as ICollection<T>;
if (collection2 != null)
{
this._size = collection2.Count;
this._array = new T[this._size];
collection2.CopyTo(this._array, 0);
}
else
{
foreach (T t in collection)
{
this.Push(t);
}
}
}
/// <summary>Gets a value indicating whether access to the <see cref="T:System.Collections.ICollection" /> is synchronized (thread safe).</summary>
/// <returns>true if access to the <see cref="T:System.Collections.ICollection" /> is synchronized (thread safe); otherwise, false. In the default implementation of <see cref="T:System.Collections.Generic.Stack`1" />, this property always returns false.</returns>
// Token: 0x1700005E RID: 94
// (get) Token: 0x0600015F RID: 351 RVA: 0x00006490 File Offset: 0x00004690
bool ICollection.IsSynchronized
{
get
{
return false;
}
}
/// <summary>Gets an object that can be used to synchronize access to the <see cref="T:System.Collections.ICollection" />.</summary>
/// <returns>An object that can be used to synchronize access to the <see cref="T:System.Collections.ICollection" />. In the default implementation of <see cref="T:System.Collections.Generic.Stack`1" />, this property always returns the current instance.</returns>
// Token: 0x1700005F RID: 95
// (get) Token: 0x06000160 RID: 352 RVA: 0x00006494 File Offset: 0x00004694
object ICollection.SyncRoot
{
get
{
return this;
}
}
/// <summary>Copies the elements of the <see cref="T:System.Collections.ICollection" /> to an <see cref="T:System.Array" />, starting at a particular <see cref="T:System.Array" /> index.</summary>
/// <param name="array">The one-dimensional <see cref="T:System.Array" /> that is the destination of the elements copied from <see cref="T:System.Collections.ICollection" />. The <see cref="T:System.Array" /> must have zero-based indexing.</param>
/// <param name="arrayIndex">The zero-based index in <paramref name="array" /> at which copying begins.</param>
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="array" /> is null.</exception>
/// <exception cref="T:System.ArgumentOutOfRangeException">
/// <paramref name="arrayIndex" /> is less than zero.</exception>
/// <exception cref="T:System.ArgumentException">
/// <paramref name="array" /> is multidimensional.-or-<paramref name="array" /> does not have zero-based indexing.-or-The number of elements in the source <see cref="T:System.Collections.ICollection" /> is greater than the available space from <paramref name="arrayIndex" /> to the end of the destination <paramref name="array" />.-or-The type of the source <see cref="T:System.Collections.ICollection" /> cannot be cast automatically to the type of the destination <paramref name="array" />.</exception>
// Token: 0x06000161 RID: 353 RVA: 0x00006498 File Offset: 0x00004698
void ICollection.CopyTo(Array dest, int idx)
{
try
{
if (this._array != null)
{
this._array.CopyTo(dest, idx);
Array.Reverse(dest, idx, this._size);
}
}
catch (ArrayTypeMismatchException)
{
throw new ArgumentException();
}
}
// Token: 0x06000162 RID: 354 RVA: 0x000064F8 File Offset: 0x000046F8
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
return this.GetEnumerator();
}
/// <summary>Returns an enumerator that iterates through a collection.</summary>
/// <returns>An <see cref="T:System.Collections.IEnumerator" /> that can be used to iterate through the collection.</returns>
// Token: 0x06000163 RID: 355 RVA: 0x00006508 File Offset: 0x00004708
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
/// <summary>Removes all objects from the <see cref="T:System.Collections.Generic.Stack`1" />.</summary>
/// <filterpriority>1</filterpriority>
// Token: 0x06000164 RID: 356 RVA: 0x00006518 File Offset: 0x00004718
public void Clear()
{
if (this._array != null)
{
Array.Clear(this._array, 0, this._array.Length);
}
this._size = 0;
this._version++;
}
/// <summary>Determines whether an element is in the <see cref="T:System.Collections.Generic.Stack`1" />.</summary>
/// <returns>true if <paramref name="item" /> is found in the <see cref="T:System.Collections.Generic.Stack`1" />; otherwise, false.</returns>
/// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.Stack`1" />. The value can be null for reference types.</param>
// Token: 0x06000165 RID: 357 RVA: 0x0000655C File Offset: 0x0000475C
public bool Contains(T t)
{
return this._array != null && Array.IndexOf<T>(this._array, t, 0, this._size) != -1;
}
/// <summary>Copies the <see cref="T:System.Collections.Generic.Stack`1" /> to an existing one-dimensional <see cref="T:System.Array" />, starting at the specified array index.</summary>
/// <param name="array">The one-dimensional <see cref="T:System.Array" /> that is the destination of the elements copied from <see cref="T:System.Collections.Generic.Stack`1" />. The <see cref="T:System.Array" /> must have zero-based indexing.</param>
/// <param name="arrayIndex">The zero-based index in <paramref name="array" /> at which copying begins.</param>
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="array" /> is null.</exception>
/// <exception cref="T:System.ArgumentOutOfRangeException">
/// <paramref name="arrayIndex" /> is less than zero.</exception>
/// <exception cref="T:System.ArgumentException">The number of elements in the source <see cref="T:System.Collections.Generic.Stack`1" /> is greater than the available space from <paramref name="arrayIndex" /> to the end of the destination <paramref name="array" />.</exception>
// Token: 0x06000166 RID: 358 RVA: 0x00006588 File Offset: 0x00004788
public void CopyTo(T[] dest, int idx)
{
if (dest == null)
{
throw new ArgumentNullException("dest");
}
if (idx < 0)
{
throw new ArgumentOutOfRangeException("idx");
}
if (this._array != null)
{
Array.Copy(this._array, 0, dest, idx, this._size);
Array.Reverse(dest, idx, this._size);
}
}
/// <summary>Returns the object at the top of the <see cref="T:System.Collections.Generic.Stack`1" /> without removing it.</summary>
/// <returns>The object at the top of the <see cref="T:System.Collections.Generic.Stack`1" />.</returns>
/// <exception cref="T:System.InvalidOperationException">The <see cref="T:System.Collections.Generic.Stack`1" /> is empty.</exception>
// Token: 0x06000167 RID: 359 RVA: 0x000065E4 File Offset: 0x000047E4
public T Peek()
{
if (this._size == 0)
{
throw new InvalidOperationException();
}
return this._array[this._size - 1];
}
/// <summary>Removes and returns the object at the top of the <see cref="T:System.Collections.Generic.Stack`1" />.</summary>
/// <returns>The object removed from the top of the <see cref="T:System.Collections.Generic.Stack`1" />.</returns>
/// <exception cref="T:System.InvalidOperationException">The <see cref="T:System.Collections.Generic.Stack`1" /> is empty.</exception>
// Token: 0x06000168 RID: 360 RVA: 0x00006618 File Offset: 0x00004818
public T Pop()
{
if (this._size == 0)
{
throw new InvalidOperationException();
}
this._version++;
T t = this._array[--this._size];
this._array[this._size] = default(T);
return t;
}
/// <summary>Inserts an object at the top of the <see cref="T:System.Collections.Generic.Stack`1" />.</summary>
/// <param name="item">The object to push onto the <see cref="T:System.Collections.Generic.Stack`1" />. The value can be null for reference types.</param>
// Token: 0x06000169 RID: 361 RVA: 0x0000667C File Offset: 0x0000487C
public void Push(T t)
{
if (this._array == null || this._size == this._array.Length)
{
Array.Resize<T>(ref this._array, (this._size != 0) ? (2 * this._size) : 16);
}
this._version++;
this._array[this._size++] = t;
}
/// <summary>Copies the <see cref="T:System.Collections.Generic.Stack`1" /> to a new array.</summary>
/// <returns>A new array containing copies of the elements of the <see cref="T:System.Collections.Generic.Stack`1" />.</returns>
// Token: 0x0600016A RID: 362 RVA: 0x000066F8 File Offset: 0x000048F8
public T[] ToArray()
{
T[] array = new T[this._size];
this.CopyTo(array, 0);
return array;
}
/// <summary>Sets the capacity to the actual number of elements in the <see cref="T:System.Collections.Generic.Stack`1" />, if that number is less than 90 percent of current capacity.</summary>
// Token: 0x0600016B RID: 363 RVA: 0x0000671C File Offset: 0x0000491C
public void TrimExcess()
{
if (this._array != null && (double)this._size < (double)this._array.Length * 0.9)
{
Array.Resize<T>(ref this._array, this._size);
}
this._version++;
}
/// <summary>Gets the number of elements contained in the <see cref="T:System.Collections.Generic.Stack`1" />.</summary>
/// <returns>The number of elements contained in the <see cref="T:System.Collections.Generic.Stack`1" />.</returns>
// Token: 0x17000060 RID: 96
// (get) Token: 0x0600016C RID: 364 RVA: 0x00006774 File Offset: 0x00004974
public int Count
{
get
{
return this._size;
}
}
/// <summary>Returns an enumerator for the <see cref="T:System.Collections.Generic.Stack`1" />.</summary>
/// <returns>An <see cref="T:System.Collections.Generic.Stack`1.Enumerator" /> for the <see cref="T:System.Collections.Generic.Stack`1" />.</returns>
// Token: 0x0600016D RID: 365 RVA: 0x0000677C File Offset: 0x0000497C
public Stack<T>.Enumerator GetEnumerator()
{
return new Stack<T>.Enumerator(this);
}
// Token: 0x04000076 RID: 118
private const int INITIAL_SIZE = 16;
// Token: 0x04000077 RID: 119
private T[] _array;
// Token: 0x04000078 RID: 120
private int _size;
// Token: 0x04000079 RID: 121
private int _version;
/// <summary>Enumerates the elements of a <see cref="T:System.Collections.Generic.Stack`1" />.</summary>
// Token: 0x02000024 RID: 36
[Serializable]
public struct Enumerator : IEnumerator, IDisposable, IEnumerator<T>
{
// Token: 0x0600016E RID: 366 RVA: 0x00006784 File Offset: 0x00004984
internal Enumerator(Stack<T> t)
{
this.parent = t;
this.idx = -2;
this._version = t._version;
}
/// <summary>Sets the enumerator to its initial position, which is before the first element in the collection. This class cannot be inherited.</summary>
/// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception>
// Token: 0x0600016F RID: 367 RVA: 0x000067A4 File Offset: 0x000049A4
void IEnumerator.Reset()
{
if (this._version != this.parent._version)
{
throw new InvalidOperationException();
}
this.idx = -2;
}
/// <summary>Gets the element at the current position of the enumerator.</summary>
/// <returns>The element in the collection at the current position of the enumerator.</returns>
/// <exception cref="T:System.InvalidOperationException">The enumerator is positioned before the first element of the collection or after the last element. </exception>
// Token: 0x17000061 RID: 97
// (get) Token: 0x06000170 RID: 368 RVA: 0x000067D8 File Offset: 0x000049D8
object IEnumerator.Current
{
get
{
return this.Current;
}
}
/// <summary>Releases all resources used by the <see cref="T:System.Collections.Generic.Stack`1.Enumerator" />.</summary>
// Token: 0x06000171 RID: 369 RVA: 0x000067E8 File Offset: 0x000049E8
public void Dispose()
{
this.idx = -2;
}
/// <summary>Advances the enumerator to the next element of the <see cref="T:System.Collections.Generic.Stack`1" />.</summary>
/// <returns>true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.</returns>
/// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception>
// Token: 0x06000172 RID: 370 RVA: 0x000067F4 File Offset: 0x000049F4
public bool MoveNext()
{
if (this._version != this.parent._version)
{
throw new InvalidOperationException();
}
if (this.idx == -2)
{
this.idx = this.parent._size;
}
return this.idx != -1 && --this.idx != -1;
}
/// <summary>Gets the element at the current position of the enumerator.</summary>
/// <returns>The element in the <see cref="T:System.Collections.Generic.Stack`1" /> at the current position of the enumerator.</returns>
/// <exception cref="T:System.InvalidOperationException">The enumerator is positioned before the first element of the collection or after the last element. </exception>
// Token: 0x17000062 RID: 98
// (get) Token: 0x06000173 RID: 371 RVA: 0x00006864 File Offset: 0x00004A64
public T Current
{
get
{
if (this.idx < 0)
{
throw new InvalidOperationException();
}
return this.parent._array[this.idx];
}
}
// Token: 0x0400007A RID: 122
private const int NOT_STARTED = -2;
// Token: 0x0400007B RID: 123
private const int FINISHED = -1;
// Token: 0x0400007C RID: 124
private Stack<T> parent;
// Token: 0x0400007D RID: 125
private int idx;
// Token: 0x0400007E RID: 126
private int _version;
}
}
}