ImmutableStack<T>
A minimalistic implementation of an immutable stack. Add members as needed.
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace NUnit.Framework.Internal
{
[NullableContext(1)]
[Nullable(0)]
internal struct ImmutableStack<[Nullable(2)] T> : IEnumerable<T>, IEnumerable
{
[Nullable(0)]
private sealed class Node
{
public T Value { get; }
[Nullable(new byte[] {
1,
0
})]
[field: Nullable(new byte[] {
1,
0
})]
public Node Next {
[return: Nullable(new byte[] {
1,
0
})]
get;
}
public Node(T value, [Nullable(new byte[] {
1,
0
})] Node next)
{
Value = value;
Next = next;
}
}
[Nullable(new byte[] {
1,
0
})]
private readonly Node _head;
[Nullable(new byte[] {
0,
1
})]
public static ImmutableStack<T> Empty {
[return: Nullable(new byte[] {
0,
1
})]
get {
return default(ImmutableStack<T>);
}
}
private ImmutableStack([Nullable(new byte[] {
1,
0
})] Node head)
{
_head = head;
}
[return: Nullable(new byte[] {
0,
1
})]
public ImmutableStack<T> Push(T value)
{
return new ImmutableStack<T>(new Node(value, _head));
}
[IteratorStateMachine(typeof(ImmutableStack<>.<GetEnumerator>d__5))]
public IEnumerator<T> GetEnumerator()
{
<GetEnumerator>d__5 <GetEnumerator>d__ = new <GetEnumerator>d__5(0);
<GetEnumerator>d__.<>4__this = this;
return <GetEnumerator>d__;
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}