Ejemplo n.º 1
0
 public PersistentQueue <T> Pop()
 {
     if (!recopy)
     {
         PersistentStack <T> Rn = R.Pop();
         PersistentQueue <T> Q  = new PersistentQueue <T>(L, Li, Rn, Ri, S, recopy, toCopy, copied);
         return(Q.CheckRecopy());
     }
     else
     {
         PersistentStack <T> Rni = Ri.Pop();
         PersistentStack <T> Rn  = R;
         Int32 curCopy           = toCopy;
         if (toCopy > 0)
         {
             --curCopy;
         }
         else
         {
             Rn = Rn.Pop();
         }
         PersistentQueue <T> Q = new PersistentQueue <T>(L, Li, Rn, Rni, S, recopy, curCopy, copied);
         return(Q.CheckNormal());
     }
 }
Ejemplo n.º 2
0
 public PersistentQueue <T> Pop()
 {
     if (!IsRecopy)
     {
         PersistentStack <T> newRight = Right.Pop();
         PersistentQueue <T> newQueue = new PersistentQueue <T>(Left, LeftRecopy, newRight, RightRecopy, TempRight, IsRecopy, ToCopy, Copied);
         return(newQueue.CheckRecopy());
     }
     else
     {
         PersistentStack <T> newRightRecopy = RightRecopy.Pop();
         PersistentStack <T> newRight       = Right;
         Int32 curToCopy = ToCopy;
         if (ToCopy > 0)
         {
             --curToCopy;
         }
         else
         {
             newRight = Right.Pop();
         }
         PersistentQueue <T> newQueue = new PersistentQueue <T>(Left, LeftRecopy, newRight, newRightRecopy, TempRight, IsRecopy, curToCopy, Copied);
         return(newQueue.CheckNormal());
     }
 }
Ejemplo n.º 3
0
        private PersistentQueue <T> AdditionalsOperations()
        {
            Int32 toDo = 3;

            PersistentStack <T> newLeft       = Left;
            PersistentStack <T> newLeftRecopy = LeftRecopy;
            PersistentStack <T> newRight      = Right;
            PersistentStack <T> newTempRight  = TempRight;

            Boolean newCopied = Copied;
            Int32   newToCopy = ToCopy;

            while (!newCopied && toDo > 0 && newRight.Count > 0)
            {
                T x = newRight.Peek();
                newRight = newRight.Pop();

                newTempRight = newTempRight.Push(x);

                --toDo;
            }

            while (toDo > 0 && newLeft.Count > 0)
            {
                newCopied = true;

                T x = newLeft.Peek();
                newLeft = newLeft.Pop();

                newRight = newRight.Push(x);

                --toDo;
            }

            while (toDo > 0 && newTempRight.Count > 0)
            {
                T x = newTempRight.Peek();
                newTempRight = newTempRight.Pop();

                if (newToCopy > 0)
                {
                    newRight = newRight.Push(x);
                    --newToCopy;
                }
                --toDo;
            }

            if (newTempRight.Count == 0)
            {
                PersistentStack <T> temp = newLeft;
                newLeft       = newLeftRecopy;
                newLeftRecopy = temp;
            }

            return(new PersistentQueue <T>(newLeft, newLeftRecopy, newRight, RightRecopy, newTempRight, IsRecopy, newToCopy, newCopied));
        }
Ejemplo n.º 4
0
        public IEnumerator <T> GetEnumerator()
        {
            PersistentStack <T> current = this;

            while (this != null)
            {
                yield return(current._Value);

                current = current._Previous;
            }
        }
Ejemplo n.º 5
0
        public PersistentQueue()
        {
            L  = new PersistentStack <T>();
            Li = new PersistentStack <T>();
            R  = new PersistentStack <T>();
            Ri = new PersistentStack <T>();
            S  = new PersistentStack <T>();

            recopy = false;
            toCopy = 0;
            copied = false;
        }
Ejemplo n.º 6
0
        public PersistentQueue()
        {
            Left        = new PersistentStack <T>();
            LeftRecopy  = new PersistentStack <T>();
            Right       = new PersistentStack <T>();
            RightRecopy = new PersistentStack <T>();
            TempRight   = new PersistentStack <T>();

            IsRecopy = false;
            ToCopy   = 0;
            Copied   = false;
        }
Ejemplo n.º 7
0
        private PersistentQueue <T> AdditionalsOperations()
        {
            Int32 toDo = 3;

            PersistentStack <T> Ln  = L;
            PersistentStack <T> Lni = Li;
            PersistentStack <T> Rn  = R;
            PersistentStack <T> Sn  = S;

            Boolean curCopied = copied;
            Int32   curCopy   = toCopy;

            while (!curCopied && toDo > 0 && Rn.Count > 0)
            {
                T x = Rn.Peek();
                Rn = Rn.Pop();
                Sn = Sn.Push(x);
                --toDo;
            }

            while (toDo > 0 && Ln.Count > 0)
            {
                curCopied = true;
                T x = Ln.Peek();
                Ln = Ln.Pop();
                Rn = Rn.Push(x);
                --toDo;
            }

            while (toDo > 0 && Sn.Count > 0)
            {
                T x = Sn.Peek();
                Sn = Sn.Pop();
                if (curCopy > 0)
                {
                    Rn = Rn.Push(x);
                    --curCopy;
                }
                --toDo;
            }

            if (Sn.Count == 0)
            {
                PersistentStack <T> t = Ln;
                Ln  = Lni;
                Lni = t;
            }

            return(new PersistentQueue <T>(Ln, Lni, Rn, Ri, Sn, recopy, curCopy, curCopied));
        }
Ejemplo n.º 8
0
 public PersistentQueue <T> Push(T element)
 {
     if (!recopy)
     {
         PersistentStack <T> Ln = L.Push(element);
         PersistentQueue <T> Q  = new PersistentQueue <T>(Ln, Li, R, Ri, S, recopy, toCopy, copied);
         return(Q.CheckRecopy());
     }
     else
     {
         PersistentStack <T> Lni = Li.Push(element);
         PersistentQueue <T> Q   = new PersistentQueue <T>(L, Lni, R, Ri, S, recopy, toCopy, copied);
         return(Q.CheckNormal());
     }
 }
Ejemplo n.º 9
0
 public PersistentQueue <T> Push(T element)
 {
     if (!IsRecopy)
     {
         PersistentStack <T> newLeft  = Left.Push(element);
         PersistentQueue <T> newQueue = new PersistentQueue <T>(newLeft, LeftRecopy, Right, RightRecopy, TempRight, IsRecopy, ToCopy, Copied);
         return(newQueue.CheckRecopy());
     }
     else
     {
         PersistentStack <T> newLeftRecopy = LeftRecopy.Push(element);
         PersistentQueue <T> newQueue      = new PersistentQueue <T>(Left, newLeftRecopy, Right, RightRecopy, TempRight, IsRecopy, ToCopy, Copied);
         return(newQueue.CheckNormal());
     }
 }
Ejemplo n.º 10
0
        private PersistentQueue(
            PersistentStack <T> newL,
            PersistentStack <T> newLi,
            PersistentStack <T> newR,
            PersistentStack <T> newRi,
            PersistentStack <T> newS,
            Boolean newRecopy,
            Int32 newToCopy,
            Boolean newCopied) : this()
        {
            L  = newL;
            Li = newLi;
            R  = newR;
            Ri = newRi;
            S  = newS;

            recopy = newRecopy;
            toCopy = newToCopy;
            copied = newCopied;
        }
Ejemplo n.º 11
0
        private PersistentQueue(
            PersistentStack <T> left,
            PersistentStack <T> leftRecopy,
            PersistentStack <T> right,
            PersistentStack <T> rightRecopy,
            PersistentStack <T> tempRight,
            Boolean isRecopy,
            Int32 toCopy,
            Boolean copied
            ) : this()
        {
            Left        = left;
            LeftRecopy  = leftRecopy;
            Right       = right;
            RightRecopy = rightRecopy;
            TempRight   = tempRight;

            IsRecopy = isRecopy;
            ToCopy   = toCopy;
            Copied   = copied;
        }
Ejemplo n.º 12
0
 private PersistentStack(PersistentStack <T> prev, T newElement) : this()
 {
     _Value    = newElement;
     Count     = prev.Count + 1;
     _Previous = prev;
 }