# HG changeset patch # User one # Date 1282917063 -32400 # Node ID 8c33fd63fea64e7c27e4e4d67f4d70622eff35e1 # Parent 7ede12c9a2e93ece93a4990fd81046f28a1d9bde no copy iterator diff -r 7ede12c9a2e9 -r 8c33fd63fea6 src/treecms/proto/test/PreOrderTreeWalker.java --- a/src/treecms/proto/test/PreOrderTreeWalker.java Fri Aug 27 22:25:48 2010 +0900 +++ b/src/treecms/proto/test/PreOrderTreeWalker.java Fri Aug 27 22:51:03 2010 +0900 @@ -2,6 +2,7 @@ import java.util.Iterator; import java.util.LinkedList; +import java.util.List; import treecms.proto.api.NodeAPI; @@ -15,26 +16,35 @@ } class IteratorState implements Iterator { - LinkedList>stack = new LinkedList>(); - LinkedListchildren; + LinkedListnodeStack; + LinkedListpositionStack; + List children; + int position; NodeAPI next; IteratorState(NodeAPI root) { - children = new LinkedList(root.getChildList()); - stack.addLast(children); + children = root.getChildList(); + nodeStack = new LinkedList(); + positionStack = new LinkedList(); + nodeStack.addLast(root); + positionStack.addLast(0); + position = 0; } public boolean hasNext() { - while (children.isEmpty()) { - if (stack.isEmpty()) return false; - children = stack.getLast(); - stack.removeLast(); + while (position>=children.size()) { + if (nodeStack.isEmpty()) return false; + children = nodeStack.getLast().getChildList(); + position = positionStack.getLast(); + nodeStack.removeLast(); + positionStack.removeLast(); } - next = children.get(0); - children.remove(0); + next = children.get(position++); if (! next.getChildList().isEmpty()) { - stack.addLast(children); - children = new LinkedList(next.getChildList()); + nodeStack.addLast(next); + positionStack.addLast(position); + children = next.getChildList(); + position = 0; } return true; }