Mercurial > hg > Members > shoshi > TreeCMSv1
changeset 4:8c33fd63fea6
no copy iterator
author | one |
---|---|
date | Fri, 27 Aug 2010 22:51:03 +0900 |
parents | 7ede12c9a2e9 |
children | 1bbee0534ece |
files | src/treecms/proto/test/PreOrderTreeWalker.java |
diffstat | 1 files changed, 22 insertions(+), 12 deletions(-) [+] |
line wrap: on
line diff
--- 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<NodeAPI> { - LinkedList<LinkedList<NodeAPI>>stack = new LinkedList<LinkedList<NodeAPI>>(); - LinkedList<NodeAPI>children; + LinkedList<NodeAPI>nodeStack; + LinkedList<Integer>positionStack; + List<NodeAPI> children; + int position; NodeAPI next; IteratorState(NodeAPI root) { - children = new LinkedList<NodeAPI>(root.getChildList()); - stack.addLast(children); + children = root.getChildList(); + nodeStack = new LinkedList<NodeAPI>(); + positionStack = new LinkedList<Integer>(); + 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<NodeAPI>(next.getChildList()); + nodeStack.addLast(next); + positionStack.addLast(position); + children = next.getChildList(); + position = 0; } return true; }