class · System

Queue<T>

public class Queue<T>

First in, first out: Enqueue at the end, Dequeue from the front (a ring buffer).

Tipo
class
Espacio de nombres
System
Ensamblado
System.Core
Acceso
public
Archivo fuente
List.ant

Parámetros de tipo

NombreRestricciones
Tcualquier tipo

Herencia

Clases base
Any Queue<T>

Constructores

new()
#

Propiedades

i32 Count { get; }
#

read-only

bool IsEmpty { get; }
#

read-only

Métodos

Enqueue(T item)
#
Dequeue() : T
#
Peek() : T
#
Clear()
#

Miembros privados e internos (4)

T[] items
#

private

i32 count
#

private

Grow()
#

private

Miembros heredados (5)

Código fuente

Queue<T> en Antril, descompilado a partir de su Larva HIR.

System.Queue.ant
// System.Queue — System.Core (decompiled from Larva HIR)

into namespace System
{
	/// First in, first out: Enqueue at the end, Dequeue from the front (a ring buffer).
	public class Queue<T>
	{
		T[] items;
		i32 head;
		i32 count;

		[NoInline]
		Grow()
		{
			T[] bigger = new T[items.Length * 2];
			loop (i32 i = 0..count)
				bigger[i] = items[(head + i) % items.Length];
			items = bigger;
			head = 0;
		}

		public:
			new()
			{
				items = new T[8];
			}

			Enqueue(T item)
			{
				if (count == items.Length)
					Grow();
				i32 at = head + count;
				if (at >= items.Length)
					at -= items.Length;
				items[at] = item;
				count++;
			}

			Dequeue() : T
			{
				if (count == 0)
					throw new InvalidOperationError("Dequeue on an empty queue");
				T item = items[head];
				items[head] = default(T);
				head++;
				if (head == items.Length)
					head = 0;
				count--;
				return item;
			}

			Peek() : T
			{
				if (count == 0)
					throw new InvalidOperationError("Peek on an empty queue");
				return items[head];
			}

			Clear()
			{
				loop (i32 i = 0..count)
					items[(head + i) % items.Length] = default(T);
				head = 0;
				count = 0;
			}

			i32 Count
			{
				get => count;
			}

			bool IsEmpty
			{
				get => count == 0;
			}
	}
}
Esc