/************************************************************************************ * @file DirectMap.java * * @author John Miller */ import java.io.*; import java.lang.reflect.Array; import static java.lang.System.out; import java.util.*; /************************************************************************************ * This class provides direct maps for looking up a value given a key. * A direct mapping table is created that is an expandable array-list of buckets. */ public class DirectMap extends AbstractMap implements Serializable, Cloneable, Map { /** The debug flag */ private static final boolean DEBUG = true; /** The class for type V. */ private final Class classV; /** The number of home buckets */ private int m; /******************************************************************************** * This inner class defines buckets (with one slot) that are stored in the direct mapping table. */ private class Bucket { int nKeys = 0; // 0 or 1 Integer key = null; V value = null; Bucket next = null; V find (Integer k) { if (key != null && key.equals (k)) return value; return null; } // find void add (Integer k, V v) { nKeys = 1; key = k; value = v; } void print () { out.println ("[ " + key + " . ]" ); } } // Bucket inner class /** The list of buckets making up the direct mapping table. */ private final List hTable; /******************************************************************************** * Construct a direct mapping table. * @param classV the class for values (V) */ public DirectMap (Class _classV, int size) { m = size; classV = _classV; hTable = new ArrayList <> (); for (var i = 0; i < m; i++) hTable.add (new Bucket ()); } // constructor /******************************************************************************** * Return a set containing all the entries as pairs of keys and values. * @return the set view of the map */ public Set > entrySet () { var enSet = new HashSet > (); // T O B E I M P L E M E N T E D return enSet; } // entrySet /******************************************************************************** * Given the key, look up the value in the direct mapping table. * @param key the key used for look up * @return the value associated with the key */ @SuppressWarnings("unchecked") public V get (Object key) { var i = h (key); var bh = hTable.get (i); return bh.find ((Integer) key); } // get /******************************************************************************** * Put the key-value pair in the direct mapping table. * @param key the key to insert * @param value the value to insert * @return the old/previous value, null if none */ public V put (Integer key, V value) { var i = h (key); // hash to i-th bucket chain var bh = hTable.get (i); // access home bucket var oldV = bh.find (key); // find old value associated with key out.println ("DirectMap.put: key = " + key + ", h() = " + i + ", value = " + value); bh.add (key, value); // put key-value pair in bucket return oldV; } // put /******************************************************************************** * Print the direct mapping table. */ public void print () { out.println ("DirectMap"); out.println ("-------------------------------------------"); for (var i = 0; i < hTable.size (); i++) { out.print ("Bucket [ " + i + " ] = "); var j = 0; for (var b = hTable.get (i); b != null; b = b.next) { if (j > 0) out.print (" \t\t --> "); b.print (); j++; } // for } // for out.println ("-------------------------------------------"); } // print /******************************************************************************** * Hash the key using hash function (identity function for direct maps). * @param key the key to hash * @return the location of the bucket chain containing the key-value pair */ private int h (Object key) { return (int) key; } /******************************************************************************** * The main method used for testing. * @param the command-line arguments (args [0] gives number of keys to insert) */ public static void main (String [] args) { var totalKeys = 40; var RANDOMLY = false; DirectMap ht = new DirectMap <> (Integer.class, totalKeys + 10); if (args.length == 1) totalKeys = Integer.valueOf (args [0]); if (RANDOMLY) { var rng = new Random (); for (var i = 1; i <= totalKeys; i += 2) ht.put (rng.nextInt (2 * totalKeys), i * i); } else { for (var i = 1; i <= totalKeys; i += 2) ht.put (i, i * i); } // if ht.print (); for (var i = 0; i <= totalKeys; i++) { out.println ("key = " + i + " value = " + ht.get (i)); } // for out.println ("-------------------------------------------"); } // main } // DirectMap class