Add locks to Hashset

By: Tahg <tahgtahv@gmail.com>
This commit is contained in:
CraftBukkit/Spigot 2011-03-07 13:43:26 -05:00
parent 16f5b6991c
commit 95de556b3a

View file

@ -1,14 +1,27 @@
package org.bukkit.craftbukkit.util; package org.bukkit.craftbukkit.util;
import static org.bukkit.craftbukkit.util.Java15Compat.Arrays_copyOf;
public class LongHashset extends LongHash { import static org.bukkit.craftbukkit.util.Java15Compat.Arrays_copyOf;
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][][]; long values[][][] = new long[256][][];
int count = 0; int count = 0;
ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
ReadLock rl = rwl.readLock();
WriteLock wl = rwl.writeLock();
public boolean isEmpty() { public boolean isEmpty() {
return count == 0; rl.lock();
try {
return count == 0;
} finally {
rl.unlock();
}
} }
public void add(int msw, int lsw) { public void add(int msw, int lsw) {
add(toLong(msw, lsw)); add(toLong(msw, lsw));
} }
@ -16,90 +29,142 @@ public class LongHashset extends LongHash {
public void add(long key) { public void add(long key) {
int mainIdx = (int) (key & 255); int mainIdx = (int) (key & 255);
int outerIdx = (int) ((key >> 32) & 255); int outerIdx = (int) ((key >> 32) & 255);
long outer[][] = values[mainIdx], inner[]; rl.lock();
if(outer == null) values[mainIdx] = outer = new long[256][]; try {
inner = outer[outerIdx]; wl.lock();
if(inner == null) { try {
outer[outerIdx] = inner = new long[1]; long outer[][] = values[mainIdx], inner[];
inner[0] = key; if (outer == null)
count++; values[mainIdx] = outer = new long[256][];
} inner = outer[outerIdx];
else { if (inner == null) {
int i; synchronized (this) {
for(i = 0; i < inner.length; i++) { outer[outerIdx] = inner = new long[1];
if(inner[i] == key) { inner[0] = key;
return; count++;
}
} else {
int i;
for (i = 0; i < inner.length; i++) {
if (inner[i] == key) {
return;
}
}
inner = Arrays_copyOf(inner, i + 1);
outer[outerIdx] = inner;
inner[i] = key;
count++;
} }
} finally {
wl.unlock();
} }
outer[outerIdx] = inner = Arrays_copyOf(inner, i+1); } finally {
inner[i] = key; rl.unlock();
count++;
} }
} }
public boolean containsKey(long key) { public boolean containsKey(long key) {
int mainIdx = (int) (key & 255); int mainIdx = (int) (key & 255);
int outerIdx = (int) ((key >> 32) & 255); int outerIdx = (int) ((key >> 32) & 255);
long outer[][] = values[mainIdx], inner[]; rl.lock();
if(outer == null) return false; try {
inner = outer[outerIdx]; long outer[][] = values[mainIdx], inner[];
if(inner == null) return false; if (outer == null)
else { return false;
for(long entry : inner) { inner = outer[outerIdx];
if(entry == key) return true; if (inner == null)
} return false;
return false; else {
} for (long entry : inner) {
} if (entry == key)
return true;
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 false;
return;
} }
} finally {
rl.unlock();
} }
} }
public long popFirst() { public void remove(long key) {
for(long[][] outer : values) { rl.lock();
if(outer == null) continue; try {
for(int i = 0; i < outer.length ; i++) { long[][] outer = this.values[(int) (key & 255)];
long[] inner = outer[i]; if (outer == null)
if(inner == null || inner.length == 0) continue; return;
count--;
long ret = inner[inner.length - 1]; long[] inner = outer[(int) ((key >> 32) & 255)];
outer[i] = Arrays_copyOf(inner, inner.length - 1); if (inner == null)
return ret; return;
int max = inner.length - 1;
for (int i = 0; i <= max; i++) {
if (inner[i] == key) {
wl.lock();
try {
count--;
if (i != max) {
inner[i] = inner[max];
}
outer[(int) ((key >> 32) & 255)] = (max == 0 ? null : Arrays_copyOf(inner, max));
} finally {
wl.unlock();
}
return;
}
} }
} finally {
rl.unlock();
}
}
public long popFirst() {
rl.lock();
try {
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;
wl.lock();
try {
count--;
long ret = inner[inner.length - 1];
outer[i] = Arrays_copyOf(inner, inner.length - 1);
return ret;
} finally {
wl.unlock();
}
}
}
} finally {
rl.unlock();
} }
return 0; return 0;
} }
public long[] keys() { public long[] keys() {
int index = 0; int index = 0;
long ret[] = new long[count]; rl.lock();
for(long[][] outer : values) { try {
if(outer == null) continue; long ret[] = new long[count];
for(long[] inner : outer) { for (long[][] outer : values) {
if(inner == null) continue; if (outer == null)
for(long entry : inner) { continue;
ret[index++] = entry; for (long[] inner : outer) {
if (inner == null)
continue;
for (long entry : inner) {
ret[index++] = entry;
}
} }
} }
return ret;
} finally {
rl.unlock();
} }
return ret;
} }
} }