Skip to content

Instantly share code, notes, and snippets.

@einarwh
Created December 17, 2013 22:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save einarwh/8013516 to your computer and use it in GitHub Desktop.
Save einarwh/8013516 to your computer and use it in GitHub Desktop.
class EvalStack
{
private const char YesThis = '1';
private const char NotThis = '0';
private readonly string _;
private EvalStack(string s) { _ = s; }
public EvalStack() : this("") {}
public bool Peek()
{
if (IsEmpty)
{
throw new Exception("Cannot Peek an empty stack.");
}
return _[0] == YesThis;
}
public EvalStack Pop()
{
if (IsEmpty)
{
throw new Exception("Cannot Pop an empty stack.");
}
return new EvalStack(_.Substring(1));
}
public EvalStack Push(bool b)
{
char c = b ? YesThis : NotThis;
return new EvalStack(c + _);
}
public bool IsEmpty
{
get { return _.Length == 0; }
}
public override bool Equals(object that)
{
return Equals(that as EvalStack);
}
public bool Equals(EvalStack that)
{
return _.Equals(that._);
}
public override int GetHashCode()
{
return (_ != null ? _.GetHashCode() : 0);
}
public override string ToString()
{
return "[" + _ + "]";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment