KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > toolkits > graph > HashReversibleGraph


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2005 Navindra Umanee <navindra@cs.mcgill.ca>
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.toolkits.graph;
21
22 import java.util.*;
23
24 /**
25  * A reversible version of HashMutableDirectedGraph
26  *
27  * @author Navindra Umanee
28  **/

29 public class HashReversibleGraph extends HashMutableDirectedGraph
30     implements ReversibleGraph
31 {
32     protected boolean reversed;
33
34     public HashReversibleGraph(DirectedGraph dg)
35     {
36         this();
37
38         for(Iterator i = dg.iterator(); i.hasNext();){
39             Object JavaDoc s = i.next();
40             addNode(s);
41         }
42
43         for(Iterator i = dg.iterator(); i.hasNext();){
44             Object JavaDoc s = i.next();
45             List succs = dg.getSuccsOf(s);
46             for(Iterator succsIt = succs.iterator(); succsIt.hasNext();){
47                 Object JavaDoc t = succsIt.next();
48                 addEdge(s, t);
49             }
50         }
51
52         /* use the same heads and tails as the original graph */
53         
54         heads.clear();
55         heads.addAll(dg.getHeads());
56         tails.clear();
57         tails.addAll(dg.getTails());
58     }
59             
60     public HashReversibleGraph()
61     {
62         super();
63         reversed = false;
64     }
65     
66     public boolean isReversed()
67     {
68         return reversed;
69     }
70
71     public ReversibleGraph reverse()
72     {
73         reversed = !reversed;
74         return this;
75     }
76
77     public void addEdge(Object JavaDoc from, Object JavaDoc to)
78     {
79         if(reversed)
80             super.addEdge(to, from);
81         else
82             super.addEdge(from, to);
83     }
84
85     public void removeEdge(Object JavaDoc from, Object JavaDoc to)
86     {
87         if(reversed)
88             super.removeEdge(to, from);
89         else
90             super.removeEdge(from, to);
91     }
92
93     public boolean containsEdge(Object JavaDoc from, Object JavaDoc to)
94     {
95         return reversed ? super.containsEdge(to, from) : super.containsEdge(from, to);
96     }
97
98     public List getHeads()
99     {
100         return reversed ? super.getTails() : super.getHeads();
101     }
102
103     public List getTails()
104     {
105         return reversed ? super.getHeads() : super.getTails();
106     }
107
108     public List getPredsOf(Object JavaDoc s)
109     {
110         return reversed ? super.getSuccsOf(s) : super.getPredsOf(s);
111     }
112     
113     public List getSuccsOf(Object JavaDoc s)
114     {
115         return reversed ? super.getPredsOf(s) : super.getSuccsOf(s);
116     }
117 }
118
Popular Tags