geforkt von Mirrors/Paper
Clean code a bit
Dieser Commit ist enthalten in:
Ursprung
d7ab277105
Commit
22baecaf7f
@ -11,7 +11,7 @@ package org.bukkit.craftbukkit.util;
|
||||
*/
|
||||
public abstract class LongHash {
|
||||
static long toLong(int msw, int lsw) {
|
||||
return ((long)msw << 32) + lsw - Integer.MIN_VALUE;
|
||||
return ((long) msw << 32) + lsw - Integer.MIN_VALUE;
|
||||
}
|
||||
|
||||
static int msw(long l) {
|
||||
|
@ -5,9 +5,8 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
|
||||
|
||||
public class LongHashset extends LongHash
|
||||
{
|
||||
long values[][][] = new long[256][][];
|
||||
public class LongHashset extends LongHash {
|
||||
long[][][] values = new long[256][][];
|
||||
int count = 0;
|
||||
ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
|
||||
ReadLock rl = rwl.readLock();
|
||||
@ -16,7 +15,7 @@ public class LongHashset extends LongHash
|
||||
public boolean isEmpty() {
|
||||
rl.lock();
|
||||
try {
|
||||
return count == 0;
|
||||
return this.count == 0;
|
||||
} finally {
|
||||
rl.unlock();
|
||||
}
|
||||
@ -27,19 +26,20 @@ public class LongHashset extends LongHash
|
||||
}
|
||||
|
||||
public void add(long key) {
|
||||
int mainIdx = (int) (key & 255);
|
||||
int outerIdx = (int) ((key >> 32) & 255);
|
||||
wl.lock();
|
||||
try {
|
||||
long outer[][] = values[mainIdx], inner[];
|
||||
if (outer == null)
|
||||
values[mainIdx] = outer = new long[256][];
|
||||
inner = outer[outerIdx];
|
||||
int mainIdx = (int) (key & 255);
|
||||
long outer[][] = this.values[mainIdx];
|
||||
if (outer == null) this.values[mainIdx] = outer = new long[256][];
|
||||
|
||||
int outerIdx = (int) ((key >> 32) & 255);
|
||||
long inner[] = outer[outerIdx];
|
||||
|
||||
if (inner == null) {
|
||||
synchronized (this) {
|
||||
outer[outerIdx] = inner = new long[1];
|
||||
inner[0] = key;
|
||||
count++;
|
||||
this.count++;
|
||||
}
|
||||
} else {
|
||||
int i;
|
||||
@ -51,7 +51,7 @@ public class LongHashset extends LongHash
|
||||
inner = Arrays_copyOf(inner, i + 1);
|
||||
outer[outerIdx] = inner;
|
||||
inner[i] = key;
|
||||
count++;
|
||||
this.count++;
|
||||
}
|
||||
} finally {
|
||||
wl.unlock();
|
||||
@ -59,23 +59,18 @@ public class LongHashset extends LongHash
|
||||
}
|
||||
|
||||
public boolean containsKey(long key) {
|
||||
int mainIdx = (int) (key & 255);
|
||||
int outerIdx = (int) ((key >> 32) & 255);
|
||||
rl.lock();
|
||||
try {
|
||||
long outer[][] = values[mainIdx], inner[];
|
||||
if (outer == null)
|
||||
return false;
|
||||
inner = outer[outerIdx];
|
||||
if (inner == null)
|
||||
return false;
|
||||
else {
|
||||
for (long entry : inner) {
|
||||
if (entry == key)
|
||||
return true;
|
||||
long[][] outer = this.values[(int) (key & 255)];
|
||||
if (outer == null) return false;
|
||||
|
||||
long[] inner = outer[(int) ((key >> 32) & 255)];
|
||||
if (inner == null) return false;
|
||||
|
||||
for (long entry: inner) {
|
||||
if (entry == key) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
} finally {
|
||||
rl.unlock();
|
||||
}
|
||||
@ -85,20 +80,19 @@ public class LongHashset extends LongHash
|
||||
wl.lock();
|
||||
try {
|
||||
long[][] outer = this.values[(int) (key & 255)];
|
||||
if (outer == null)
|
||||
return;
|
||||
if (outer == null) return;
|
||||
|
||||
long[] inner = outer[(int) ((key >> 32) & 255)];
|
||||
if (inner == null)
|
||||
return;
|
||||
if (inner == null) return;
|
||||
|
||||
int max = inner.length - 1;
|
||||
for (int i = 0; i <= max; i++) {
|
||||
if (inner[i] == key) {
|
||||
count--;
|
||||
this.count--;
|
||||
if (i != max) {
|
||||
inner[i] = inner[max];
|
||||
}
|
||||
|
||||
outer[(int) ((key >> 32) & 255)] = (max == 0 ? null : Arrays_copyOf(inner, max));
|
||||
return;
|
||||
}
|
||||
@ -111,16 +105,17 @@ public class LongHashset extends LongHash
|
||||
public long popFirst() {
|
||||
wl.lock();
|
||||
try {
|
||||
for (long[][] outer : values) {
|
||||
if (outer == null)
|
||||
continue;
|
||||
for (long[][] outer: this.values) {
|
||||
if (outer == null) continue;
|
||||
|
||||
for (int i = 0; i < outer.length; i++) {
|
||||
long[] inner = outer[i];
|
||||
if (inner == null || inner.length == 0)
|
||||
continue;
|
||||
count--;
|
||||
if (inner == null || inner.length == 0) continue;
|
||||
|
||||
this.count--;
|
||||
long ret = inner[inner.length - 1];
|
||||
outer[i] = Arrays_copyOf(inner, inner.length - 1);
|
||||
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
@ -134,14 +129,14 @@ public class LongHashset extends LongHash
|
||||
int index = 0;
|
||||
rl.lock();
|
||||
try {
|
||||
long ret[] = new long[count];
|
||||
for (long[][] outer : values) {
|
||||
if (outer == null)
|
||||
continue;
|
||||
for (long[] inner : outer) {
|
||||
if (inner == null)
|
||||
continue;
|
||||
for (long entry : inner) {
|
||||
long[] ret = new long[this.count];
|
||||
for (long[][] outer: this.values) {
|
||||
if (outer == null) continue;
|
||||
|
||||
for (long[] inner: outer) {
|
||||
if (inner == null) continue;
|
||||
|
||||
for (long entry: inner) {
|
||||
ret[index++] = entry;
|
||||
}
|
||||
}
|
||||
|
@ -6,15 +6,14 @@ import net.minecraft.server.Chunk;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import static org.bukkit.craftbukkit.util.Java15Compat.Arrays_copyOf;
|
||||
|
||||
public class LongHashtable<V> extends LongHash
|
||||
{
|
||||
Object values[][][] = new Object[256][][];
|
||||
public class LongHashtable<V> extends LongHash {
|
||||
Object[][][] values = new Object[256][][];
|
||||
Entry cache = null;
|
||||
|
||||
public void put(int msw, int lsw, V value) {
|
||||
put(toLong(msw, lsw), value);
|
||||
if (value instanceof Chunk) {
|
||||
Chunk c = (Chunk)value;
|
||||
Chunk c = (Chunk) value;
|
||||
if (msw != c.j || lsw != c.k) {
|
||||
MinecraftServer.a.info("Chunk (" + c.j + ", " + c.k +") stored at (" + msw + ", " + lsw + ")");
|
||||
Throwable x = new Throwable();
|
||||
@ -27,7 +26,7 @@ public class LongHashtable<V> extends LongHash
|
||||
public V get(int msw, int lsw) {
|
||||
V value = get(toLong(msw, lsw));
|
||||
if (value instanceof Chunk) {
|
||||
Chunk c = (Chunk)value;
|
||||
Chunk c = (Chunk) value;
|
||||
if (msw != c.j || lsw != c.k) {
|
||||
MinecraftServer.a.info("Chunk (" + c.j + ", " + c.k +") stored at (" + msw + ", " + lsw + ")");
|
||||
Throwable x = new Throwable();
|
||||
@ -40,22 +39,24 @@ public class LongHashtable<V> extends LongHash
|
||||
|
||||
public synchronized void put(long key, V value) {
|
||||
int mainIdx = (int) (key & 255);
|
||||
Object[][] outer = this.values[mainIdx];
|
||||
if (outer == null) this.values[mainIdx] = outer = new Object[256][];
|
||||
|
||||
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];
|
||||
Object[] inner = outer[outerIdx];
|
||||
|
||||
if (inner == null) {
|
||||
outer[outerIdx] = inner = new Object[5];
|
||||
inner[0] = cache = new Entry(key, value);
|
||||
inner[0] = this.cache = new Entry(key, value);
|
||||
} else {
|
||||
int i;
|
||||
for (i = 0; i < inner.length; i++) {
|
||||
if (inner[i] == null || ((Entry) inner[i]).key == key) {
|
||||
inner[i] = cache = new Entry(key, value);
|
||||
inner[i] = this.cache = new Entry(key, value);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
outer[outerIdx] = inner = Arrays_copyOf(inner, i + i);
|
||||
inner[i] = new Entry(key, value);
|
||||
}
|
||||
@ -66,29 +67,26 @@ public class LongHashtable<V> extends LongHash
|
||||
}
|
||||
|
||||
public synchronized boolean containsKey(long key) {
|
||||
if (cache != null && cache.key == key)
|
||||
return true;
|
||||
int mainIdx = (int) (key & 255);
|
||||
if (this.cache != null && cache.key == key) return true;
|
||||
|
||||
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;
|
||||
else {
|
||||
Object[][] outer = this.values[(int) (key & 255)];
|
||||
if (outer == null) return false;
|
||||
|
||||
Object[] inner = outer[outerIdx];
|
||||
if (inner == null) return false;
|
||||
|
||||
for (int i = 0; i < inner.length; i++) {
|
||||
Entry e = (Entry) inner[i];
|
||||
if (e == null)
|
||||
if (e == null) {
|
||||
return false;
|
||||
else if (e.key == key) {
|
||||
cache = e;
|
||||
} else if (e.key == key) {
|
||||
this.cache = e;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void remove(long key) {
|
||||
Object[][] outer = this.values[(int) (key & 255)];
|
||||
@ -98,19 +96,16 @@ public class LongHashtable<V> extends LongHash
|
||||
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;
|
||||
this.cache = null;
|
||||
return;
|
||||
}
|
||||
}
|
||||
@ -118,15 +113,16 @@ public class LongHashtable<V> extends LongHash
|
||||
|
||||
public synchronized ArrayList<V> values() {
|
||||
ArrayList<V> ret = new ArrayList<V>();
|
||||
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;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@ -134,14 +130,13 @@ public class LongHashtable<V> extends LongHash
|
||||
return ret;
|
||||
}
|
||||
|
||||
private class Entry
|
||||
{
|
||||
private class Entry {
|
||||
long key;
|
||||
Object value;
|
||||
|
||||
Entry(long k, Object v) {
|
||||
key = k;
|
||||
value = v;
|
||||
this.key = k;
|
||||
this.value = v;
|
||||
}
|
||||
}
|
||||
}
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren