Removed unuse builder from project
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / util / HashtableOfPackage.java
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
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.core.compiler.CharOperation;
14 import net.sourceforge.phpdt.internal.compiler.lookup.PackageBinding;
15
16 public final class HashtableOfPackage {
17         // to avoid using Enumerations, walk the individual tables skipping nulls
18         public char[] keyTable[];
19
20         public PackageBinding valueTable[];
21
22         int elementSize; // number of elements in the table
23
24         int threshold;
25
26         public HashtableOfPackage() {
27                 this(3); // usually not very large
28         }
29
30         public HashtableOfPackage(int size) {
31                 this.elementSize = 0;
32                 this.threshold = size; // size represents the expected number of
33                                                                 // elements
34                 int extraRoom = (int) (size * 1.75f);
35                 if (this.threshold == extraRoom)
36                         extraRoom++;
37                 this.keyTable = new char[extraRoom][];
38                 this.valueTable = new PackageBinding[extraRoom];
39         }
40
41         public boolean containsKey(char[] key) {
42                 int index = CharOperation.hashCode(key) % valueTable.length;
43                 int keyLength = key.length;
44                 char[] currentKey;
45                 while ((currentKey = keyTable[index]) != null) {
46                         if (currentKey.length == keyLength
47                                         && CharOperation.prefixEquals(currentKey, key))
48                                 return true;
49                         index = (index + 1) % keyTable.length;
50                 }
51                 return false;
52         }
53
54         public PackageBinding get(char[] key) {
55                 int index = CharOperation.hashCode(key) % valueTable.length;
56                 int keyLength = key.length;
57                 char[] currentKey;
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;
63                 }
64                 return null;
65         }
66
67         public PackageBinding put(char[] key, PackageBinding value) {
68                 int index = CharOperation.hashCode(key) % valueTable.length;
69                 int keyLength = key.length;
70                 char[] currentKey;
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;
76                 }
77                 keyTable[index] = key;
78                 valueTable[index] = value;
79
80                 // assumes the threshold is never equal to the size of the table
81                 if (++elementSize > threshold)
82                         rehash();
83                 return value;
84         }
85
86         private void rehash() {
87                 HashtableOfPackage newHashtable = new HashtableOfPackage(
88                                 elementSize * 2); // double the number of expected elements
89                 char[] currentKey;
90                 for (int i = keyTable.length; --i >= 0;)
91                         if ((currentKey = keyTable[i]) != null)
92                                 newHashtable.put(currentKey, valueTable[i]);
93
94                 this.keyTable = newHashtable.keyTable;
95                 this.valueTable = newHashtable.valueTable;
96                 this.threshold = newHashtable.threshold;
97         }
98
99         public int size() {
100                 return elementSize;
101         }
102
103         public String toString() {
104                 String s = ""; //$NON-NLS-1$
105                 PackageBinding pkg;
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$
109                 return s;
110         }
111 }