From 7dd95dce931810ddaf14f3ad1e64c4d0d18974e6 Mon Sep 17 00:00:00 2001 From: jzhou Date: Sat, 12 Feb 2011 00:40:45 +0000 Subject: [PATCH] Make special MGC versions of some library class like HashMap and Vector etc. to avoid braking compilation of non-MGC build --- Robust/src/ClassLibrary/HashMap.java | 6 +- Robust/src/ClassLibrary/MGC/HashMap.java | 132 +++++++++++++++++ Robust/src/ClassLibrary/{ => MGC}/Map.java | 0 Robust/src/ClassLibrary/MGC/Math.java | 138 ++++++++++++++++++ Robust/src/ClassLibrary/{ => MGC}/Set.java | 0 Robust/src/ClassLibrary/MGC/System.java | 104 ++++++++++++++ Robust/src/ClassLibrary/MGC/Vector.java | 157 +++++++++++++++++++++ Robust/src/ClassLibrary/Math.java | 2 - Robust/src/ClassLibrary/System.java | 17 +-- Robust/src/ClassLibrary/Vector.java | 26 +--- 10 files changed, 536 insertions(+), 46 deletions(-) create mode 100644 Robust/src/ClassLibrary/MGC/HashMap.java rename Robust/src/ClassLibrary/{ => MGC}/Map.java (100%) create mode 100644 Robust/src/ClassLibrary/MGC/Math.java rename Robust/src/ClassLibrary/{ => MGC}/Set.java (100%) create mode 100644 Robust/src/ClassLibrary/MGC/System.java create mode 100644 Robust/src/ClassLibrary/MGC/Vector.java diff --git a/Robust/src/ClassLibrary/HashMap.java b/Robust/src/ClassLibrary/HashMap.java index 780159d9..158bb253 100644 --- a/Robust/src/ClassLibrary/HashMap.java +++ b/Robust/src/ClassLibrary/HashMap.java @@ -1,4 +1,4 @@ -public class HashMap implements Map { +public class HashMap{ HashEntry[] table; float loadFactor; int numItems; @@ -56,8 +56,8 @@ public class HashMap implements Map { } /* 0=keys, 1=values */ - public Iterator iterator(int type) { - return (Iterator)(new HashMapIterator(this, type)); + public HashMapIterator iterator(int type) { + return (new HashMapIterator(this, type)); } Object remove(Object key) { diff --git a/Robust/src/ClassLibrary/MGC/HashMap.java b/Robust/src/ClassLibrary/MGC/HashMap.java new file mode 100644 index 00000000..780159d9 --- /dev/null +++ b/Robust/src/ClassLibrary/MGC/HashMap.java @@ -0,0 +1,132 @@ +public class HashMap implements Map { + HashEntry[] table; + float loadFactor; + int numItems; + + public HashMap() { + init(16, 0.75f); + } + + public HashMap(int initialCapacity) { + init(initialCapacity, 0.75f); + } + + public HashMap(int initialCapacity, float loadFactor) { + init(initialCapacity, loadFactor); + } + + private void init(int initialCapacity, float loadFactor) { + table=new HashEntry[initialCapacity]; + this.loadFactor=loadFactor; + this.numItems=0; + } + + 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; + } + + void resize() { + int newCapacity=2*table.length+1; + HashEntry[] oldtable=table; + this.table=new HashEntry[newCapacity]; + + for(int i=0; i(loadFactor*table.length)) { + //Resize the table + resize(); + } + int bin=hash(key, table.length); + HashEntry ptr=table[bin]; + while(ptr!=null) { + if (ptr.key.equals(key)) { + Object oldvalue=ptr.value; + ptr.value=value; + return oldvalue; + } + ptr=ptr.next; + } + HashEntry he=new HashEntry(); + he.value=value; + he.key=key; + he.next=table[bin]; + table[bin]=he; + return null; + } +} diff --git a/Robust/src/ClassLibrary/Map.java b/Robust/src/ClassLibrary/MGC/Map.java similarity index 100% rename from Robust/src/ClassLibrary/Map.java rename to Robust/src/ClassLibrary/MGC/Map.java diff --git a/Robust/src/ClassLibrary/MGC/Math.java b/Robust/src/ClassLibrary/MGC/Math.java new file mode 100644 index 00000000..878eb0b2 --- /dev/null +++ b/Robust/src/ClassLibrary/MGC/Math.java @@ -0,0 +1,138 @@ +public class Math { + + public static double setPI() { + double PI = 3.14159265358979323846; + return PI; + } + + // an alias for setPI() + public static double PI() { + double PI = 3.14159265358979323846; + return PI; + } + + public static int abs(int x) { + if (x < 0) { + return -x; + } else { + return x; + } + } + + public static double fabs(double x) { + if (x < 0) { + return -x; + } else { + return x; + } + } + + public static double abs(double x) { + if (x < 0) { + return -x; + } else { + return x; + } + } + + public static float abs(float a) { + if (a<0) + return -a; + else return a; + } + + public static double max(double a, double b) { + if(a == b) + return a; + if(a > b) { + return a; + } else { + return b; + } + } + + public static int max(int a, int b) { + if(a == b) + return a; + if(a > b) { + return a; + } else { + return b; + } + } + + public static int imax(int a, int b) { + if(a == b) + return a; + if(a > b) { + return a; + } else { + return b; + } + } + + public static int imin(int a, int b) { + if(a == b) + return a; + if(a > b) { + return b; + } else { + return a; + } + } + + /** sqrt(a^2 + b^2) without under/overflow. **/ + public static double hypot(double a, double b) { + double r; + if (fabs(a) > fabs(b)) { + r = b/a; + r = fabs(a)*sqrt(1+r*r); + } else if (b != 0) { + r = a/b; + r = fabs(b)*sqrt(1+r*r); + } else { + r = 0.0; + } + return r; + } + + public static double rint(double x) { + double y = ceil(x); + double d = y - x; + if( d == 0.5 ) { + if( ((int)y)%2 == 0 ) { + return y; + } else { + return y - 1.0; + } + } else if( d < 0.5 ) { + return y; + } + return y - 1.0; + } + + public static native double sin(double a); + public static native double cos(double a); + public static native double asin(double a); + public static native double acos(double a); + public static native double tan(double a); + public static native double atan(double a); + public static native double atan2(double a, double b); + public static native double exp(double a); + public static native double sqrt(double a); + public static native double log(double a); + public static native double pow(double a, double b); + + public static native double ceil(double a); + public static native double floor(double a); + + public static native float sinf(float a); + public static native float cosf(float a); + public static native float expf(float a); + public static native float sqrtf(float a); + public static native float logf(float a); + public static native float powf(float a, float b); + public static native float ceilf(float a); + + public static native float IEEEremainder(float f1, float f2); +} diff --git a/Robust/src/ClassLibrary/Set.java b/Robust/src/ClassLibrary/MGC/Set.java similarity index 100% rename from Robust/src/ClassLibrary/Set.java rename to Robust/src/ClassLibrary/MGC/Set.java diff --git a/Robust/src/ClassLibrary/MGC/System.java b/Robust/src/ClassLibrary/MGC/System.java new file mode 100644 index 00000000..74a6c66f --- /dev/null +++ b/Robust/src/ClassLibrary/MGC/System.java @@ -0,0 +1,104 @@ +import java.util.Properties; + +public class System { + PrintStream out; + + public System() { + out = new PrintStream("System.out"); + } + + public static void printInt(int x) { + String s=String.valueOf(x); + printString(s); + } + + public static native long currentTimeMillis(); + + public static native long microTimes(); + + public static native long getticks(); + + public static native void printString(String s); + + public static void println(String s) { + System.printString(s+"\n"); + } + + public static void println(Object o) { + System.printString(""+o+"\n"); + } + + public static void println(int o) { + System.printString(""+o+"\n"); + } + + public static void println(double o) { + System.printString(""+o+"\n"); + } + + public static void println(long o) { + System.printString(""+o+"\n"); + } + + public static void println() { + System.printString("\n"); + } + + public static void print(String s) { + System.printString(s); + } + + public static void print(Object o) { + System.printString(""+o); + } + + public static void print(int o) { + System.printString(""+o); + } + + public static void print(double o) { + System.printString(""+o); + } + + public static void print(long o) { + System.printString(""+o); + } + + public static void error() { + System.printString("Error (Use Breakpoint on ___System______error method for more information!)\n"); + } + + public static native void exit(int status); + + public static native void printI(int status); + + public static native void clearPrefetchCache(); + + public static native void rangePrefetch(Object o, short[] offsets); + + public static native void deepArrayCopy(Object dst, Object src); + + public static native void Assert(boolean status); + + /* Only used for microbenchmark testing of SingleTM version */ + public static native void logevent(int event); + public static native void logevent(); + + /* Only used for microbenchmark testing of SingleTM version */ + public static native void initLog(); + + public static native void flushToFile(int threadid); + /* Only used for microbenchmark testing of SingleTM version */ + + public static native void arraycopy(Object src, int srcPos, Object dst, int destPos, int length); + + // for disjoint reachability analysis + public static void genReach(); + + private static Properties props; + private static native Properties initProperties(Properties props); + + public static Properties getProperties() { + return props; + } +} diff --git a/Robust/src/ClassLibrary/MGC/Vector.java b/Robust/src/ClassLibrary/MGC/Vector.java new file mode 100644 index 00000000..5f506edf --- /dev/null +++ b/Robust/src/ClassLibrary/MGC/Vector.java @@ -0,0 +1,157 @@ +public class Vector implements Set { + Object[] array; + int size; + int capacityIncrement; + + public Vector() { + capacityIncrement=0; + size=0; + array=new Object[10]; + } + + public Vector(int size) { + capacityIncrement=0; + this.size=0; + array=new Object[size]; + } + + public boolean isEmpty() { + return size==0; + } + + public void clear() { + size=0; + array=new Object[10]; + } + + public int indexOf(Object elem) { + return indexOf(elem, 0); + } + + public int indexOf(Object elem, int index) { + for(int i=index; i=size) { + System.printString("Illegal Vector.elementAt\n"); + System.exit(-1); + return null; + } + return array[index]; + } + + public void setElementAt(Object obj, int index) { + if (index array.length) { + int newsize; + if (capacityIncrement<=0) + newsize=array.length*2; + else + newsize=array.length+capacityIncrement; + if (newsizesize) { + System.printString("Illegal Vector.insertElementAt\n"); + System.exit(-1); + } + + if (size==array.length) { + ensureCapacity(size+1); + } + size++; + for(int i=size-1; i>index; --i) { + array[i] = array[i-1]; + } + array[index] = obj; + } + + public void removeElementAt(int index) { + if (index<0||index>=size) { + System.printString("Illegal Vector.removeElementAt\n"); + System.exit(-1); + } + removeElement(array, index, size); + size--; + array[size]=null; + } + + public static native void removeElement(Object[] array, int index, int size); + + public void removeAllElements() { + int s = size; + for(int i = 0; isize) { @@ -138,20 +130,4 @@ public class Vector implements Set { removeElementAt(0); } } - - public Object[] toArray() { - Object[] tarray = new Object[this.size]; - for(int i = 0; i < this.size; i++) { - tarray[i] = this.array[i]; - } - return tarray; - } - - public Vector(Set s) { - Object[] sarray = s.toArray(); - capacityIncrement=0; - this.size=sarray.length; - array=sarray; - } - } -- 2.34.1