1 /* 2 * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package org.graalvm.compiler.graph; 24 25 import java.util.Arrays; 26 import java.util.Iterator; 27 import java.util.NoSuchElementException; 28 29 class TypedGraphNodeIterator<T extends IterableNodeType> implements Iterator<T> { 30 31 private final Graph graph; 32 private final int[] ids; 33 private final Node[] current; 34 35 private int currentIdIndex; 36 private boolean needsForward; 37 38 TypedGraphNodeIterator(NodeClass<?> clazz, Graph graph) { 39 this.graph = graph; 40 ids = clazz.iterableIds(); 41 currentIdIndex = 0; 42 current = new Node[ids.length]; 43 Arrays.fill(current, Graph.PLACE_HOLDER); 44 needsForward = true; 45 } 46 47 private Node findNext() { 48 if (needsForward) { 49 forward(); 50 } else { 51 Node c = current(); 52 Node afterDeleted = graph.getIterableNodeNext(c); 53 if (afterDeleted == null) { 54 needsForward = true; 55 } else if (c != afterDeleted) { 56 setCurrent(afterDeleted); 57 } 58 } 59 if (needsForward) { 60 return null; 61 } 62 return current(); 63 } 64 65 private void forward() { 66 needsForward = false; 67 int startIdx = currentIdIndex; 68 while (true) { 69 Node next; 70 if (current() == Graph.PLACE_HOLDER) { 71 next = graph.getIterableNodeStart(ids[currentIdIndex]); 72 } else { 73 next = graph.getIterableNodeNext(current().typeCacheNext); 74 } 75 if (next == null) { 76 currentIdIndex++; 77 if (currentIdIndex >= ids.length) { 78 currentIdIndex = 0; 79 } 80 if (currentIdIndex == startIdx) { 81 needsForward = true; 82 return; 83 } 84 } else { 85 setCurrent(next); 86 break; 87 } 88 } 89 } 90 | 1 /* 2 * Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package org.graalvm.compiler.graph; 24 25 import java.util.Iterator; 26 import java.util.NoSuchElementException; 27 28 class TypedGraphNodeIterator<T extends IterableNodeType> implements Iterator<T> { 29 30 private final Graph graph; 31 private final int[] ids; 32 private final Node[] current; 33 34 private int currentIdIndex; 35 private boolean needsForward; 36 37 TypedGraphNodeIterator(NodeClass<?> clazz, Graph graph) { 38 this.graph = graph; 39 ids = clazz.iterableIds(); 40 currentIdIndex = 0; 41 current = new Node[ids.length]; 42 needsForward = true; 43 } 44 45 private Node findNext() { 46 if (needsForward) { 47 forward(); 48 } else { 49 Node c = current(); 50 Node afterDeleted = graph.getIterableNodeNext(c); 51 if (afterDeleted == null) { 52 needsForward = true; 53 } else if (c != afterDeleted) { 54 setCurrent(afterDeleted); 55 } 56 } 57 if (needsForward) { 58 return null; 59 } 60 return current(); 61 } 62 63 private void forward() { 64 needsForward = false; 65 int startIdx = currentIdIndex; 66 while (true) { 67 Node next; 68 if (current() == null) { 69 next = graph.getIterableNodeStart(ids[currentIdIndex]); 70 } else { 71 next = graph.getIterableNodeNext(current().typeCacheNext); 72 } 73 if (next == null) { 74 currentIdIndex++; 75 if (currentIdIndex >= ids.length) { 76 currentIdIndex = 0; 77 } 78 if (currentIdIndex == startIdx) { 79 needsForward = true; 80 return; 81 } 82 } else { 83 setCurrent(next); 84 break; 85 } 86 } 87 } 88 |