org.cdlib.xtf.util
Class FastHashMap<K,V>

Object
  extended by FastHashMap<K,V>

public class FastHashMap<K,V>
extends Object

A fast but inflexible hash table where the keys are strings and the size is fixed. Handles consecutive keys gracefully, but doesn't support resizing, deletion, or iteration.

Author:
Martin Haye

Nested Class Summary
private static class FastHashMap.Ent<K,V>
          Keeps track of a single entry in the hash table.
 
Field Summary
private  int curSize
           
private  FastHashMap.Ent[] ents
           
private  int hashSize
           
static Tester tester
          Basic regression test
 
Constructor Summary
FastHashMap(int maxSize)
          Create the hash table that can comfortably hold the specified number of entries.
 
Method Summary
 boolean contains(K key)
          Checks if the hash contains an entry for the given key.
 V get(K key)
          Retrieves the entry for the given key.
private  int hashSlot(K key)
          Calculate the hash slot for a given key
 void put(K key, V val)
          Sets the entry for the given key number.
 int size()
          Tells how many entries are currently in the hash table
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

hashSize

private final int hashSize

ents

private final FastHashMap.Ent[] ents

curSize

private int curSize

tester

public static final Tester tester
Basic regression test

Constructor Detail

FastHashMap

public FastHashMap(int maxSize)
Create the hash table that can comfortably hold the specified number of entries. The actual table is created to be the smallest prime greater than size*2.

Parameters:
maxSize - Max # of entries
Method Detail

put

public void put(K key,
                V val)
Sets the entry for the given key number. If one already exists, the old value is replaced. Using null for the value can be useful if one only needs to check for key presence using contains().


hashSlot

private final int hashSlot(K key)
Calculate the hash slot for a given key


contains

public boolean contains(K key)
Checks if the hash contains an entry for the given key.


get

public V get(K key)
Retrieves the entry for the given key.

Parameters:
key - Key to look for
Returns:
The associated value, or null if not found.

size

public int size()
Tells how many entries are currently in the hash table