13
0
geforkt von Mirrors/Paper

Synchronized and moved Hash classes

By: FrozenCow <frozencow@gmail.com>
Dieser Commit ist enthalten in:
CraftBukkit/Spigot 2011-03-01 19:19:50 +01:00
Ursprung dc8f7eb294
Commit 2803cc2b23
4 geänderte Dateien mit 287 neuen und 1 gelöschten Zeilen

Datei anzeigen

@ -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;

Datei anzeigen

@ -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);
}

Datei anzeigen

@ -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;
}
}

Datei anzeigen

@ -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;
}
}
}