# HG changeset patch # User Andreas Woess # Date 1405008509 -7200 # Node ID 17f7331dcc4f032c5204bc68f1c9d4da9be547c3 # Parent d41922beb512e738f430f8dd72388add26f0d3f5 Truffle: move iterator to NodeClass diff -r d41922beb512 -r 17f7331dcc4f graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/Node.java --- a/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/Node.java Thu Jul 10 18:08:29 2014 +0200 +++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/Node.java Thu Jul 10 18:08:29 2014 +0200 @@ -357,11 +357,9 @@ * @return the iterator */ public final Iterable getChildren() { - final Node node = this; return new Iterable() { - public Iterator iterator() { - return new NodeUtil.NodeIterator(node); + return NodeUtil.makeIterator(Node.this); } }; } diff -r d41922beb512 -r 17f7331dcc4f graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeUtil.java --- a/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeUtil.java Thu Jul 10 18:08:29 2014 +0200 +++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeUtil.java Thu Jul 10 18:08:29 2014 +0200 @@ -172,6 +172,7 @@ private final long parentOffset; private final long[] childOffsets; private final long[] childrenOffsets; + private final Class clazz; public static NodeClass get(Class clazz) { return nodeClasses.get(clazz); @@ -210,6 +211,7 @@ this.parentOffset = parentOffsetsList.get(0); this.childOffsets = toLongArray(childOffsetsList); this.childrenOffsets = toLongArray(childrenOffsetsList); + this.clazz = clazz; } public NodeField[] getFields() { @@ -242,73 +244,76 @@ } return false; } - } - static class NodeIterator implements Iterator { - - private final Node node; - private final NodeClass nodeClass; - private final int childrenCount; - private int index; - - protected NodeIterator(Node node) { - this.node = node; - this.index = 0; - this.nodeClass = NodeClass.get(node.getClass()); - this.childrenCount = childrenCount(); - } - - private int childrenCount() { - int nodeCount = nodeClass.childOffsets.length; - for (long fieldOffset : nodeClass.childrenOffsets) { - Node[] children = ((Node[]) unsafe.getObject(node, fieldOffset)); - if (children != null) { - nodeCount += children.length; - } - } - return nodeCount; + public Iterator makeIterator(Node node) { + assert clazz.isInstance(node); + return new NodeIterator(node); } - private Node nodeAt(int idx) { - int nodeCount = nodeClass.childOffsets.length; - if (idx < nodeCount) { - return (Node) unsafe.getObject(node, nodeClass.childOffsets[idx]); - } else { - for (long fieldOffset : nodeClass.childrenOffsets) { - Node[] nodeArray = (Node[]) unsafe.getObject(node, fieldOffset); - if (idx < nodeCount + nodeArray.length) { - return nodeArray[idx - nodeCount]; + private final class NodeIterator implements Iterator { + private final Node node; + private final int childrenCount; + private int index; + + protected NodeIterator(Node node) { + this.node = node; + this.index = 0; + this.childrenCount = childrenCount(); + } + + private int childrenCount() { + int nodeCount = childOffsets.length; + for (long fieldOffset : childrenOffsets) { + Node[] children = ((Node[]) unsafe.getObject(node, fieldOffset)); + if (children != null) { + nodeCount += children.length; } - nodeCount += nodeArray.length; + } + return nodeCount; + } + + private Node nodeAt(int idx) { + int nodeCount = childOffsets.length; + if (idx < nodeCount) { + return (Node) unsafe.getObject(node, childOffsets[idx]); + } else { + for (long fieldOffset : childrenOffsets) { + Node[] nodeArray = (Node[]) unsafe.getObject(node, fieldOffset); + if (idx < nodeCount + nodeArray.length) { + return nodeArray[idx - nodeCount]; + } + nodeCount += nodeArray.length; + } + } + return null; + } + + private void forward() { + if (index < childrenCount) { + index++; } } - return null; - } + + public boolean hasNext() { + return index < childrenCount; + } - private void forward() { - if (index < childrenCount) { - index++; + public Node next() { + try { + return nodeAt(index); + } finally { + forward(); + } + } + + public void remove() { + throw new UnsupportedOperationException(); } } - - @Override - public boolean hasNext() { - return index < childrenCount; - } + } - @Override - public Node next() { - try { - return nodeAt(index); - } finally { - forward(); - } - } - - @Override - public void remove() { - throw new UnsupportedOperationException(); - } + static Iterator makeIterator(Node node) { + return NodeClass.get(node.getClass()).makeIterator(node); } private static long[] toLongArray(List list) {