diff --git a/com/xgiovio/HashDictionaryChainingTest.java b/com/xgiovio/ChainingHashTableTest.java similarity index 85% rename from com/xgiovio/HashDictionaryChainingTest.java rename to com/xgiovio/ChainingHashTableTest.java index 4d14ce9..87bf30d 100644 --- a/com/xgiovio/HashDictionaryChainingTest.java +++ b/com/xgiovio/ChainingHashTableTest.java @@ -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 def = new Logfile(); - - HashDictionaryChaining h = new HashDictionaryChaining(def,5); + ChainingHashTable h = new ChainingHashTable(); System.out.println(h.load_factor()); diff --git a/com/xgiovio/HashDictionaryLinearProbingTest.java b/com/xgiovio/LinearProbingHashTableTest.java similarity index 89% rename from com/xgiovio/HashDictionaryLinearProbingTest.java rename to com/xgiovio/LinearProbingHashTableTest.java index d027bc2..60bf02d 100644 --- a/com/xgiovio/HashDictionaryLinearProbingTest.java +++ b/com/xgiovio/LinearProbingHashTableTest.java @@ -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 def = new MyEntry("null",0); - - HashDictionaryLinearProbing h = new HashDictionaryLinearProbing(def,5); + LinearProbingHashTable h = new LinearProbingHashTable(); System.out.println(h.load_factor()); diff --git a/com/xgiovio/hash/functions.java b/com/xgiovio/hash/functions.java deleted file mode 100644 index 7d6514f..0000000 --- a/com/xgiovio/hash/functions.java +++ /dev/null @@ -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; - } - - -} diff --git a/dictionary/HashDictionaryChaining.java b/dictionary/ChainingHashTable.java similarity index 71% rename from dictionary/HashDictionaryChaining.java rename to dictionary/ChainingHashTable.java index bcaa415..643ce62 100644 --- a/dictionary/HashDictionaryChaining.java +++ b/dictionary/ChainingHashTable.java @@ -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 implements Dictionary { - IndexList> table = null; - Logfile AVAIBLE = null; +//Complete implementation by xgiovio +//Collisions resolved via chaining with LogFile + + +public class ChainingHashTable implements Dictionary { + + IndexList> table = null; + LogFile AVAIBLE = null; int avaible_slots = 0; int elements = 0; + int p = 0; + int scale = 0; + int shift = 0; - public HashDictionaryChaining ( Logfile IN_AVAIBLE, int in_size){ - table = new ArrayIndexList>(in_size); - AVAIBLE = IN_AVAIBLE; + public ChainingHashTable(int in_p, int in_size){ + table = new ArrayIndexList>(in_size); + AVAIBLE = new LogFile(); + 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 implements Dictionary { } } + 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 implements Dictionary { 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 implements Dictionary { public Entry 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 n = table.get(h); + LogFile n = table.get(h); return n.find(key); } @Override public Iterable> 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 n = table.get(h); + LogFile n = table.get(h); return n.entries(); } @@ -93,15 +108,15 @@ public class HashDictionaryChaining implements Dictionary { checkKey(key); if (load_factor() >= 0.9 ) reformat(); - int h = hash(key,raw_size()); + int h = hash(key); if (table.get(h) == AVAIBLE) { - Logfile n = new Logfile(); + LogFile n = new LogFile(); table.set(h,n); avaible_slots--; elements++; return n.insert(key,value); } else { - Logfile n = table.get(h); + LogFile n = table.get(h); elements++; return n.insert(key,value); } @@ -111,10 +126,10 @@ public class HashDictionaryChaining implements Dictionary { public Entry remove(Entry 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 n = table.get(h); + LogFile n = table.get(h); Entry t = n.remove(e); elements--; if (n.size() == 0){ @@ -152,9 +167,12 @@ public class HashDictionaryChaining implements Dictionary { Iterable> ite = entries(); int new_size = raw_size() * 2; - IndexList> table2 = new ArrayIndexList>(new_size); + IndexList> table2 = new ArrayIndexList>(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); diff --git a/dictionary/HashDictionaryLinearProbing.java b/dictionary/LinearProbingHashTable.java similarity index 84% rename from dictionary/HashDictionaryLinearProbing.java rename to dictionary/LinearProbingHashTable.java index cb8d0eb..8b9b9fb 100644 --- a/dictionary/HashDictionaryLinearProbing.java +++ b/dictionary/LinearProbingHashTable.java @@ -17,16 +17,27 @@ import java.util.Iterator; * Date: 11/05/2014 * Time: 19:28 */ -public class HashDictionaryLinearProbing implements Dictionary { + + //Complete implementation by xgiovio + // Collisions resolved with linear probing + +public class LinearProbingHashTable implements Dictionary { IndexList> table = null; Entry AVAIBLE = null; int avaible_slots = 0; int elements = 0; + int p = 0; + int scale = 0; + int shift = 0; - public HashDictionaryLinearProbing(Entry IN_AVAIBLE, int in_size){ + public LinearProbingHashTable(int in_p, int in_size){ table = new ArrayIndexList>(in_size); - AVAIBLE = IN_AVAIBLE; + AVAIBLE = new MyEntry(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 implements Dictionary { } } + 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 implements Dictionary { 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 implements Dictionary { public Entry 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 implements Dictionary { public Iterable> findAll(K key) throws InvalidKeyException { checkKey(key); NodePositionList> to_return = new NodePositionList>(); - 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 implements Dictionary { checkKey(key); if (load_factor() >= 0.5 ) reformat(); - int h = hash(key,raw_size()); + int h = hash(key); if (table.get(h) == AVAIBLE) { MyEntry n = new MyEntry(key,value); table.set(h,n); @@ -136,7 +154,7 @@ public class HashDictionaryLinearProbing implements Dictionary { public Entry remove(Entry 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 implements Dictionary { IndexList> table2 = new ArrayIndexList>(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); diff --git a/dictionary/Logfile.java b/dictionary/Logfile.java index 712bc01..702afb8 100644 --- a/dictionary/Logfile.java +++ b/dictionary/Logfile.java @@ -14,7 +14,7 @@ import java.security.InvalidKeyException; * Date: 11/05/2014 * Time: 19:04 */ -public class Logfile implements Dictionary { +public class LogFile implements Dictionary { NodePositionList> list = new NodePositionList>(); diff --git a/map/HashTableMap.java b/map/HashTableMap.java index 891e41a..057a992 100644 --- a/map/HashTableMap.java +++ b/map/HashTableMap.java @@ -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 implements Map { return values; } -} + + public String toString() { + + Iterator> it = entries().iterator(); + + String to_return = ""; + to_return = to_return + "["; + + for (;it.hasNext();){ + Entry t = it.next(); + + if (it.hasNext()) { + + to_return+=(t.toString() + " , "); + } else { + + to_return+=(t.toString()); + } + } + + return to_return+= "]"; + } +}