|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object sdds.HashTable<K,V>
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
public abstract class HashTable<K,V>
A simple hash-table (the abstract superclass of DHashTable
and SHashTable
).
The performance of each operation varies for DHashTable
and SHashTable
, but in both cases searching for a value based on its key takes O(1) amortized time (assuming the hash codes of keys are uniformly distributed).
Note that key/value mappings can be added to the hash table using ==
or Object.equals(java.lang.Object)
. Using add(K, V)
and addEquals(K, V)
on the same table may put it in an inconsistent state. For example, say that K1 is mapped to V1 using add(K, V)
and K2 is mapped to V2 using add(K, V)
. If K1.equals(K2), remapping K1 using addEquals(K, V)
could overwrite either one of the above mappings.
Field Summary | |
---|---|
protected static int |
DEFAULT_EXPANSION_FACTOR The default number of times larger that the hash table will get when it needs to expand |
protected static int |
DEFAULT_INITIAL_SIZE The default number of mapping that can exist in a new hash table |
protected static float |
DEFAULT_LOAD_FACTOR_THRESHOLD The load factor at or above which the hash table will expand |
protected int |
size The number of mapping in this hash table |
protected sdds.HashTable.Node<K,V>[] |
table The buckets |
Constructor Summary | |
---|---|
|
HashTable() Construct an empty hash table. |
protected |
HashTable(int size, sdds.HashTable.Node<K,V>[] table) Construct a hash table with a given number of mappings and given set of buckets. |
|
HashTable(K[] keys, V[] values) Construct a hash table which initially contains mappings for a given set of keys and values. |
Method Summary | |
---|---|
abstract HashTable<K,V> |
add(K[] keys, V[] values) Add multiple key/value mappings to the hash table. |
abstract HashTable<K,V> |
add(K key, V value) Add a key/value mapping to the hash table. |
abstract HashTable<K,V> |
addEquals(K[] keys, V[] values) Add multiple key/value mappings to the hash table. |
abstract HashTable<K,V> |
addEquals(K key, V value) Add a key/value mapping to the hash table. |
abstract HashTable<K,V> |
clone() Return a new hash table which Object.equals(java.lang.Object) this hash table but is not == to this hash table. |
protected sdds.HashTable.Node<K,V>[] |
cloneTable() Clone the buckets, including every link in every chain. |
boolean |
equals(java.lang.Object other) Check if this hash table is the same as another. |
V |
get(K key) Return the value mapped to a given key. |
protected static int |
getBucket(java.lang.Object obj, sdds.HashTable.Node<?,?>[] table) Given an object that needs to be placed in a bucket, this function return the number of the bucket in which to place the object. |
V |
getEquals(K key) Return the value mapped to a given key. |
protected float |
getLoadFactor() Return the load factor of the hash table. |
java.util.Iterator<K> |
keys() Return an Iterator that will yield all the keys in the hash table. |
protected sdds.HashTable.Node<K,V>[] |
rehash() Expand the buckets into the number of current buckets times DEFAULT_EXPANSION_FACTOR . |
protected sdds.HashTable.Node<K,V>[] |
rehash(int buckets) Expand the buckets into buckets new buckets. |
abstract HashTable<K,V> |
remove(K key) Remove a key/value mapping from the hash table. |
abstract HashTable<K,V> |
remove(K[] keys) Remove multiple key/value mappings from the hash table. |
abstract HashTable<K,V> |
removeEquals(K key) Remove a key/value mapping from the hash table. |
abstract HashTable<K,V> |
removeEquals(K[] keys) Remove multiple key/value mappings from the hash table. |
int |
size() Return the number of mappings in the hash table. |
java.lang.String |
toString() Return a string representation of this hash table and all its elements. |
java.util.Iterator<V> |
values() Return an Iterator that will yield all the values in the hash table. |
Methods inherited from class java.lang.Object |
---|
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
protected static final int DEFAULT_INITIAL_SIZE
protected static final int DEFAULT_EXPANSION_FACTOR
protected static final float DEFAULT_LOAD_FACTOR_THRESHOLD
protected int size
protected sdds.HashTable.Node<K,V>[] table
Constructor Detail |
---|
protected HashTable(int size, sdds.HashTable.Node<K,V>[] table)
Construct a hash table with a given number of mappings and given set of buckets.
size
- the number of mappings in the hash table
table
- the buckets
public HashTable()
Construct an empty hash table.
public HashTable(K[] keys, V[] values)
Construct a hash table which initially contains mappings for a given set of keys and values.
Mappings are added as if using add(K, V)
.
keys
- the objects which can be used to retrieve the values
values
- the objects to be retrieved using the keys
java.lang.ArrayIndexOutOfBoundsException
- if the array of keys is not the same length as the array of values
Method Detail |
---|
protected static final int getBucket(java.lang.Object obj, sdds.HashTable.Node<?,?>[] table)
Given an object that needs to be placed in a bucket, this function return the number of the bucket in which to place the object.
obj
- the object to place
table
- the buckets
protected float getLoadFactor()
Return the load factor of the hash table.
The load factor is defined as the number of mapping divided by the number of buckets.
protected final sdds.HashTable.Node<K,V>[] rehash(int buckets)
Expand the buckets into buckets
new buckets. Re-map all existing mappings to their new buckets.
buckets
- the new number of buckets
protected final sdds.HashTable.Node<K,V>[] rehash()
Expand the buckets into the number of current buckets times DEFAULT_EXPANSION_FACTOR
. Re-map all existing mappings to their new buckets.
public int size()
Return the number of mappings in the hash table.
public abstract HashTable<K,V> add(K key, V value)
Add a key/value mapping to the hash table. If a mapping exists whose key is ==
to this key, it is replaced.
key
- the object which can be used to retrieve the value
value
- the object to be retrieved using the key
public abstract HashTable<K,V> add(K[] keys, V[] values)
Add multiple key/value mappings to the hash table. If mappings exist whose keys are ==
to the given keys, they are replaced.
keys
- the objects which can be used to retrieve the values
values
- the objects to be retrieved using the keys
java.lang.ArrayIndexOutOfBoundsException
- if the array of keys is not the same length as the array of values
public abstract HashTable<K,V> addEquals(K key, V value)
Add a key/value mapping to the hash table. If a mapping exists whose key Object.equals(java.lang.Object)
this key, it is replaced.
key
- the object which can be used to retrieve the value
value
- the object to be retrieved using the key
public abstract HashTable<K,V> addEquals(K[] keys, V[] values)
Add multiple key/value mappings to the hash table. If mappings exist whose keys are Object.equals(java.lang.Object)
to the given keys, they are replaced.
keys
- the objects which can be used to retrieve the values
values
- the objects to be retrieved using the keys
java.lang.ArrayIndexOutOfBoundsException
- if the array of keys is not the same length as the array of values
public V get(K key)
Return the value mapped to a given key. Keys are searched using ==
.
key
- the object that was mapped to the desired value
public V getEquals(K key)
Return the value mapped to a given key. Keys are searched using Object.equals(java.lang.Object)
.
key
- the object that was mapped to the desired value
public abstract HashTable<K,V> remove(K key)
Remove a key/value mapping from the hash table. Keys are searched using ==
.
key
- the key of the key/value mapping to be removed
public abstract HashTable<K,V> remove(K[] keys)
Remove multiple key/value mappings from the hash table. Keys are searched using ==
.
keys
- the keys of the key/value mappings to be removed
public abstract HashTable<K,V> removeEquals(K key)
Remove a key/value mapping from the hash table. Keys are searched using Object.equals(java.lang.Object)
.
key
- the key of the key/value mapping to be removed
public abstract HashTable<K,V> removeEquals(K[] keys)
Remove multiple key/value mappings from the hash table. Keys are searched using Object.equals(java.lang.Object)
.
keys
- the keys of the key/value mappings to be removed
public java.util.Iterator<K> keys()
Return an Iterator
that will yield all the keys in the hash table.
public java.util.Iterator<V> values()
Return an Iterator
that will yield all the values in the hash table.
public boolean equals(java.lang.Object other)
Check if this hash table is the same as another.
Two hash tables are considered the same if they contain the same key/value mappings. Keys and values are compared using Object.equals(java.lang.Object)
.
The class of the hash table is not taken into account. In other words, a DHashTable
can be equal to an SHashTable
if they contain the same key/value mappings.
equals
in class
java.lang.Object
other
- another hash table
public abstract HashTable<K,V> clone()
Return a new hash table which Object.equals(java.lang.Object)
this hash table but is not ==
to this hash table.
clone
in class
java.lang.Object
protected sdds.HashTable.Node<K,V>[] cloneTable()
Clone the buckets, including every link in every chain.
public java.lang.String toString()
Return a string representation of this hash table and all its elements.
toString
in class
java.lang.Object