data_structures
Class SHashTable<K,V>

java.lang.Object
  extended by data_structures.HashTable<K,V>
      extended by data_structures.SHashTable<K,V>
Type Parameters:
K - the type of object that will act as keys for the hash table
V - the type of object that is being stored in the hash table
All Implemented Interfaces:
java.lang.Cloneable

public class SHashTable<K,V>
extends HashTable<K,V>

A simple static HashTable.

In general, the time for each operation is as follows:

Author:
Stephen G. Ware

Field Summary
 
Fields inherited from class data_structures.HashTable
DEFAULT_EXPANSION_FACTOR, DEFAULT_INITIAL_SIZE, DEFAULT_LOAD_FACTOR_THRESHOLD, size, table
 
Constructor Summary
  SHashTable()
          Construct an empty static hash table.
protected SHashTable(int size, data_structures.HashTable.Node<K,V>[] table)
          Construct a static hash table with a given number of mappings and given set of buckets.
  SHashTable(K[] keys, V[] values)
          Construct a static hash table which initially contains mappings for a given set of keys and values.
 
Method Summary
 SHashTable<K,V> add(K[] keys, V[] values)
          Add multiple key/value mappings to the static hash table.
 SHashTable<K,V> add(K key, V value)
          Add a key/value mapping to the static hash table.
 SHashTable<K,V> addEquals(K[] keys, V[] values)
          Add multiple key/value mappings to the static hash table.
 SHashTable<K,V> addEquals(K key, V value)
          Add a key/value mapping to the static hash table.
 SHashTable<K,V> clone()
          Return a deep copy of the static hash table.
protected  SHashTable<K,V> makeHashTable(int size, data_structures.HashTable.Node<K,V>[] table)
          This protected method is used instead of a constructor to create new hash tables when this hash table is modified.
 SHashTable<K,V> remove(K key)
          Remove a key/value mapping from the static hash table.
 SHashTable<K,V> remove(K[] keys)
          Remove multiple key/value mappings from the static hash table.
 SHashTable<K,V> removeEquals(K key)
          Remove a key/value mapping from the static hash table.
 SHashTable<K,V> removeEquals(K[] keys)
          Remove multiple key/value mappings from the static hash table.
 
Methods inherited from class data_structures.HashTable
cloneTable, equals, get, getBucket, getEquals, getLoadFactor, keys, rehash, rehash, size, toString, values
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SHashTable

protected SHashTable(int size,
                     data_structures.HashTable.Node<K,V>[] table)

Construct a static hash table with a given number of mappings and given set of buckets.

Parameters:
size - the number of mappings in the hash table
table - the buckets

SHashTable

public SHashTable()

Construct an empty static hash table.


SHashTable

public SHashTable(K[] keys,
                  V[] values)

Construct a static hash table which initially contains mappings for a given set of keys and values.

Mappings are added as if using add(K, V).

This constructor is more efficient in both time and space than creating an empty static hash table and then adding the key/value mappings.

Parameters:
keys - the objects which can be used to retrieve the values
values - the objects to be retrieved using the keys
Throws:
java.lang.ArrayIndexOutOfBoundsException - if the array of keys is not the same length as the array of values
Method Detail

makeHashTable

protected SHashTable<K,V> makeHashTable(int size,
                                        data_structures.HashTable.Node<K,V>[] table)

This protected method is used instead of a constructor to create new hash tables when this hash table is modified.

All subclasses of SHashTable should use this method instead of a constructor. This method can be overridden in a subclass and made to return objects of the subclass's type. This avoids the new to re-write the modifying methods such as add(K, V) and remove(K) in every subclass.

Parameters:
size - the number of mappings in the hash table
table - the buckets

add

public SHashTable<K,V> add(K key,
                           V value)

Add a key/value mapping to the static hash table. If a mapping exists whose key is == to this key, it is replaced.

Specified by:
add in class HashTable<K,V>
Parameters:
key - the object which can be used to retrieve the value
value - the object to be retrieved using the key
Returns:
a clone of the hash table with the mapping added

add

public SHashTable<K,V> add(K[] keys,
                           V[] values)

Add multiple key/value mappings to the static hash table. If mappings exist whose keys are == to the given keys, they are replaced.

This method is more efficient in both time and space than making multiple calls to add(K, V).

Specified by:
add in class HashTable<K,V>
Parameters:
keys - the objects which can be used to retrieve the values
values - the objects to be retrieved using the keys
Returns:
a clone of the hash table with the mappings added
Throws:
java.lang.ArrayIndexOutOfBoundsException - if the array of keys is not the same length as the array of values

addEquals

public SHashTable<K,V> addEquals(K key,
                                 V value)

Add a key/value mapping to the static hash table. If a mapping exists whose key Object.equals(java.lang.Object) this key, it is replaced.

Specified by:
addEquals in class HashTable<K,V>
Parameters:
key - the object which can be used to retrieve the value
value - the object to be retrieved using the key
Returns:
a clone of the hash table with the mapping added

addEquals

public SHashTable<K,V> addEquals(K[] keys,
                                 V[] values)

Add multiple key/value mappings to the static hash table. If mappings exist whose keys are Object.equals(java.lang.Object) to the given keys, they are replaced.

This method is more efficient in both time and space than multiple calls to addEquals(K, V).

Specified by:
addEquals in class HashTable<K,V>
Parameters:
keys - the objects which can be used to retrieve the values
values - the objects to be retrieved using the keys
Returns:
a clone of the hash table with the mappings added
Throws:
java.lang.ArrayIndexOutOfBoundsException - if the array of keys is not the same length as the array of values

remove

public SHashTable<K,V> remove(K key)

Remove a key/value mapping from the static hash table. Keys are searched using ==.

Specified by:
remove in class HashTable<K,V>
Parameters:
key - the key of the key/value mapping to be removed
Returns:
a clone of the hash table with the mapping removed

remove

public SHashTable<K,V> remove(K[] keys)

Remove multiple key/value mappings from the static hash table. Keys are searched using ==.

This method is more efficient in both time and space than multiple calls to remove(K).

Specified by:
remove in class HashTable<K,V>
Parameters:
keys - the keys of the key/value mappings to be removed
Returns:
a clone of the hash table with the mappings removed

removeEquals

public SHashTable<K,V> removeEquals(K key)

Remove a key/value mapping from the static hash table. Keys are searched using Object.equals(java.lang.Object).

Specified by:
removeEquals in class HashTable<K,V>
Parameters:
key - the key of the key/value mapping to be removed
Returns:
a clone of the hash table with the mapping removed

removeEquals

public SHashTable<K,V> removeEquals(K[] keys)

Remove multiple key/value mappings from the static hash table. Keys are searched using Object.equals(java.lang.Object).

This method is more efficient in both time and space than multiple calls to removeEquals(K).

Specified by:
removeEquals in class HashTable<K,V>
Parameters:
keys - the keys of the key/value mappings to be removed
Returns:
a hash table exactly like this one, but with the mappings removed

clone

public SHashTable<K,V> clone()

Return a deep copy of the static hash table.

The clone hash table can be modified without affecting the original.

Specified by:
clone in class HashTable<K,V>