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.PackageBinding;
16 public final class HashtableOfPackage {
17 // to avoid using Enumerations, walk the individual tables skipping nulls
18 public char[] keyTable[];
20 public PackageBinding valueTable[];
22 int elementSize; // number of elements in the table
26 public HashtableOfPackage() {
27 this(3); // usually not very large
30 public HashtableOfPackage(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 PackageBinding[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 PackageBinding 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 PackageBinding put(char[] key, PackageBinding 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 HashtableOfPackage newHashtable = new HashtableOfPackage(
88 elementSize * 2); // double the number of expected elements
90 for (int i = keyTable.length; --i >= 0;)
91 if ((currentKey = keyTable[i]) != null)
92 newHashtable.put(currentKey, valueTable[i]);
94 this.keyTable = newHashtable.keyTable;
95 this.valueTable = newHashtable.valueTable;
96 this.threshold = newHashtable.threshold;
103 public String toString() {
104 String s = ""; //$NON-NLS-1$
106 for (int i = 0, length = valueTable.length; i < length; i++)
107 if ((pkg = valueTable[i]) != null)
108 s += pkg.toString() + "\n"; //$NON-NLS-1$