GAM456-SpaceInvaders / SpaceInvaders / 0_Manager / DLink / DLinkIterator.cs
DLinkIterator.cs
Raw
using System;
using System.Diagnostics;


namespace SpaceInvaders
{
    public class DLinkIterator : Iterator
    {
        //-------------------------------------------------------------------
        //  FIELDS
        //-------------------------------------------------------------------

        public bool isDone;
        public Node pHead;
        public Node pCurr;

        //---------------------------------------------------------------
        //  CONSTRUCTION
        //---------------------------------------------------------------

        public DLinkIterator() : base()
        {
            this.Clear();
        }


        //-------------------------------------------------------------------
        //  PUBLIC METHODS
        //-------------------------------------------------------------------
     
        //reset iterator to first element
        public void Reset(DLink pHead)
        {
            //reset if list exists
            if (pHead != null)
            {
                this.isDone = false;
                this.pHead = pHead;
                this.pCurr = pHead;
            }
            else
            {
                this.Clear();
            }
        }

        //return first node
        public override Node First()
        {
            if(this.pHead != null)
            {
                this.isDone = false;
                this.pCurr = this.pHead;
            }
            else
            {
                this.Clear();
            }

            return pCurr;
        }

        //get next node
        public override Node Next()
        {
            DLink pLink = (DLink)this.pCurr;
            if (pLink != null)
            {
                //if link exists, get next one
                pLink = pLink.pNext;
            }
            this.pCurr = pLink;

            //if link is null, list is done
            if (pLink == null)
            {
                isDone = true;
            }
            else
            {
                isDone = false;
            }

            return pLink;
        }

        public override Node Peek()
        {
            Node pNode = this.pCurr;
            return pNode;
        }

        //return bool status
        public override bool IsDone()
        {
            return isDone;
        }

        //---------------------------------------------------------------
        //  PRIVATE METHODS
        //---------------------------------------------------------------

        private void Clear()
        {
            this.pHead = null;
            this.pCurr = null;
            this.isDone = true;
        }

    }
}