From: linsw Date: Fri, 8 Apr 2011 19:24:31 +0000 (+0000) Subject: Added the Generic N-Tuple capability. Main File is MultiHash which uses Tuples interf... X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=ce3edfb207dcd372e8daca4f9602ffc662a2cf46;p=IRC.git Added the Generic N-Tuple capability. Main File is MultiHash which uses Tuples interface file and Tuple to get the job done. OptimizedView is a single view of one of the tables. --- diff --git a/Robust/src/Util/MultiHash.java b/Robust/src/Util/MultiHash.java new file mode 100755 index 00000000..2d02d25c --- /dev/null +++ b/Robust/src/Util/MultiHash.java @@ -0,0 +1,120 @@ +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Hashtable; +import java.util.Set; + +public class MultiHash{ + private int[] views; + private Hashtable viewTable = new Hashtable(); + + public MultiHash(){ + } + + // Pass in the look up map + public MultiHash(int[] bitmapArray){ + this.views = bitmapArray; + for(int i = 0; i < views.length; i++){ + Hashtable ht = new Hashtable(); + viewTable.put(views[i], ht); + } + } + + // For each view add it to its view hashtable + public void put(Tuples o){ + // Tune the Tuple for each view and add it to its designated hashtable + for(int i = 0; i < views.length; i++){ + int tupleKey = generateTupleKey(o, views[i]); + Hashtable tuplesTable = (Hashtable) viewTable.get(views[i]); + if(tuplesTable.containsKey(tupleKey)){ + Set tupleSet = (Set) tuplesTable.get(tupleKey); + tupleSet.add(o); + }else{ + Set tupleSet = new HashSet(); + tupleSet.add(o); + tuplesTable.put(tupleKey, tupleSet); + } + } + } + + public int generateTupleKey(Tuples o, int viewIndex){ + ArrayList indices = findIndices(viewIndex); + ArrayList obj = new ArrayList(); + for(int i = 0; i < indices.size(); i++){ + obj.add(o.get(indices.get(i))); + } + return obj.hashCode()^29; + } + + private ArrayList findIndices(int viewIndex){ + int mask = 1; + ArrayList indices = new ArrayList(); + for(int i = 0; i < 31; i++){ + if((mask & viewIndex) != 0){ + indices.add(i); + } + mask = mask << 1; + } + return indices; + } + + public Tuples get(int bitmap, Tuple o){ + Tuples tuple = new Tuple(); // + int tupleKey = generateTupleKey(o, bitmap); + Hashtable tuplesTable = (Hashtable) viewTable.get(bitmap); + if(tuplesTable.containsKey(tupleKey)){ + Set tupleSet = (Set) tuplesTable.get(tupleKey); + tuple = convertToTuple(tupleSet); + return tuple; + } + return null; + } + + private Tuples convertToTuple(Set tupleSet){ + Object[] tuples = tupleSet.toArray(); + ArrayList o = new ArrayList(); + for(int i = 0; i < tuples.length; i++){ + o.add(tuples[i]); + } + Tuples tuple = new Tuple(o); + return tuple; + } + + public void remove(Tuples o){ +// System.out.println("removed called"+viewTable.toString()); + for(int i = 0; i < views.length; i++){ + int tupleKey = generateTupleKey(o, views[i]); + Hashtable tuplesTable = (Hashtable) viewTable.get(views[i]); + if(tuplesTable.containsKey(tupleKey)){ + tuplesTable.remove(tupleKey); + }else{ + System.out.println("Cannot find such key"); + } + } + } + + public OptimizedView getOptimizedView(int bitMapView){ + Hashtable tmp = (Hashtable) viewTable.get(bitMapView); + OptimizedView ov = new OptimizedView(bitMapView, tmp, this); + return ov; + } + + /* Debug visualizations */ + public void drawTierTwoTable(){ + for(int i = 0; i < views.length; i++){ + Hashtable tmp = (Hashtable) viewTable.get(views[i]); + System.out.println("Hashtable "+i+":\t"+tmp.keySet().toString()); + Object[] keySets = tmp.keySet().toArray(); + for(int j = 0; j < keySets.length; j++){ + System.out.println(tmp.get(keySets[j])); + } + } + } + + public int[] getViews(){ + return views; + } + + public Hashtable getTable(){ + return viewTable; + } +} diff --git a/Robust/src/Util/OptimizedView.java b/Robust/src/Util/OptimizedView.java new file mode 100755 index 00000000..bd7cb429 --- /dev/null +++ b/Robust/src/Util/OptimizedView.java @@ -0,0 +1,66 @@ +import java.util.ArrayList; +import java.util.Hashtable; +import java.util.Set; + +public class OptimizedView extends MultiHash +{ + private int bitMapView; + private Hashtable table; + private MultiHash parent; + + public OptimizedView(int bitMapView, Hashtable table, MultiHash parent) { + this.bitMapView = bitMapView; + this.table = table; + this.parent = parent; + } + + public void remove(Tuple o){ + parent.remove(o); + } + public Tuples get(Tuples o){ + Tuples tuple = new Tuple(); + + int tupleKey = generateTupleKey(o); + if(table.containsKey(tupleKey)){ + Set tupleSet = (Set) table.get(tupleKey); + tuple = convertToTuple(tupleSet); + return tuple; + } + return null; + } + + private Tuples convertToTuple(Set tupleSet){ + Object[] tuples = tupleSet.toArray(); + ArrayList o = new ArrayList(); + for(int i = 0; i < tuples.length; i++){ + o.add(tuples[i]); + } + Tuples tuple = new Tuple(o); + return tuple; + } + + public int generateTupleKey(Tuples o){ + ArrayList indices = findIndices(bitMapView); + ArrayList obj = new ArrayList(); + for(int i = 0; i < indices.size(); i++){ + obj.add(o.get(indices.get(i))); + } + return obj.hashCode()^29; + } + + private ArrayList findIndices(int viewIndex){ + int mask = 1; + ArrayList indices = new ArrayList(); + for(int i = 0; i < 31; i++){ + if((mask & viewIndex) != 0){ + indices.add(i); + } + mask = mask << 1; + } + return indices; + } + + public String toString(){ + return table.toString(); + } +} diff --git a/Robust/src/Util/Tuple.java b/Robust/src/Util/Tuple.java new file mode 100644 index 00000000..2783f063 --- /dev/null +++ b/Robust/src/Util/Tuple.java @@ -0,0 +1,41 @@ +import java.util.ArrayList; + +public class Tuple implements Tuples{ + ArrayList o; + + public Tuple(){ + o = new ArrayList(); + } + + public Tuple(ArrayList o){ + this.o = o; + } + + public Object get(int i){ + return o.get(i); + } + + public void remove(int i){ + o.remove(i); + } + + public ArrayList getList(){ + return o; + } + + public int size(){ + return o.size(); + } + + public int hashCode(){ + return o.hashCode(); + } + + public String toString(){ + String tmp=""; + for(int i = 0; i < o.size(); i++){ + tmp += o.get(i)+" "; + } + return tmp; + } +} diff --git a/Robust/src/Util/Tuples.java b/Robust/src/Util/Tuples.java new file mode 100755 index 00000000..435f51d0 --- /dev/null +++ b/Robust/src/Util/Tuples.java @@ -0,0 +1,10 @@ +import java.util.ArrayList; + +public interface Tuples { + int size(); + int hashCode(); + void remove(int i); + ArrayList getList(); + Object get(int i); + String toString(); +} \ No newline at end of file