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;
14 import net.sourceforge.phpdt.internal.compiler.lookup.ReferenceBinding;
16 public final class HashtableOfType {
17 // to avoid using Enumerations, walk the individual tables skipping nulls
18 public char[] keyTable[];
19 public ReferenceBinding valueTable[];
21 int elementSize; // number of elements in the table
23 public HashtableOfType() {
26 public HashtableOfType(int size) {
28 this.threshold = size; // size represents the expected number of elements
29 int extraRoom = (int) (size * 1.75f);
30 if (this.threshold == extraRoom)
32 this.keyTable = new char[extraRoom][];
33 this.valueTable = new ReferenceBinding[extraRoom];
35 public boolean containsKey(char[] key) {
36 int index = CharOperation.hashCode(key) % valueTable.length;
37 int keyLength = key.length;
39 while ((currentKey = keyTable[index]) != null) {
40 if (currentKey.length == keyLength && CharOperation.prefixEquals(currentKey, key))
42 index = (index + 1) % keyTable.length;
46 public ReferenceBinding get(char[] key) {
47 int index = CharOperation.hashCode(key) % valueTable.length;
48 int keyLength = key.length;
50 while ((currentKey = keyTable[index]) != null) {
51 if (currentKey.length == keyLength && CharOperation.prefixEquals(currentKey, key))
52 return valueTable[index];
53 index = (index + 1) % keyTable.length;
57 public ReferenceBinding put(char[] key, ReferenceBinding value) {
58 int index = CharOperation.hashCode(key) % valueTable.length;
59 int keyLength = key.length;
61 while ((currentKey = keyTable[index]) != null) {
62 if (currentKey.length == keyLength && CharOperation.prefixEquals(currentKey, key))
63 return valueTable[index] = value;
64 index = (index + 1) % keyTable.length;
66 keyTable[index] = key;
67 valueTable[index] = value;
69 // assumes the threshold is never equal to the size of the table
70 if (++elementSize > threshold)
74 private void rehash() {
75 HashtableOfType newHashtable = new HashtableOfType(elementSize < 100 ? 100 : elementSize * 2); // double the number of expected elements
77 for (int i = keyTable.length; --i >= 0;)
78 if ((currentKey = keyTable[i]) != null)
79 newHashtable.put(currentKey, valueTable[i]);
81 this.keyTable = newHashtable.keyTable;
82 this.valueTable = newHashtable.valueTable;
83 this.threshold = newHashtable.threshold;
88 public String toString() {
89 String s = ""; //$NON-NLS-1$
90 ReferenceBinding type;
91 for (int i = 0, length = valueTable.length; i < length; i++)
92 if ((type = valueTable[i]) != null)
93 s += type.toString() + "\n"; //$NON-NLS-1$