class · System

Queue<T>

public class Queue<T>

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

Kind
class
Namespace
System
Assembly
System.Core
Access
public
Source file
List.ant

Type parameters

NameConstraints
Tany type

Inheritance

Base classes
Any Queue<T>

Constructors

new()
#

Properties

i32 Count { get; }
#

read-only

bool IsEmpty { get; }
#

read-only

Methods

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

Private and internal members (4)

T[] items
#

private

i32 count
#

private

Grow()
#

private

Inherited members (5)

Source

Queue<T> as Antril, decompiled from its 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