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

415 lines
16 KiB
C#

using System;
using System.Runtime.InteropServices;
namespace System.Collections.Generic
{
/// <summary>Represents a first-in, first-out collection of objects.</summary>
/// <typeparam name="T">Specifies the type of elements in the queue.</typeparam>
/// <filterpriority>1</filterpriority>
// Token: 0x0200000E RID: 14
[ComVisible(false)]
[Serializable]
public class Queue<T> : IEnumerable<T>, ICollection, IEnumerable
{
/// <summary>Initializes a new instance of the <see cref="T:System.Collections.Generic.Queue`1" /> class that is empty and has the default initial capacity.</summary>
// Token: 0x06000043 RID: 67 RVA: 0x00002CB8 File Offset: 0x00000EB8
public Queue()
{
this._array = new T[0];
}
/// <summary>Initializes a new instance of the <see cref="T:System.Collections.Generic.Queue`1" /> class that is empty and has the specified initial capacity.</summary>
/// <param name="capacity">The initial number of elements that the <see cref="T:System.Collections.Generic.Queue`1" /> can contain.</param>
/// <exception cref="T:System.ArgumentOutOfRangeException">
/// <paramref name="capacity" /> is less than zero.</exception>
// Token: 0x06000044 RID: 68 RVA: 0x00002CCC File Offset: 0x00000ECC
public Queue(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.Queue`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 whose elements are copied to the new <see cref="T:System.Collections.Generic.Queue`1" />.</param>
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="collection" /> is null.</exception>
// Token: 0x06000045 RID: 69 RVA: 0x00002D00 File Offset: 0x00000F00
public Queue(IEnumerable<T> collection)
{
if (collection == null)
{
throw new ArgumentNullException("collection");
}
ICollection<T> collection2 = collection as ICollection<T>;
int num = ((collection2 == null) ? 0 : collection2.Count);
this._array = new T[num];
foreach (T t in collection)
{
this.Enqueue(t);
}
}
/// <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="index">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="index" /> 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="index" /> 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: 0x06000046 RID: 70 RVA: 0x00002D9C File Offset: 0x00000F9C
void ICollection.CopyTo(Array array, int idx)
{
if (array == null)
{
throw new ArgumentNullException();
}
if (idx > array.Length)
{
throw new ArgumentOutOfRangeException();
}
if (array.Length - idx < this._size)
{
throw new ArgumentOutOfRangeException();
}
if (this._size == 0)
{
return;
}
try
{
int num = this._array.Length;
int num2 = num - this._head;
Array.Copy(this._array, this._head, array, idx, Math.Min(this._size, num2));
if (this._size > num2)
{
Array.Copy(this._array, 0, array, idx + num2, this._size - num2);
}
}
catch (ArrayTypeMismatchException)
{
throw new ArgumentException();
}
}
/// <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.Queue`1" />, this property always returns false.</returns>
// Token: 0x17000010 RID: 16
// (get) Token: 0x06000047 RID: 71 RVA: 0x00002E70 File Offset: 0x00001070
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.Queue`1" />, this property always returns the current instance.</returns>
// Token: 0x17000011 RID: 17
// (get) Token: 0x06000048 RID: 72 RVA: 0x00002E74 File Offset: 0x00001074
object ICollection.SyncRoot
{
get
{
return this;
}
}
// Token: 0x06000049 RID: 73 RVA: 0x00002E78 File Offset: 0x00001078
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: 0x0600004A RID: 74 RVA: 0x00002E88 File Offset: 0x00001088
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
/// <summary>Removes all objects from the <see cref="T:System.Collections.Generic.Queue`1" />.</summary>
/// <filterpriority>1</filterpriority>
// Token: 0x0600004B RID: 75 RVA: 0x00002E98 File Offset: 0x00001098
public void Clear()
{
Array.Clear(this._array, 0, this._array.Length);
this._head = (this._tail = (this._size = 0));
this._version++;
}
/// <summary>Determines whether an element is in the <see cref="T:System.Collections.Generic.Queue`1" />.</summary>
/// <returns>true if <paramref name="item" /> is found in the <see cref="T:System.Collections.Generic.Queue`1" />; otherwise, false.</returns>
/// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.Queue`1" />. The value can be null for reference types.</param>
// Token: 0x0600004C RID: 76 RVA: 0x00002EE0 File Offset: 0x000010E0
public bool Contains(T item)
{
if (item == null)
{
foreach (T t in this)
{
if (t == null)
{
return true;
}
}
}
else
{
foreach (T t2 in this)
{
if (item.Equals(t2))
{
return true;
}
}
}
return false;
}
/// <summary>Copies the <see cref="T:System.Collections.Generic.Queue`1" /> elements 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.Queue`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.Queue`1" /> is greater than the available space from <paramref name="arrayIndex" /> to the end of the destination <paramref name="array" />.</exception>
// Token: 0x0600004D RID: 77 RVA: 0x00002FCC File Offset: 0x000011CC
public void CopyTo(T[] array, int idx)
{
if (array == null)
{
throw new ArgumentNullException();
}
((ICollection)this).CopyTo(array, idx);
}
/// <summary>Removes and returns the object at the beginning of the <see cref="T:System.Collections.Generic.Queue`1" />.</summary>
/// <returns>The object that is removed from the beginning of the <see cref="T:System.Collections.Generic.Queue`1" />.</returns>
/// <exception cref="T:System.InvalidOperationException">The <see cref="T:System.Collections.Generic.Queue`1" /> is empty.</exception>
// Token: 0x0600004E RID: 78 RVA: 0x00002FE4 File Offset: 0x000011E4
public T Dequeue()
{
T t = this.Peek();
this._array[this._head] = default(T);
if (++this._head == this._array.Length)
{
this._head = 0;
}
this._size--;
this._version++;
return t;
}
/// <summary>Returns the object at the beginning of the <see cref="T:System.Collections.Generic.Queue`1" /> without removing it.</summary>
/// <returns>The object at the beginning of the <see cref="T:System.Collections.Generic.Queue`1" />.</returns>
/// <exception cref="T:System.InvalidOperationException">The <see cref="T:System.Collections.Generic.Queue`1" /> is empty.</exception>
// Token: 0x0600004F RID: 79 RVA: 0x00003054 File Offset: 0x00001254
public T Peek()
{
if (this._size == 0)
{
throw new InvalidOperationException();
}
return this._array[this._head];
}
/// <summary>Adds an object to the end of the <see cref="T:System.Collections.Generic.Queue`1" />.</summary>
/// <param name="item">The object to add to the <see cref="T:System.Collections.Generic.Queue`1" />. The value can be null for reference types.</param>
// Token: 0x06000050 RID: 80 RVA: 0x00003084 File Offset: 0x00001284
public void Enqueue(T item)
{
if (this._size == this._array.Length || this._tail == this._array.Length)
{
this.SetCapacity(Math.Max(Math.Max(this._size, this._tail) * 2, 4));
}
this._array[this._tail] = item;
if (++this._tail == this._array.Length)
{
this._tail = 0;
}
this._size++;
this._version++;
}
/// <summary>Copies the <see cref="T:System.Collections.Generic.Queue`1" /> elements to a new array.</summary>
/// <returns>A new array containing elements copied from the <see cref="T:System.Collections.Generic.Queue`1" />.</returns>
// Token: 0x06000051 RID: 81 RVA: 0x0000312C File Offset: 0x0000132C
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.Queue`1" />, if that number is less than 90 percent of current capacity.</summary>
// Token: 0x06000052 RID: 82 RVA: 0x00003150 File Offset: 0x00001350
public void TrimExcess()
{
if ((double)this._size < (double)this._array.Length * 0.9)
{
this.SetCapacity(this._size);
}
}
// Token: 0x06000053 RID: 83 RVA: 0x00003180 File Offset: 0x00001380
private void SetCapacity(int new_size)
{
if (new_size == this._array.Length)
{
return;
}
if (new_size < this._size)
{
throw new InvalidOperationException("shouldnt happen");
}
T[] array = new T[new_size];
if (this._size > 0)
{
this.CopyTo(array, 0);
}
this._array = array;
this._tail = this._size;
this._head = 0;
this._version++;
}
/// <summary>Gets the number of elements contained in the <see cref="T:System.Collections.Generic.Queue`1" />.</summary>
/// <returns>The number of elements contained in the <see cref="T:System.Collections.Generic.Queue`1" />.</returns>
// Token: 0x17000012 RID: 18
// (get) Token: 0x06000054 RID: 84 RVA: 0x000031F8 File Offset: 0x000013F8
public int Count
{
get
{
return this._size;
}
}
/// <summary>Returns an enumerator that iterates through the <see cref="T:System.Collections.Generic.Queue`1" />.</summary>
/// <returns>An <see cref="T:System.Collections.Generic.Queue`1.Enumerator" /> for the <see cref="T:System.Collections.Generic.Queue`1" />.</returns>
// Token: 0x06000055 RID: 85 RVA: 0x00003200 File Offset: 0x00001400
public Queue<T>.Enumerator GetEnumerator()
{
return new Queue<T>.Enumerator(this);
}
// Token: 0x04000033 RID: 51
private T[] _array;
// Token: 0x04000034 RID: 52
private int _head;
// Token: 0x04000035 RID: 53
private int _tail;
// Token: 0x04000036 RID: 54
private int _size;
// Token: 0x04000037 RID: 55
private int _version;
/// <summary>Enumerates the elements of a <see cref="T:System.Collections.Generic.Queue`1" />.</summary>
// Token: 0x0200000F RID: 15
[Serializable]
public struct Enumerator : IEnumerator, IDisposable, IEnumerator<T>
{
// Token: 0x06000056 RID: 86 RVA: 0x00003208 File Offset: 0x00001408
internal Enumerator(Queue<T> q)
{
this.q = q;
this.idx = -2;
this.ver = q._version;
}
/// <summary>Sets the enumerator to its initial position, which is before the first element in the collection.</summary>
/// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception>
// Token: 0x06000057 RID: 87 RVA: 0x00003228 File Offset: 0x00001428
void IEnumerator.Reset()
{
if (this.ver != this.q._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: 0x17000013 RID: 19
// (get) Token: 0x06000058 RID: 88 RVA: 0x0000325C File Offset: 0x0000145C
object IEnumerator.Current
{
get
{
return this.Current;
}
}
/// <summary>Releases all resources used by the <see cref="T:System.Collections.Generic.Queue`1.Enumerator" />.</summary>
// Token: 0x06000059 RID: 89 RVA: 0x0000326C File Offset: 0x0000146C
public void Dispose()
{
this.idx = -2;
}
/// <summary>Advances the enumerator to the next element of the <see cref="T:System.Collections.Generic.Queue`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: 0x0600005A RID: 90 RVA: 0x00003278 File Offset: 0x00001478
public bool MoveNext()
{
if (this.ver != this.q._version)
{
throw new InvalidOperationException();
}
if (this.idx == -2)
{
this.idx = this.q._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.Queue`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: 0x17000014 RID: 20
// (get) Token: 0x0600005B RID: 91 RVA: 0x000032E8 File Offset: 0x000014E8
public T Current
{
get
{
if (this.idx < 0)
{
throw new InvalidOperationException();
}
return this.q._array[(this.q._size - 1 - this.idx + this.q._head) % this.q._array.Length];
}
}
// Token: 0x04000038 RID: 56
private const int NOT_STARTED = -2;
// Token: 0x04000039 RID: 57
private const int FINISHED = -1;
// Token: 0x0400003A RID: 58
private Queue<T> q;
// Token: 0x0400003B RID: 59
private int idx;
// Token: 0x0400003C RID: 60
private int ver;
}
}
}