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;
13 import net.sourceforge.phpdt.internal.compiler.lookup.ReferenceBinding;
15 public final class HashtableOfType {
16 // to avoid using Enumerations, walk the individual tables skipping nulls
17 public char[] keyTable[];
18 public ReferenceBinding valueTable[];
20 int elementSize; // number of elements in the table
22 public HashtableOfType() {
25 public HashtableOfType(int size) {
27 this.threshold = size; // size represents the expected number of elements
28 int extraRoom = (int) (size * 1.75f);
29 if (this.threshold == extraRoom)
31 this.keyTable = new char[extraRoom][];
32 this.valueTable = new ReferenceBinding[extraRoom];
34 public boolean containsKey(char[] key) {
35 int index = CharOperation.hashCode(key) % valueTable.length;
36 int keyLength = key.length;
38 while ((currentKey = keyTable[index]) != null) {
39 if (currentKey.length == keyLength && CharOperation.prefixEquals(currentKey, key))
41 index = (index + 1) % keyTable.length;
45 public ReferenceBinding get(char[] key) {
46 int index = CharOperation.hashCode(key) % valueTable.length;
47 int keyLength = key.length;
49 while ((currentKey = keyTable[index]) != null) {
50 if (currentKey.length == keyLength && CharOperation.prefixEquals(currentKey, key))
51 return valueTable[index];
52 index = (index + 1) % keyTable.length;
56 public ReferenceBinding put(char[] key, ReferenceBinding value) {
57 int index = CharOperation.hashCode(key) % valueTable.length;
58 int keyLength = key.length;
60 while ((currentKey = keyTable[index]) != null) {
61 if (currentKey.length == keyLength && CharOperation.prefixEquals(currentKey, key))
62 return valueTable[index] = value;
63 index = (index + 1) % keyTable.length;
65 keyTable[index] = key;
66 valueTable[index] = value;
68 // assumes the threshold is never equal to the size of the table
69 if (++elementSize > threshold)
73 private void rehash() {
74 HashtableOfType newHashtable = new HashtableOfType(elementSize < 100 ? 100 : elementSize * 2); // double the number of expected elements
76 for (int i = keyTable.length; --i >= 0;)
77 if ((currentKey = keyTable[i]) != null)
78 newHashtable.put(currentKey, valueTable[i]);
80 this.keyTable = newHashtable.keyTable;
81 this.valueTable = newHashtable.valueTable;
82 this.threshold = newHashtable.threshold;
87 public String toString() {
88 String s = ""; //$NON-NLS-1$
89 ReferenceBinding type;
90 for (int i = 0, length = valueTable.length; i < length; i++)
91 if ((type = valueTable[i]) != null)
92 s += type.toString() + "\n"; //$NON-NLS-1$