Fixed bugs to Dictionary Classes
This commit is contained in:
@@ -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);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user