summaryrefslogtreecommitdiffstats
path: root/src/main/java/net/minecraft/server/LongHashset.java
blob: 6e65a351da1795e33e6a55bbcce4c7c4735a367d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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;
    }
    
    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;
    }
}