From: stephey <stephey>
Date: Thu, 31 Mar 2011 02:27:46 +0000 (+0000)
Subject: Optimized HashMap/HashSet and added System.gc() for manual garbage collection invokation
X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=01b5ab8fa44bc553a740b04aab338ab04fcc9190;p=IRC.git

Optimized HashMap/HashSet and added System.gc() for manual garbage collection invokation
---

diff --git a/Robust/src/ClassLibrary/HashMap.java b/Robust/src/ClassLibrary/HashMap.java
index 158bb253..79ecb6d5 100644
--- a/Robust/src/ClassLibrary/HashMap.java
+++ b/Robust/src/ClassLibrary/HashMap.java
@@ -2,6 +2,7 @@ public class HashMap{
   HashEntry[] table;
   float loadFactor;
   int numItems;
+  int threshold;
 
   public HashMap() {
     init(16, 0.75f);
@@ -16,24 +17,31 @@ public class HashMap{
   }
 
   private void init(int initialCapacity, float loadFactor) {
-    table=new HashEntry[initialCapacity];
+    table=new HashEntry[computeCapacity(initialCapacity)];
     this.loadFactor=loadFactor;
     this.numItems=0;
+    this.threshold=(int)(loadFactor*table.length);
+  }
+  
+  private static int computeCapacity(int capacity) {
+      int x=16;
+      while(x<capacity)
+	  x=x<<1;
+      return x;
   }
 
   private static int hash(Object o, int length) {
-    if (o==null)
-      return 0;
-    int value=o.hashCode()%length;
-    if (value<0)
-      return -value;
-    return value;
+      int orig=o.hashCode();
+      orig=orig^(orig>>>22)^(orig>>>10);
+      orig=orig^(orig>>>8)^(orig>>4);
+      return orig&(length-1);
   }
 
   void resize() {
-    int newCapacity=2*table.length+1;
+    int newCapacity=table.length<<1;
     HashEntry[] oldtable=table;
     this.table=new HashEntry[newCapacity];
+    this.threshold=(int) (newCapacity*loadFactor);
 
     for(int i=0; i<oldtable.length; i++) {
       HashEntry e=oldtable[i];
@@ -47,6 +55,12 @@ public class HashMap{
     }
   }
 
+    public void clear() {
+	for(int i=0;i<table.length;i++)
+	    table[i]=null;
+	numItems=0;
+    }
+
   public boolean isEmpty() {
     return numItems==0;
   }
@@ -108,7 +122,7 @@ public class HashMap{
 
   Object put(Object key, Object value) {
     numItems++;
-    if (numItems>(loadFactor*table.length)) {
+    if (numItems>threshold) {
       //Resize the table
       resize();
     }
diff --git a/Robust/src/ClassLibrary/HashSet.java b/Robust/src/ClassLibrary/HashSet.java
index 4145f636..4e4a1bcb 100644
--- a/Robust/src/ClassLibrary/HashSet.java
+++ b/Robust/src/ClassLibrary/HashSet.java
@@ -18,6 +18,9 @@ public class HashSet {
   public boolean isEmpty() {
     return map.isEmpty();
   }
+  public void clear() {
+    map.clear();
+  }
   public boolean contains(Object o) {
     return map.containsKey(o);
   }
diff --git a/Robust/src/ClassLibrary/System.java b/Robust/src/ClassLibrary/System.java
index e36beaee..f02562b1 100644
--- a/Robust/src/ClassLibrary/System.java
+++ b/Robust/src/ClassLibrary/System.java
@@ -4,6 +4,8 @@ public class System {
     printString(s);
   }
 
+    public static native void gc();
+
   public static native long currentTimeMillis();
   
   public static native long microTimes();