_JAVA AND INTERNET PROGRAMMING_ by Arthur van Hoff Listing One package containers; /* Hashtable collision list. */ class HashtableEntry { int hash; Object key; Object value; HashtableEntry next; } /* Hashtable class. Maps keys to values. Any object can * be used as a key and/or value. * To sucessfully store and retrieve objects from a hash * table the object used as the key must implement the hashCode() * and equals() methods. * This example creates a hashtable of numbers. It uses the names of * the numbers as keys: * * Hashtable numbers = new Hashtable(); * numbers.put("one", new Integer(1)); * numbers.put("two", new Integer(1)); * numbers.put("three", new Integer(1)); * * To retrieve a number use: * Integer n = (Integer)numbers.get("two"); * if (n != null) { * System.out.println("two = " + n); * } */ public final class Hashtable { private HashtableEntry table[]; private int count; private int threshold; private final float loadFactor = 0.75; /* Construct a new, empty hashtable. */ public Hashtable() { table = new HashtableEntry[101]; threshold = (int)(table.length * loadFactor); } /* Return the size of the hashtable */ public int size() { return count; } /* Gets the object associated with a key in the hashtable. */ public synchronized Object get(Object key) { HashtableEntry tab[] = table; int hash = key.hashCode(); int index = (hash & 0x7FFFFFFF) % tab.length; for (HashtableEntry e = tab[index] ; e != null ; e = e.next) { if ((e.hash == hash) && e.key.equals(key)) { return e.value; } } return null; } /* Rehashes the content of the table into a bigger table. */ private void rehash() { int oldCapacity = table.length; HashtableEntry oldTable[] = table; int newCapacity = oldCapacity * 2 + 1; HashtableEntry newTable[] = new HashtableEntry[newCapacity]; threshold = (int)(newCapacity * loadFactor); table = newTable; for (int i = oldCapacity ; i-- > 0 ;) { for (HashtableEntry old = oldTable[i] ; old != null ; ) { HashtableEntry e = old; old = old.next; int index = (e.hash & 0x7FFFFFFF) % newCapacity; e.next = newTable[index]; newTable[index] = e; } } } /* Puts the specified element into the hashtable, using the specified * key. The element may be retrieved by doing a get() with the same key. */ public synchronized Object put(Object key, Object value) { // Make sure the value is not null if (value == null) { throw new NullPointerException(); } // Makes sure the key is not already in the hashtable. HashtableEntry tab[] = table; int hash = key.hashCode(); int index = (hash & 0x7FFFFFFF) % tab.length; for (HashtableEntry e = tab[index] ; e != null ; e = e.next) { if ((e.hash == hash) && e.key.equals(key)) { Object old = e.value; e.value = value; return old; } } if (count >= threshold) { // Rehash the table if the threshold is exceeded rehash(); return put(key, value); } // Creates the new entry. HashtableEntry e = new HashtableEntry(); e.hash = hash; e.key = key; e.value = value; e.next = tab[index]; tab[index] = e; count++; return null; } } Listing Two int sum(int array[]) { int sum = 0; for (int i = array.length ; --i >= 0 ; ) { sum += array[i]; } return sum; } Listing Three Method int sum(int []) 0 iconst 0 ;; sum = 0 1 istore R1 2 aload R0 ;; i = array.length; 3 arraylength 4 istore R2 5 goto 14 8 iload R1 ;; sum += array[i] 9 aload R0 10 iload R2 11 iaload 12 iadd 13 istore R1 14 iinc R2 -1 ;; if (--i >= 0) then goto 8 17 iload R2 18 ifge 8 21 iload R1 ;; return sum 22 ireturn Listing Four Method int sum(int []) save %sp, -96, %sp tst %i0 ;; is the array null? mov 0, %l0 ;; set the sum to 0 teq %0, 16 ;; perform a hardware trap on null ld [%i0 + 4], %g1 ;; get the array length field into %l1 ba test srl %g1, 5, %l1 loop: ld [%i0 + 4], %o0 ;; get the array length into %g4 srl %o0, 5, %g4 sll %l1, 2, %g3 ;; convert i into an offset cmp %g4, %l1 ;; is the index out of range tltu %0, 17 ;; perform a hardware trap if it is ld [%g2 + %g3],%g1 ;; get the value add %l0, %g1, %l0 ;; add it into the sum test: addcc %l1, -1, %l1 ;; add -1 to the index bge loop ;; loop if necessary nop jmpl %i7 + 8 ;; return the value now in %l0 restore %g0, %l0, %o0 Listing Five class FileInputStream extends InputStream { private int fd = -1; /** Open a file for reading. */ private native int open(String name); /** Create an input file given a file name. */ public FileInputStream(String name) { fd = open(name); } /** Read a byte. */ public native int read(); /** Close the input stream. */ public native void close(); /** Close the stream when the stream is finalized. */ protected void finalize() { close(); } }