package edu.vt.marian.search; import java.util.*; import java.io.*; import java.util.*; import edu.vt.marian.common.*; /** * Provides hash table functionality for (singly-linked) weighted * objects keyed on FullIDs. This class utilises the FullIDTable * class to provide the basic hash table functionality, but also * allows an implicit "threading" of the weighted objects in the hash * table. So, the hash table allows direct access to any part of the * logical ordering of the items in the table, but the "next" * "pointers" stored in each hashed object allow the next logical item * in the ordering to be retrieved. *
* This method implicitly relies upon the hashCode method to be * defined in the object that is going to be used as a key object. * * @author Paul Mather * @version 0.9 * @see edu.vt.marian.common.WtdObjSingleLink * @see edu.vt.marian.common.WtdObj * @see java.util.Hashtable * @see edu.vt.marian.common.Debug */ public class WtdObjTable extends FullIDTable { /** * Create an empty hash table for (singly-linked) weighted objects */ public WtdObjTable(Debug debug) { super(debug); } /** * Create an empty hash table for (singly-linked) weighted objects * specifying initial capacity and load factor. * @param initialCapacity initial size of hash table * @param loadFactor occupancy threshold before growth needed * @param debug debugging object */ public WtdObjTable(int initialCapacity, float loadFactor, Debug debug) { super(initialCapacity, loadFactor, debug); } /** * Create an empty hash table for (singly-linked) weighted objects * specifying initial capacity, but with default load factor. * @param initialCapacity initial size of hash table * @param debug debugging object */ public WtdObjTable(int initialCapacity, Debug debug) { super(initialCapacity, debug); } /** * Return the next weighted object in this "threaded" hash * table based upon the given weighted object from the table. * The FullID of the WtdObj passed in is used to derive the * key of the item. It is expected that both the WtdObj * and the WtdObj it points to exist in the table for a * non-null result to be returned. *
* This is really just a wrapper for the same method that * takes a FullID key as its argument. * @param wtdObj (singly-linked) weighted object in table * @return the WtdObj logically "pointed" to by the given * WtdObj in the table */ public WtdObj getNext(WtdObj wtdObj) { if (wtdObj == null) return null; else return getNext( wtdObj.getID() ); } /** * Return the next weighted object in this "threaded" hash table * based upon the supplied FullID key. It is expected that both * the WtdObj keyed by the supplied FullID, and the WtdObj pointed * to exist in the table for a non-null result to be returned. * @param wtdObj (singly-linked) weighted object in table * @return the WtdObj logically "pointed" to by the given WtdObj * in the table */ public WtdObj getNext(FullID itemKey) { WtdObjSingleLink itemHashed = null; WtdObj nextWtdObj = null; if (itemKey != null) { // Look up WtdObjSingleLink hashed by this FullID itemHashed = (WtdObjSingleLink) get(itemKey); // Check whether the key actually hashed to something if (itemHashed == null) // There was no item in the table! debug.dumpTrace("WtdObjTable.getNext[FullID]: Supplied " + "FullID not in table!"); else { // Now look up the item pointed to by the "next" // "pointer" of this WtdObjSingleLink itemHashed = (WtdObjSingleLink) get(itemHashed.getNext()); if (itemHashed != null) { // The "next" "pointer" hashed to an extant item in the // table, so return the WtdObj part nextWtdObj = itemHashed.getWtdObj(); } } } return nextWtdObj; } /** * Return the WtdObj keyed by the given FullID. This is really * just wrapping around the "raw" access method of get(). * @param key FullID key of hashed object * @return WtdObj of item hashed to by key, or null if the key * was not in the table. * @see java.util.Hashtable#get * @see edu.vt.marian.common.WtdObjTable * @see edu.vt.marian.common.WtdObjSingleLink */ public WtdObj retrieve( FullID key ) { WtdObjSingleLink itemHashed; itemHashed = (WtdObjSingleLink) get( key ); if (itemHashed != null) // Return a copy of the WtdObj portion return itemHashed.getWtdObj(); else return null; } /** * Return the WtdObj keyed by the given WtdObj. This is really * just wrapping around the "retrieve(FullID)" method above. * @param key FullID key of hashed object * @return WtdObj of item hashed to by key, or null if the key * was not in the table. * @see java.util.Hashtable#get * @see edu.vt.marian.common.WtdObjTable * @see edu.vt.marian.common.WtdObjSingleLink */ public WtdObj retrieve( WtdObj key ) { if (key != null) // Retrieve the WtdObj using the key from the supplied WtdObj return retrieve(key.getID()); else return null; } /** * Return an enumeration for the elements of this hash table of * weighted objects. Because there is no real guarantee of * ordering, we simply use the default Hashtable elements() method, * but modify nextElement() to unpackage the hashed * WtdObjSingleLink object to return just its WtdObj component. * @return enumeration object for this weighted object hash table */ public Enumeration elements() { /** * Local class to allow iterating over hashed items in * weighted object hash table * @author Paul Mather * @version 0.9 * @see edu.vt.marian.common.WtdObjSingleLink * @see edu.vt.marian.common.WtdObj * @see java.util.Hashtable */ class WtdObjTableEnum implements Enumeration { /** * The hashed WtdObjSingleLink elements in this * WtdObjTable */ Enumeration elts; public WtdObjTableEnum(Hashtable outer) { // Get an enumeration of the elements in this table // elts = Hashtable.this.elements(); elts = outer.elements(); } /** * Return true if this table enumeration has more * elements, else return false. * @return boolean denoting whether there are elements * remaining in this table's enumeration * @see java.util.Enumeration#hasMoreElements */ public boolean hasMoreElements() { return elts.hasMoreElements(); } /** * Return the next weighted object element in this table's * enumeration. * @return Object that is the next in the sequence * @see java.util.Enumeration#nextElement * @see edu.vt.marian.common.WtdObjSingleLink#getWtdObj */ public Object nextElement() throws NoSuchElementException { WtdObjSingleLink hashedItem; hashedItem = (WtdObjSingleLink) elts.nextElement(); return hashedItem.getWtdObj(); } } return new WtdObjTableEnum(this); } }