mirror of
https://github.com/tu-darmstadt-informatik/AuD18.git
synced 2025-12-13 09:55:49 +00:00
Add debug output, fix dot file export
This commit is contained in:
parent
eeaada2da0
commit
245170729b
@ -6,6 +6,7 @@ import java.io.FileReader;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import frame.Entry;
|
import frame.Entry;
|
||||||
|
|
||||||
@ -50,6 +51,7 @@ public class HashTable {
|
|||||||
this.probingMode = collisionResolution.equals("quadratic_probing") ? 1 : 0;
|
this.probingMode = collisionResolution.equals("quadratic_probing") ? 1 : 0;
|
||||||
this.valueCount = 0;
|
this.valueCount = 0;
|
||||||
this.capacity = k;
|
this.capacity = k;
|
||||||
|
System.out.println("CONSTRUCTOR CALLED "+hashFunction+" "+collisionResolution);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -107,15 +109,15 @@ public class HashTable {
|
|||||||
public boolean insert(Entry insertEntry) {
|
public boolean insert(Entry insertEntry) {
|
||||||
int address = this.getHashAddr(insertEntry);
|
int address = this.getHashAddr(insertEntry);
|
||||||
int base = address;
|
int base = address;
|
||||||
int i = 1;
|
int i = 0;
|
||||||
|
|
||||||
// Find a free or deleted slot to insert the entry
|
// Find a free or deleted slot to insert the entry
|
||||||
while(this.data[address] != null && !this.data[address].isDeleted()) {
|
while(this.data[address] != null && !this.data[address].isDeleted()) {
|
||||||
// Don't insert if there is no more space (this shouldn't happen due to auto-rehashing) or if the key is already present
|
// Don't insert if there is no more space (this shouldn't happen due to auto-rehashing) or if the key is already present
|
||||||
if(i > this.capacity || this.data[address].getKey().equals(insertEntry.getKey()))
|
if(i > this.capacity || this.data[address].getKey().equals(insertEntry.getKey()))
|
||||||
return false;
|
return false;
|
||||||
address = this.getProbingAddr(base, i);
|
|
||||||
i += 1;
|
i += 1;
|
||||||
|
address = this.getProbingAddr(base, i);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we are using a previously unused slot, increase the valueCount for accurate load calculation
|
// If we are using a previously unused slot, increase the valueCount for accurate load calculation
|
||||||
@ -125,6 +127,8 @@ public class HashTable {
|
|||||||
// Save the entry
|
// Save the entry
|
||||||
this.data[address] = insertEntry;
|
this.data[address] = insertEntry;
|
||||||
|
|
||||||
|
//System.out.println("Inserting at "+address+" on try "+i+": "+insertEntry.getKey());
|
||||||
|
|
||||||
// Trigger rehash if load factor > 0.75
|
// Trigger rehash if load factor > 0.75
|
||||||
if(((double) this.valueCount) / this.capacity > 0.75)
|
if(((double) this.valueCount) / this.capacity > 0.75)
|
||||||
this.rehash();
|
this.rehash();
|
||||||
@ -211,10 +215,10 @@ public class HashTable {
|
|||||||
dot.add("splines=true;");
|
dot.add("splines=true;");
|
||||||
dot.add("nodesep=.01;");
|
dot.add("nodesep=.01;");
|
||||||
dot.add("rankdir=LR;");
|
dot.add("rankdir=LR;");
|
||||||
dot.add("node[fontsize=8,shape=record,height=.1;]");
|
dot.add("node[fontsize=8,shape=record,height=.1];");
|
||||||
|
|
||||||
// Generate definition for address boxes / labels
|
// Generate definition for address boxes / labels
|
||||||
String addressdef = "[fontsize=12,label=\"";
|
String addressdef = "ht[fontsize=12,label=\"";
|
||||||
for(int i = 0; i < this.capacity; i++) {
|
for(int i = 0; i < this.capacity; i++) {
|
||||||
addressdef += "<f"+Integer.toString(i)+">"+Integer.toString(i)+(i < this.capacity - 1 ? "|" : "");
|
addressdef += "<f"+Integer.toString(i)+">"+Integer.toString(i)+(i < this.capacity - 1 ? "|" : "");
|
||||||
}
|
}
|
||||||
@ -244,6 +248,12 @@ public class HashTable {
|
|||||||
|
|
||||||
// Finally, add a closing bracket
|
// Finally, add a closing bracket
|
||||||
dot.add("}");
|
dot.add("}");
|
||||||
|
|
||||||
|
// Dump dot code for debugging
|
||||||
|
System.out.println("BEGIN DATA DUMP");
|
||||||
|
System.out.println(this.capacity + (this.hashFunction == 1 ? " folding " : this.hashFunction == 2 ? " midSquare " : " division ")+(this.probingMode == 0 ? "linear" : "quadratic"));
|
||||||
|
System.out.println(dot.stream().collect(Collectors.joining("\n")));
|
||||||
|
System.out.println("END DATA DUMP");
|
||||||
return dot;
|
return dot;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -266,6 +276,8 @@ public class HashTable {
|
|||||||
newSize -= 1;
|
newSize -= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
System.out.println("REHASH: "+newSize);
|
||||||
|
|
||||||
// Save old HashTable
|
// Save old HashTable
|
||||||
int oldSize = this.capacity;
|
int oldSize = this.capacity;
|
||||||
Entry[] oldData = this.data;
|
Entry[] oldData = this.data;
|
||||||
@ -394,7 +406,7 @@ public class HashTable {
|
|||||||
return (base + i) % this.capacity;
|
return (base + i) % this.capacity;
|
||||||
else {
|
else {
|
||||||
// i-th try in quadratic mode is ([home address] - ceil((i/2)² * (-1)^i) mod capacity
|
// i-th try in quadratic mode is ([home address] - ceil((i/2)² * (-1)^i) mod capacity
|
||||||
int nextTry = ((int)(base - Math.ceil(Math.pow(((float) i)/2, 2))*Math.pow(-1, i))) % this.capacity;
|
int nextTry = ((int)(base - Math.ceil(Math.pow(((double) i)/2, 2))*Math.pow(-1, i))) % this.capacity;
|
||||||
return nextTry < 0 ? nextTry + this.capacity : nextTry; // Add capacity to result if negative
|
return nextTry < 0 ? nextTry + this.capacity : nextTry; // Add capacity to result if negative
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user