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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core.util;
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.
18 public final class SimpleLookupTable implements Cloneable {
20 // to avoid using Enumerations, walk the individual tables skipping nulls
21 public Object[] keyTable;
23 public Object[] valueTable;
25 public int elementSize; // number of elements in the table
29 public SimpleLookupTable() {
33 public SimpleLookupTable(int size) {
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];
43 public Object clone() throws CloneNotSupportedException {
44 SimpleLookupTable result = (SimpleLookupTable) super.clone();
45 result.elementSize = this.elementSize;
46 result.threshold = this.threshold;
48 int length = this.keyTable.length;
49 result.keyTable = new Object[length];
50 System.arraycopy(this.keyTable, 0, result.keyTable, 0, length);
52 length = this.valueTable.length;
53 result.valueTable = new Object[length];
54 System.arraycopy(this.valueTable, 0, result.valueTable, 0, length);
58 public boolean containsKey(Object key) {
59 int length = keyTable.length;
60 int index = (key.hashCode() & 0x7FFFFFFF) % length;
62 while ((currentKey = keyTable[index]) != null) {
63 if (currentKey.equals(key))
65 if (++index == length)
71 public Object get(Object key) {
72 int length = keyTable.length;
73 int index = (key.hashCode() & 0x7FFFFFFF) % length;
75 while ((currentKey = keyTable[index]) != null) {
76 if (currentKey.equals(key))
77 return valueTable[index];
78 if (++index == length)
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]))
92 public Object put(Object key, Object value) {
93 int length = keyTable.length;
94 int index = (key.hashCode() & 0x7FFFFFFF) % length;
96 while ((currentKey = keyTable[index]) != null) {
97 if (currentKey.equals(key))
98 return valueTable[index] = value;
99 if (++index == length)
102 keyTable[index] = key;
103 valueTable[index] = value;
105 // assumes the threshold is never equal to the size of the table
106 if (++elementSize > threshold)
111 public Object removeKey(Object key) {
112 int length = keyTable.length;
113 int index = (key.hashCode() & 0x7FFFFFFF) % length;
115 while ((currentKey = keyTable[index]) != null) {
116 if (currentKey.equals(key)) {
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
125 if (++index == length)
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)) {
138 valueTable[i] = null;
139 if (!rehash && keyTable[i + 1 == l ? 0 : i + 1] != null)
140 rehash = true; // only needed if a possible collision
148 private void rehash() {
149 SimpleLookupTable newLookupTable = new SimpleLookupTable(
150 elementSize * 2); // double the number of expected elements
152 for (int i = keyTable.length; --i >= 0;)
153 if ((currentKey = keyTable[i]) != null)
154 newLookupTable.put(currentKey, valueTable[i]);
156 this.keyTable = newLookupTable.keyTable;
157 this.valueTable = newLookupTable.valueTable;
158 this.elementSize = newLookupTable.elementSize;
159 this.threshold = newLookupTable.threshold;
162 public String toString() {
163 String s = ""; //$NON-NLS-1$
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$