1 /*******************************************************************************
2 * Copyright (c) 2000, 2001, 2002 International Business Machines Corp. and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v0.5
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v05.html
9 * IBM Corporation - initial API and implementation
10 ******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.util;
14 * Hashtable of {char[] --> Object }
16 public final class HashtableOfObject implements Cloneable {
18 // to avoid using Enumerations, walk the individual tables skipping nulls
19 public char[] keyTable[];
20 public Object valueTable[];
22 public int elementSize; // number of elements in the table
25 public HashtableOfObject() {
29 public HashtableOfObject(int size) {
32 this.threshold = size; // size represents the expected number of elements
33 int extraRoom = (int) (size * 1.75f);
34 if (this.threshold == extraRoom)
36 this.keyTable = new char[extraRoom][];
37 this.valueTable = new Object[extraRoom];
40 public Object clone() throws CloneNotSupportedException {
41 HashtableOfObject result = (HashtableOfObject) super.clone();
42 result.elementSize = this.elementSize;
43 result.threshold = this.threshold;
45 int length = this.keyTable.length;
46 result.keyTable = new char[length][];
47 System.arraycopy(this.keyTable, 0, result.keyTable, 0, length);
49 length = this.valueTable.length;
50 result.valueTable = new Object[length];
51 System.arraycopy(this.valueTable, 0, result.valueTable, 0, length);
55 public boolean containsKey(char[] key) {
57 int index = CharOperation.hashCode(key) % valueTable.length;
58 int keyLength = key.length;
60 while ((currentKey = keyTable[index]) != null) {
61 if (currentKey.length == keyLength
62 && CharOperation.prefixEquals(currentKey, key))
64 index = (index + 1) % keyTable.length;
69 public Object get(char[] key) {
71 int index = CharOperation.hashCode(key) % valueTable.length;
72 int keyLength = key.length;
74 while ((currentKey = keyTable[index]) != null) {
75 if (currentKey.length == keyLength
76 && CharOperation.prefixEquals(currentKey, key))
77 return valueTable[index];
78 index = (index + 1) % keyTable.length;
83 public Object put(char[] key, Object value) {
85 int index = CharOperation.hashCode(key) % valueTable.length;
86 int keyLength = key.length;
88 while ((currentKey = keyTable[index]) != null) {
89 if (currentKey.length == keyLength
90 && CharOperation.prefixEquals(currentKey, key))
91 return valueTable[index] = value;
92 index = (index + 1) % keyTable.length;
94 keyTable[index] = key;
95 valueTable[index] = value;
97 // assumes the threshold is never equal to the size of the table
98 if (++elementSize > threshold)
103 public Object removeKey(char[] key) {
105 int index = CharOperation.hashCode(key) % valueTable.length;
106 int keyLength = key.length;
108 while ((currentKey = keyTable[index]) != null) {
109 if (currentKey.length == keyLength
110 && CharOperation.prefixEquals(currentKey, key)) {
111 Object value = valueTable[index];
113 keyTable[index] = null;
114 valueTable[index] = null;
118 index = (index + 1) % keyTable.length;
123 private void rehash() {
125 HashtableOfObject newHashtable = new HashtableOfObject(elementSize * 2); // double the number of expected elements
127 for (int i = keyTable.length; --i >= 0;)
128 if ((currentKey = keyTable[i]) != null)
129 newHashtable.put(currentKey, valueTable[i]);
131 this.keyTable = newHashtable.keyTable;
132 this.valueTable = newHashtable.valueTable;
133 this.threshold = newHashtable.threshold;
140 public String toString() {
141 String s = ""; //$NON-NLS-1$
143 for (int i = 0, length = valueTable.length; i < length; i++)
144 if ((object = valueTable[i]) != null)
145 s += new String(keyTable[i]) + " -> " + object.toString() + "\n"; //$NON-NLS-2$ //$NON-NLS-1$