Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- public struct StackLite<T>
- {
- internal readonly T _Value;
- internal readonly object _Next;
- public StackLite(T Value)
- {
- _Value = Value;
- _Next = default(StackLite<T>);
- }
- internal StackLite(T Value, StackLite<T> Next)
- {
- _Value = Value;
- _Next = Next;
- }
- }
- public static class StackLiteEx
- {
- public static void Push<T>(ref this StackLite<T> @this, T value) => @this = new StackLite<T>(value, @this);
- public static T Peek<T>(ref this StackLite<T> @this)
- {
- if (@this.Equals(default(StackLite<T>))) throw new Exception("Stack is empty.");
- return @this._Value;
- }
- public static T Pop<T>(ref this StackLite<T> @this)
- {
- if (@this.Equals(default(StackLite<T>))) throw new Exception("Stack is empty.");
- T ret = @this._Value;
- @this = (StackLite<T>)@this._Next;
- return ret;
- }
- public static bool TryPeek<T>(ref this StackLite<T> @this, out T Value)
- {
- if (@this.Equals(default(StackLite<T>)))
- {
- Value = default;
- return false;
- }
- else
- {
- Value = @this._Value;
- return true;
- }
- }
- public static bool TryPop<T>(ref this StackLite<T> @this, out T Value)
- {
- if (@this.Equals(default(StackLite<T>)))
- {
- Value = default;
- return false;
- }
- else
- {
- Value = @this._Value;
- @this = (StackLite<T>)@this._Next;
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment