Fix bug #672
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / util / HashtableOfLong.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.util;
12
13 /**
14  * Hashtable for non-zero long keys.
15  */
16
17 public final class HashtableOfLong {
18         // to avoid using Enumerations, walk the individual tables skipping nulls
19         public long[] keyTable;
20
21         public Object[] valueTable;
22
23         int elementSize; // number of elements in the table
24
25         int threshold;
26
27         public HashtableOfLong() {
28                 this(13);
29         }
30
31         public HashtableOfLong(int size) {
32                 this.elementSize = 0;
33                 this.threshold = size; // size represents the expected number of
34                                                                 // elements
35                 int extraRoom = (int) (size * 1.75f);
36                 if (this.threshold == extraRoom)
37                         extraRoom++;
38                 this.keyTable = new long[extraRoom];
39                 this.valueTable = new Object[extraRoom];
40         }
41
42         public boolean containsKey(long key) {
43                 int index = ((int) (key >>> 32)) % valueTable.length;
44                 long currentKey;
45                 while ((currentKey = keyTable[index]) != 0) {
46                         if (currentKey == key)
47                                 return true;
48                         index = (index + 1) % keyTable.length;
49                 }
50                 return false;
51         }
52
53         public Object get(long key) {
54                 int index = ((int) (key >>> 32)) % valueTable.length;
55                 long currentKey;
56                 while ((currentKey = keyTable[index]) != 0) {
57                         if (currentKey == key)
58                                 return valueTable[index];
59                         index = (index + 1) % keyTable.length;
60                 }
61                 return null;
62         }
63
64         public Object put(long key, Object value) {
65                 int index = ((int) (key >>> 32)) % valueTable.length;
66                 long currentKey;
67                 while ((currentKey = keyTable[index]) != 0) {
68                         if (currentKey == key)
69                                 return valueTable[index] = value;
70                         index = (index + 1) % keyTable.length;
71                 }
72                 keyTable[index] = key;
73                 valueTable[index] = value;
74
75                 // assumes the threshold is never equal to the size of the table
76                 if (++elementSize > threshold)
77                         rehash();
78                 return value;
79         }
80
81         private void rehash() {
82                 HashtableOfLong newHashtable = new HashtableOfLong(elementSize * 2); // double
83                                                                                                                                                                 // the
84                                                                                                                                                                 // number
85                                                                                                                                                                 // of
86                                                                                                                                                                 // expected
87                                                                                                                                                                 // elements
88                 long currentKey;
89                 for (int i = keyTable.length; --i >= 0;)
90                         if ((currentKey = keyTable[i]) != 0)
91                                 newHashtable.put(currentKey, valueTable[i]);
92
93                 this.keyTable = newHashtable.keyTable;
94                 this.valueTable = newHashtable.valueTable;
95                 this.threshold = newHashtable.threshold;
96         }
97
98         public int size() {
99                 return elementSize;
100         }
101
102         public String toString() {
103                 String s = ""; //$NON-NLS-1$
104                 Object object;
105                 for (int i = 0, length = valueTable.length; i < length; i++)
106                         if ((object = valueTable[i]) != null)
107                                 s += keyTable[i] + " -> " + object.toString() + "\n"; //$NON-NLS-2$ //$NON-NLS-1$
108                 return s;
109         }
110 }