Adds loadFactor in OAHT

This commit is contained in:
Abhinav Sarkar 2019-06-29 09:38:30 +05:30
parent 4973ae6b73
commit cf8d64192d
2 changed files with 42 additions and 16 deletions

View File

@ -0,0 +1,5 @@
package net.abhinavsarkar.algorist;
public class BinarySearchTree
{
}

View File

@ -9,19 +9,35 @@ public class OpenAddressingHashTable<K, V> implements Iterable<OpenAddressingHas
private static final Entry REMOVED = new Entry(0,0);
private Object[] store;
private final double loadFactor;
private final ProbingStrategy probingStrategy;
private Entry<K, V> head;
private Entry<K, V> tail;
private int size;
private int actualSize;
public OpenAddressingHashTable(int initialCapacity, ProbingStrategy probingStrategy) {
this.store = new Object[initialCapacity];
public OpenAddressingHashTable(int initialCapacity, double loadFactor, ProbingStrategy probingStrategy) {
this.store = new Object[nextPowerOf2(initialCapacity)];
this.loadFactor = loadFactor;
this.probingStrategy = probingStrategy;
}
public OpenAddressingHashTable(int initialCapacity)
public OpenAddressingHashTable(int initialCapacity, double loadFactor)
{
this(initialCapacity, new LinearProbing());
this(initialCapacity, loadFactor, new LinearProbing());
}
public OpenAddressingHashTable(int initialCapacity) {
this(initialCapacity, 0.75);
}
public OpenAddressingHashTable() {
this(16);
}
private static int nextPowerOf2(int num)
{
return num == 1 ? 1 : Integer.highestOneBit(num - 1) * 2;
}
public Optional<V> get(K key) {
@ -53,25 +69,23 @@ public class OpenAddressingHashTable<K, V> implements Iterable<OpenAddressingHas
}
public void put(K key, V val) {
if (!doPut(new Entry<>(key, val), store, false)) {
if (actualSize >= loadFactor * store.length) {
resize();
put(key, val);
}
doPut(new Entry<>(key, val), store, false);
}
private void resize() {
Object[] nStore = new Object[store.length * 2];
for (Entry<K, V> entry : this)
{
if (!doPut(entry, nStore, true))
{
throw new IllegalStateException("Something is wrong");
}
doPut(entry, nStore, true);
}
this.store = nStore;
store = nStore;
actualSize = size;
}
private boolean doPut(Entry<K,V> nEntry, Object[] store, boolean resize) {
private void doPut(Entry<K,V> nEntry, Object[] store, boolean resize) {
int trial = 0;
int baseIndex = calcIndex(nEntry.key, store.length);
while (trial < store.length) {
@ -83,10 +97,13 @@ public class OpenAddressingHashTable<K, V> implements Iterable<OpenAddressingHas
{
linkNewEntry(nEntry);
size++;
if (entry != REMOVED) {
actualSize++;
}
}
store[index] = nEntry;
return true;
return;
}
else if (entry.key.equals(nEntry.key))
{
@ -94,11 +111,11 @@ public class OpenAddressingHashTable<K, V> implements Iterable<OpenAddressingHas
linkUpdatedEntry(entry);
}
entry.val = nEntry.val;
return true;
return;
}
trial++;
}
return false;
throw new IllegalStateException("Impossible");
}
private void linkNewEntry(Entry<K, V> nEntry)
@ -278,11 +295,15 @@ public class OpenAddressingHashTable<K, V> implements Iterable<OpenAddressingHas
public static void main(String[] args)
{
OpenAddressingHashTable<String, String> table =
new OpenAddressingHashTable<>(4, new LinearProbing());
new OpenAddressingHashTable<>(2, 0.75, new LinearProbing());
table.put("a", "abhinav");
table.put("b", "batman");
table.put("c", "carol");
table.put("c", "carly");
table.remove("a");
System.out.println(table.size());
table.put("z", "zellman");
table.put("w", "walker");
System.out.println(table);