The name field is now protected (PHPVarDeclaration needs)
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / util / HashtableOfPackage.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  ******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.util;
12
13 import net.sourceforge.phpdt.internal.compiler.lookup.PackageBinding;
14
15 public final class HashtableOfPackage {
16         // to avoid using Enumerations, walk the individual tables skipping nulls
17         public char[] keyTable[];
18         public PackageBinding valueTable[];
19
20         int elementSize; // number of elements in the table
21         int threshold;
22 public HashtableOfPackage() {
23         this(3); // usually not very large
24 }
25 public HashtableOfPackage(int size) {
26         this.elementSize = 0;
27         this.threshold = size; // size represents the expected number of elements
28         int extraRoom = (int) (size * 1.75f);
29         if (this.threshold == extraRoom)
30                 extraRoom++;
31         this.keyTable = new char[extraRoom][];
32         this.valueTable = new PackageBinding[extraRoom];
33 }
34 public boolean containsKey(char[] key) {
35         int index = CharOperation.hashCode(key) % valueTable.length;
36         int keyLength = key.length;
37         char[] currentKey;
38         while ((currentKey = keyTable[index]) != null) {
39                 if (currentKey.length == keyLength && CharOperation.prefixEquals(currentKey, key))
40                         return true;
41                 index = (index + 1) % keyTable.length;
42         }
43         return false;
44 }
45 public PackageBinding get(char[] key) {
46         int index = CharOperation.hashCode(key) % valueTable.length;
47         int keyLength = key.length;
48         char[] currentKey;
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;
53         }
54         return null;
55 }
56 public PackageBinding put(char[] key, PackageBinding value) {
57         int index = CharOperation.hashCode(key) % valueTable.length;
58         int keyLength = key.length;
59         char[] currentKey;
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;
64         }
65         keyTable[index] = key;
66         valueTable[index] = value;
67
68         // assumes the threshold is never equal to the size of the table
69         if (++elementSize > threshold)
70                 rehash();
71         return value;
72 }
73 private void rehash() {
74         HashtableOfPackage newHashtable = new HashtableOfPackage(elementSize * 2); // double the number of expected elements
75         char[] currentKey;
76         for (int i = keyTable.length; --i >= 0;)
77                 if ((currentKey = keyTable[i]) != null)
78                         newHashtable.put(currentKey, valueTable[i]);
79
80         this.keyTable = newHashtable.keyTable;
81         this.valueTable = newHashtable.valueTable;
82         this.threshold = newHashtable.threshold;
83 }
84 public int size() {
85         return elementSize;
86 }
87 public String toString() {
88         String s = ""; //$NON-NLS-1$
89         PackageBinding pkg;
90         for (int i = 0, length = valueTable.length; i < length; i++)
91                 if ((pkg = valueTable[i]) != null)
92                         s += pkg.toString() + "\n"; //$NON-NLS-1$
93         return s;
94 }
95 }