3
0
Mirror von https://github.com/PaperMC/Paper.git synchronisiert 2024-12-20 21:40:06 +01:00

Clean code a bit

Dieser Commit ist enthalten in:
Erik Broes 2011-03-12 14:59:50 +01:00
Ursprung d7ab277105
Commit 22baecaf7f
3 geänderte Dateien mit 92 neuen und 102 gelöschten Zeilen

Datei anzeigen

@ -11,7 +11,7 @@ package org.bukkit.craftbukkit.util;
*/ */
public abstract class LongHash { public abstract class LongHash {
static long toLong(int msw, int lsw) { 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) { static int msw(long l) {

Datei anzeigen

@ -5,9 +5,8 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock; import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
public class LongHashset extends LongHash public class LongHashset extends LongHash {
{ long[][][] values = new long[256][][];
long values[][][] = new long[256][][];
int count = 0; int count = 0;
ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(); ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
ReadLock rl = rwl.readLock(); ReadLock rl = rwl.readLock();
@ -16,7 +15,7 @@ public class LongHashset extends LongHash
public boolean isEmpty() { public boolean isEmpty() {
rl.lock(); rl.lock();
try { try {
return count == 0; return this.count == 0;
} finally { } finally {
rl.unlock(); rl.unlock();
} }
@ -27,19 +26,20 @@ public class LongHashset extends LongHash
} }
public void add(long key) { public void add(long key) {
int mainIdx = (int) (key & 255);
int outerIdx = (int) ((key >> 32) & 255);
wl.lock(); wl.lock();
try { try {
long outer[][] = values[mainIdx], inner[]; int mainIdx = (int) (key & 255);
if (outer == null) long outer[][] = this.values[mainIdx];
values[mainIdx] = outer = new long[256][]; if (outer == null) this.values[mainIdx] = outer = new long[256][];
inner = outer[outerIdx];
int outerIdx = (int) ((key >> 32) & 255);
long inner[] = outer[outerIdx];
if (inner == null) { if (inner == null) {
synchronized (this) { synchronized (this) {
outer[outerIdx] = inner = new long[1]; outer[outerIdx] = inner = new long[1];
inner[0] = key; inner[0] = key;
count++; this.count++;
} }
} else { } else {
int i; int i;
@ -51,7 +51,7 @@ public class LongHashset extends LongHash
inner = Arrays_copyOf(inner, i + 1); inner = Arrays_copyOf(inner, i + 1);
outer[outerIdx] = inner; outer[outerIdx] = inner;
inner[i] = key; inner[i] = key;
count++; this.count++;
} }
} finally { } finally {
wl.unlock(); wl.unlock();
@ -59,23 +59,18 @@ public class LongHashset extends LongHash
} }
public boolean containsKey(long key) { public boolean containsKey(long key) {
int mainIdx = (int) (key & 255);
int outerIdx = (int) ((key >> 32) & 255);
rl.lock(); rl.lock();
try { try {
long outer[][] = values[mainIdx], inner[]; long[][] outer = this.values[(int) (key & 255)];
if (outer == null) if (outer == null) return false;
return false;
inner = outer[outerIdx]; long[] inner = outer[(int) ((key >> 32) & 255)];
if (inner == null) if (inner == null) return false;
return false;
else { for (long entry: inner) {
for (long entry : inner) { if (entry == key) return true;
if (entry == key)
return true;
}
return false;
} }
return false;
} finally { } finally {
rl.unlock(); rl.unlock();
} }
@ -85,20 +80,19 @@ public class LongHashset extends LongHash
wl.lock(); wl.lock();
try { try {
long[][] outer = this.values[(int) (key & 255)]; long[][] outer = this.values[(int) (key & 255)];
if (outer == null) if (outer == null) return;
return;
long[] inner = outer[(int) ((key >> 32) & 255)]; long[] inner = outer[(int) ((key >> 32) & 255)];
if (inner == null) if (inner == null) return;
return;
int max = inner.length - 1; int max = inner.length - 1;
for (int i = 0; i <= max; i++) { for (int i = 0; i <= max; i++) {
if (inner[i] == key) { if (inner[i] == key) {
count--; this.count--;
if (i != max) { if (i != max) {
inner[i] = inner[max]; inner[i] = inner[max];
} }
outer[(int) ((key >> 32) & 255)] = (max == 0 ? null : Arrays_copyOf(inner, max)); outer[(int) ((key >> 32) & 255)] = (max == 0 ? null : Arrays_copyOf(inner, max));
return; return;
} }
@ -111,16 +105,17 @@ public class LongHashset extends LongHash
public long popFirst() { public long popFirst() {
wl.lock(); wl.lock();
try { try {
for (long[][] outer : values) { for (long[][] outer: this.values) {
if (outer == null) if (outer == null) continue;
continue;
for (int i = 0; i < outer.length; i++) { for (int i = 0; i < outer.length; i++) {
long[] inner = outer[i]; long[] inner = outer[i];
if (inner == null || inner.length == 0) if (inner == null || inner.length == 0) continue;
continue;
count--; this.count--;
long ret = inner[inner.length - 1]; long ret = inner[inner.length - 1];
outer[i] = Arrays_copyOf(inner, inner.length - 1); outer[i] = Arrays_copyOf(inner, inner.length - 1);
return ret; return ret;
} }
} }
@ -134,14 +129,14 @@ public class LongHashset extends LongHash
int index = 0; int index = 0;
rl.lock(); rl.lock();
try { try {
long ret[] = new long[count]; long[] ret = new long[this.count];
for (long[][] outer : values) { for (long[][] outer: this.values) {
if (outer == null) if (outer == null) continue;
continue;
for (long[] inner : outer) { for (long[] inner: outer) {
if (inner == null) if (inner == null) continue;
continue;
for (long entry : inner) { for (long entry: inner) {
ret[index++] = entry; ret[index++] = entry;
} }
} }

Datei anzeigen

@ -6,15 +6,14 @@ import net.minecraft.server.Chunk;
import net.minecraft.server.MinecraftServer; import net.minecraft.server.MinecraftServer;
import static org.bukkit.craftbukkit.util.Java15Compat.Arrays_copyOf; import static org.bukkit.craftbukkit.util.Java15Compat.Arrays_copyOf;
public class LongHashtable<V> extends LongHash public class LongHashtable<V> extends LongHash {
{ Object[][][] values = new Object[256][][];
Object values[][][] = new Object[256][][];
Entry cache = null; Entry cache = null;
public void put(int msw, int lsw, V value) { public void put(int msw, int lsw, V value) {
put(toLong(msw, lsw), value); put(toLong(msw, lsw), value);
if (value instanceof Chunk) { if (value instanceof Chunk) {
Chunk c = (Chunk)value; Chunk c = (Chunk) value;
if (msw != c.j || lsw != c.k) { if (msw != c.j || lsw != c.k) {
MinecraftServer.a.info("Chunk (" + c.j + ", " + c.k +") stored at (" + msw + ", " + lsw + ")"); MinecraftServer.a.info("Chunk (" + c.j + ", " + c.k +") stored at (" + msw + ", " + lsw + ")");
Throwable x = new Throwable(); Throwable x = new Throwable();
@ -27,7 +26,7 @@ public class LongHashtable<V> extends LongHash
public V get(int msw, int lsw) { public V get(int msw, int lsw) {
V value = get(toLong(msw, lsw)); V value = get(toLong(msw, lsw));
if (value instanceof Chunk) { if (value instanceof Chunk) {
Chunk c = (Chunk)value; Chunk c = (Chunk) value;
if (msw != c.j || lsw != c.k) { if (msw != c.j || lsw != c.k) {
MinecraftServer.a.info("Chunk (" + c.j + ", " + c.k +") stored at (" + msw + ", " + lsw + ")"); MinecraftServer.a.info("Chunk (" + c.j + ", " + c.k +") stored at (" + msw + ", " + lsw + ")");
Throwable x = new Throwable(); Throwable x = new Throwable();
@ -40,22 +39,24 @@ public class LongHashtable<V> extends LongHash
public synchronized void put(long key, V value) { public synchronized void put(long key, V value) {
int mainIdx = (int) (key & 255); 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); int outerIdx = (int) ((key >> 32) & 255);
Object outer[][] = this.values[mainIdx], inner[]; Object[] inner = outer[outerIdx];
if (outer == null)
this.values[mainIdx] = outer = new Object[256][];
inner = outer[outerIdx];
if (inner == null) { if (inner == null) {
outer[outerIdx] = inner = new Object[5]; outer[outerIdx] = inner = new Object[5];
inner[0] = cache = new Entry(key, value); inner[0] = this.cache = new Entry(key, value);
} else { } else {
int i; int i;
for (i = 0; i < inner.length; i++) { for (i = 0; i < inner.length; i++) {
if (inner[i] == null || ((Entry) inner[i]).key == key) { 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; return;
} }
} }
outer[outerIdx] = inner = Arrays_copyOf(inner, i + i); outer[outerIdx] = inner = Arrays_copyOf(inner, i + i);
inner[i] = new Entry(key, value); inner[i] = new Entry(key, value);
} }
@ -66,28 +67,25 @@ public class LongHashtable<V> extends LongHash
} }
public synchronized boolean containsKey(long key) { public synchronized boolean containsKey(long key) {
if (cache != null && cache.key == key) if (this.cache != null && cache.key == key) return true;
return true;
int mainIdx = (int) (key & 255);
int outerIdx = (int) ((key >> 32) & 255); int outerIdx = (int) ((key >> 32) & 255);
Object outer[][] = this.values[mainIdx], inner[]; Object[][] outer = this.values[(int) (key & 255)];
if (outer == null) if (outer == null) return false;
return false;
inner = outer[outerIdx]; Object[] inner = outer[outerIdx];
if (inner == null) if (inner == null) return false;
return false;
else { for (int i = 0; i < inner.length; i++) {
for (int i = 0; i < inner.length; i++) { Entry e = (Entry) inner[i];
Entry e = (Entry) inner[i]; if (e == null) {
if (e == null) return false;
return false; } else if (e.key == key) {
else if (e.key == key) { this.cache = e;
cache = e; return true;
return true;
}
} }
return false;
} }
return false;
} }
public synchronized void remove(long key) { public synchronized void remove(long key) {
@ -98,19 +96,16 @@ public class LongHashtable<V> extends LongHash
if (inner == null) return; if (inner == null) return;
for (int i = 0; i < inner.length; i++) { for (int i = 0; i < inner.length; i++) {
// No more data! bail
if (inner[i] == null) continue; if (inner[i] == null) continue;
// Found our key -- purge it
if (((Entry) inner[i]).key == key) { if (((Entry) inner[i]).key == key) {
// Move all the elements down
for (i++; i < inner.length; i++) { for (i++; i < inner.length; i++) {
if (inner[i] == null) break; if (inner[i] == null) break;
inner[i-1] = inner[i]; inner[i-1] = inner[i];
} }
inner[i-1] = null; inner[i-1] = null;
cache = null; this.cache = null;
return; return;
} }
} }
@ -118,15 +113,16 @@ public class LongHashtable<V> extends LongHash
public synchronized ArrayList<V> values() { public synchronized ArrayList<V> values() {
ArrayList<V> ret = new ArrayList<V>(); ArrayList<V> ret = new ArrayList<V>();
for (Object[][] outer : this.values) {
if (outer == null) for (Object[][] outer: this.values) {
continue; if (outer == null) continue;
for (Object[] inner : outer) {
if (inner == null) for (Object[] inner: outer) {
continue; if (inner == null) continue;
for (Object entry : inner) {
if (entry == null) for (Object entry: inner) {
break; if (entry == null) break;
ret.add((V) ((Entry) entry).value); ret.add((V) ((Entry) entry).value);
} }
} }
@ -134,14 +130,13 @@ public class LongHashtable<V> extends LongHash
return ret; return ret;
} }
private class Entry private class Entry {
{
long key; long key;
Object value; Object value;
Entry(long k, Object v) { Entry(long k, Object v) {
key = k; this.key = k;
value = v; this.value = v;
} }
} }
} }