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
15 * and values are Objects. It also uses linear probing to resolve collisions
16 * rather than a 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;
22 public Object[] valueTable;
23 public int elementSize; // number of elements in the table
26 public SimpleLookupTable() {
30 public SimpleLookupTable(int size) {
31 if (size < 3) size = 3;
33 this.threshold = size + 1; // size is the expected number of elements
34 int tableLength = 2 * size + 1;
35 this.keyTable = new Object[tableLength];
36 this.valueTable = new Object[tableLength];
39 public Object clone() throws CloneNotSupportedException {
40 SimpleLookupTable result = (SimpleLookupTable) super.clone();
41 result.elementSize = this.elementSize;
42 result.threshold = this.threshold;
44 int length = this.keyTable.length;
45 result.keyTable = new Object[length];
46 System.arraycopy(this.keyTable, 0, result.keyTable, 0, length);
48 length = this.valueTable.length;
49 result.valueTable = new Object[length];
50 System.arraycopy(this.valueTable, 0, result.valueTable, 0, length);
54 public boolean containsKey(Object key) {
55 int length = keyTable.length;
56 int index = (key.hashCode() & 0x7FFFFFFF) % length;
58 while ((currentKey = keyTable[index]) != null) {
59 if (currentKey.equals(key)) return true;
60 if (++index == length) index = 0;
65 public Object get(Object key) {
66 int length = keyTable.length;
67 int index = (key.hashCode() & 0x7FFFFFFF) % length;
69 while ((currentKey = keyTable[index]) != null) {
70 if (currentKey.equals(key)) return valueTable[index];
71 if (++index == length) index = 0;
76 public Object keyForValue(Object valueToMatch) {
77 if (valueToMatch != null)
78 for (int i = 0, l = valueTable.length; i < l; i++)
79 if (valueToMatch.equals(valueTable[i]))
84 public Object put(Object key, Object value) {
85 int length = keyTable.length;
86 int index = (key.hashCode() & 0x7FFFFFFF) % length;
88 while ((currentKey = keyTable[index]) != null) {
89 if (currentKey.equals(key)) return valueTable[index] = value;
90 if (++index == length) index = 0;
92 keyTable[index] = key;
93 valueTable[index] = value;
95 // assumes the threshold is never equal to the size of the table
96 if (++elementSize > threshold) rehash();
100 public void removeKey(Object key) {
101 int length = keyTable.length;
102 int index = (key.hashCode() & 0x7FFFFFFF) % length;
104 while ((currentKey = keyTable[index]) != null) {
105 if (currentKey.equals(key)) {
107 keyTable[index] = null;
108 valueTable[index] = null;
109 if (keyTable[index + 1 == length ? 0 : index + 1] != null)
110 rehash(); // only needed if a possible collision existed
113 if (++index == length) index = 0;
117 public void removeValue(Object valueToRemove) {
118 boolean rehash = false;
119 for (int i = 0, l = valueTable.length; i < l; i++) {
120 Object value = valueTable[i];
121 if (value != null && value.equals(valueToRemove)) {
124 valueTable[i] = null;
125 if (!rehash && keyTable[i + 1 == l ? 0 : i + 1] != null)
126 rehash = true; // only needed if a possible collision existed
129 if (rehash) rehash();
132 private void rehash() {
133 SimpleLookupTable newLookupTable = new SimpleLookupTable(elementSize * 2); // double the number of expected elements
135 for (int i = keyTable.length; --i >= 0;)
136 if ((currentKey = keyTable[i]) != null)
137 newLookupTable.put(currentKey, valueTable[i]);
139 this.keyTable = newLookupTable.keyTable;
140 this.valueTable = newLookupTable.valueTable;
141 this.elementSize = newLookupTable.elementSize;
142 this.threshold = newLookupTable.threshold;
145 public String toString() {
146 String s = ""; //$NON-NLS-1$
148 for (int i = 0, l = valueTable.length; i < l; i++)
149 if ((object = valueTable[i]) != null)
150 s += keyTable[i].toString() + " -> " + object.toString() + "\n"; //$NON-NLS-2$ //$NON-NLS-1$