using System; using System.Diagnostics; namespace SpaceInvaders { public class ReverseIterator : TreeIterator { //------------------------------------------------------------------- // FIELDS //------------------------------------------------------------------- private Component pRoot; private Component pCurr; private Component pPrev; //------------------------------------------------------------------- // CLASS METHODS //------------------------------------------------------------------- public ReverseIterator(Component pStart) { //check component exist & is correct type Debug.Assert(pStart != null); Debug.Assert(pStart.type == Component.Type.Composite); //TODO: fix Keenan's hack...? ForwardIterator pForward = new ForwardIterator(pStart); this.pRoot = pStart; this.pCurr = this.pRoot; this.pPrev = null; Component pPrevNode = this.pRoot; //reverse pointer Component pNode = pForward.First(); while (!pForward.IsDone()) { //keep ref pPrevNode = pNode; //get next pNode = pForward.Next(); if (pNode != null) { pNode.pReverse = pPrevNode; } } pRoot.pReverse = pPrevNode; } //------------------------------------------------------------------- // OVERRIDDEN METHODS //------------------------------------------------------------------- public override Component First() { Debug.Assert(this.pRoot != null); this.pCurr = this.pRoot.pReverse; return this.pCurr; } public override Component Curr() { return this.pCurr; } public override Component Next() { Debug.Assert(this.pCurr != null); this.pPrev = this.pCurr; this.pCurr = this.pCurr.pReverse; return this.pCurr; } public override bool IsDone() { return (this.pPrev == this.pRoot); } } }