Skip to content

Commit

Permalink
Add ImmutableStack class
Browse files Browse the repository at this point in the history
  • Loading branch information
blowfishpro committed Aug 26, 2017
1 parent d2fd007 commit 536ff0c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
37 changes: 37 additions & 0 deletions Collections/ImmutableStack.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;

namespace ModuleManager.Collections
{
public class ImmutableStack<T>
{
public readonly T value;
public readonly ImmutableStack<T> parent;

public ImmutableStack(T value)
{
this.value = value;
}

private ImmutableStack(T value, ImmutableStack<T> parent)
{
this.value = value;
this.parent = parent;
}

public bool IsRoot => parent == null;
public ImmutableStack<T> Root => IsRoot? this : parent.Root;

public ImmutableStack<T> Push(T newValue)
{
return new ImmutableStack<T>(newValue, this);
}

public ImmutableStack<T> Pop()
{
if (IsRoot) throw new InvalidOperationException("Cannot pop from the root of a stack");
return parent;
}

public ImmutableStack<T> ReplaceValue(T newValue) => new ImmutableStack<T>(newValue, parent);
}
}
1 change: 1 addition & 0 deletions ModuleManager.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<Compile Include="Cats\CatManager.cs" />
<Compile Include="Cats\CatMover.cs" />
<Compile Include="Cats\CatOrbiter.cs" />
<Compile Include="Collections\ImmutableStack.cs" />
<Compile Include="MMPatchLoader.cs" />
<Compile Include="ModuleManager.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down

0 comments on commit 536ff0c

Please sign in to comment.