diff --git a/src/main/java/org/bukkit/craftbukkit/util/LongHash.java b/src/main/java/org/bukkit/craftbukkit/util/LongHash.java index 6dea2679f1..f3fc7d1958 100644 --- a/src/main/java/org/bukkit/craftbukkit/util/LongHash.java +++ b/src/main/java/org/bukkit/craftbukkit/util/LongHash.java @@ -11,13 +11,13 @@ 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) { return (int) (l >> 32); } - + static int lsw(long l) { return (int) (l & 0xFFFFFFFF) + Integer.MIN_VALUE; } @@ -29,8 +29,8 @@ public abstract class LongHash { public void remove(int msw, int lsw) { remove(toLong(msw, lsw)); } - + public abstract boolean containsKey(long key); - + public abstract void remove(long key); } diff --git a/src/main/java/org/bukkit/craftbukkit/util/LongHashset.java b/src/main/java/org/bukkit/craftbukkit/util/LongHashset.java index 148aedbf69..592f3254f6 100644 --- a/src/main/java/org/bukkit/craftbukkit/util/LongHashset.java +++ b/src/main/java/org/bukkit/craftbukkit/util/LongHashset.java @@ -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; - } - return false; + 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; } } @@ -151,4 +146,4 @@ public class LongHashset extends LongHash rl.unlock(); } } -} \ No newline at end of file +} diff --git a/src/main/java/org/bukkit/craftbukkit/util/LongHashtable.java b/src/main/java/org/bukkit/craftbukkit/util/LongHashtable.java index bb46c621ac..bcf78df357 100644 --- a/src/main/java/org/bukkit/craftbukkit/util/LongHashtable.java +++ b/src/main/java/org/bukkit/craftbukkit/util/LongHashtable.java @@ -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 extends LongHash -{ - Object values[][][] = new Object[256][][]; +public class LongHashtable 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 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 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,28 +67,25 @@ public class LongHashtable 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 { - for (int i = 0; i < inner.length; i++) { - Entry e = (Entry) inner[i]; - if (e == null) - return false; - else if (e.key == key) { - cache = e; - return true; - } + 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) { + return false; + } else if (e.key == key) { + this.cache = e; + return true; } - return false; } + return false; } public synchronized void remove(long key) { @@ -98,35 +96,33 @@ public class LongHashtable 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; } - } + } } - + public synchronized ArrayList values() { ArrayList ret = new ArrayList(); - 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 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; } } -} \ No newline at end of file +}