class · System

Stack<T>

public class Stack<T>

Last in, first out: Push and Pop at the top.

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 Stack<T>

Constructores

new()
#

Propiedades

i32 Count { get; }
#

read-only

bool IsEmpty { get; }
#

read-only

Métodos

Push(T item)
#
Pop() : T
#
Peek() : T
#
Clear()
#

Miembros privados e internos (3)

T[] items
#

private

i32 count
#

private

Grow()
#

private

Miembros heredados (5)

Código fuente

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

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

into namespace System
{
	/// Last in, first out: Push and Pop at the top.
	public class Stack<T>
	{
		T[] items;
		i32 count;

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

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

			Push(T item)
			{
				if (count == items.Length)
					Grow();
				items[count] = item;
				count++;
			}

			Pop() : T
			{
				if (count == 0)
					throw new InvalidOperationError("Pop on an empty stack");
				count--;
				T top = items[count];
				items[count] = default(T);
				return top;
			}

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

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

			i32 Count
			{
				get => count;
			}

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