001 /*
002 * This file is part of the Jikes RVM project (http://jikesrvm.org).
003 *
004 * This file is licensed to You under the Eclipse Public License (EPL);
005 * You may not use this file except in compliance with the License. You
006 * may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/eclipse-1.0.php
009 *
010 * See the COPYRIGHT.txt file distributed with this work for information
011 * regarding copyright ownership.
012 */
013 package org.jikesrvm.compilers.opt.util;
014
015 import java.util.Enumeration;
016 import java.util.NoSuchElementException;
017
018
019 public final class DepthFirstEnumerator implements Enumeration<GraphNode> {
020 Stack<GraphNode> stack;
021 int mark;
022
023 public DepthFirstEnumerator(GraphNode start, int markNumber) {
024 stack = new Stack<GraphNode>();
025 stack.push(start);
026 mark = markNumber;
027 }
028
029 public boolean hasMoreElements() {
030 if (stack == null) {
031 return false;
032 }
033
034 for (GraphNode node : stack) {
035 if (node.getScratch() != mark) {
036 return true;
037 }
038 }
039 return false;
040 }
041
042 public GraphNode nextElement() {
043 return next();
044 }
045
046 public GraphNode next() {
047 if (stack == null) {
048 throw new NoSuchElementException("DepthFirstEnumerator");
049 }
050 while (!stack.isEmpty()) {
051 GraphNode node = stack.pop();
052 if (node.getScratch() != mark) {
053 for (Enumeration<GraphNode> e = node.outNodes(); e.hasMoreElements();) {
054 GraphNode n = e.nextElement();
055 if (n != null) {
056 stack.push(n);
057 }
058 }
059 node.setScratch(mark);
060 return node;
061 }
062 }
063 throw new NoSuchElementException("DepthFirstEnumerator");
064 }
065 }