Chnaged hash method for dictionary to hash cod + mad
This commit is contained in:
@@ -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>>();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user