3
0
Mirror von https://github.com/PaperMC/Paper.git synchronisiert 2024-11-18 14:00:05 +01:00
Paper/src/main/java/net/minecraft/server/LongHashset.java

105 Zeilen
2.9 KiB
Java

2011-02-07 06:56:07 +01:00
package net.minecraft.server;
import java.util.ArrayList;
import java.util.Arrays;
public class LongHashset<V> extends LongHash<V> {
long values[][][] = new long[256][][];
int count = 0;
public boolean isEmpty() {
return count == 0;
}
2011-02-10 20:33:41 +01:00
public void add(int msw, int lsw) {
add(toLong(msw, lsw));
}
2011-02-07 06:56:07 +01:00
2011-02-10 20:33:41 +01:00
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;
2011-02-07 06:56:07 +01:00
count++;
}
else {
int i;
2011-02-10 20:33:41 +01:00
for(i = 0; i < inner.length; i++) {
if(inner[i] == key) {
2011-02-07 06:56:07 +01:00
return;
}
}
2011-02-10 20:33:41 +01:00
outer[0] = inner = Arrays.copyOf(inner, i+1);
inner[i] = key;
2011-02-07 06:56:07 +01:00
count++;
}
}
public boolean containsKey(long key) {
2011-02-10 20:33:41 +01:00
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;
2011-02-07 06:56:07 +01:00
else {
2011-02-10 20:33:41 +01:00
for(long entry : inner) {
2011-02-07 06:56:07 +01:00
if(entry == key) return true;
}
return false;
}
}
public void remove(long key) {
2011-02-10 20:33:41 +01:00
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];
2011-02-07 06:56:07 +01:00
}
2011-02-10 20:33:41 +01:00
outer[(int) ((key >> 32) & 255)] = (max == 0 ? null : Arrays.copyOf(inner, max));
return;
2011-02-07 06:56:07 +01:00
}
2011-02-10 20:33:41 +01:00
}
2011-02-07 06:56:07 +01:00
}
public long popFirst() {
for(long[][] outer : values) {
if(outer == null) continue;
2011-02-10 20:33:41 +01:00
for(int i = 0; i < outer.length ; i++) {
long[] inner = outer[i];
2011-02-07 06:56:07 +01:00
if(inner == null) continue;
2011-02-10 20:33:41 +01:00
count--;
2011-02-07 06:56:07 +01:00
long ret = inner[inner.length - 1];
2011-02-10 20:33:41 +01:00
outer[i] = Arrays.copyOf(inner, inner.length - 1);
2011-02-07 06:56:07 +01:00
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;
}
}