/** * */ // package edu.neu.ccs.evergreen.ir; /** * @author mohsen * The RelationNumberFactory relation is a utility class that aids in * creating relation numbers. * Note that the created relation numbers can be manipulated using bitwise operations * provided that they share the same variables and same variable ordering * for example: * int x = 0; * int y = 1; * int z = 2; * int w = 3; * int r_or_xy = RelationNumberFactory.or(4,x,y) * int r_or_zw = RelationNumberFactory.or(4,z,w) * int r_or_xyzw = r_or_xy|r_or_zw * * The relation provides: * or(arbitrary number of variables, * and(arbitrary number of variables, * imply (two vars) * equiv (two vars) * xor (two vars) * * Use RelationCore.getMagicNumber(rank,x,1) to get a relation representing x * Use RelationCore.getMagicNumber(rank,x,0) to get a relation representing !x */ public class RelationNumberFactory { /** * Given a set of variable positions, return a relation that is the or of these variables * Use -ve variable positions to denote negeated variables * @param rank the rank of the created relation * @param variablePositions an arbitrary number of variable positions * @return a relation number that represents the oring of the variables at the given positions */ public static int or(int rank, int ... variablePositions){ int relationNumber=0; for(int variablePosition: variablePositions){ // Note that the getMagicNumber will check for the validity of the given variablePosition int var = (variablePosition<0)?-variablePosition:variablePosition; relationNumber|= RelationCore.getMagicNumber(rank, var, 1); } return nMap(relationNumber, rank, variablePositions); } /** * Given a set of variable positions, return a relation that is the anding of these variables * Use -ve variable positions to denote negeated variables * @param rank the rank of the created relation * @param variablePositions an arbitrary number of variable positions * @return a relation number that represents the anding of the variables at the given positions */ public static int and(int rank, int ... variablePositions){ int relationNumber = RelationCore.getMask(rank); for(int variablePosition: variablePositions){ // Note that the getMagicNumber will check for the validity of the given variablePosition int var = (variablePosition<0)?-variablePosition:variablePosition; relationNumber&= RelationCore.getMagicNumber(rank, var, 1); } return nMap(relationNumber, rank, variablePositions); } /** * nMaps a number of variables in a certain relation * Use -ve variable positions to denote the variables to be negated * @param relationNumber * @param rank the rank of the created relation * @param variablePositions an arbitrary number of variable positions * @return a relation number that represents the given relation with the given set of variables renamed */ public static int nMap(int relationNumber,int rank, int ... variablePositions){ for(int variablePosition: variablePositions){ // Note that the getMagicNumber will check for the validity of the given variablePosition if(variablePosition<0){ //nmap the relation RelationCore.nMap(relationNumber, rank, variablePosition); } } return relationNumber; } /** * Given two variables x,y returns a relation that represents x==>y * Use -ve variable positions to denote negeated variables * @param rank the rank of the created relation * @param variablePositions two variable positions * @return a relation number that represents the x=>y where x,y are the given two variable positions */ public static int imply(int rank, int ... variablePositions){ if(variablePositions.length>2) throw new IllegalArgumentException("imply supports up to 2 variables. Passed "+variablePositions.length); return or(rank, -variablePositions[0],variablePositions[1]); } /** * Given two variables x,y returns a relation that represents x=y * Use -ve variable positions to denote negeated variables * @param rank the rank of the created relation * @param variablePositions two variable positions * @return a relation number that represents the x=y where x,y are the given two variable positions */ public static int equiv(int rank, int ... variablePositions){ if(variablePositions.length>2) throw new IllegalArgumentException("imply supports up to 2 variables. Passed "+variablePositions.length); int bothNeg = and(rank,-variablePositions[0],-variablePositions[1]); int bothPos = and(rank,variablePositions[0],variablePositions[1]); return bothNeg|bothPos; } /** * Given two variables x,y returns a relation that represents xor(x,y) * Use -ve variable positions to denote negeated variables * @param rank the rank of the created relation * @param variablePositions two variable positions * @return a relation number that represents the xor(x,y) where x,y are the given two variable positions */ public static int xor(int rank, int ... variablePositions){ if(variablePositions.length>2) throw new IllegalArgumentException("imply supports up to 2 variables. Passed "+variablePositions.length); int posNeg = and(rank,variablePositions[0],-variablePositions[1]); int negPos = and(rank,-variablePositions[0],variablePositions[1]); return posNeg|negPos; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(""+(-3&0xFFFFFFFF)); System.out.println(""+(-3&0xFFFFFFFF)); } }