-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d2fd007
commit 536ff0c
Showing
2 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters