00001 package edu.ksu.cis.bandera.pdgslicer; 00002 00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 00004 * Bandera, a Java(TM) analysis and transformation toolkit * 00005 * Copyright (C) 1998, 1999 Hongjun Zheng (zheng@cis.ksu.edu) * 00006 * All rights reserved. * 00007 * * 00008 * This work was done as a project in the SAnToS Laboratory, * 00009 * Department of Computing and Information Sciences, Kansas State * 00010 * University, USA (http://www.cis.ksu.edu/santos). * 00011 * It is understood that any modification not identified as such is * 00012 * not covered by the preceding statement. * 00013 * * 00014 * This work is free software; you can redistribute it and/or * 00015 * modify it under the terms of the GNU Library General Public * 00016 * License as published by the Free Software Foundation; either * 00017 * version 2 of the License, or (at your option) any later version. * 00018 * * 00019 * This work is distributed in the hope that it will be useful, * 00020 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00021 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00022 * Library General Public License for more details. * 00023 * * 00024 * You should have received a copy of the GNU Library General Public * 00025 * License along with this toolkit; if not, write to the * 00026 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * 00027 * Boston, MA 02111-1307, USA. * 00028 * * 00029 * Java is a trademark of Sun Microsystems, Inc. * 00030 * * 00031 * To submit a bug report, send a comment, or get the latest news on * 00032 * this project and other SAnToS projects, please visit the web-site * 00033 * http://www.cis.ksu.edu/santos * 00034 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 00035 00036 import java.util.BitSet; 00037 import ca.mcgill.sable.util.*; 00038 import ca.mcgill.sable.soot.jimple.*; 00039 /** 00040 * This class contains some utilities for operations of 00041 * {@link BitSet BitSet} and {@link Set Set}. 00042 * <br> Currently, all methods in this class are 00043 * <code>static</code>. 00044 */ 00045 public class SetUtil { 00046 00047 /** 00048 * Clear elements of a bit set in terms of a ruler bit set. 00049 * <p> 00050 * @return result bit set. 00051 * @param orignalSet a bit set need to be cleared by a ruler. 00052 * @param notSet ruler bit set. 00053 */ 00054 static BitSet bitSetAndNot(BitSet originalSet, BitSet notSet) { 00055 BitSet cloneOrig = (BitSet) originalSet.clone(); 00056 for (int i = 0; i < notSet.size(); i++) 00057 if (notSet.get(i)) 00058 cloneOrig.clear(i); 00059 return cloneOrig; 00060 } 00061 /** 00062 * Transform a bit set of statements into a set of {@link Stmt Stmt}. 00063 * <p> 00064 * @return a set of {@link Stmt Stmt} which is corresponding to 00065 * the <code>bitSet</code>. 00066 * @param bitSet a bit set of statements indexed with <code>stmtList</code>. 00067 * @param stmtList statement list. 00068 */ 00069 public static Set bitSetToStmtSet(BitSet bitSet, StmtList stmtList) { 00070 Set stmtSet = new ArraySet(); 00071 for (int i = 0; i < bitSet.size(); i++) { 00072 if (!bitSet.get(i)) 00073 continue; 00074 stmtSet.add((Stmt) stmtList.get(i)); 00075 } 00076 return stmtSet; 00077 } 00078 /** 00079 * See if a bit set is empty 00080 * <p> 00081 * @return <code>true</code> if it is empty; <code>false</code> otherwise. 00082 * @param bs query bit set. 00083 */ 00084 public static boolean emptyBitSet(BitSet bs) { 00085 for (int i = 0; i < bs.size(); i++) 00086 if (bs.get(i)) 00087 return false; 00088 return true; 00089 } 00090 /** 00091 * See if a bit set is empty 00092 * <p> 00093 * @return <code>true</code> if it is empty; <code>false</code> otherwise. 00094 * @param bs query bit set. 00095 */ 00096 public static boolean emptyBitSetWithLength(BitSet bs, int length) { 00097 for (int i = 0; i < length; i++) 00098 if (bs.get(i)) 00099 return false; 00100 return true; 00101 } 00102 /** 00103 * Insert the method's description here. 00104 * Creation date: (00-12-6 11:01:22) 00105 * @param bs java.util.BitSet 00106 */ 00107 public static void initializeBitSetToAllFalse(BitSet bs) { 00108 for (int i = 0; i < bs.size(); i++) 00109 bs.clear(i); 00110 } 00111 /** 00112 * Insert the method's description here. 00113 * Creation date: (00-12-6 11:01:22) 00114 * @param bs java.util.BitSet 00115 */ 00116 public static void initializeBitSetToAllTrue(BitSet bs) { 00117 for (int i = 0; i < bs.size(); i++) 00118 bs.set(i); 00119 } 00120 /** 00121 * Get logical size of a bit set. Logical size means 00122 * how many element in a bit set is set to <code>true</code>. 00123 * <p> 00124 * @return logical size of the bit set. 00125 * @param bs query bit set. 00126 */ 00127 static int logicalSizeOfBitSet(BitSet bs) { 00128 int size = 0; 00129 for (int i=0; i<bs.size(); i++) 00130 if (bs.get(i)) size++; 00131 return size; 00132 } 00133 /** 00134 * Get intersection of two {@link Set Set}. 00135 * <p> 00136 * @return intersection of two Set. 00137 * @param set1 first set. 00138 * @param set2 second set. 00139 */ 00140 public static Set setIntersection(Set set1, Set set2) { 00141 Set returnSet = new ArraySet(); 00142 for (Iterator i = set1.iterator(); i.hasNext();) 00143 { 00144 Object obj = (Object) i.next(); 00145 if (set2.contains(obj)) returnSet.add(obj); 00146 } 00147 return returnSet; 00148 } 00149 /** 00150 * Transform an array of {@link Stmt Stmt} to a bit set indexed 00151 * with given statement list. 00152 * <p> 00153 * @return a bit set of statement indexed with <code>localStmtList</code>. 00154 * @param stmtSet a statement set for transforming. 00155 * @param localStmtList a statement list for indexing. 00156 */ 00157 static BitSet stmtArrayToBitSet(Stmt[] stmtSet, StmtList localStmtList) { 00158 BitSet bitSetOfStmt = new BitSet(localStmtList.size()); 00159 for (int stmtIt = 0; stmtIt < stmtSet.length; stmtIt++) { 00160 Stmt stmt = stmtSet[stmtIt]; 00161 bitSetOfStmt.set(localStmtList.indexOf(stmt)); 00162 } 00163 return bitSetOfStmt; 00164 } 00165 /** 00166 * Transform a {@link Set Set} of {@link Stmt Stmt} to 00167 * a bit set indexed with a given stmatement list. 00168 * <p> 00169 * @return a bit set of statement indexed with <code>localStmtList</code>. 00170 * @param stmtSet a set of {@link Stmt Stmt}. 00171 * @param localStmtList statement list for indexing. 00172 */ 00173 static BitSet stmtSetToBitSet(Set stmtSet, StmtList localStmtList) { 00174 BitSet bitSetOfStmt = new BitSet(localStmtList.size()); 00175 for (Iterator stmtIt = stmtSet.iterator(); stmtIt.hasNext();) { 00176 Stmt stmt = (Stmt) stmtIt.next(); 00177 bitSetOfStmt.set(localStmtList.indexOf(stmt)); 00178 } 00179 return bitSetOfStmt; 00180 } 00181 }