diff options
author | FrozenCow <frozencow@gmail.com> | 2011-03-01 19:19:50 +0100 |
---|---|---|
committer | Tahg <tahgtahv@gmail.com> | 2011-03-01 16:37:19 -0500 |
commit | 9832ce06f11f17d5b8ca80079392958284c00738 (patch) | |
tree | 7365c8d9bf0812ffc76da6cfbca964f0b5d72cbc /src/main/java/org | |
parent | 13fb0e07ce5c8220c83757aa4db7e9c2d70d4206 (diff) | |
download | craftbukkit-9832ce06f11f17d5b8ca80079392958284c00738.tar craftbukkit-9832ce06f11f17d5b8ca80079392958284c00738.tar.gz craftbukkit-9832ce06f11f17d5b8ca80079392958284c00738.tar.lz craftbukkit-9832ce06f11f17d5b8ca80079392958284c00738.tar.xz craftbukkit-9832ce06f11f17d5b8ca80079392958284c00738.zip |
Synchronized and moved Hash classes
Diffstat (limited to 'src/main/java/org')
4 files changed, 287 insertions, 1 deletions
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java index 6d535a19..bdaefb3f 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java @@ -8,7 +8,6 @@ import net.minecraft.server.ItemInWorldManager; import net.minecraft.server.Packet; import net.minecraft.server.Packet3Chat; import net.minecraft.server.Packet6SpawnPosition; -import net.minecraft.server.Packet9Respawn; import net.minecraft.server.ServerConfigurationManager; import net.minecraft.server.WorldServer; import org.bukkit.Location; diff --git a/src/main/java/org/bukkit/craftbukkit/util/LongHash.java b/src/main/java/org/bukkit/craftbukkit/util/LongHash.java new file mode 100644 index 00000000..6dea2679 --- /dev/null +++ b/src/main/java/org/bukkit/craftbukkit/util/LongHash.java @@ -0,0 +1,36 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package org.bukkit.craftbukkit.util; + +/** + * + * @author Nathan + */ +public abstract class LongHash { + static long toLong(int msw, int lsw) { + 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; + } + + public boolean containsKey(int msw, int lsw) { + return containsKey(toLong(msw, lsw)); + } + + 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 new file mode 100644 index 00000000..ecc69831 --- /dev/null +++ b/src/main/java/org/bukkit/craftbukkit/util/LongHashset.java @@ -0,0 +1,105 @@ +package org.bukkit.craftbukkit.util; +import java.util.Arrays; + +public class LongHashset extends LongHash { + long values[][][] = new long[256][][]; + int count = 0; + + public boolean isEmpty() { + return count == 0; + } + + public void add(int msw, int lsw) { + add(toLong(msw, lsw)); + } + + public void add(long key) { + int mainIdx = (int) (key & 255); + int outerIdx = (int) ((key >> 32) & 255); + long outer[][] = values[mainIdx], inner[]; + if(outer == null) values[mainIdx] = outer = new long[256][]; + inner = outer[outerIdx]; + if(inner == null) { + outer[outerIdx] = inner = new long[1]; + inner[0] = key; + count++; + } + else { + int i; + for(i = 0; i < inner.length; i++) { + if(inner[i] == key) { + return; + } + } + outer[outerIdx] = inner = Arrays.copyOf(inner, i+1); + inner[i] = key; + count++; + } + } + + public boolean containsKey(long key) { + int mainIdx = (int) (key & 255); + int outerIdx = (int) ((key >> 32) & 255); + 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; + } + } + + public void remove(long key) { + long[][] outer = this.values[(int) (key & 255)]; + if (outer == null) return; + + long[] inner = outer[(int) ((key >> 32) & 255)]; + if (inner == null) return; + + int max = inner.length - 1; + for(int i = 0; i <= max; i++) { + if(inner[i] == key) { + count--; + if(i != max) { + inner[i] = inner[max]; + } + outer[(int) ((key >> 32) & 255)] = (max == 0 ? null : Arrays.copyOf(inner, max)); + return; + } + } + } + + public long popFirst() { + for(long[][] outer : 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--; + long ret = inner[inner.length - 1]; + outer[i] = Arrays.copyOf(inner, inner.length - 1); + return ret; + + } + } + return 0; + } + + public long[] keys() { + int index = 0; + 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) { + ret[index++] = entry; + } + } + } + return ret; + } +}
\ 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 new file mode 100644 index 00000000..5ff9b595 --- /dev/null +++ b/src/main/java/org/bukkit/craftbukkit/util/LongHashtable.java @@ -0,0 +1,146 @@ +package org.bukkit.craftbukkit.util; + +import java.util.ArrayList; +import java.util.Arrays; +import net.minecraft.server.Chunk; +import net.minecraft.server.MinecraftServer; + +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; + if(msw != c.j || lsw != c.k) { + MinecraftServer.a.info("Chunk (" + c.j + ", " + c.k +") stored at (" + msw + ", " + lsw + ")"); + Throwable x = new Throwable(); + x.fillInStackTrace(); + x.printStackTrace(); + } + } + } + + public V get(int msw, int lsw) { + V value = get(toLong(msw, lsw)); + if(value instanceof Chunk) { + 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(); + x.fillInStackTrace(); + x.printStackTrace(); + } + } + return value; + } + + public synchronized void put(long key, V value) { + 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 { + 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); + return; + } + } + outer[outerIdx] = inner = Arrays.copyOf(inner, i + i); + inner[i] = new Entry(key, value); + } + } + + public synchronized V get(long key) { + return containsKey(key) ? (V) cache.value : null; + } + + public synchronized boolean containsKey(long key) { + 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; + 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; + } + } + return false; + } + } + + public synchronized void remove(long key) { + 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; + } + } + } + + 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; + ret.add((V) ((Entry) entry).value); + } + } + } + return ret; + } + + private class Entry + { + long key; + Object value; + + Entry(long k, Object v) { + key = k; + value = v; + } + } +}
\ No newline at end of file |