1 pnkfelix 1.1.2.1 // EdgesIterator.java, created Mon Apr 5 16:15:47 1999 by pnkfelix 2 cananian 1.1.2.6 // Copyright (C) 1999 Felix S. Klock II <pnkfelix@mit.edu> 3 pnkfelix 1.1.2.1 // Licensed under the terms of the GNU GPL; see COPYING for details. 4 pnkfelix 1.1.2.1 package harpoon.Analysis; 5 pnkfelix 1.1.2.1 6 cananian 1.1.2.5 import harpoon.ClassFile.HCodeEdge; 7 cananian 1.1.2.5 import harpoon.ClassFile.HCodeElement; 8 cananian 1.1.2.4 import harpoon.IR.Properties.CFGraphable; 9 cananian 1.1.2.5 import harpoon.IR.Properties.CFGrapher; 10 cananian 1.5 import net.cscott.jutil.UnmodifiableIterator; 11 cananian 1.5 import net.cscott.jutil.WorkSet; 12 pnkfelix 1.1.2.1 13 pnkfelix 1.1.2.1 import java.util.Iterator; 14 pnkfelix 1.1.2.1 import java.util.Set; 15 pnkfelix 1.1.2.1 import java.util.HashSet; 16 pnkfelix 1.1.2.1 17 pnkfelix 1.1.2.1 /** 18 pnkfelix 1.1.2.1 * <code>EdgesIterator</code> is a generic iterator for a set of 19 cananian 1.1.2.4 * <code>CFGraphable</code> objects. 20 pnkfelix 1.1.2.1 * 21 cananian 1.1.2.6 * @author Felix S. Klock II <pnkfelix@mit.edu> 22 cananian 1.6 * @version $Id: EdgesIterator.java,v 1.6 2004/02/08 03:19:12 cananian Exp $ 23 pnkfelix 1.1.2.1 */ 24 cananian 1.1.2.3 public class EdgesIterator extends UnmodifiableIterator implements Iterator { 25 cananian 1.1.2.5 private CFGrapher grapher; 26 pnkfelix 1.1.2.1 27 cananian 1.1.2.5 private WorkSet worklist; 28 pnkfelix 1.1.2.1 private Set done; 29 pnkfelix 1.1.2.1 30 cananian 1.1.2.5 /** Convenience constructor. */ 31 cananian 1.1.2.5 public EdgesIterator(CFGraphable e) { 32 cananian 1.1.2.5 this(e, CFGrapher.DEFAULT); 33 cananian 1.1.2.5 } 34 pnkfelix 1.1.2.1 /** Creates a <code>EdgesIterator</code> for all the edges 35 pnkfelix 1.1.2.1 reachable by recursively traversing the successors of 36 pnkfelix 1.1.2.1 <code>e</code>. Predecessors are not included in the set. 37 pnkfelix 1.1.2.1 */ 38 cananian 1.1.2.5 public EdgesIterator(HCodeElement e, CFGrapher grapher) { 39 cananian 1.1.2.5 this.grapher = grapher; 40 cananian 1.1.2.5 worklist = new WorkSet(); 41 pnkfelix 1.1.2.1 done = new HashSet(); 42 pnkfelix 1.1.2.1 worklist.add(e); 43 pnkfelix 1.1.2.1 done.add(e); 44 pnkfelix 1.1.2.1 } 45 pnkfelix 1.1.2.1 46 pnkfelix 1.1.2.1 /** Checks if the set is empty. 47 cananian 1.1.2.4 <BR> <B>effects:</B> returns true if more <code>CFGraphable</code> 48 pnkfelix 1.1.2.1 remain in the set. Else returns false. 49 pnkfelix 1.1.2.1 */ 50 pnkfelix 1.1.2.1 public boolean hasNext() { return worklist.size()!=0; } 51 pnkfelix 1.1.2.1 52 cananian 1.1.2.4 /** Returns an <code>CFGraphable</code> if one remains. 53 pnkfelix 1.1.2.1 <BR> <B>requires:</B> <code>this.hasNext()</code> == true. 54 cananian 1.1.2.4 <BR> <B>effects:</B> returns an <code>CFGraphable</code> from the 55 pnkfelix 1.1.2.1 set contained in <code>this</code> and removes it from the 56 pnkfelix 1.1.2.1 set. 57 pnkfelix 1.1.2.1 */ 58 pnkfelix 1.1.2.1 public Object next() { 59 cananian 1.1.2.5 HCodeElement e = (HCodeElement) worklist.pop(); 60 cananian 1.6 for (Object edgeO : grapher.succC(e)) { 61 cananian 1.6 HCodeEdge edge = (HCodeEdge) edgeO; 62 cananian 1.4 HCodeElement ne = edge.to(); 63 pnkfelix 1.1.2.1 if (!done.contains(ne)) { 64 pnkfelix 1.1.2.1 done.add(ne); 65 pnkfelix 1.1.2.1 worklist.add(ne); 66 pnkfelix 1.1.2.1 } 67 pnkfelix 1.1.2.1 } 68 pnkfelix 1.1.2.1 return e; 69 pnkfelix 1.1.2.1 } 70 cananian 1.2 }