summaryrefslogtreecommitdiffstats
path: root/src/main/java/net/minecraft/server/LongHashtable.java
blob: b9dc00e1d3749765edbb0145e976cd78c66336c4 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package net.minecraft.server;

import java.util.ArrayList;
import java.util.Arrays;

public class LongHashtable<V> extends LongHash
{
    Object values[][][] = new Object[256][][];
    Entry cache = null;

    public void put(int msw, int lsw, V value) {
        put(toLong(msw, lsw), value);
        if(value instanceof Chunk) {
            Chunk c = (Chunk)value;
            if(msw != c.j || lsw != c.k) {
                MinecraftServer.a.info("Chunk (" + c.j + ", " + c.k +") stored at  (" + msw + ", " + lsw + ")");
                Throwable x = new Throwable();
                x.fillInStackTrace();
                x.printStackTrace();
            }
        }
    }

    public V get(int msw, int lsw) {
        V value = get(toLong(msw, lsw));
        if(value instanceof Chunk) {
            Chunk c = (Chunk)value;
            if(msw != c.j || lsw != c.k) {
                MinecraftServer.a.info("Chunk (" + c.j + ", " + c.k +") stored at  (" + msw + ", " + lsw + ")");
                Throwable x = new Throwable();
                x.fillInStackTrace();
                x.printStackTrace();
            }
        }
        return value;
    }

    public void put(long key, V value) {
        int mainIdx = (int) (key & 255);
        int outerIdx = (int) ((key >> 32) & 255);
        Object outer[][] = this.values[mainIdx], inner[];
        if (outer == null)
            this.values[mainIdx] = outer = new Object[256][];
        inner = outer[outerIdx];
        if (inner == null) {
            outer[outerIdx] = inner = new Object[5];
            inner[0] = cache = new Entry(key, value);
        } else {
            int i;
            for (i = 0; i < inner.length; i++) {
                if (inner[i] == null || ((Entry) inner[i]).key == key) {
                    inner[i] = cache = new Entry(key, value);
                    return;
                }
            }
            outer[outerIdx] = inner = Arrays.copyOf(inner, i + i);
            inner[i] = new Entry(key, value);
        }
    }

    public V get(long key) {
        synchronized(this) {
            return containsKey(key) ? (V) cache.value : null;
        }
    }

    public boolean containsKey(long key) {
        if (cache != null && cache.key == key)
            return true;
        int mainIdx = (int) (key & 255);
        int outerIdx = (int) ((key >> 32) & 255);
        Object outer[][] = this.values[mainIdx], inner[];
        if (outer == null)
            return false;
        inner = outer[outerIdx];
        if (inner == null)
            return false;
        else {
            for (int i = 0; i < inner.length; i++) {
                Entry e = (Entry) inner[i];
                if (e == null)
                    return false;
                else if (e.key == key) {
                    cache = e;
                    return true;
                }
            }
            return false;
        }
    }

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

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

        for (int i = 0; i < inner.length; i++) {
            // No more data! bail
            if (inner[i] == null) continue;

            // Found our key -- purge it
            if (((Entry) inner[i]).key == key) {

                // Move all the elements down
                for (i++; i < inner.length; i++) {
                    if (inner[i] == null) break;
                    inner[i-1] = inner[i];
                }
                inner[i-1] = null;
                cache = null;
                return;
            }
        }        
    }
    
    public ArrayList<V> values() {
        ArrayList<V> ret = new ArrayList<V>();
        for (Object[][] outer : this.values) {
            if (outer == null)
                continue;
            for (Object[] inner : outer) {
                if (inner == null)
                    continue;
                for (Object entry : inner) {
                    if (entry == null)
                        break;
                    ret.add((V) ((Entry) entry).value);
                }
            }
        }
        return ret;
    }

    private class Entry
    {
        long key;
        Object value;

        Entry(long k, Object v) {
            key = k;
            value = v;
        }
    }
}