From 245170729bd852e3f825316a154685bc6c5ad811 Mon Sep 17 00:00:00 2001 From: rec0de Date: Wed, 16 May 2018 16:22:07 +0200 Subject: [PATCH] Add debug output, fix dot file export --- HashTable/src/lab/HashTable.java | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/HashTable/src/lab/HashTable.java b/HashTable/src/lab/HashTable.java index 478d781..ca0a69c 100644 --- a/HashTable/src/lab/HashTable.java +++ b/HashTable/src/lab/HashTable.java @@ -6,6 +6,7 @@ import java.io.FileReader; import java.io.IOException; import java.math.BigInteger; import java.util.ArrayList; +import java.util.stream.Collectors; import frame.Entry; @@ -50,6 +51,7 @@ public class HashTable { this.probingMode = collisionResolution.equals("quadratic_probing") ? 1 : 0; this.valueCount = 0; this.capacity = k; + System.out.println("CONSTRUCTOR CALLED "+hashFunction+" "+collisionResolution); } /** @@ -107,15 +109,15 @@ public class HashTable { public boolean insert(Entry insertEntry) { int address = this.getHashAddr(insertEntry); int base = address; - int i = 1; + int i = 0; // Find a free or deleted slot to insert the entry 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 if(i > this.capacity || this.data[address].getKey().equals(insertEntry.getKey())) return false; - address = this.getProbingAddr(base, i); i += 1; + address = this.getProbingAddr(base, i); } // 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 this.data[address] = insertEntry; + //System.out.println("Inserting at "+address+" on try "+i+": "+insertEntry.getKey()); + // Trigger rehash if load factor > 0.75 if(((double) this.valueCount) / this.capacity > 0.75) this.rehash(); @@ -211,10 +215,10 @@ public class HashTable { dot.add("splines=true;"); dot.add("nodesep=.01;"); 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 - String addressdef = "[fontsize=12,label=\""; + String addressdef = "ht[fontsize=12,label=\""; for(int i = 0; i < this.capacity; i++) { addressdef += ""+Integer.toString(i)+(i < this.capacity - 1 ? "|" : ""); } @@ -244,6 +248,12 @@ public class HashTable { // Finally, add a closing bracket 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; } @@ -266,6 +276,8 @@ public class HashTable { newSize -= 1; } + System.out.println("REHASH: "+newSize); + // Save old HashTable int oldSize = this.capacity; Entry[] oldData = this.data; @@ -394,7 +406,7 @@ public class HashTable { return (base + i) % this.capacity; else { // 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 } }