summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/bukkit/craftbukkit/util/LongAbstractHashtable.java
blob: 2da732cce23328fb007d3bcf3cd1de66ec1079a1 (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
107
108
109
110
111
112
113
114
115
package org.bukkit.craftbukkit.util;

import net.minecraft.server.Chunk;
import net.minecraft.server.MinecraftServer;

import java.util.ArrayList;

import static org.bukkit.craftbukkit.util.Java15Compat.Arrays_copyOf;

public abstract class LongAbstractHashtable extends LongHash {

    EntryBase[][][] values = new EntryBase[256][][];
    EntryBase cache = null;

    public void put(int msw, int lsw, EntryBase entry) {
        put(entry);
    }

    public EntryBase getEntry(int msw, int lsw) {
        return getEntry(toLong(msw, lsw));
    }

    public synchronized void put(EntryBase entry) {
        int mainIdx = (int) (entry.key & 255);
        EntryBase[][] outer = this.values[mainIdx];
        if (outer == null) this.values[mainIdx] = outer = new EntryBase[256][];

        int outerIdx = (int) ((entry.key >> 32) & 255);
        EntryBase[] inner = outer[outerIdx];

        if (inner == null) {
            outer[outerIdx] = inner = new EntryBase[5];
            inner[0] = this.cache = entry;
        } else {
            int i;
            for (i = 0; i < inner.length; i++) {
                if (inner[i] == null || inner[i].key == entry.key) {
                    inner[i] = this.cache = entry;
                    return;
                }
            }

            outer[outerIdx] = inner = Arrays_copyOf(inner, i + i);
            inner[i] = entry;
        }
    }

    public synchronized EntryBase getEntry(long key) {
        return containsKey(key) ? cache : null;
    }

    public synchronized boolean containsKey(long key) {
        if (this.cache != null && cache.key == key) return true;

        int outerIdx = (int) ((key >> 32) & 255);
        EntryBase[][] outer = this.values[(int) (key & 255)];
        if (outer == null) return false;

        EntryBase[] inner = outer[outerIdx];
        if (inner == null) return false;

        for (int i = 0; i < inner.length; i++) {
            EntryBase e = inner[i];
            if (e == null) {
                return false;
            } else if (e.key == key) {
                this.cache = e;
                return true;
            }
        }
        return false;
    }

    public synchronized void remove(long key) {
        EntryBase[][] outer = this.values[(int) (key & 255)];
        if (outer == null) return;

        EntryBase[] inner = outer[(int) ((key >> 32) & 255)];
        if (inner == null) return;

        for (int i = 0; i < inner.length; i++) {
            if (inner[i] == null) continue;

            if (inner[i].key == key) {
                for (i++; i < inner.length; i++) {
                    if (inner[i] == null) break;
                    inner[i-1] = inner[i];
                }

                inner[i-1] = null;
                this.cache = null;
                return;
            }
        }
    }

    public synchronized ArrayList<EntryBase> entries() {
        ArrayList<EntryBase> ret = new ArrayList<EntryBase>();

        for (EntryBase[][] outer: this.values) {
            if (outer == null) continue;

            for (EntryBase[] inner: outer) {
                if (inner == null) continue;

                for (EntryBase entry: inner) {
                    if (entry == null) break;

                    ret.add(entry);
                }
            }
        }
        return ret;
    }
}