From 06cab6b5bb3e0d46e3fb6e12e2847db658bf198a Mon Sep 17 00:00:00 2001 From: UsefSaeed Date: Fri, 13 May 2022 00:30:33 +0200 Subject: [PATCH] method2 nearly done check driver for important notes --- .idea/workspace.xml | 55 ++++++++++++++- src/Driver.java | 27 ++++++++ src/PerfectHashing/Method1.java | 55 +++++++++++++-- src/PerfectHashing/Method2.java | 115 +++++++++++++++++++++++++++++++- 4 files changed, 243 insertions(+), 9 deletions(-) create mode 100644 src/Driver.java diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 8e99ea9..074e001 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,7 +1,12 @@ - + + + + + + \ No newline at end of file diff --git a/src/Driver.java b/src/Driver.java new file mode 100644 index 0000000..37b2dbf --- /dev/null +++ b/src/Driver.java @@ -0,0 +1,27 @@ +public class Driver { + public static void main(String[] args){ + int[] a = {12,21,32,43,4,5,3,356,3224,54,455}; + PerfectHashing.Method1 m1 = new PerfectHashing.Method1(a); + PerfectHashing.Method2 m2 = new PerfectHashing.Method2(a); + /** + * MONO DO YOUR THING (ANALYSIS) + */ + } +} +/** + * Important Notes: + * 1. Turned out that collision may happen in perfect hashing method2 for some reason + * 1.1. Realised that though testing + * 1.2. Used method1 code (still incomplete) in method2 perfectRehash() after figuring out all collisions + * 1.3. Once the mainLogic() in method1 works perfectly, method2 also works perfectly + * 1.3.1. finalHashTable includes all elements passed to method1 as well as zeros in null places + * 1.3.2. hashMatrix includes matrix used to get the element's keys + * 2. Method2 is fully tested (not including the method1 part for sure) + * 3. If no collision takes place in method2, method1 is not used. So, testcase runs successfully + * 4. Don't forget the search() in method1 because I was about to, lol + * 5. Running the same testcase several times will produce different outcome due to randomization + * 5.1. may make debugging certain cases really difficult as some runs may work and others not + * 6. I'll sleep at 4-5 am and wake up at 12 pm, be free at 1-6 pm + * 7. Yeah. I also added some methods from method2 you will need in method1, some getters and attributes that are also needed + * GOODLUCK BRO + */ \ No newline at end of file diff --git a/src/PerfectHashing/Method1.java b/src/PerfectHashing/Method1.java index 9ad5752..e0a526f 100644 --- a/src/PerfectHashing/Method1.java +++ b/src/PerfectHashing/Method1.java @@ -1,16 +1,61 @@ package PerfectHashing; +import java.util.Random; + public class Method1 { - private int[] input; - private int size; + private final int[] input; + private final int size; + private int[] hashMatrix; + private int[] finalHashTable; public Method1(int[] input) { this.input = input; - this.size = input.length*input.length; + this.size = input.length; + this.hashMatrix = generateHashMatrix(this.size*this.size); + this.finalHashTable = new int[this.size*this.size]; + this.mainLogic(); } - int[] generateHashMatrix(){ + int[] generateHashMatrix(int size){ int[] res = new int[size]; - return res; + Random rand = new Random(); + for (int i=0; i(); + beenHereBefore[key] = true; + collisionCont[key].add(input[i]); + } + for (int i=0;i) collisionCont[i]); + Method1 m = new Method1(lvl2CurrentBlock); + hashLvl2[i] = m.getFinalHashTable(); + hashMatricesLvl2[i] = m.getHashMatrix(); + } + + } + + int[] convertToArray(ArrayList a){ + int[] b = new int[a.size()]; + int i=0; + for (int elem : a) b[i++] = elem; + return b; + } + + void search(int rtf){ + int key1 = getKey(mainHashMatrix,rtf); + boolean found = false; + if(!usedLvl2){ + if (hashLvl1[key1]==rtf) found = true; + }else{ + int key2 = getKey(hashMatricesLvl2[key1],rtf); + if (hashLvl2[key1][key2]==rtf) found = true; + } + if (found) + System.out.print("Found it :)"); + else + System.out.print("It doesn't exist :("); + } + +// public static void main(String[] args){ +// int[] a = {12,21,32,43,4,5,3,356,3224,54,455}; +// Method2 m = new Method2(a); +// } }