Mercurial > hg > graal-jvmci-8
changeset 16467:17f7331dcc4f
Truffle: move iterator to NodeClass
author | Andreas Woess <andreas.woess@jku.at> |
---|---|
date | Thu, 10 Jul 2014 18:08:29 +0200 |
parents | d41922beb512 |
children | 9fa5872291c1 |
files | graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/Node.java graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeUtil.java |
diffstat | 2 files changed, 63 insertions(+), 60 deletions(-) [+] |
line wrap: on
line diff
--- 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<Node> getChildren() { - final Node node = this; return new Iterable<Node>() { - public Iterator<Node> iterator() { - return new NodeUtil.NodeIterator(node); + return NodeUtil.makeIterator(Node.this); } }; }
--- 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<? extends Node> clazz; public static NodeClass get(Class<? extends Node> 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<Node> { - - 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<Node> 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<Node> { + 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<Node> makeIterator(Node node) { + return NodeClass.get(node.getClass()).makeIterator(node); } private static long[] toLongArray(List<Long> list) {