01: /* Soot - a J*va Optimization Framework
02: * Copyright (C) 2003 Ondrej Lhotak
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2.1 of the License, or (at your option) any later version.
08: *
09: * This library is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12: * Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public
15: * License along with this library; if not, write to the
16: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17: * Boston, MA 02111-1307, USA.
18: */
19:
20: package soot.jimple.toolkits.pointer;
21:
22: import java.util.*;
23: import soot.tagkit.*;
24:
25: public class DependenceGraph implements Attribute {
26: private final static String NAME = "DependenceGraph";
27: HashSet<Edge> edges = new HashSet<Edge>();
28:
29: protected class Edge {
30: short from;
31: short to;
32:
33: Edge(short from, short to) {
34: this .from = from;
35: this .to = to;
36: }
37:
38: public int hashCode() {
39: return (((from) << 16) + to);
40: }
41:
42: public boolean equals(Object other) {
43: Edge o = (Edge) other;
44: return from == o.from && to == o.to;
45: }
46: }
47:
48: public boolean areAdjacent(short from, short to) {
49: if (from > to)
50: return areAdjacent(to, from);
51: if (from < 0 || to < 0)
52: return false;
53: if (from == to)
54: return true;
55: return edges.contains(new Edge(from, to));
56: }
57:
58: public void addEdge(short from, short to) {
59: if (from < 0)
60: throw new RuntimeException("from < 0");
61: if (to < 0)
62: throw new RuntimeException("to < 0");
63: if (from > to) {
64: addEdge(to, from);
65: return;
66: }
67: edges.add(new Edge(from, to));
68: }
69:
70: public String getName() {
71: return NAME;
72: }
73:
74: public void setValue(byte[] v) {
75: throw new RuntimeException("Not Supported");
76: }
77:
78: public byte[] getValue() {
79: byte[] ret = new byte[4 * edges.size()];
80: int i = 0;
81: for (Edge e : edges) {
82: ret[i + 0] = (byte) ((e.from >> 8) & 0xff);
83: ret[i + 1] = (byte) (e.from & 0xff);
84: ret[i + 2] = (byte) ((e.to >> 8) & 0xff);
85: ret[i + 3] = (byte) (e.to & 0xff);
86: i += 4;
87: }
88: return ret;
89: }
90:
91: public String toString() {
92: StringBuffer buf = new StringBuffer("Dependences");
93: for (Edge e : edges) {
94: buf.append("( " + e.from + ", " + e.to + " ) ");
95: }
96: return buf.toString();
97: }
98: }
|