All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.github.jnthnclt.os.lab.collections.oh.OHash Maven / Gradle / Ivy
package com.github.jnthnclt.os.lab.collections.oh;
import com.github.jnthnclt.os.lab.collections.KeyValueStream;
import java.util.concurrent.Semaphore;
/**
*
* @author jonathan.colt
*/
public class OHash implements OH {
private final OHasher hasher;
private final OHEqualer equaler;
private volatile OHState state;
public OHash(OHState state, OHasher hasher, OHEqualer equaler) {
this.hasher = hasher;
this.equaler = equaler;
this.state = state;
}
@Override
public long size() {
return state.size();
}
@Override
public void clear() {
state = state.allocate(0);
}
private long hash(OHState state, long keyShuffle) {
keyShuffle += keyShuffle >> 8; // shuffle bits to avoid worst case clustering
if (keyShuffle < 0) {
keyShuffle = -keyShuffle;
}
return keyShuffle % state.capacity();
}
V firstValue() {
OHState s = state;
K skipped = s.skipped();
long i = s.first();
if (i != -1) {
K key;
key = s.key(i);
if (key != null && key != skipped) {
return s.value(i);
}
}
return null;
}
V removeFirstValue() {
OHState s = state;
K skipped = s.skipped();
long i = s.first();
if (i != -1) {
K key;
key = s.key(i);
if (key != null && key != skipped) {
V v = s.value(i);
remove(key);
return v;
}
}
return null;
}
@Override
public V get(K key) {
return get(hasher.hashCode(key), key);
}
@Override
public V get(long hashCode, K key) {
OHState s = state;
K skipped = s.skipped();
if (key == null || key == skipped) {
return null;
}
if (s.size() == 0) {
return null;
}
long capacity = s.capacity();
long start = hash(s, hashCode);
for (long i = start, j = 0, k = capacity; // stack vars for efficiency
j < k; // max search for key
i = (++i) % k, j++) { // wraps around table
K storedKey = s.key(i);
if (storedKey == skipped) {
continue;
}
if (storedKey == null) {
return null;
}
if (equaler.equals(storedKey, key)) {
return s.value(i);
}
}
return null;
}
@Override
public void remove(K key) {
remove(hasher.hashCode(key), key);
}
@SuppressWarnings("unchecked")
@Override
public void remove(long hashCode, K key) {
OHState s = state;
K skipped = s.skipped();
if (key == null || key == skipped) {
return;
}
if (s.size() == 0) {
return;
}
long capacity = s.capacity();
long start = hash(s, hashCode);
for (long i = start, j = 0, k = capacity; // stack vars for efficiency
j < k; // max search for key
i = (++i) % k, j++) { // wraps around table
K storedKey = s.key(i);
if (storedKey == skipped) {
continue;
}
if (storedKey == null) {
return;
}
if (equaler.equals(storedKey, key)) {
s.remove(i, skipped, null);
long next = (i + 1) % k;
if (s.key(next) == null) {
for (long z = i, y = 0; y < capacity; z = (z + capacity - 1) % k, y++) {
if (s.key(z) != skipped) {
break;
}
s.clear(z);
}
}
return;
}
}
}
@Override
public void put(K key, V value) {
put(hasher.hashCode(key), key, value);
}
@SuppressWarnings("unchecked")
@Override
public void put(long hashCode, K key, V value) {
OHState s = state;
long capacity = s.capacity();
if (s.size() * 2 >= capacity) {
OHState to = s.allocate(capacity * 2);
rehash(s, to);
state = to;
s = to;
}
internalPut(s, hashCode, key, value);
}
private void internalPut(OHState s, long hashCode, K key, V value) {
long capacity = s.capacity();
long start = hash(s, hashCode);
K skipped = s.skipped();
for (long i = start, j = 0, k = capacity; // stack vars for efficiency
j < k; // max search for available slot
i = (++i) % k, j++) {
// wraps around table
K storedKey = s.key(i);
if (storedKey == key) {
s.update(i, key, value);
return;
}
if (storedKey == null || storedKey == skipped) {
s.link(i, key, value);
return;
}
if (equaler.equals(storedKey, key)) {
s.update(i, key, value);
return;
}
}
}
private void rehash(OHState from, OHState to) {
long i = from.first();
K skipped = to.skipped();
while (i != -1) {
K storedKey = from.key(i);
if (storedKey != null && storedKey != skipped) {
long hash = hasher.hashCode(storedKey);
internalPut(to, hash, storedKey, from.value(i));
}
i = from.next(i);
}
}
@Override
public boolean stream(Semaphore semaphore, KeyValueStream stream) throws Exception {
OHState s = state;
long c = s.capacity();
if (c <= 0) {
return true;
}
K skipped = s.skipped();
long i = s.first();
while (i != -1) {
K key;
V value = null;
semaphore.acquire();
try {
key = s.key(i);
if (key != null && key != skipped) {
value = s.value(i);
}
} finally {
semaphore.release();
}
if (key != null && key != skipped) {
if (!stream.keyValue(key, value)) {
return false;
}
}
i = s.next(i);
}
return true;
}
@Override
public String toString() {
return "OHash{" + "hasher=" + hasher + ", equaler=" + equaler + ", state=" + state + '}';
}
}