package org.jcodings.util;
public class IntHash<V> extends Hash<V> {
public IntHash() {
super();
}
public IntHash(int size) {
super(size);
}
protected void init() {
head = new IntHashEntry<V>();
}
public static final class IntHashEntry<V> extends HashEntry<V> {
public IntHashEntry(int hash, HashEntry<V> next, V value, HashEntry<V> head) {
super(hash, next, value, head);
}
public IntHashEntry() {
super();
}
}
public V put(int key, V value) {
checkResize();
int hash = hashValue(key);
int i = bucketIndex(hash, table.length);
for (IntHashEntry<V> entry = (IntHashEntry<V>)table[i]; entry != null; entry = (IntHashEntry<V>)entry.next) {
if (entry.hash == hash) {
entry.value = value;
return value;
}
}
table[i] = new IntHashEntry<V>(hash, table[i], value, head);
size++;
return null;
}
public void putDirect(int key, V value) {
checkResize();
final int hash = hashValue(key);
final int i = bucketIndex(hash, table.length);
table[i] = new IntHashEntry<V>(hash, table[i], value, head);
size++;
}
public V get(int key) {
int hash = hashValue(key);
for (IntHashEntry<V> entry = (IntHashEntry<V>)table[bucketIndex(hash, table.length)]; entry != null; entry = (IntHashEntry<V>)entry.next) {
if (entry.hash == hash) return entry.value;
}
return null;
}
public V delete(int key) {
int hash = hashValue(key);
int i = bucketIndex(hash, table.length);
IntHashEntry<V> entry = (IntHashEntry<V>)table[i];
if (entry == null) return null;
if (entry.hash == hash) {
table[i] = entry.next;
size--;
entry.remove();
return entry.value;
}
for (; entry.next != null; entry = (IntHashEntry<V>)entry.next) {
HashEntry<V> tmp = entry.next;
if (tmp.hash == hash && entry.equals(key)) {
entry.next = entry.next.next;
size--;
tmp.remove();
return tmp.value;
}
}
return null;
}
}