summaryrefslogtreecommitdiff
path: root/java/src/IceInternal/IntMap.java
blob: 85293209c2ed734dbb6f4c8379e057155dec345d (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// **********************************************************************
//
// Copyright (c) 2003
// ZeroC, Inc.
// Billerica, MA, USA
//
// All Rights Reserved.
//
// Ice is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License version 2 as published by
// the Free Software Foundation.
//
// **********************************************************************

package IceInternal;

public class IntMap
{
    public
    IntMap(int initialCapacity, float loadFactor)
    {
        if(initialCapacity > MAXIMUM_CAPACITY)
        {
            initialCapacity = MAXIMUM_CAPACITY;
        }

        // Find a power of 2 >= initialCapacity
        int capacity = 1;
        while(capacity < initialCapacity)
        {
            capacity <<= 1;
        }

        _loadFactor = loadFactor;
        _threshold = (int)(capacity * loadFactor);
        _table = new Entry[capacity];
    }

    public
    IntMap(int initialCapacity)
    {
        this(initialCapacity, DEFAULT_LOAD_FACTOR);
    }

    public
    IntMap()
    {
        _loadFactor = DEFAULT_LOAD_FACTOR;
        _threshold = (int)(DEFAULT_INITIAL_CAPACITY);
        _table = new Entry[DEFAULT_INITIAL_CAPACITY];
    }

    public int
    size()
    {
        return _size;
    }

    public boolean
    isEmpty()
    {
        return _size == 0;
    }

    public Object
    get(int key)
    {
        int i = indexFor(key, _table.length);
        Entry e = _table[i];
        while(true)
        {
            if(e == null)
            {
                return e;
            }
            if(key == e.key)
            {
                return e.value;
            }
            e = e.next;
        }
    }

    public boolean
    containsKey(int key)
    {
        int i = indexFor(key, _table.length);
        Entry e = _table[i];
        while(e != null)
        {
            if(key == e.key)
            {
                return true;
            }
            e = e.next;
        }
        return false;
    }

    public Object
    put(int key, Object value)
    {
        int i = indexFor(key, _table.length);

        for(Entry e = _table[i]; e != null; e = e.next)
        {
            if(key == e.key)
            {
                Object oldValue = e.value;
                e.value = value;
                return oldValue;
            }
        }

        _modCount++;
        addEntry(key, value, i);
        return null;
    }

    public Object
    remove(int key)
    {
        int i = indexFor(key, _table.length);
        Entry prev = _table[i];
        Entry e = prev;

        while(e != null)
        {
            Entry next = e.next;
            if(key == e.key)
            {
                _modCount++;
                _size--;
                if(prev == e)
                {
                    _table[i] = next;
                }
                else
                {
                    prev.next = next;
                }
                e.next = _entryCache;
                _entryCache = e;
                return e.value;
            }
            prev = e;
            e = next;
        }

        return (e == null ? e : e.value);
    }

    public void
    clear()
    {
        _modCount++;
        Entry tab[] = _table;
        for(int i = 0; i < tab.length; i++)
        {
            tab[i] = null;
        }
        _size = 0;
    }

    public java.util.Iterator
    entryIterator()
    {
        return new EntryIterator();
    }

    public static final class Entry
    {
        int key;
        Object value;
        Entry next;

        Entry(int k, Object v, Entry n)
        {
            key = k;
            value = v;
            next = n;
        }

        public int
        getKey()
        {
            return key;
        }

        public Object
        getValue()
        {
            return value;
        }

        public Object
        setValue(Object newValue)
        {
            Object oldValue = value;
            value = newValue;
            return oldValue;
        }
    }

    private static int
    indexFor(int key, int length)
    {
        return key & (length - 1);
    }

    private void
    addEntry(int key, Object value, int bucketIndex)
    {
        Entry e;
        if(_entryCache != null)
        {
            e = _entryCache;
            _entryCache = _entryCache.next;
            e.key = key;
            e.value = value;
            e.next = _table[bucketIndex];
        }
        else
        {
            e = new Entry(key, value, _table[bucketIndex]);
        }
        _table[bucketIndex] = e;
        if(_size++ >= _threshold)
        {
            resize(2 * _table.length);
        }
    }

    private void
    resize(int newCapacity)
    {
        // assert (newCapacity & -newCapacity) == newCapacity; // power of 2
        Entry[] oldTable = _table;
        int oldCapacity = oldTable.length;

        // check if needed
        if(_size < _threshold || oldCapacity > newCapacity)
        {
            return;
        }

        Entry[] newTable = new Entry[newCapacity];
        transfer(newTable);
        _table = newTable;
        _threshold = (int)(newCapacity * _loadFactor);
    }

    private void
    transfer(Entry[] newTable)
    {
        Entry[] src = _table;
        int newCapacity = newTable.length;
        for(int j = 0; j < src.length; j++)
        {
            Entry e = src[j];
            if(e != null)
            {
                src[j] = null;
                do
                {
                    Entry next = e.next;
                    int i = indexFor(e.key, newCapacity);
                    e.next = newTable[i];
                    newTable[i] = e;
                    e = next;
                }
                while(e != null);
            }
        }
    }

    private class EntryIterator implements java.util.Iterator
    {
        EntryIterator()
        {
            _expectedModCount = _modCount;
            Entry[] t = _table;
            int i = t.length;
            Entry n = null;
            if(_size != 0) // advance to first entry
            {
                while(i > 0 && (n = t[--i]) == null)
                    ;
            }
            _next = n;
            _index = i;
        }

        public boolean
        hasNext()
        {
            return _next != null;
        }

        public Object
        next()
        {
            if(_modCount != _expectedModCount)
            {
                throw new java.util.ConcurrentModificationException();
            }
            Entry e = _next;
            if(e == null)
            {
                throw new java.util.NoSuchElementException();
            }

            Entry n = e.next;
            Entry[] t = _table;
            int i = _index;
            while(n == null && i > 0)
            {
                n = t[--i];
            }
            _index = i;
            _next = n;
            return _current = e;
        }

        public void
        remove()
        {
            if(_current == null)
            {
                throw new IllegalStateException();
            }
            if(_modCount != _expectedModCount)
            {
                throw new java.util.ConcurrentModificationException();
            }
            int k = _current.key;
            _current = null;
            IntMap.this.remove(k);
            _expectedModCount = _modCount;
        }

        private Entry _next;
        private int _expectedModCount;
        private int _index;
        private Entry _current;
    }

    //
    // The default initial capacity - MUST be a power of two.
    //
    private static final int DEFAULT_INITIAL_CAPACITY = 16;

    //
    // The maximum capacity, used if a higher value is implicitly specified
    // by either of the constructors with arguments.
    // MUST be a power of two <= 1<<30.
    //
    private static final int MAXIMUM_CAPACITY = 1 << 30;

    //
    // The default load factor.
    //
    private static final float DEFAULT_LOAD_FACTOR = 0.75f;

    //
    // The table, resized as necessary. Length MUST Always be a power of two.
    //
    private Entry[] _table;

    //
    // The number of key-value mappings contained in this map.
    //
    private int _size;

    //
    // The next size value at which to resize (capacity * load factor).
    //
    private int _threshold;

    //
    // The load factor for the hash table.
    //
    private final float _loadFactor;

    //
    // The number of times this map has been structurally modified
    // Structural modifications are those that change the number of
    // mappings in the map or otherwise modify its internal structure
    // (e.g., rehash). This field is used to make iterators fail-fast.
    //
    private volatile int _modCount;

    private Entry _entryCache;
}