A massive organize imports and formatting of the sources using default Eclipse code...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / util / HashtableOfType.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.ReferenceBinding;
15
16 public final class HashtableOfType {
17         // to avoid using Enumerations, walk the individual tables skipping nulls
18         public char[] keyTable[];
19
20         public ReferenceBinding valueTable[];
21
22         int elementSize; // number of elements in the table
23
24         int threshold;
25
26         public HashtableOfType() {
27                 this(3);
28         }
29
30         public HashtableOfType(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 ReferenceBinding[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 ReferenceBinding 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 ReferenceBinding put(char[] key, ReferenceBinding 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                 HashtableOfType newHashtable = new HashtableOfType(
88                                 elementSize < 100 ? 100 : elementSize * 2); // double the number
89                                                                                                                         // of expected
90                                                                                                                         // elements
91                 char[] currentKey;
92                 for (int i = keyTable.length; --i >= 0;)
93                         if ((currentKey = keyTable[i]) != null)
94                                 newHashtable.put(currentKey, valueTable[i]);
95
96                 this.keyTable = newHashtable.keyTable;
97                 this.valueTable = newHashtable.valueTable;
98                 this.threshold = newHashtable.threshold;
99         }
100
101         public int size() {
102                 return elementSize;
103         }
104
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$
111                 return s;
112         }
113 }