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{
ChainingHashTable<String,Integer> h = new ChainingHashTable<String, Integer>();
ChainingHashTable<String,Integer> h = new ChainingHashTable<String, Integer>(1);
System.out.println(h.load_factor());

View File

@@ -17,7 +17,8 @@ public class LinearProbingHashTableTest {
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());
@@ -89,12 +90,15 @@ public class LinearProbingHashTableTest {
Entry<String,Integer> f = h.insert("7", 100);
System.out.println(h);
h.remove(f);
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
public Entry<K, V> remove(Entry<K, V> e) throws InvalidEntryException {
if (e == null)
@@ -180,7 +196,7 @@ public class ChainingHashTable<K,V> implements Dictionary<K,V> {
table = table2;
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);
int h = hash(key);
int count = 0;
int i = 0;
for (;count < raw_size();){
if (table.get(h+i) == AVAIBLE)
if (table.get( (h+count)%raw_size() ) == AVAIBLE)
return null;
if (table.get(h+i).getKey().equals(key))
return table.get(h+i);
i= (i + 1)% raw_size();
if (table.get((h+count)%raw_size()) == null)
continue;
if (table.get((h+count)%raw_size()).getKey().equals(key))
return table.get((h+count)%raw_size());
count++;
}
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>>();
int h = hash(key);
int count = 0;
int i = 0;
for (;count < raw_size();){
if (table.get(h+i) == AVAIBLE)
if (table.get((h+count)%raw_size()) == AVAIBLE)
return to_return;
if (table.get(h+i).getKey().equals(key))
to_return.addLast( table.get(h+i));
i= (i + 1)% raw_size();
if (table.get((h+count)%raw_size()) == null)
continue;
if (table.get((h+count)%raw_size()).getKey().equals(key))
to_return.addLast( table.get((h+count)%raw_size()));
count++;
}
return to_return;
@@ -126,29 +126,51 @@ public class LinearProbingHashTable<K,V> implements Dictionary<K,V> {
if (load_factor() >= 0.5 )
reformat();
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);
table.set(h,n);
avaible_slots--;
elements++;
return n;
} else {
int i = 1;
int count = 1;
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);
table.set(h + i, n);
table.set((h+count)%raw_size(), n);
avaible_slots--;
elements++;
return n;
}
i = (i + 1) % raw_size();
count++;
}
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
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());
if (table.get(h) == AVAIBLE)
throw new InvalidEntryException();
if (table.get(h) == e) {
Entry<K, V> removed = table.get(h);
table.remove(h);
table.set(h,AVAIBLE);
elements--;
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);
if (table.get(h) == null){
} else {
if (table.get(h) == e) {
table.set(h, null);
elements--;
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++;
}
@@ -189,7 +215,7 @@ public class LinearProbingHashTable<K,V> implements Dictionary<K,V> {
NodePositionList<Entry<K,V>> ret = new NodePositionList<Entry<K, V>>();
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));
}
}
@@ -220,7 +246,7 @@ public class LinearProbingHashTable<K,V> implements Dictionary<K,V> {
table = table2;
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;
}
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
public Entry<K, V> remove(Entry<K, V> e) throws InvalidEntryException {
LocationAwareEntry<K,V> t = checkEntry(e);

View File

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