Chnaged hash method for dictionary to hash cod + mad
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
package com.xgiovio;
|
||||
|
||||
import dictionary.HashDictionaryChaining;
|
||||
import dictionary.Logfile;
|
||||
import exceptions.EmptyListException;
|
||||
import dictionary.ChainingHashTable;
|
||||
import dictionary.LogFile;
|
||||
import priorityqueue.Entry;
|
||||
|
||||
import java.security.InvalidKeyException;
|
||||
@@ -14,13 +13,11 @@ import java.util.Iterator;
|
||||
* Date: 11/05/2014
|
||||
* Time: 21:17
|
||||
*/
|
||||
public class HashDictionaryChainingTest {
|
||||
public class ChainingHashTableTest {
|
||||
|
||||
public static void main(String[] args) throws InvalidKeyException{
|
||||
|
||||
Logfile<String,Integer> def = new Logfile<String, Integer>();
|
||||
|
||||
HashDictionaryChaining<String,Integer> h = new HashDictionaryChaining<String, Integer>(def,5);
|
||||
ChainingHashTable<String,Integer> h = new ChainingHashTable<String, Integer>();
|
||||
|
||||
System.out.println(h.load_factor());
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.xgiovio;
|
||||
|
||||
import dictionary.HashDictionaryLinearProbing;
|
||||
import dictionary.LinearProbingHashTable;
|
||||
import priorityqueue.Entry;
|
||||
import priorityqueue.MyEntry;
|
||||
|
||||
@@ -13,13 +13,11 @@ import java.util.Iterator;
|
||||
* Date: 11/05/2014
|
||||
* Time: 21:17
|
||||
*/
|
||||
public class HashDictionaryLinearProbingTest {
|
||||
public class LinearProbingHashTableTest {
|
||||
|
||||
public static void main(String[] args) throws InvalidKeyException{
|
||||
|
||||
MyEntry<String,Integer> def = new MyEntry<String, Integer>("null",0);
|
||||
|
||||
HashDictionaryLinearProbing<String,Integer> h = new HashDictionaryLinearProbing(def,5);
|
||||
LinearProbingHashTable<String,Integer> h = new LinearProbingHashTable();
|
||||
|
||||
System.out.println(h.load_factor());
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
package com.xgiovio.hash;
|
||||
|
||||
/**
|
||||
* Created with MONSTER.
|
||||
* User: xgiovio
|
||||
* Date: 11/05/2014
|
||||
* Time: 19:25
|
||||
*/
|
||||
public class functions {
|
||||
|
||||
|
||||
//http://m.smithworx.com/cs235/notes/view.php?file=extra%20notes/rodham/35-36-com.xgiovio.hash-tables/HashFunctions.java
|
||||
public static int hash_horner( String key, int tableSize ) {
|
||||
|
||||
int hashVal = 0;
|
||||
|
||||
for ( int i = 0; i < key.length(); ++i ) {
|
||||
hashVal = (hashVal * 37) + key.charAt( i );
|
||||
}
|
||||
|
||||
hashVal %= tableSize;
|
||||
|
||||
if ( hashVal < 0 ) {
|
||||
hashVal += tableSize;
|
||||
}
|
||||
|
||||
return hashVal;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -5,10 +5,7 @@ import arraylist.IndexList;
|
||||
import exceptions.HashExceptionKey;
|
||||
import exceptions.InvalidEntryException;
|
||||
import position.NodePositionList;
|
||||
import position.PositionList;
|
||||
import priorityqueue.Entry;
|
||||
import priorityqueue.MyEntry;
|
||||
import sun.rmi.log.ReliableLog;
|
||||
|
||||
import java.security.InvalidKeyException;
|
||||
import java.util.Iterator;
|
||||
@@ -19,16 +16,28 @@ import java.util.Iterator;
|
||||
* Date: 11/05/2014
|
||||
* Time: 19:28
|
||||
*/
|
||||
public class HashDictionaryChaining <K,V> implements Dictionary<K,V> {
|
||||
|
||||
IndexList<Logfile<K,V>> table = null;
|
||||
Logfile<K,V> AVAIBLE = null;
|
||||
//Complete implementation by xgiovio
|
||||
//Collisions resolved via chaining with LogFile
|
||||
|
||||
|
||||
public class ChainingHashTable<K,V> implements Dictionary<K,V> {
|
||||
|
||||
IndexList<LogFile<K,V>> table = null;
|
||||
LogFile<K,V> AVAIBLE = null;
|
||||
int avaible_slots = 0;
|
||||
int elements = 0;
|
||||
int p = 0;
|
||||
int scale = 0;
|
||||
int shift = 0;
|
||||
|
||||
public HashDictionaryChaining ( Logfile<K,V> IN_AVAIBLE, int in_size){
|
||||
table = new ArrayIndexList<Logfile<K, V>>(in_size);
|
||||
AVAIBLE = IN_AVAIBLE;
|
||||
public ChainingHashTable(int in_p, int in_size){
|
||||
table = new ArrayIndexList<LogFile<K, V>>(in_size);
|
||||
AVAIBLE = new LogFile<K, V>();
|
||||
p = in_p;
|
||||
java.util.Random rand = new java.util.Random();
|
||||
scale = rand.nextInt(p-1) + 1;
|
||||
shift = rand.nextInt(p);
|
||||
|
||||
for (int i= 0 ; i< in_size ;i++){
|
||||
table.add(i,AVAIBLE);
|
||||
@@ -36,6 +45,15 @@ public class HashDictionaryChaining <K,V> implements Dictionary<K,V> {
|
||||
}
|
||||
}
|
||||
|
||||
public ChainingHashTable(int in_size){
|
||||
this(109345121,in_size);
|
||||
}
|
||||
|
||||
public ChainingHashTable(){
|
||||
this(109345121,100);
|
||||
|
||||
}
|
||||
|
||||
public float load_factor () throws InvalidKeyException{
|
||||
float ret = (size() / (float)raw_size());
|
||||
if ( ret >= 0.9 ) {
|
||||
@@ -59,11 +77,8 @@ public class HashDictionaryChaining <K,V> implements Dictionary<K,V> {
|
||||
return (size() == 0);
|
||||
}
|
||||
|
||||
protected int hash ( K key, int size){
|
||||
|
||||
if ( (key instanceof String) )
|
||||
return com.xgiovio.hash.functions.hash_horner( (String)key ,size );
|
||||
throw new HashExceptionKey();
|
||||
protected int hash ( K key){
|
||||
return (int) ((Math.abs(key.hashCode()*scale + shift) % p) % raw_size());
|
||||
}
|
||||
|
||||
|
||||
@@ -71,20 +86,20 @@ public class HashDictionaryChaining <K,V> implements Dictionary<K,V> {
|
||||
public Entry<K, V> find(K key) throws InvalidKeyException {
|
||||
|
||||
checkKey(key);
|
||||
int h = hash(key, raw_size());
|
||||
int h = hash(key);
|
||||
if (table.get(h) == AVAIBLE)
|
||||
return null;
|
||||
Logfile<K, V> n = table.get(h);
|
||||
LogFile<K, V> n = table.get(h);
|
||||
return n.find(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<Entry<K, V>> findAll(K key) throws InvalidKeyException {
|
||||
checkKey(key);
|
||||
int h = hash(key,raw_size());
|
||||
int h = hash(key);
|
||||
if (table.get(h) == AVAIBLE)
|
||||
return null;
|
||||
Logfile<K, V> n = table.get(h);
|
||||
LogFile<K, V> n = table.get(h);
|
||||
return n.entries();
|
||||
}
|
||||
|
||||
@@ -93,15 +108,15 @@ public class HashDictionaryChaining <K,V> implements Dictionary<K,V> {
|
||||
checkKey(key);
|
||||
if (load_factor() >= 0.9 )
|
||||
reformat();
|
||||
int h = hash(key,raw_size());
|
||||
int h = hash(key);
|
||||
if (table.get(h) == AVAIBLE) {
|
||||
Logfile<K, V> n = new Logfile<K, V>();
|
||||
LogFile<K, V> n = new LogFile<K, V>();
|
||||
table.set(h,n);
|
||||
avaible_slots--;
|
||||
elements++;
|
||||
return n.insert(key,value);
|
||||
} else {
|
||||
Logfile<K, V> n = table.get(h);
|
||||
LogFile<K, V> n = table.get(h);
|
||||
elements++;
|
||||
return n.insert(key,value);
|
||||
}
|
||||
@@ -111,10 +126,10 @@ public class HashDictionaryChaining <K,V> implements Dictionary<K,V> {
|
||||
public Entry<K, V> remove(Entry<K, V> e) throws InvalidEntryException {
|
||||
if (e == null)
|
||||
throw new InvalidEntryException();
|
||||
int h = hash(e.getKey(),raw_size());
|
||||
int h = hash(e.getKey());
|
||||
if (table.get(h) == AVAIBLE)
|
||||
throw new InvalidEntryException();
|
||||
Logfile<K, V> n = table.get(h);
|
||||
LogFile<K, V> n = table.get(h);
|
||||
Entry<K,V> t = n.remove(e);
|
||||
elements--;
|
||||
if (n.size() == 0){
|
||||
@@ -152,9 +167,12 @@ public class HashDictionaryChaining <K,V> implements Dictionary<K,V> {
|
||||
|
||||
Iterable<Entry<K,V>> ite = entries();
|
||||
int new_size = raw_size() * 2;
|
||||
IndexList<Logfile<K,V>> table2 = new ArrayIndexList<Logfile<K, V>>(new_size);
|
||||
IndexList<LogFile<K,V>> table2 = new ArrayIndexList<LogFile<K, V>>(new_size);
|
||||
avaible_slots = 0;
|
||||
elements = 0;
|
||||
java.util.Random rand = new java.util.Random();
|
||||
scale = rand.nextInt(p-1) + 1; // new hash scaling factor
|
||||
shift = rand.nextInt(p); //// new hash shifting factor
|
||||
|
||||
for (int i= 0 ; i< (new_size) ;i++){
|
||||
table2.add(i,AVAIBLE);
|
||||
@@ -17,16 +17,27 @@ import java.util.Iterator;
|
||||
* Date: 11/05/2014
|
||||
* Time: 19:28
|
||||
*/
|
||||
public class HashDictionaryLinearProbing<K,V> implements Dictionary<K,V> {
|
||||
|
||||
//Complete implementation by xgiovio
|
||||
// Collisions resolved with linear probing
|
||||
|
||||
public class LinearProbingHashTable<K,V> implements Dictionary<K,V> {
|
||||
|
||||
IndexList<Entry<K,V>> table = null;
|
||||
Entry<K,V> AVAIBLE = null;
|
||||
int avaible_slots = 0;
|
||||
int elements = 0;
|
||||
int p = 0;
|
||||
int scale = 0;
|
||||
int shift = 0;
|
||||
|
||||
public HashDictionaryLinearProbing(Entry<K, V> IN_AVAIBLE, int in_size){
|
||||
public LinearProbingHashTable(int in_p, int in_size){
|
||||
table = new ArrayIndexList<Entry<K, V>>(in_size);
|
||||
AVAIBLE = IN_AVAIBLE;
|
||||
AVAIBLE = new MyEntry<K, V>(null,null);
|
||||
p = in_p;
|
||||
java.util.Random rand = new java.util.Random();
|
||||
scale = rand.nextInt(p-1) + 1;
|
||||
shift = rand.nextInt(p);
|
||||
|
||||
for (int i= 0 ; i< in_size ;i++){
|
||||
table.add(i,AVAIBLE);
|
||||
@@ -34,6 +45,16 @@ public class HashDictionaryLinearProbing<K,V> implements Dictionary<K,V> {
|
||||
}
|
||||
}
|
||||
|
||||
public LinearProbingHashTable(int in_size){
|
||||
this(109345121,in_size);
|
||||
}
|
||||
|
||||
public LinearProbingHashTable(){
|
||||
this(109345121,100);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public float load_factor () throws InvalidKeyException{
|
||||
float ret = (size() / (float)raw_size());
|
||||
if ( ret >= 0.5 ) {
|
||||
@@ -57,11 +78,8 @@ public class HashDictionaryLinearProbing<K,V> implements Dictionary<K,V> {
|
||||
return (size() == 0);
|
||||
}
|
||||
|
||||
protected int hash ( K key, int size){
|
||||
|
||||
if ( (key instanceof String) )
|
||||
return com.xgiovio.hash.functions.hash_horner( (String)key ,size );
|
||||
throw new HashExceptionKey();
|
||||
protected int hash ( K key) {
|
||||
return (int) ((Math.abs(key.hashCode()*scale + shift) % p) % raw_size());
|
||||
}
|
||||
|
||||
|
||||
@@ -70,7 +88,7 @@ public class HashDictionaryLinearProbing<K,V> implements Dictionary<K,V> {
|
||||
public Entry<K, V> find(K key) throws InvalidKeyException {
|
||||
|
||||
checkKey(key);
|
||||
int h = hash(key, raw_size());
|
||||
int h = hash(key);
|
||||
int count = 0;
|
||||
int i = 0;
|
||||
for (;count < raw_size();){
|
||||
@@ -88,7 +106,7 @@ public class HashDictionaryLinearProbing<K,V> implements Dictionary<K,V> {
|
||||
public Iterable<Entry<K, V>> findAll(K key) throws InvalidKeyException {
|
||||
checkKey(key);
|
||||
NodePositionList<Entry<K,V>> to_return = new NodePositionList<Entry<K, V>>();
|
||||
int h = hash(key, raw_size());
|
||||
int h = hash(key);
|
||||
int count = 0;
|
||||
int i = 0;
|
||||
for (;count < raw_size();){
|
||||
@@ -107,7 +125,7 @@ public class HashDictionaryLinearProbing<K,V> implements Dictionary<K,V> {
|
||||
checkKey(key);
|
||||
if (load_factor() >= 0.5 )
|
||||
reformat();
|
||||
int h = hash(key,raw_size());
|
||||
int h = hash(key);
|
||||
if (table.get(h) == AVAIBLE) {
|
||||
MyEntry<K, V> n = new MyEntry<K, V>(key,value);
|
||||
table.set(h,n);
|
||||
@@ -136,7 +154,7 @@ public class HashDictionaryLinearProbing<K,V> implements Dictionary<K,V> {
|
||||
public Entry<K, V> remove(Entry<K, V> e) throws InvalidEntryException {
|
||||
if (e == null)
|
||||
throw new InvalidEntryException();
|
||||
int h = hash(e.getKey(),raw_size());
|
||||
int h = hash(e.getKey());
|
||||
if (table.get(h) == AVAIBLE)
|
||||
throw new InvalidEntryException();
|
||||
if (table.get(h) == e) {
|
||||
@@ -192,6 +210,9 @@ public class HashDictionaryLinearProbing<K,V> implements Dictionary<K,V> {
|
||||
IndexList<Entry<K,V>> table2 = new ArrayIndexList<Entry<K, V>>(new_size);
|
||||
avaible_slots = 0;
|
||||
elements = 0;
|
||||
java.util.Random rand = new java.util.Random();
|
||||
scale = rand.nextInt(p-1) + 1; // new hash scaling factor
|
||||
shift = rand.nextInt(p); //// new hash shifting factor
|
||||
|
||||
for (int i= 0 ; i< (new_size) ;i++){
|
||||
table2.add(i,AVAIBLE);
|
||||
@@ -14,7 +14,7 @@ import java.security.InvalidKeyException;
|
||||
* Date: 11/05/2014
|
||||
* Time: 19:04
|
||||
*/
|
||||
public class Logfile<K,V> implements Dictionary<K,V> {
|
||||
public class LogFile<K,V> implements Dictionary<K,V> {
|
||||
|
||||
NodePositionList<Entry<K,V>> list = new NodePositionList<Entry<K,V>>();
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import position.PositionList;
|
||||
import priorityqueue.Entry;
|
||||
|
||||
import java.security.InvalidKeyException;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* A hash table data structure that uses linear probing to handle
|
||||
@@ -181,5 +182,27 @@ public class HashTableMap<K,V> implements Map<K,V> {
|
||||
return values;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
|
||||
Iterator<Entry<K,V>> it = entries().iterator();
|
||||
|
||||
String to_return = "";
|
||||
to_return = to_return + "[";
|
||||
|
||||
for (;it.hasNext();){
|
||||
Entry<K,V> t = it.next();
|
||||
|
||||
if (it.hasNext()) {
|
||||
|
||||
to_return+=(t.toString() + " , ");
|
||||
} else {
|
||||
|
||||
to_return+=(t.toString());
|
||||
}
|
||||
}
|
||||
|
||||
return to_return+= "]";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user