class · System

Stack<T>

public class Stack<T>

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

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

Type parameters

NameConstraints
Tany type

Inheritance

Base classes
Any Stack<T>

Constructors

new()
#

Properties

i32 Count { get; }
#

read-only

bool IsEmpty { get; }
#

read-only

Methods

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

Private and internal members (3)

T[] items
#

private

i32 count
#

private

Grow()
#

private

Inherited members (5)

Source

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