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.compiler.util;
13 import net.sourceforge.phpdt.core.compiler.CharOperation;
16 * Hashtable of {char[] --> Object }
18 public final class HashtableOfObject implements Cloneable {
20 // to avoid using Enumerations, walk the individual tables skipping nulls
21 public char[] keyTable[];
22 public Object valueTable[];
24 public int elementSize; // number of elements in the table
27 public HashtableOfObject() {
31 public HashtableOfObject(int size) {
34 this.threshold = size; // size represents the expected number of elements
35 int extraRoom = (int) (size * 1.75f);
36 if (this.threshold == extraRoom)
38 this.keyTable = new char[extraRoom][];
39 this.valueTable = new Object[extraRoom];
42 public Object clone() throws CloneNotSupportedException {
43 HashtableOfObject result = (HashtableOfObject) super.clone();
44 result.elementSize = this.elementSize;
45 result.threshold = this.threshold;
47 int length = this.keyTable.length;
48 result.keyTable = new char[length][];
49 System.arraycopy(this.keyTable, 0, result.keyTable, 0, length);
51 length = this.valueTable.length;
52 result.valueTable = new Object[length];
53 System.arraycopy(this.valueTable, 0, result.valueTable, 0, length);
57 public boolean containsKey(char[] key) {
59 int index = CharOperation.hashCode(key) % valueTable.length;
60 int keyLength = key.length;
62 while ((currentKey = keyTable[index]) != null) {
63 if (currentKey.length == keyLength
64 && CharOperation.prefixEquals(currentKey, key))
66 index = (index + 1) % keyTable.length;
71 public Object get(char[] key) {
73 int index = CharOperation.hashCode(key) % valueTable.length;
74 int keyLength = key.length;
76 while ((currentKey = keyTable[index]) != null) {
77 if (currentKey.length == keyLength
78 && CharOperation.prefixEquals(currentKey, key))
79 return valueTable[index];
80 index = (index + 1) % keyTable.length;
85 public Object put(char[] key, Object value) {
87 int index = CharOperation.hashCode(key) % valueTable.length;
88 int keyLength = key.length;
90 while ((currentKey = keyTable[index]) != null) {
91 if (currentKey.length == keyLength
92 && CharOperation.prefixEquals(currentKey, key))
93 return valueTable[index] = value;
94 index = (index + 1) % keyTable.length;
96 keyTable[index] = key;
97 valueTable[index] = value;
99 // assumes the threshold is never equal to the size of the table
100 if (++elementSize > threshold)
105 public Object removeKey(char[] key) {
107 int index = CharOperation.hashCode(key) % valueTable.length;
108 int keyLength = key.length;
110 while ((currentKey = keyTable[index]) != null) {
111 if (currentKey.length == keyLength
112 && CharOperation.prefixEquals(currentKey, key)) {
113 Object value = valueTable[index];
115 keyTable[index] = null;
116 valueTable[index] = null;
120 index = (index + 1) % keyTable.length;
125 private void rehash() {
127 HashtableOfObject newHashtable = new HashtableOfObject(elementSize * 2); // double the number of expected elements
129 for (int i = keyTable.length; --i >= 0;)
130 if ((currentKey = keyTable[i]) != null)
131 newHashtable.put(currentKey, valueTable[i]);
133 this.keyTable = newHashtable.keyTable;
134 this.valueTable = newHashtable.valueTable;
135 this.threshold = newHashtable.threshold;
142 public String toString() {
143 String s = ""; //$NON-NLS-1$
145 for (int i = 0, length = valueTable.length; i < length; i++)
146 if ((object = valueTable[i]) != null)
147 s += new String(keyTable[i]) + " -> " + object.toString() + "\n"; //$NON-NLS-2$ //$NON-NLS-1$