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[];
20 public ReferenceBinding valueTable[];
22 int elementSize; // number of elements in the table
26 public HashtableOfType() {
30 public HashtableOfType(int size) {
32 this.threshold = size; // size represents the expected number of
34 int extraRoom = (int) (size * 1.75f);
35 if (this.threshold == extraRoom)
37 this.keyTable = new char[extraRoom][];
38 this.valueTable = new ReferenceBinding[extraRoom];
41 public boolean containsKey(char[] key) {
42 int index = CharOperation.hashCode(key) % valueTable.length;
43 int keyLength = key.length;
45 while ((currentKey = keyTable[index]) != null) {
46 if (currentKey.length == keyLength
47 && CharOperation.prefixEquals(currentKey, key))
49 index = (index + 1) % keyTable.length;
54 public ReferenceBinding get(char[] key) {
55 int index = CharOperation.hashCode(key) % valueTable.length;
56 int keyLength = key.length;
58 while ((currentKey = keyTable[index]) != null) {
59 if (currentKey.length == keyLength
60 && CharOperation.prefixEquals(currentKey, key))
61 return valueTable[index];
62 index = (index + 1) % keyTable.length;
67 public ReferenceBinding put(char[] key, ReferenceBinding value) {
68 int index = CharOperation.hashCode(key) % valueTable.length;
69 int keyLength = key.length;
71 while ((currentKey = keyTable[index]) != null) {
72 if (currentKey.length == keyLength
73 && CharOperation.prefixEquals(currentKey, key))
74 return valueTable[index] = value;
75 index = (index + 1) % keyTable.length;
77 keyTable[index] = key;
78 valueTable[index] = value;
80 // assumes the threshold is never equal to the size of the table
81 if (++elementSize > threshold)
86 private void rehash() {
87 HashtableOfType newHashtable = new HashtableOfType(
88 elementSize < 100 ? 100 : elementSize * 2); // double the number
92 for (int i = keyTable.length; --i >= 0;)
93 if ((currentKey = keyTable[i]) != null)
94 newHashtable.put(currentKey, valueTable[i]);
96 this.keyTable = newHashtable.keyTable;
97 this.valueTable = newHashtable.valueTable;
98 this.threshold = newHashtable.threshold;
105 public String toString() {
106 String s = ""; //$NON-NLS-1$
107 ReferenceBinding type;
108 for (int i = 0, length = valueTable.length; i < length; i++)
109 if ((type = valueTable[i]) != null)
110 s += type.toString() + "\n"; //$NON-NLS-1$