TizzyT

StackLite<T> -TizzyT

Jul 26th, 2018
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.63 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3.  
  4. public struct StackLite<T>
  5. {
  6.     internal readonly T _Value;
  7.     internal readonly object _Next;
  8.  
  9.     public StackLite(T Value)
  10.     {
  11.         _Value = Value;
  12.         _Next = default(StackLite<T>);
  13.     }
  14.  
  15.     internal StackLite(T Value, StackLite<T> Next)
  16.     {
  17.         _Value = Value;
  18.         _Next = Next;
  19.     }
  20. }
  21.  
  22. public static class StackLiteEx
  23. {
  24.     public static void Push<T>(ref this StackLite<T> @this, T value) => @this = new StackLite<T>(value, @this);
  25.  
  26.     public static T Peek<T>(ref this StackLite<T> @this)
  27.     {
  28.         if (@this.Equals(default(StackLite<T>))) throw new Exception("Stack is empty.");
  29.         return @this._Value;
  30.     }
  31.  
  32.     public static T Pop<T>(ref this StackLite<T> @this)
  33.     {
  34.         if (@this.Equals(default(StackLite<T>))) throw new Exception("Stack is empty.");
  35.         T ret = @this._Value;
  36.         @this = (StackLite<T>)@this._Next;
  37.         return ret;
  38.     }
  39.  
  40.     public static bool TryPeek<T>(ref this StackLite<T> @this, out T Value)
  41.     {
  42.         if (@this.Equals(default(StackLite<T>)))
  43.         {
  44.             Value = default;
  45.             return false;
  46.         }
  47.         else
  48.         {
  49.             Value = @this._Value;
  50.             return true;
  51.         }
  52.     }
  53.  
  54.     public static bool TryPop<T>(ref this StackLite<T> @this, out T Value)
  55.     {
  56.         if (@this.Equals(default(StackLite<T>)))
  57.         {
  58.             Value = default;
  59.             return false;
  60.         }
  61.         else
  62.         {
  63.             Value = @this._Value;
  64.             @this = (StackLite<T>)@this._Next;
  65.             return true;
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment