3
0
Mirror von https://github.com/PaperMC/Paper.git synchronisiert 2024-11-17 05:20:05 +01:00

Add locks to Hashset

Dieser Commit ist enthalten in:
Tahg 2011-03-07 13:43:26 -05:00
Ursprung 54a05f3ce2
Commit 67cf10656c

Datei anzeigen

@ -1,12 +1,25 @@
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() {
rl.lock();
try {
return count == 0; return count == 0;
} finally {
rl.unlock();
}
} }
public void add(int msw, int lsw) { public void add(int msw, int 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);
rl.lock();
try {
wl.lock();
try {
long outer[][] = values[mainIdx], inner[]; long outer[][] = values[mainIdx], inner[];
if(outer == null) values[mainIdx] = outer = new long[256][]; if (outer == null)
values[mainIdx] = outer = new long[256][];
inner = outer[outerIdx]; inner = outer[outerIdx];
if (inner == null) { if (inner == null) {
synchronized (this) {
outer[outerIdx] = inner = new long[1]; outer[outerIdx] = inner = new long[1];
inner[0] = key; inner[0] = key;
count++; count++;
} }
else { } else {
int i; int i;
for (i = 0; i < inner.length; i++) { for (i = 0; i < inner.length; i++) {
if (inner[i] == key) { if (inner[i] == key) {
return; return;
} }
} }
outer[outerIdx] = inner = Arrays_copyOf(inner, i+1); inner = Arrays_copyOf(inner, i + 1);
outer[outerIdx] = inner;
inner[i] = key; inner[i] = key;
count++; count++;
} }
} finally {
wl.unlock();
}
} finally {
rl.unlock();
}
} }
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);
rl.lock();
try {
long outer[][] = values[mainIdx], inner[]; long outer[][] = values[mainIdx], inner[];
if(outer == null) return false; if (outer == null)
return false;
inner = outer[outerIdx]; inner = outer[outerIdx];
if(inner == null) return false; if (inner == null)
return false;
else { else {
for (long entry : inner) { for (long entry : inner) {
if(entry == key) return true; if (entry == key)
return true;
} }
return false; return false;
} }
} finally {
rl.unlock();
}
} }
public void remove(long key) { public void remove(long key) {
rl.lock();
try {
long[][] outer = this.values[(int) (key & 255)]; long[][] outer = this.values[(int) (key & 255)];
if (outer == null) return; if (outer == null)
return;
long[] inner = outer[(int) ((key >> 32) & 255)]; long[] inner = outer[(int) ((key >> 32) & 255)];
if (inner == null) return; if (inner == null)
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) {
wl.lock();
try {
count--; 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));
} finally {
wl.unlock();
}
return; return;
} }
} }
} finally {
rl.unlock();
}
} }
public long popFirst() { public long popFirst() {
rl.lock();
try {
for (long[][] outer : values) { for (long[][] outer : values) {
if(outer == null) continue; if (outer == null)
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) continue; if (inner == null || inner.length == 0)
continue;
wl.lock();
try {
count--; 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;
} finally {
wl.unlock();
}
} }
} }
} finally {
rl.unlock();
}
return 0; return 0;
} }
public long[] keys() { public long[] keys() {
int index = 0; int index = 0;
rl.lock();
try {
long ret[] = new long[count]; long ret[] = new long[count];
for (long[][] outer : values) { for (long[][] outer : values) {
if(outer == null) continue; if (outer == null)
continue;
for (long[] inner : outer) { for (long[] inner : outer) {
if(inner == null) continue; if (inner == null)
continue;
for (long entry : inner) { for (long entry : inner) {
ret[index++] = entry; ret[index++] = entry;
} }
} }
} }
return ret; return ret;
} finally {
rl.unlock();
}
} }
} }