Fixed bugs to Dictionary Classes

This commit is contained in:
2014-05-17 23:17:11 +02:00
parent 4dab60c3fa
commit f1e6f18939
6 changed files with 96 additions and 39 deletions

View File

@@ -17,7 +17,7 @@ public class ChainingHashTableTest {
public static void main(String[] args) throws InvalidKeyException{ public static void main(String[] args) throws InvalidKeyException{
ChainingHashTable<String,Integer> h = new ChainingHashTable<String, Integer>(); ChainingHashTable<String,Integer> h = new ChainingHashTable<String, Integer>(1);
System.out.println(h.load_factor()); System.out.println(h.load_factor());

View File

@@ -17,7 +17,8 @@ public class LinearProbingHashTableTest {
public static void main(String[] args) throws InvalidKeyException{ public static void main(String[] args) throws InvalidKeyException{
LinearProbingHashTable<String,Integer> h = new LinearProbingHashTable(); LinearProbingHashTable<String,Integer> h = new LinearProbingHashTable(1);
System.out.println(h.load_factor()); System.out.println(h.load_factor());
@@ -89,12 +90,15 @@ public class LinearProbingHashTableTest {
Entry<String,Integer> f = h.insert("7", 100); Entry<String,Integer> f = h.insert("7", 100);
System.out.println(h); System.out.println(h);
h.remove(f); h.remove(f);
System.out.println(h); System.out.println(h);
} }
} }

View File

@@ -122,6 +122,22 @@ public class ChainingHashTable<K,V> implements Dictionary<K,V> {
} }
} }
protected Entry<K, V> insert_refactor(Entry<K,V> in_e) throws InvalidKeyException {
// in_e not checked because used internally
int h = hash(in_e.getKey());
if (table.get(h) == AVAIBLE) {
LogFile<K, V> n = new LogFile<K, V>();
table.set(h,n);
avaible_slots--;
elements++;
return n.insert(in_e);
} else {
LogFile<K, V> n = table.get(h);
elements++;
return n.insert(in_e);
}
}
@Override @Override
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)
@@ -180,7 +196,7 @@ public class ChainingHashTable<K,V> implements Dictionary<K,V> {
table = table2; table = table2;
for (Entry<K,V> e : ite){ for (Entry<K,V> e : ite){
insert(e.getKey(),e.getValue()); insert_refactor(e);
} }
} }

View File

@@ -90,13 +90,13 @@ public class LinearProbingHashTable<K,V> implements Dictionary<K,V> {
checkKey(key); checkKey(key);
int h = hash(key); int h = hash(key);
int count = 0; int count = 0;
int i = 0;
for (;count < raw_size();){ for (;count < raw_size();){
if (table.get(h+i) == AVAIBLE) if (table.get( (h+count)%raw_size() ) == AVAIBLE)
return null; return null;
if (table.get(h+i).getKey().equals(key)) if (table.get((h+count)%raw_size()) == null)
return table.get(h+i); continue;
i= (i + 1)% raw_size(); if (table.get((h+count)%raw_size()).getKey().equals(key))
return table.get((h+count)%raw_size());
count++; count++;
} }
return null; return null;
@@ -108,13 +108,13 @@ public class LinearProbingHashTable<K,V> implements Dictionary<K,V> {
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); int h = hash(key);
int count = 0; int count = 0;
int i = 0;
for (;count < raw_size();){ for (;count < raw_size();){
if (table.get(h+i) == AVAIBLE) if (table.get((h+count)%raw_size()) == AVAIBLE)
return to_return; return to_return;
if (table.get(h+i).getKey().equals(key)) if (table.get((h+count)%raw_size()) == null)
to_return.addLast( table.get(h+i)); continue;
i= (i + 1)% raw_size(); if (table.get((h+count)%raw_size()).getKey().equals(key))
to_return.addLast( table.get((h+count)%raw_size()));
count++; count++;
} }
return to_return; return to_return;
@@ -126,29 +126,51 @@ public class LinearProbingHashTable<K,V> implements Dictionary<K,V> {
if (load_factor() >= 0.5 ) if (load_factor() >= 0.5 )
reformat(); reformat();
int h = hash(key); int h = hash(key);
if (table.get(h) == AVAIBLE) { if (table.get(h) == AVAIBLE || table.get(h ) == null ) {
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);
avaible_slots--; avaible_slots--;
elements++; elements++;
return n; return n;
} else { } else {
int i = 1;
int count = 1; int count = 1;
for ( ; count < raw_size() ;) { for ( ; count < raw_size() ;) {
if (table.get(h + i) == AVAIBLE) { if (table.get((h+count)%raw_size()) == AVAIBLE || table.get((h+count)%raw_size()) == null ) {
MyEntry<K, V> n = new MyEntry<K, V>(key, value); MyEntry<K, V> n = new MyEntry<K, V>(key, value);
table.set(h + i, n); table.set((h+count)%raw_size(), n);
avaible_slots--; avaible_slots--;
elements++; elements++;
return n; return n;
} }
i = (i + 1) % raw_size();
count++; count++;
} }
return null; // non dovresti essere qui a causa del load factor return null; // non dovresti essere qui a causa del load factor
} }
} }
protected Entry<K, V> insert_refactor(Entry<K, V> in_e) throws InvalidKeyException {
// no check of in_e because this function is used internally from reformat
int h = hash(in_e.getKey());
if (table.get(h) == AVAIBLE || table.get(h ) == null ) {
Entry<K, V> n = in_e;
table.set(h,n);
avaible_slots--;
elements++;
return n;
} else {
int count = 1;
for ( ; count < raw_size() ;) {
if (table.get((h+count)%raw_size()) == AVAIBLE || table.get((h+count)%raw_size()) == null ) {
Entry<K, V> n = in_e;
table.set((h+count)%raw_size(), n);
avaible_slots--;
elements++;
return n;
}
count++;
}
return null; // non dovresti essere qui a causa del load factor
}
}
@Override @Override
public Entry<K, V> remove(Entry<K, V> e) throws InvalidEntryException { public Entry<K, V> remove(Entry<K, V> e) throws InvalidEntryException {
@@ -157,25 +179,29 @@ public class LinearProbingHashTable<K,V> implements Dictionary<K,V> {
int h = hash(e.getKey()); 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) == null){
Entry<K, V> removed = table.get(h);
table.remove(h); } else {
table.set(h,AVAIBLE); if (table.get(h) == e) {
elements--; table.set(h, null);
avaible_slots++;
return removed;
}
int count = 1;
for (int i = 1; count < raw_size() ; i = (i + 1) % raw_size()){
if (table.get(h + i) == AVAIBLE)
throw new InvalidEntryException();
if (table.get(h + i) == e) {
Entry<K, V> removed = table.get(h + i);
table.remove(h + i);
table.set(h + i,AVAIBLE);
elements--; elements--;
avaible_slots++; avaible_slots++;
return removed; return e;
}
}
int count = 1;
for (; count < raw_size() ;){
if (table.get((h+count)%raw_size()) == AVAIBLE)
throw new InvalidEntryException();
if (table.get((h+count)%raw_size()) == null){
} else {
if (table.get((h+count)%raw_size()) == e) {
table.set((h+count)%raw_size(), null);
elements--;
avaible_slots++;
return e;
}
} }
count++; count++;
} }
@@ -189,7 +215,7 @@ public class LinearProbingHashTable<K,V> implements Dictionary<K,V> {
NodePositionList<Entry<K,V>> ret = new NodePositionList<Entry<K, V>>(); NodePositionList<Entry<K,V>> ret = new NodePositionList<Entry<K, V>>();
for (int i = 0 ; i< raw_size() ;i++){ for (int i = 0 ; i< raw_size() ;i++){
if (table.get(i) != AVAIBLE){ if (table.get(i) != AVAIBLE && table.get(i) != null ){
ret.addLast(table.get(i)); ret.addLast(table.get(i));
} }
} }
@@ -220,7 +246,7 @@ public class LinearProbingHashTable<K,V> implements Dictionary<K,V> {
table = table2; table = table2;
for (Entry<K,V> e : ite){ for (Entry<K,V> e : ite){
insert(e.getKey(),e.getValue()); insert_refactor(e);
} }
} }

View File

@@ -62,6 +62,18 @@ public class LogFile<K,V> implements Dictionary<K,V> {
return t; return t;
} }
protected Entry<K, V> insert(Entry<K,V> in_e) throws InvalidKeyException {
//in_e not used because used internally
LocationAwareEntry<K,V> t = (LocationAwareEntry<K,V>)in_e;
list.addLast(t);
t.setLocation(list.last());
return t;
}
@Override @Override
public Entry<K, V> remove(Entry<K, V> e) throws InvalidEntryException { public Entry<K, V> remove(Entry<K, V> e) throws InvalidEntryException {
LocationAwareEntry<K,V> t = checkEntry(e); LocationAwareEntry<K,V> t = checkEntry(e);

View File

@@ -47,8 +47,7 @@ public class OrderedListSet <E> implements Set<E> {
@Override @Override
public Set<E> union(Set<E> B) { public Set<E> union(Set<E> B) {return null;
new MergeUnion<E>(this.L, ,c);
} }
@Override @Override