1
2
3
4 package joeq.Compiler.Dataflow;
5
6 import java.util.Collection;
7 import java.util.Comparator;
8 import java.util.Iterator;
9 import java.util.List;
10 import java.util.SortedSet;
11 import java.util.TreeSet;
12 import jwutil.collections.MapFactory;
13 import jwutil.graphs.Graph;
14 import jwutil.graphs.Traversals;
15
16 /***
17 * SortedSetSolver
18 *
19 * @author John Whaley
20 * @version $Id: SortedSetSolver.java 1931 2004-09-22 22:17:47Z joewhaley $
21 */
22 public class SortedSetSolver extends WorklistSolver {
23
24 /*** All locations in the graph, stored in a list. */
25 protected List allNodes;
26 /*** Worklist of locations, sorted by priority. */
27 protected SortedSet worklist;
28 /*** Location ordering function. */
29 protected Comparator ordering;
30
31 public SortedSetSolver(MapFactory f) {
32 super(f);
33 }
34 public SortedSetSolver() {
35 super();
36 }
37 public SortedSetSolver(Comparator c) {
38 super();
39 setOrder(c);
40 }
41
42 /*** Set the default ordering for this solver.
43 *
44 * @param c comparator object that implements the ordering
45 */
46 public void setOrder(Comparator c) {
47 this.ordering = c;
48 }
49
50
51
52
53 public void initialize(Problem p, Graph graph) {
54 super.initialize(p, graph);
55 allNodes = Traversals.reversePostOrder(graphNavigator, boundaries);
56 if (TRACE) System.out.println("Order: "+allNodes);
57 }
58
59
60
61
62 public Iterator allLocations() {
63 return allNodes.iterator();
64 }
65
66
67
68
69 protected void initializeWorklist() {
70 worklist = new TreeSet(ordering);
71 worklist.addAll(allNodes);
72 worklist.removeAll(boundaries);
73 }
74
75
76
77
78 protected boolean hasNext() {
79 return !worklist.isEmpty();
80 }
81
82
83
84
85 protected Object pull() {
86 Iterator i = worklist.iterator();
87 Object o = i.next();
88 i.remove();
89 return o;
90 }
91
92
93
94
95 protected void pushAll(Collection c) {
96 worklist.addAll(c);
97 }
98 }