KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > jimple > toolkits > pointer > DependenceGraph


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2003 Ondrej Lhotak
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * 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 import soot.*;
22 import java.util.*;
23 import soot.tagkit.*;
24
25 public class DependenceGraph implements Attribute
26 {
27     private final static String JavaDoc NAME = "DependenceGraph";
28     HashSet edges = new HashSet();
29
30     protected class Edge {
31     short from;
32     short to;
33     Edge( short from, short to ) { this.from = from; this.to = to; }
34     public int hashCode() { return ( (((int) from) << 16) + to ); }
35     public boolean equals( Object JavaDoc other ) {
36         Edge o = (Edge) other;
37         return from == o.from && to == o.to;
38     }
39     }
40     
41     public boolean areAdjacent( short from, short to ) {
42     if( from > to ) return areAdjacent( to, from );
43     if( from < 0 || to < 0 ) return false;
44     if( from == to ) return true;
45     return edges.contains( new Edge( from, to ) );
46     }
47     public void addEdge( short from, short to ) {
48     if( from < 0 ) throw new RuntimeException JavaDoc( "from < 0" );
49     if( to < 0 ) throw new RuntimeException JavaDoc( "to < 0" );
50     if( from > to ) {
51         addEdge( to, from );
52         return;
53     }
54     edges.add( new Edge( from, to ) );
55     }
56     public String JavaDoc getName()
57     {
58     return NAME;
59     }
60
61     public void setValue(byte[] v) {
62     throw new RuntimeException JavaDoc( "Not Supported" );
63     }
64
65     public byte[] getValue() {
66     byte[] ret = new byte[4*edges.size()];
67     int i = 0;
68     for( Iterator it = edges.iterator(); it.hasNext(); ) {
69         Edge e = (Edge) it.next();
70         ret[i+0] = (byte) ( (e.from >> 8) & 0xff );
71         ret[i+1] = (byte) ( e.from & 0xff );
72         ret[i+2] = (byte) ( (e.to >> 8) & 0xff );
73         ret[i+3] = (byte) ( e.to & 0xff );
74         i += 4;
75     }
76     return ret;
77     }
78
79     public String JavaDoc toString()
80     {
81     StringBuffer JavaDoc buf = new StringBuffer JavaDoc( "Dependences" );
82     for( Iterator it = edges.iterator(); it.hasNext(); ) {
83         Edge e = (Edge) it.next();
84         buf.append( "( "+e.from+", "+e.to+" ) " );
85     }
86     return buf.toString();
87     }
88 }
89
Popular Tags