From 01b5ab8fa44bc553a740b04aab338ab04fcc9190 Mon Sep 17 00:00:00 2001 From: stephey Date: Thu, 31 Mar 2011 02:27:46 +0000 Subject: [PATCH] Optimized HashMap/HashSet and added System.gc() for manual garbage collection invokation --- Robust/src/ClassLibrary/HashMap.java | 32 ++++++++++++++++++++-------- Robust/src/ClassLibrary/HashSet.java | 3 +++ Robust/src/ClassLibrary/System.java | 2 ++ 3 files changed, 28 insertions(+), 9 deletions(-) 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>>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(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(); -- 2.34.1