diff --git a/HashTable/.classpath b/HashTable/.classpath new file mode 100644 index 0000000..f7d6ef1 --- /dev/null +++ b/HashTable/.classpath @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/HashTable/.project b/HashTable/.project new file mode 100644 index 0000000..3a68dca --- /dev/null +++ b/HashTable/.project @@ -0,0 +1,17 @@ + + + HashTable + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/HashTable/TestFile1 b/HashTable/TestFile1 new file mode 100644 index 0000000..9b4bdd8 --- /dev/null +++ b/HashTable/TestFile1 @@ -0,0 +1,20 @@ +FQM43;LRD5;OK +GFF8A;HE1Z;Error +NUBE0;BK3G;OK +77CD4;RAXN;Error +M90AX;SC86;Error +H9HPL;MEFY;OK +5F6CC;CF0A;Error +67TOV;8T77;OK +EKDQ7;JSNZ;Error +K3PRT;FEB8;OK +6LYZN;GFWQ;Error +7FJZB;1D7C;OK +UABQ6;JB0N;Error +AW25X;S6OS;OK +1WWPZ;NA86;Error +SGVW5;C6KK;Error +FQM43;UEPL;OK +G2CF7;EW5S;Error +K2X4K;3XV5;OK +K2X4K;3XV5;OK \ No newline at end of file diff --git a/HashTable/src/frame/AllTests.java b/HashTable/src/frame/AllTests.java new file mode 100644 index 0000000..65fbcbb --- /dev/null +++ b/HashTable/src/frame/AllTests.java @@ -0,0 +1,594 @@ +package frame; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.BufferedWriter; +import java.io.FileWriter; +import java.io.IOException; +import java.time.Duration; +import java.util.ArrayList; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.TestReporter; +import org.junit.jupiter.api.TestInstance.Lifecycle; + +import lab.HashTable; + +@DisplayName("HashTable tests") +public class AllTests { + + protected int[] getAdresses(ArrayList table, String key) { + + for (String line : table) { + if (line.matches(".*" + key + ".*")) { + String[] lastPart = line.split("\\x7C"); + if (lastPart.length == 3) { + String allNumbersAndEnd = lastPart[line.split("\\x7C").length - 1].substring(0, + lastPart[line.split("\\x7C").length - 1].indexOf("}")); + String[] allNumbers = allNumbersAndEnd.split(","); + int[] numbers = new int[allNumbers.length]; + for (int i = 0; i < allNumbers.length; i++) { + numbers[i] = Integer.valueOf(allNumbers[i].trim()); + } + return numbers; + } + } + } + + return null; + } + + protected void printArrayList(ArrayList dot) { + try { + FileWriter fw = new FileWriter("test.txt"); + BufferedWriter bw = new BufferedWriter(fw); + + for (String string : dot) { + bw.write(string + System.getProperty("line.separator")); + } + + bw.close(); + fw.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Nested + @TestInstance(Lifecycle.PER_CLASS) + @DisplayName("HashTable(Division, Linear)") + class HashTableDivisionLinear { + + @Test + @DisplayName("Read test file") + public void testReadTestFile1_DivisionLinear(TestReporter reporter) { + HashTable table = new HashTable(10, "division", "linear_probing"); + int loaded = table.loadFromFile("TestFile1"); + assertTrue(loaded == 19, "Didn't load 19 entries from TestFile1 with division and linear_probing."); + } + + @Test + @DisplayName("Insert") + public void testInsert_DivisionLinear() { + Entry testEntry1 = new Entry(); + testEntry1.setKey("ABCDELDXS"); + testEntry1.setData("OK"); + + HashTable table = new HashTable(10, "division", "linear_probing"); + assertTrue(table.insert(testEntry1)); + } + + @Test + @DisplayName("Find") + public void testFind_DivisionLinear() { + Entry testEntry1 = new Entry(); + testEntry1.setKey("ABCDELDXS"); + testEntry1.setData("OK"); + HashTable table = new HashTable(10, "division", "linear_probing"); + + boolean inserted = table.insert(testEntry1); + assertTrue(inserted); + Entry foundEntry = table.find(testEntry1.getKey()); + assertNotNull(foundEntry, "Didn't find Entry " + testEntry1.getKey() + "."); + assertTrue(foundEntry == testEntry1, "Inserted Entry " + testEntry1.getKey() + " and found Entry " + + foundEntry.getKey() + " are not the same."); + } + + @Test + @DisplayName("Delete") + public void testDelete_DivisionLinear() { + Entry testEntry1 = new Entry(); + testEntry1.setKey("ABCDELDXS"); + testEntry1.setData("OK"); + + HashTable table = new HashTable(10, "division", "linear_probing"); + + boolean inserted = table.insert(testEntry1); + assertTrue(inserted); + + Entry deletedEntry = table.delete(testEntry1.getKey()); + assertNotNull(deletedEntry); + + Entry e = table.find(testEntry1.getKey()); + assertNull(e); + } + + @Test + @DisplayName("Home address") + public void testHomeAdress_DivisionLinear() { + Entry testEntry1 = new Entry(); + testEntry1.setKey("Z8IG4LDXS"); + testEntry1.setData("OK"); + + HashTable table = new HashTable(10, "division", "linear_probing"); + table.insert(testEntry1); + ArrayList tableArrayList = table.getHashTable(); + int[] adresses = getAdresses(tableArrayList, testEntry1.getKey()); + assertTrue(adresses == null, testEntry1.getKey() + " is not at home adress."); + } + + @Test + @DisplayName("Collision") + public void testCollisionAdress_DivisionLinear() { + Entry testEntry1 = new Entry(); + testEntry1.setKey("Z8IG4LDXS"); + testEntry1.setData("OK"); + + Entry testEntry2 = new Entry(); + testEntry2.setKey("X8IG4LDXS"); + testEntry2.setData("OK"); + + HashTable table = new HashTable(10, "division", "linear_probing"); + table.insert(testEntry1); + table.insert(testEntry2); + ArrayList tableArrayList = table.getHashTable(); + int[] adresses = getAdresses(tableArrayList, testEntry2.getKey()); + assertTrue(adresses != null, testEntry2.getKey() + " is at home adress."); + assertTrue(adresses.length == 1 && adresses[0] == 2, testEntry2.getKey() + " should have home adress 2."); + } + } + + @Nested + @TestInstance(Lifecycle.PER_CLASS) + @DisplayName("HashTable(Division, Quadratic)") + class HashTableDivisionQuadratic { + + @Test + @DisplayName("Read test file") + public void testReadTestFile1_DivisionQuadratic() { + HashTable table = new HashTable(10, "division", "quadratic_probing"); + assertTrue(table.loadFromFile("TestFile1") == 19, + "Didn't load 19 entries from TestFile1 with division and quadratic_probing."); + } + + @Test + @DisplayName("Insert") + public void testInsert_DivisionQuadratic() { + Entry testEntry1 = new Entry(); + testEntry1.setKey("ABCDELDXS"); + testEntry1.setData("OK"); + HashTable table = new HashTable(10, "division", "quadratic_probing"); + assertNotNull(table.insert(testEntry1)); + } + + @Test + @DisplayName("Delete") + public void testDelete_DivisionQuadratic() { + Entry testEntry1 = new Entry(); + testEntry1.setKey("ABCDELDXS"); + testEntry1.setData("OK"); + HashTable table = new HashTable(10, "division", "quadratic_probing"); + assertNotNull(table.insert(testEntry1)); + assertNotNull(table.delete(testEntry1.getKey())); + assertNull(table.find(testEntry1.getKey())); + } + + @Test + @DisplayName("Find") + public void testFind_DivisionQuadratic() { + Entry testEntry1 = new Entry(); + testEntry1.setKey("ABCDELDXS"); + testEntry1.setData("OK"); + HashTable table = new HashTable(10, "division", "quadratic_probing"); + assertNotNull(table.insert(testEntry1)); + Entry foundEntry = table.find(testEntry1.getKey()); + assertNotNull(foundEntry, "Didn't find Entry " + testEntry1.getKey() + "."); + assertTrue(foundEntry == testEntry1, "Inserted Entry " + testEntry1.getKey() + " and found Entry " + + foundEntry.getKey() + " are not the same."); + } + + @Test + @DisplayName("Home address") + public void testHomeAdress_DivisionQuadratic() { + Entry testEntry1 = new Entry(); + testEntry1.setKey("Z8IG4LDXS"); + testEntry1.setData("OK"); + HashTable table = new HashTable(10, "division", "quadratic_probing"); + table.insert(testEntry1); + ArrayList tableArrayList = table.getHashTable(); + int[] adresses = getAdresses(tableArrayList, testEntry1.getKey()); + assertTrue(adresses == null, testEntry1.getKey() + " is not at home adress."); + } + + @Test + @DisplayName("Collision") + public void testCollisionAdress_DivisionQuadratic() { + Entry testEntry1 = new Entry(); + testEntry1.setKey("Z8IG4LDXS"); + testEntry1.setData("OK"); + Entry testEntry2 = new Entry(); + testEntry2.setKey("X8IG4LDXS"); + testEntry2.setData("OK"); + HashTable table = new HashTable(10, "division", "quadratic_probing"); + table.insert(testEntry1); + table.insert(testEntry2); + ArrayList tableArrayList = table.getHashTable(); + int[] adresses = getAdresses(tableArrayList, testEntry2.getKey()); + assertTrue(adresses != null, testEntry2.getKey() + " is at home adress."); + assertTrue(adresses.length == 1 && adresses[0] == 2, testEntry2.getKey() + " should have home adress 2."); + } + } + + @Nested + @TestInstance(Lifecycle.PER_CLASS) + @DisplayName("HashTable(MidSquare, Linear)") + class HashTableMidSquareLinear { + + @Test + @DisplayName("Read test file") + public void testReadTestFile1_MidSquareLinear() { + HashTable table = new HashTable(10, "mid_square", "linear_probing"); + assertTrue(table.loadFromFile("TestFile1") == 19, + "Didn't load 19 entries from TestFile1 with mid_square and linear_probing."); + } + + @Test + @DisplayName("Insert") + public void testInsert_MidSquareLinear() { + Entry testEntry1 = new Entry(); + testEntry1.setKey("ABCDELDXS"); + testEntry1.setData("OK"); + + HashTable table = new HashTable(10, "mid_square", "linear_probing"); + assertNotNull(table.insert(testEntry1)); + } + + @Test + @DisplayName("Find") + public void testFind_MidSquareLinear() { + Entry testEntry1 = new Entry(); + testEntry1.setKey("ABCDELDXS"); + testEntry1.setData("OK"); + + HashTable table = new HashTable(10, "mid_square", "linear_probing"); + assertNotNull(table.insert(testEntry1)); + Entry foundEntry = table.find(testEntry1.getKey()); + assertNotNull(foundEntry, "Didn't find Entry " + testEntry1.getKey() + "."); + assertTrue(foundEntry == testEntry1, "Inserted Entry " + testEntry1.getKey() + " and found Entry " + + foundEntry.getKey() + " are not the same."); + } + + @Test + @DisplayName("Delete") + public void testDelete_MidSquareLinear() { + Entry testEntry1 = new Entry(); + testEntry1.setKey("ABCDELDXS"); + testEntry1.setData("OK"); + + HashTable table = new HashTable(10, "mid_square", "linear_probing"); + assertNotNull(table.insert(testEntry1)); + assertNotNull(table.delete(testEntry1.getKey())); + assertNull(table.find(testEntry1.getKey())); + } + + @Test + @DisplayName("Home address") + public void testHomeAdress_MidSquareLinear() { + Entry testEntry1 = new Entry(); + testEntry1.setKey("Z8IG4LDXS"); + testEntry1.setData("OK"); + + HashTable table = new HashTable(10, "mid_square", "linear_probing"); + table.insert(testEntry1); + ArrayList tableArrayList = table.getHashTable(); + int[] adresses = getAdresses(tableArrayList, testEntry1.getKey()); + assertTrue(adresses == null, testEntry1.getKey() + " is not at home adress."); + } + + @Test + @DisplayName("Collision") + public void testCollisionAdress_MidSquareLinear() { + + Entry testEntry1 = new Entry(); + testEntry1.setKey("Z8IG4LDXS"); + + testEntry1.setData("OK"); + + Entry testEntry2 = new Entry(); + testEntry2.setKey("F5HF8MECA"); + testEntry2.setData("OK"); + + HashTable table = new HashTable(10, "mid_square", "linear_probing"); + table.insert(testEntry1); + table.insert(testEntry2); + ArrayList tableArrayList = table.getHashTable(); + int[] adresses = getAdresses(tableArrayList, testEntry2.getKey()); + assertTrue(adresses != null, testEntry2.getKey() + " is at home adress."); + assertTrue(adresses.length == 1 && adresses[0] == 0, testEntry2.getKey() + " should have home adress 3."); + + } + } + + @Nested + @TestInstance(Lifecycle.PER_CLASS) + @DisplayName("HashTable(MidSquare, Quadratic)") + class HashTableMidSquareQuadratic { + + @Test + @DisplayName("Read test file") + public void testReadTestFile1_MidSquareQuadractic() { + HashTable table = new HashTable(10, "mid_square", "quadratic_probing"); + assertTrue(table.loadFromFile("TestFile1") == 19, + "Didn't load 19 entries from TestFile1 with mid_square and quadratic_probing."); + } + + @Test + @DisplayName("Insert") + public void testInsert_MidSquareQuadratic() { + Entry testEntry1 = new Entry(); + testEntry1.setKey("ABCDELDXS"); + testEntry1.setData("OK"); + + HashTable table = new HashTable(10, "mid_square", "quadratic_probing"); + assertNotNull(table.insert(testEntry1)); + } + + @Test + @DisplayName("Find") + public void testFind_MidSquareQuadratic() { + Entry testEntry1 = new Entry(); + testEntry1.setKey("ABCDELDXS"); + testEntry1.setData("OK"); + + HashTable table = new HashTable(10, "mid_square", "quadratic_probing"); + assertNotNull(table.insert(testEntry1)); + Entry foundEntry = table.find(testEntry1.getKey()); + assertNotNull(foundEntry, "Didn't find Entry " + testEntry1.getKey() + "."); + assertTrue(foundEntry == testEntry1, "Inserted Entry " + testEntry1.getKey() + " and found Entry " + + foundEntry.getKey() + " are not the same."); + } + + @Test + @DisplayName("Delete") + public void testDelete_MidSquareQuadratic() { + Entry testEntry1 = new Entry(); + testEntry1.setKey("ABCDELDXS"); + testEntry1.setData("OK"); + + HashTable table = new HashTable(10, "mid_square", "quadratic_probing"); + assertNotNull(table.insert(testEntry1)); + assertNotNull(table.delete(testEntry1.getKey())); + assertNull(table.find(testEntry1.getKey())); + } + + @Test + @DisplayName("Home address") + public void testHomeAdress_MidSquareQuadratic() { + Entry testEntry1 = new Entry(); + testEntry1.setKey("Z8IG4LDXS"); + testEntry1.setData("OK"); + + HashTable table = new HashTable(10, "mid_square", "quadratic_probing"); + table.insert(testEntry1); + ArrayList tableArrayList = table.getHashTable(); + int[] adresses = getAdresses(tableArrayList, testEntry1.getKey()); + assertTrue(adresses == null, testEntry1.getKey() + " is not at home adress."); + } + + @Test + @DisplayName("Collision") + public void testCollisionAdress_MidSquareQuadratic() { + + Entry testEntry1 = new Entry(); + testEntry1.setKey("Z8IG4LDXS"); + + testEntry1.setData("OK"); + + Entry testEntry2 = new Entry(); + testEntry2.setKey("F5HF8MECA"); + testEntry2.setData("OK"); + + HashTable table = new HashTable(10, "mid_square", "quadratic_probing"); + table.insert(testEntry1); + table.insert(testEntry2); + ArrayList tableArrayList = table.getHashTable(); + int[] adresses = getAdresses(tableArrayList, testEntry2.getKey()); + assertTrue(adresses != null, testEntry2.getKey() + " is at home adress."); + assertTrue(adresses.length == 1 && adresses[0] == 0, testEntry2.getKey() + " should have home adress 0."); + } + + } + + @Nested + @TestInstance(Lifecycle.PER_CLASS) + @DisplayName("HashTable(Folding, Linear)") + class HashTableFoldingLinear { + + @Test + @DisplayName("Read test file") + public void testReadTestFile1_FoldingLinear() { + HashTable table = new HashTable(10, "folding", "linear_probing"); + assertTrue(table.loadFromFile("TestFile1") == 19, + "Didn't load 19 entries from TestFile1 with folding and linear_probing."); + } + + @Test + @DisplayName("Insert") + public void testInsert_FoldingLinear() { + Entry testEntry1 = new Entry(); + testEntry1.setKey("ABCDELDXS"); + testEntry1.setData("OK"); + + HashTable table = new HashTable(10, "folding", "linear_probing"); + assertNotNull(table.insert(testEntry1)); + } + + @Test + @DisplayName("Find") + public void testFind_FoldingLinear() { + Entry testEntry1 = new Entry(); + testEntry1.setKey("ABCDELDXS"); + testEntry1.setData("OK"); + + HashTable table = new HashTable(10, "folding", "linear_probing"); + assertNotNull(table.insert(testEntry1)); + Entry foundEntry = table.find(testEntry1.getKey()); + assertNotNull(foundEntry, "Didn't find Entry " + testEntry1.getKey() + "."); + assertTrue(foundEntry == testEntry1, "Inserted Entry " + testEntry1.getKey() + " and found Entry " + + foundEntry.getKey() + " are not the same."); + } + + @Test + @DisplayName("Delete") + public void testDelete_FoldingLinear() { + Entry testEntry1 = new Entry(); + testEntry1.setKey("ABCDELDXS"); + testEntry1.setData("OK"); + + HashTable table = new HashTable(10, "folding", "linear_probing"); + assertNotNull(table.insert(testEntry1)); + assertNotNull(table.delete(testEntry1.getKey())); + assertNull(table.find(testEntry1.getKey())); + } + + @Test + @DisplayName("Home address") + public void testHomeAdress_FoldingLinear() { + Entry testEntry1 = new Entry(); + testEntry1.setKey("Z8IG4LDXS"); + testEntry1.setData("OK"); + + HashTable table = new HashTable(10, "folding", "linear_probing"); + table.insert(testEntry1); + ArrayList tableArrayList = table.getHashTable(); + int[] adresses = getAdresses(tableArrayList, testEntry1.getKey()); + assertTrue(adresses == null, testEntry1.getKey() + " is not at home adress."); + } + + @Test + @DisplayName("Collision") + public void testCollisionAdress_FoldingLinear() { + Entry testEntry1 = new Entry(); + testEntry1.setKey("Z8IG4LDXS"); + testEntry1.setData("OK"); + + Entry testEntry2 = new Entry(); + testEntry2.setKey("Z7IG5LDXS"); + testEntry2.setData("OK"); + + HashTable table = new HashTable(10, "folding", "linear_probing"); + table.insert(testEntry1); + table.insert(testEntry2); + ArrayList tableArrayList = table.getHashTable(); + int[] adresses = getAdresses(tableArrayList, testEntry2.getKey()); + + assertTrue(adresses != null, testEntry2.getKey() + " is at home adress."); + assertTrue(adresses.length == 1 && adresses[0] == 5, testEntry2.getKey() + " should have home adress 5."); + + } + } + + @Nested + @TestInstance(Lifecycle.PER_CLASS) + @DisplayName("HashTable(Folding, Quadratic)") + class HashTableFoldingQuadratic { + + @Test + @DisplayName("Read test file") + public void testReadTestFile1_FoldingQuadratic() { + HashTable table = new HashTable(10, "folding", "quadratic_probing"); + assertTrue(table.loadFromFile("TestFile1") == 19, + "Didn't load 19 entries from TestFile1 with folding and quadratic_probing."); + } + + @Test + @DisplayName("Insert") + public void testInsert_FoldingQuadratic() { + Entry testEntry1 = new Entry(); + testEntry1.setKey("ABCDELDXS"); + testEntry1.setData("OK"); + + HashTable table = new HashTable(10, "folding", "quadratic_probing"); + assertNotNull(table.insert(testEntry1)); + } + + @Test + @DisplayName("Find") + public void testFind_FoldingQuadratic() { + Entry testEntry1 = new Entry(); + testEntry1.setKey("ABCDELDXS"); + testEntry1.setData("OK"); + + HashTable table = new HashTable(10, "folding", "quadratic_probing"); + assertNotNull(table.insert(testEntry1)); + Entry foundEntry = table.find(testEntry1.getKey()); + assertNotNull(foundEntry, "Didn't find Entry " + testEntry1.getKey() + "."); + assertTrue(foundEntry == testEntry1, "Inserted Entry " + testEntry1.getKey() + " and found Entry " + + foundEntry.getKey() + " are not the same."); + } + + @Test + @DisplayName("Delete") + public void testDelete_FoldingQuadratic() { + Entry testEntry1 = new Entry(); + testEntry1.setKey("ABCDELDXS"); + testEntry1.setData("OK"); + + HashTable table = new HashTable(10, "folding", "quadratic_probing"); + assertNotNull(table.insert(testEntry1)); + assertNotNull(table.delete(testEntry1.getKey())); + assertNull(table.find(testEntry1.getKey())); + } + + @Test + @DisplayName("Home address") + public void testHomeAdress_FoldingQuadratic() { + Entry testEntry1 = new Entry(); + testEntry1.setKey("Z8IG4LDXS"); + testEntry1.setData("OK"); + + HashTable table = new HashTable(10, "folding", "quadratic_probing"); + table.insert(testEntry1); + ArrayList tableArrayList = table.getHashTable(); + int[] adresses = getAdresses(tableArrayList, testEntry1.getKey()); + assertTrue(adresses == null, testEntry1.getKey() + " is not at home adress."); + } + + @Test + @DisplayName("Collision") + public void testCollisionAdress_FoldingQuadratic() { + Entry testEntry1 = new Entry(); + testEntry1.setKey("Z8IG4LDXS"); + testEntry1.setData("OK"); + + Entry testEntry2 = new Entry(); + testEntry2.setKey("Z7IG5LDXS"); + testEntry2.setData("OK"); + + HashTable table = new HashTable(10, "folding", "quadratic_probing"); + table.insert(testEntry1); + table.insert(testEntry2); + ArrayList tableArrayList = table.getHashTable(); + int[] adresses = getAdresses(tableArrayList, testEntry2.getKey()); + assertTrue(adresses != null, testEntry2.getKey() + " is at home adress."); + assertTrue(adresses.length == 1 && adresses[0] == 5, testEntry2.getKey() + " should have home adress 5."); + } + } + +} \ No newline at end of file diff --git a/HashTable/src/frame/Entry.java b/HashTable/src/frame/Entry.java new file mode 100644 index 0000000..0c4e184 --- /dev/null +++ b/HashTable/src/frame/Entry.java @@ -0,0 +1,68 @@ +package frame; + +public class Entry implements Comparable { + + private String key; + private String data; + private boolean deleted; + + /** Creates a new instance of Entry */ + public Entry() { + this.key = null; + this.data = null; + this.deleted = false; + } + + public Entry(String bookSerialNumber, String ReaderID, String Status) { + this.key = new String(bookSerialNumber.concat(ReaderID)); + this.data = new String(Status); + } + + public void setKey(String newKey) { + this.key = new String(newKey); + } + + public void setData(String newData) { + this.data = new String(newData); + } + + public String getKey() { + if (this.key != null) { + return this.key; + } else { + return null; + } + } + + public String getData() { + if (this.data != null) { + return this.data; + } else { + return null; + } + } + + public void markDeleted() { + this.deleted = true; + } + + public boolean isDeleted() { + return this.deleted; + } + + public String toString() { + String r = new String(); + if (this.key != null) { + r = this.key.substring(0, 5) + ";" + this.key.substring(5); + } + if (this.data != null) { + r = r + ";" + this.data; + } + + return r; + } + + public int compareTo(Entry e) { + return this.key.compareTo(e.getKey()); + } +} \ No newline at end of file diff --git a/HashTable/src/lab/HashTable.java b/HashTable/src/lab/HashTable.java new file mode 100644 index 0000000..2085759 --- /dev/null +++ b/HashTable/src/lab/HashTable.java @@ -0,0 +1,144 @@ +package lab; + +import java.util.ArrayList; + +import frame.Entry; + +/* + * Implements a Hash-Table structure as introduced in the + * lecture to store the information read by the RFID + * readers in the library. + * + * Make sure that you have tested all the given test cases + * given on the homepage before you submit your solution. + * + */ + +public class HashTable { + + /** + * The constructor + * + * @param initialCapacity + * represents the initial size of the Hash Table. + * @param hashFunction + * can have the following values: division folding mid_square + * @param collisionResolution + * can have the following values: linear_probing quadratic_probing + * + * The Hash-Table itself should be implemented as an array of entries + * (Entry[] in Java) and no other implementation will be accepted. + * When the load factor exceeds 75%, the capacity of the Hash-Table + * should be increased as described in the method rehash below. We + * assume a bucket factor of 1. + */ + public HashTable(int k, String hashFunction, String collisionResolution) { + /** + * Add your code here + */ + } + + /** + * This method takes as input the name of a file containing a sequence of + * entries that should be inserted into the Hash-Table in the order they appear + * in the file. You cannot make any assumptions on the order of the entries nor + * is it allowed to change the order given in the file. You can assume that the + * file is located in the same directory as the executable program. The input + * file is similar to the input file for lab 1. The return value is the number + * of entries successfully inserted into the Hash-Table. + * + * @param filename + * name of the file containing the entries + * @return returns the number of entries successfully inserted in the + * Hash-Table. + */ + public int loadFromFile(String filename) { + /** + * Add your code here + */ + return 0; + } + + /** + * This method inserts the entry insertEntry into the Hash-Table. Note that you + * have to deal with collisions if you want to insert an entry into a slot which + * is not empty. This method returns true if the insertion of the entry + * insertEntry is successful and false if the key of this entry already exists + * in the Hash-Table (the existing key/value pair is left unchanged). + * + * @param insertEntry + * entry to insert into the Hash-table + * @return returns true if the entry insertEntry is successfully inserted false + * if the entry already exists in the Hash-Table + */ + public boolean insert(Entry insertEntry) { + /** + * Add your code here + */ + return false; + } + + /** + * This method deletes the entry from the Hash-Table, having deleteKey as key + * This method returns the entry, having deleteKey as key if the deletion is + * successful and null if the key deleteKey is not found in the Hash-Table. + * + * @param deleteKey + * key of the entry to delete from the Hash-Table + * @return returns the deleted entry if the deletion ends successfully null if + * the entry is not found in the Hash-Table + */ + public Entry delete(String deleteKey) { + /** + * Add your code here + */ + return null; + } + + /** + * This method searches in the Hash-Table for the entry with key searchKey. It + * returns the entry, having searchKey as key if such an entry is found, null + * otherwise. + * + * @param searchKey + * key of the entry to find in the Hash-table + * @return returns the entry having searchKey as key if such an entry exists + * null if the entry is not found in the Hash-Table + */ + public Entry find(String searchKey) { + /** + * Add your code here + */ + return null; + } + + /** + * This method returns a ArrayList containing the output Hash-Table. The + * output should be directly interpretable dot code. Each item in the ArrayList + * corresponds to one line of the output Hash-Table. The nodes of the output + * Hash-Table should contain the keys of the entries and also the data. + * + * @return returns the output Hash-Table in directly interpretable dot code + */ + public ArrayList getHashTable() { + /** + * Add your code here + */ + return null; + } + + /** + * This method increases the capacity of the Hash-Table and reorganizes it, in + * order to accommodate and access its entries more efficiently. This method is + * called automatically when the load factor exceeds 75%. To increase the size + * of the Hash-Table, you multiply the actual capacity by 10 and search for the + * closest primary number less than the result of this multiplication. For + * example if the actual capacity of the Hash-Table is 101, the capacity will be + * increased to 1009, which is the closest primary number less than (101*10). + */ + private void rehash() { + /** + * Add your code here + */ + } +}