0caafdd608d1d0cd43e909398cbd4a6e82278a7f
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / util / SimpleLookupTable.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.core.util;
12
13 /**
14  * A simple lookup table is a non-synchronized Hashtable, whose keys and values
15  * are Objects. It also uses linear probing to resolve collisions rather than a
16  * linked list of hash table entries.
17  */
18 public final class SimpleLookupTable implements Cloneable {
19
20         // to avoid using Enumerations, walk the individual tables skipping nulls
21         public Object[] keyTable;
22
23         public Object[] valueTable;
24
25         public int elementSize; // number of elements in the table
26
27         public int threshold;
28
29         public SimpleLookupTable() {
30                 this(13);
31         }
32
33         public SimpleLookupTable(int size) {
34                 if (size < 3)
35                         size = 3;
36                 this.elementSize = 0;
37                 this.threshold = size + 1; // size is the expected number of elements
38                 int tableLength = 2 * size + 1;
39                 this.keyTable = new Object[tableLength];
40                 this.valueTable = new Object[tableLength];
41         }
42
43         public Object clone() throws CloneNotSupportedException {
44                 SimpleLookupTable result = (SimpleLookupTable) super.clone();
45                 result.elementSize = this.elementSize;
46                 result.threshold = this.threshold;
47
48                 int length = this.keyTable.length;
49                 result.keyTable = new Object[length];
50                 System.arraycopy(this.keyTable, 0, result.keyTable, 0, length);
51
52                 length = this.valueTable.length;
53                 result.valueTable = new Object[length];
54                 System.arraycopy(this.valueTable, 0, result.valueTable, 0, length);
55                 return result;
56         }
57
58         public boolean containsKey(Object key) {
59                 int length = keyTable.length;
60                 int index = (key.hashCode() & 0x7FFFFFFF) % length;
61                 Object currentKey;
62                 while ((currentKey = keyTable[index]) != null) {
63                         if (currentKey.equals(key))
64                                 return true;
65                         if (++index == length)
66                                 index = 0;
67                 }
68                 return false;
69         }
70
71         public Object get(Object key) {
72                 int length = keyTable.length;
73                 int index = (key.hashCode() & 0x7FFFFFFF) % length;
74                 Object currentKey;
75                 while ((currentKey = keyTable[index]) != null) {
76                         if (currentKey.equals(key))
77                                 return valueTable[index];
78                         if (++index == length)
79                                 index = 0;
80                 }
81                 return null;
82         }
83
84         public Object keyForValue(Object valueToMatch) {
85                 if (valueToMatch != null)
86                         for (int i = 0, l = valueTable.length; i < l; i++)
87                                 if (valueToMatch.equals(valueTable[i]))
88                                         return keyTable[i];
89                 return null;
90         }
91
92         public Object put(Object key, Object value) {
93                 int length = keyTable.length;
94                 int index = (key.hashCode() & 0x7FFFFFFF) % length;
95                 Object currentKey;
96                 while ((currentKey = keyTable[index]) != null) {
97                         if (currentKey.equals(key))
98                                 return valueTable[index] = value;
99                         if (++index == length)
100                                 index = 0;
101                 }
102                 keyTable[index] = key;
103                 valueTable[index] = value;
104
105                 // assumes the threshold is never equal to the size of the table
106                 if (++elementSize > threshold)
107                         rehash();
108                 return value;
109         }
110
111         public Object removeKey(Object key) {
112                 int length = keyTable.length;
113                 int index = (key.hashCode() & 0x7FFFFFFF) % length;
114                 Object currentKey;
115                 while ((currentKey = keyTable[index]) != null) {
116                         if (currentKey.equals(key)) {
117                                 elementSize--;
118                                 Object oldValue = valueTable[index];
119                                 keyTable[index] = null;
120                                 valueTable[index] = null;
121                                 if (keyTable[index + 1 == length ? 0 : index + 1] != null)
122                                         rehash(); // only needed if a possible collision existed
123                                 return oldValue;
124                         }
125                         if (++index == length)
126                                 index = 0;
127                 }
128                 return null;
129         }
130
131         public void removeValue(Object valueToRemove) {
132                 boolean rehash = false;
133                 for (int i = 0, l = valueTable.length; i < l; i++) {
134                         Object value = valueTable[i];
135                         if (value != null && value.equals(valueToRemove)) {
136                                 elementSize--;
137                                 keyTable[i] = null;
138                                 valueTable[i] = null;
139                                 if (!rehash && keyTable[i + 1 == l ? 0 : i + 1] != null)
140                                         rehash = true; // only needed if a possible collision
141                                                                         // existed
142                         }
143                 }
144                 if (rehash)
145                         rehash();
146         }
147
148         private void rehash() {
149                 SimpleLookupTable newLookupTable = new SimpleLookupTable(
150                                 elementSize * 2); // double the number of expected elements
151                 Object currentKey;
152                 for (int i = keyTable.length; --i >= 0;)
153                         if ((currentKey = keyTable[i]) != null)
154                                 newLookupTable.put(currentKey, valueTable[i]);
155
156                 this.keyTable = newLookupTable.keyTable;
157                 this.valueTable = newLookupTable.valueTable;
158                 this.elementSize = newLookupTable.elementSize;
159                 this.threshold = newLookupTable.threshold;
160         }
161
162         public String toString() {
163                 String s = ""; //$NON-NLS-1$
164                 Object object;
165                 for (int i = 0, l = valueTable.length; i < l; i++)
166                         if ((object = valueTable[i]) != null)
167                                 s += keyTable[i].toString() + " -> " + object.toString() + "\n"; //$NON-NLS-2$ //$NON-NLS-1$
168                 return s;
169         }
170 }