Skip to content

Instantly share code, notes, and snippets.

@cdduarte
Created June 5, 2014 04:13
Show Gist options
  • Save cdduarte/df0c05d46156f6c78ee0 to your computer and use it in GitHub Desktop.
Save cdduarte/df0c05d46156f6c78ee0 to your computer and use it in GitHub Desktop.
/**
* MyHashTable - implementation of generic Hash Table class,
* similar to Java HashMap<K,V> class.
*
* @author christopherduarte
* @reference robertirwin
*
* CIS551
* Lab16
*/
import java.util.LinkedList;
import java.util.ListIterator;
interface KeyedValue<K>
{
public K getKey();
}
@SuppressWarnings("unchecked") // prevents warnings for casts
public class MyHashMap<K extends Comparable<K>,V extends KeyedValue<K>>
{
private int tableSize = 0;
private int count = 0;
private Object[] table;
/*********************************************************************
* Constructors
*********************************************************************/
MyHashMap() {
this.table = new Object[16];
this.tableSize = 16;
this.count = 0;
for(int i = 0; i < this.tableSize; ++i)
this.table[i] = null;
}
MyHashMap(int initialCapacity) throws IllegalArgumentException {
if ( initialCapacity < 0 )
throw new IllegalArgumentException();
this.table = new Object[initialCapacity];
this.tableSize = initialCapacity;
this.count = 0;
for(int i = 0; i < this.tableSize; ++i)
this.table[i] = null;
}
public V search(K key)
{
int i = Math.abs(key.hashCode() % this.tableSize); // hash key to get chain to search
if ( this.table[i] == null )
return null;
ListIterator<V> li =
((LinkedList<V>)this.table[i]).listIterator(0);
while ( li.hasNext() ) {
V it;
it = li.next();
if ( key.compareTo(it.getKey()) == 0 )
return it;
}
return null;
}
public void insert(V value)
{
int i = Math.abs(value.getKey().hashCode() % this.tableSize); // hash item's key
// create chain, if needed
if ( this.table[i] == null )
this.table[i] = new LinkedList<V>();
((LinkedList<V>)this.table[i]).addFirst(value); // insert at head of list (O(1))
++this.count;
}
public void delete(V value)
{
int i = Math.abs(value.getKey().hashCode() % this.tableSize); // hash item's key
if ( this.table[i] == null )
return;
if ( ((LinkedList<V>)this.table[i]).remove(value) ) // O(n/m)
--this.count;
--this.tableSize;
}
public void clear() {
Object[] tab = table;
for (int i = 0; i < tab.length; i++)
tab[i] = null;
++this.count;
this.tableSize = 0;
}
public Object clone() {
MyHashMap<K,V> result = null;
result.table = new Object[table.length];
result.count = this.count;
result.tableSize = this.tableSize;
return result;
}
public boolean containsKey(Object key) {
int i = Math.abs(key.hashCode() % this.tableSize);
return this.table[i] != null;
}
public V get(K key) {
V value = search(key);
return value;
}
public boolean isEmpty() {
return tableSize == 0;
}
public V put(K key, V value) {
int i = Math.abs(key.hashCode() % this.tableSize);
V tmp = search(key);
if ( tmp == null) {
return null;
}
this.table[i] = value;
return tmp;
}
public V remove(Object key) {
int i = Math.abs(key.hashCode() % this.tableSize);
if ( this.table[i] == null ) {
return null; }
ListIterator<V> li =
((LinkedList<V>)this.table[i]).listIterator(0);
while ( li.hasNext() ) {
V tmp = li.next();
if ( ((Comparable<K>) key).compareTo(tmp.getKey()) == 0 )
this.table[i] = null;
return tmp;
}
return null;
}
} // public class MyHashTable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment