3
0
Mirror von https://github.com/PaperMC/Paper.git synchronisiert 2024-11-18 14:00:05 +01:00
Paper/src/main/java/net/minecraft/server/LongHashtable.java

125 Zeilen
3.5 KiB
Java

2011-02-07 06:56:07 +01:00
package net.minecraft.server;
2011-02-10 20:33:41 +01:00
2011-02-07 06:56:07 +01:00
import java.util.ArrayList;
import java.util.Arrays;
2011-02-10 20:33:41 +01:00
public class LongHashtable<V> extends LongHash
{
2011-02-07 06:56:07 +01:00
Object values[][][] = new Object[256][][];
Entry cache = null;
2011-02-10 20:33:41 +01:00
2011-02-07 06:56:07 +01:00
public void put(int msw, int lsw, V value) {
put(toLong(msw, lsw), value);
}
public V get(int msw, int lsw) {
return get(toLong(msw, lsw));
}
public void put(long key, V value) {
2011-02-10 20:33:41 +01:00
int mainIdx = (int) (key & 255);
int outerIdx = (int) ((key >> 32) & 255);
Object outer[][] = this.values[mainIdx], inner[];
if (outer == null)
this.values[mainIdx] = outer = new Object[256][];
inner = outer[outerIdx];
if (inner == null) {
outer[outerIdx] = inner = new Object[5];
inner[0] = cache = new Entry(key, value);
} else {
2011-02-07 06:56:07 +01:00
int i;
2011-02-10 20:33:41 +01:00
for (i = 0; i < inner.length; i++) {
if (inner[i] == null || ((Entry) inner[i]).key == key) {
inner[i] = cache = new Entry(key, value);
2011-02-07 06:56:07 +01:00
return;
}
}
2011-02-10 20:33:41 +01:00
outer[outerIdx] = inner = Arrays.copyOf(inner, i + i);
inner[i] = new Entry(key, value);
2011-02-07 06:56:07 +01:00
}
}
2011-02-10 20:33:41 +01:00
2011-02-07 06:56:07 +01:00
public V get(long key) {
return containsKey(key) ? (V) cache.value : null;
}
2011-02-10 20:33:41 +01:00
2011-02-07 06:56:07 +01:00
public boolean containsKey(long key) {
2011-02-10 20:33:41 +01:00
if (cache != null && cache.key == key)
return true;
int mainIdx = (int) (key & 255);
int outerIdx = (int) ((key >> 32) & 255);
Object outer[][] = this.values[mainIdx], inner[];
if (outer == null)
return false;
inner = outer[outerIdx];
if (inner == null)
return false;
2011-02-07 06:56:07 +01:00
else {
2011-02-10 20:33:41 +01:00
for (int i = 0; i < inner.length; i++) {
Entry e = (Entry) inner[i];
if (e == null)
return false;
else if (e.key == key) {
2011-02-07 06:56:07 +01:00
cache = e;
return true;
}
}
return false;
}
}
2011-02-10 20:33:41 +01:00
2011-02-07 06:56:07 +01:00
public void remove(long key) {
2011-02-10 20:33:41 +01:00
Object[][] outer = this.values[(int) (key & 255)];
if (outer == null) return;
Object[] inner = outer[(int) ((key >> 32) & 255)];
if (inner == null) return;
for (int i = 0; i < inner.length; i++) {
// No more data! bail
if (inner[i] == null) continue;
// Found our key -- purge it
if (((Entry) inner[i]).key == key) {
// Move all the elements down
for (i++; i < inner.length; i++) {
if (inner[i] == null) break;
inner[i-1] = inner[i];
}
inner[i-1] = null;
cache = null;
return;
}
}
2011-02-07 06:56:07 +01:00
}
2011-02-10 20:33:41 +01:00
2011-02-07 06:56:07 +01:00
public ArrayList<V> values() {
ArrayList<V> ret = new ArrayList<V>();
2011-02-10 20:33:41 +01:00
for (Object[][] outer : this.values) {
if (outer == null)
continue;
for (Object[] inner : outer) {
if (inner == null)
continue;
for (Object entry : inner) {
if (entry == null)
break;
ret.add((V) ((Entry) entry).value);
2011-02-07 06:56:07 +01:00
}
}
}
return ret;
}
2011-02-10 20:33:41 +01:00
private class Entry
{
2011-02-07 06:56:07 +01:00
long key;
Object value;
2011-02-10 20:33:41 +01:00
2011-02-07 06:56:07 +01:00
Entry(long k, Object v) {
key = k;
value = v;
}
2011-02-10 20:33:41 +01:00
}
}