aanzet hash table

This commit is contained in:
Wouter Groeneveld 2018-03-27 09:21:32 +02:00
parent 8bcbd6c823
commit 02a2424565
8 changed files with 150 additions and 0 deletions

View File

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library" scope="TEST">
<library name="JUnit5.0">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.0.0/junit-jupiter-api-5.0.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.0.0/apiguardian-api-1.0.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.0.0/opentest4j-1.0.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.0.0/junit-platform-commons-1.0.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>

View File

@ -0,0 +1,19 @@
package be.brainbaking.datastructures.hashing;
public class DoubleProbeHash implements Hashable {
private final int max;
public DoubleProbeHash(int max) {
this.max = max;
}
private int auxiliarHash(Object key, int probeStep) {
return (1 + key.hashCode()) * (max - 1);
}
@Override
public int hash(Object key, int probeStep) {
return (auxiliarHash(key, probeStep) + probeStep) % max;
}
}

View File

@ -0,0 +1,7 @@
package be.brainbaking.datastructures.hashing;
public interface Hashable {
int hash(Object key, int i);
}

View File

@ -0,0 +1,15 @@
package be.brainbaking.datastructures.hashing;
public class LinearProbeHash implements Hashable {
private final int max;
public LinearProbeHash(int max) {
this.max = max;
}
@Override
public int hash(Object key, int probeStep) {
return (key.hashCode() + probeStep) % max;
}
}

View File

@ -0,0 +1,15 @@
package be.brainbaking.datastructures.hashing;
public class QuadraticProbeHash implements Hashable {
private final int max;
public QuadraticProbeHash(int max) {
this.max = max;
}
@Override
public int hash(Object key, int probeStep) {
return (key.hashCode() + probeStep * probeStep) % max;
}
}

View File

@ -0,0 +1,10 @@
package be.brainbaking.datastructures.hashtable;
public class HashBucket<T> {
private final T key;
public HashBucket(T key) {
this.key = key;
}
}

View File

@ -0,0 +1,48 @@
package be.brainbaking.datastructures.hashtable;
import be.brainbaking.datastructures.hashing.Hashable;
import be.brainbaking.datastructures.hashing.LinearProbeHash;
public class HashTable<T> {
private final HashBucket<T>[] table;
private final Hashable hasher;
public HashTable() {
this(10);
}
private HashTable(int initialSize) {
table = new HashBucket[10];
hasher = new LinearProbeHash(10);
}
public void add(T key) {
add(key, 0);
}
public void remove(T key) {
throw new UnsupportedOperationException();
// TODO "deleted" stuff
}
public int search(T key) {
throw new UnsupportedOperationException();
// TODO return key if exists
}
private void add(T key, int probeStep) {
if(probeStep > table.length) {
throw new UnsupportedOperationException("unable to add key to table, everything occupied?");
}
int hash = hasher.hash(key, probeStep);
if(table[hash] != null) {
add(key, probeStep + 1);
} else {
table[hash] = new HashBucket<>(key);
}
}
}

View File

@ -0,0 +1,12 @@
package be.brainbaking.datastructures.hashtable;
import org.junit.jupiter.api.Test;
public class HashTableTest {
@Test
public void add() {
}
}