Chnaged hash method for dictionary to hash cod + mad
This commit is contained in:
@@ -1,8 +1,7 @@
|
|||||||
package com.xgiovio;
|
package com.xgiovio;
|
||||||
|
|
||||||
import dictionary.HashDictionaryChaining;
|
import dictionary.ChainingHashTable;
|
||||||
import dictionary.Logfile;
|
import dictionary.LogFile;
|
||||||
import exceptions.EmptyListException;
|
|
||||||
import priorityqueue.Entry;
|
import priorityqueue.Entry;
|
||||||
|
|
||||||
import java.security.InvalidKeyException;
|
import java.security.InvalidKeyException;
|
||||||
@@ -14,13 +13,11 @@ import java.util.Iterator;
|
|||||||
* Date: 11/05/2014
|
* Date: 11/05/2014
|
||||||
* Time: 21:17
|
* Time: 21:17
|
||||||
*/
|
*/
|
||||||
public class HashDictionaryChainingTest {
|
public class ChainingHashTableTest {
|
||||||
|
|
||||||
public static void main(String[] args) throws InvalidKeyException{
|
public static void main(String[] args) throws InvalidKeyException{
|
||||||
|
|
||||||
Logfile<String,Integer> def = new Logfile<String, Integer>();
|
ChainingHashTable<String,Integer> h = new ChainingHashTable<String, Integer>();
|
||||||
|
|
||||||
HashDictionaryChaining<String,Integer> h = new HashDictionaryChaining<String, Integer>(def,5);
|
|
||||||
|
|
||||||
System.out.println(h.load_factor());
|
System.out.println(h.load_factor());
|
||||||
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
package com.xgiovio;
|
package com.xgiovio;
|
||||||
|
|
||||||
import dictionary.HashDictionaryLinearProbing;
|
import dictionary.LinearProbingHashTable;
|
||||||
import priorityqueue.Entry;
|
import priorityqueue.Entry;
|
||||||
import priorityqueue.MyEntry;
|
import priorityqueue.MyEntry;
|
||||||
|
|
||||||
@@ -13,13 +13,11 @@ import java.util.Iterator;
|
|||||||
* Date: 11/05/2014
|
* Date: 11/05/2014
|
||||||
* Time: 21:17
|
* Time: 21:17
|
||||||
*/
|
*/
|
||||||
public class HashDictionaryLinearProbingTest {
|
public class LinearProbingHashTableTest {
|
||||||
|
|
||||||
public static void main(String[] args) throws InvalidKeyException{
|
public static void main(String[] args) throws InvalidKeyException{
|
||||||
|
|
||||||
MyEntry<String,Integer> def = new MyEntry<String, Integer>("null",0);
|
LinearProbingHashTable<String,Integer> h = new LinearProbingHashTable();
|
||||||
|
|
||||||
HashDictionaryLinearProbing<String,Integer> h = new HashDictionaryLinearProbing(def,5);
|
|
||||||
|
|
||||||
System.out.println(h.load_factor());
|
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.HashExceptionKey;
|
||||||
import exceptions.InvalidEntryException;
|
import exceptions.InvalidEntryException;
|
||||||
import position.NodePositionList;
|
import position.NodePositionList;
|
||||||
import position.PositionList;
|
|
||||||
import priorityqueue.Entry;
|
import priorityqueue.Entry;
|
||||||
import priorityqueue.MyEntry;
|
|
||||||
import sun.rmi.log.ReliableLog;
|
|
||||||
|
|
||||||
import java.security.InvalidKeyException;
|
import java.security.InvalidKeyException;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
@@ -19,16 +16,28 @@ import java.util.Iterator;
|
|||||||
* Date: 11/05/2014
|
* Date: 11/05/2014
|
||||||
* Time: 19:28
|
* Time: 19:28
|
||||||
*/
|
*/
|
||||||
public class HashDictionaryChaining <K,V> implements Dictionary<K,V> {
|
|
||||||
|
|
||||||
IndexList<Logfile<K,V>> table = null;
|
//Complete implementation by xgiovio
|
||||||
Logfile<K,V> AVAIBLE = null;
|
//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 avaible_slots = 0;
|
||||||
int elements = 0;
|
int elements = 0;
|
||||||
|
int p = 0;
|
||||||
|
int scale = 0;
|
||||||
|
int shift = 0;
|
||||||
|
|
||||||
public HashDictionaryChaining ( Logfile<K,V> IN_AVAIBLE, int in_size){
|
public ChainingHashTable(int in_p, int in_size){
|
||||||
table = new ArrayIndexList<Logfile<K, V>>(in_size);
|
table = new ArrayIndexList<LogFile<K, V>>(in_size);
|
||||||
AVAIBLE = IN_AVAIBLE;
|
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++){
|
for (int i= 0 ; i< in_size ;i++){
|
||||||
table.add(i,AVAIBLE);
|
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{
|
public float load_factor () throws InvalidKeyException{
|
||||||
float ret = (size() / (float)raw_size());
|
float ret = (size() / (float)raw_size());
|
||||||
if ( ret >= 0.9 ) {
|
if ( ret >= 0.9 ) {
|
||||||
@@ -59,11 +77,8 @@ public class HashDictionaryChaining <K,V> implements Dictionary<K,V> {
|
|||||||
return (size() == 0);
|
return (size() == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected int hash ( K key, int size){
|
protected int hash ( K key){
|
||||||
|
return (int) ((Math.abs(key.hashCode()*scale + shift) % p) % raw_size());
|
||||||
if ( (key instanceof String) )
|
|
||||||
return com.xgiovio.hash.functions.hash_horner( (String)key ,size );
|
|
||||||
throw new HashExceptionKey();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -71,20 +86,20 @@ public class HashDictionaryChaining <K,V> implements Dictionary<K,V> {
|
|||||||
public Entry<K, V> find(K key) throws InvalidKeyException {
|
public Entry<K, V> find(K key) throws InvalidKeyException {
|
||||||
|
|
||||||
checkKey(key);
|
checkKey(key);
|
||||||
int h = hash(key, raw_size());
|
int h = hash(key);
|
||||||
if (table.get(h) == AVAIBLE)
|
if (table.get(h) == AVAIBLE)
|
||||||
return null;
|
return null;
|
||||||
Logfile<K, V> n = table.get(h);
|
LogFile<K, V> n = table.get(h);
|
||||||
return n.find(key);
|
return n.find(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Iterable<Entry<K, V>> findAll(K key) throws InvalidKeyException {
|
public Iterable<Entry<K, V>> findAll(K key) throws InvalidKeyException {
|
||||||
checkKey(key);
|
checkKey(key);
|
||||||
int h = hash(key,raw_size());
|
int h = hash(key);
|
||||||
if (table.get(h) == AVAIBLE)
|
if (table.get(h) == AVAIBLE)
|
||||||
return null;
|
return null;
|
||||||
Logfile<K, V> n = table.get(h);
|
LogFile<K, V> n = table.get(h);
|
||||||
return n.entries();
|
return n.entries();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,15 +108,15 @@ public class HashDictionaryChaining <K,V> implements Dictionary<K,V> {
|
|||||||
checkKey(key);
|
checkKey(key);
|
||||||
if (load_factor() >= 0.9 )
|
if (load_factor() >= 0.9 )
|
||||||
reformat();
|
reformat();
|
||||||
int h = hash(key,raw_size());
|
int h = hash(key);
|
||||||
if (table.get(h) == AVAIBLE) {
|
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);
|
table.set(h,n);
|
||||||
avaible_slots--;
|
avaible_slots--;
|
||||||
elements++;
|
elements++;
|
||||||
return n.insert(key,value);
|
return n.insert(key,value);
|
||||||
} else {
|
} else {
|
||||||
Logfile<K, V> n = table.get(h);
|
LogFile<K, V> n = table.get(h);
|
||||||
elements++;
|
elements++;
|
||||||
return n.insert(key,value);
|
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 {
|
public Entry<K, V> remove(Entry<K, V> e) throws InvalidEntryException {
|
||||||
if (e == null)
|
if (e == null)
|
||||||
throw new InvalidEntryException();
|
throw new InvalidEntryException();
|
||||||
int h = hash(e.getKey(),raw_size());
|
int h = hash(e.getKey());
|
||||||
if (table.get(h) == AVAIBLE)
|
if (table.get(h) == AVAIBLE)
|
||||||
throw new InvalidEntryException();
|
throw new InvalidEntryException();
|
||||||
Logfile<K, V> n = table.get(h);
|
LogFile<K, V> n = table.get(h);
|
||||||
Entry<K,V> t = n.remove(e);
|
Entry<K,V> t = n.remove(e);
|
||||||
elements--;
|
elements--;
|
||||||
if (n.size() == 0){
|
if (n.size() == 0){
|
||||||
@@ -152,9 +167,12 @@ public class HashDictionaryChaining <K,V> implements Dictionary<K,V> {
|
|||||||
|
|
||||||
Iterable<Entry<K,V>> ite = entries();
|
Iterable<Entry<K,V>> ite = entries();
|
||||||
int new_size = raw_size() * 2;
|
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;
|
avaible_slots = 0;
|
||||||
elements = 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++){
|
for (int i= 0 ; i< (new_size) ;i++){
|
||||||
table2.add(i,AVAIBLE);
|
table2.add(i,AVAIBLE);
|
||||||
@@ -17,16 +17,27 @@ import java.util.Iterator;
|
|||||||
* Date: 11/05/2014
|
* Date: 11/05/2014
|
||||||
* Time: 19:28
|
* 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;
|
IndexList<Entry<K,V>> table = null;
|
||||||
Entry<K,V> AVAIBLE = null;
|
Entry<K,V> AVAIBLE = null;
|
||||||
int avaible_slots = 0;
|
int avaible_slots = 0;
|
||||||
int elements = 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);
|
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++){
|
for (int i= 0 ; i< in_size ;i++){
|
||||||
table.add(i,AVAIBLE);
|
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{
|
public float load_factor () throws InvalidKeyException{
|
||||||
float ret = (size() / (float)raw_size());
|
float ret = (size() / (float)raw_size());
|
||||||
if ( ret >= 0.5 ) {
|
if ( ret >= 0.5 ) {
|
||||||
@@ -57,11 +78,8 @@ public class HashDictionaryLinearProbing<K,V> implements Dictionary<K,V> {
|
|||||||
return (size() == 0);
|
return (size() == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected int hash ( K key, int size){
|
protected int hash ( K key) {
|
||||||
|
return (int) ((Math.abs(key.hashCode()*scale + shift) % p) % raw_size());
|
||||||
if ( (key instanceof String) )
|
|
||||||
return com.xgiovio.hash.functions.hash_horner( (String)key ,size );
|
|
||||||
throw new HashExceptionKey();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -70,7 +88,7 @@ public class HashDictionaryLinearProbing<K,V> implements Dictionary<K,V> {
|
|||||||
public Entry<K, V> find(K key) throws InvalidKeyException {
|
public Entry<K, V> find(K key) throws InvalidKeyException {
|
||||||
|
|
||||||
checkKey(key);
|
checkKey(key);
|
||||||
int h = hash(key, raw_size());
|
int h = hash(key);
|
||||||
int count = 0;
|
int count = 0;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (;count < raw_size();){
|
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 {
|
public Iterable<Entry<K, V>> findAll(K key) throws InvalidKeyException {
|
||||||
checkKey(key);
|
checkKey(key);
|
||||||
NodePositionList<Entry<K,V>> to_return = new NodePositionList<Entry<K, V>>();
|
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 count = 0;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (;count < raw_size();){
|
for (;count < raw_size();){
|
||||||
@@ -107,7 +125,7 @@ public class HashDictionaryLinearProbing<K,V> implements Dictionary<K,V> {
|
|||||||
checkKey(key);
|
checkKey(key);
|
||||||
if (load_factor() >= 0.5 )
|
if (load_factor() >= 0.5 )
|
||||||
reformat();
|
reformat();
|
||||||
int h = hash(key,raw_size());
|
int h = hash(key);
|
||||||
if (table.get(h) == AVAIBLE) {
|
if (table.get(h) == AVAIBLE) {
|
||||||
MyEntry<K, V> n = new MyEntry<K, V>(key,value);
|
MyEntry<K, V> n = new MyEntry<K, V>(key,value);
|
||||||
table.set(h,n);
|
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 {
|
public Entry<K, V> remove(Entry<K, V> e) throws InvalidEntryException {
|
||||||
if (e == null)
|
if (e == null)
|
||||||
throw new InvalidEntryException();
|
throw new InvalidEntryException();
|
||||||
int h = hash(e.getKey(),raw_size());
|
int h = hash(e.getKey());
|
||||||
if (table.get(h) == AVAIBLE)
|
if (table.get(h) == AVAIBLE)
|
||||||
throw new InvalidEntryException();
|
throw new InvalidEntryException();
|
||||||
if (table.get(h) == e) {
|
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);
|
IndexList<Entry<K,V>> table2 = new ArrayIndexList<Entry<K, V>>(new_size);
|
||||||
avaible_slots = 0;
|
avaible_slots = 0;
|
||||||
elements = 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++){
|
for (int i= 0 ; i< (new_size) ;i++){
|
||||||
table2.add(i,AVAIBLE);
|
table2.add(i,AVAIBLE);
|
||||||
@@ -14,7 +14,7 @@ import java.security.InvalidKeyException;
|
|||||||
* Date: 11/05/2014
|
* Date: 11/05/2014
|
||||||
* Time: 19:04
|
* 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>>();
|
NodePositionList<Entry<K,V>> list = new NodePositionList<Entry<K,V>>();
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import position.PositionList;
|
|||||||
import priorityqueue.Entry;
|
import priorityqueue.Entry;
|
||||||
|
|
||||||
import java.security.InvalidKeyException;
|
import java.security.InvalidKeyException;
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A hash table data structure that uses linear probing to handle
|
* 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;
|
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