Added patch from #1437426: error in assign template
[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         public PackageBinding valueTable[];
20
21         int elementSize; // number of elements in the table
22         int threshold;
23 public HashtableOfPackage() {
24         this(3); // usually not very large
25 }
26 public HashtableOfPackage(int size) {
27         this.elementSize = 0;
28         this.threshold = size; // size represents the expected number of elements
29         int extraRoom = (int) (size * 1.75f);
30         if (this.threshold == extraRoom)
31                 extraRoom++;
32         this.keyTable = new char[extraRoom][];
33         this.valueTable = new PackageBinding[extraRoom];
34 }
35 public boolean containsKey(char[] key) {
36         int index = CharOperation.hashCode(key) % valueTable.length;
37         int keyLength = key.length;
38         char[] currentKey;
39         while ((currentKey = keyTable[index]) != null) {
40                 if (currentKey.length == keyLength && CharOperation.prefixEquals(currentKey, key))
41                         return true;
42                 index = (index + 1) % keyTable.length;
43         }
44         return false;
45 }
46 public PackageBinding get(char[] key) {
47         int index = CharOperation.hashCode(key) % valueTable.length;
48         int keyLength = key.length;
49         char[] currentKey;
50         while ((currentKey = keyTable[index]) != null) {
51                 if (currentKey.length == keyLength && CharOperation.prefixEquals(currentKey, key))
52                         return valueTable[index];
53                 index = (index + 1) % keyTable.length;
54         }
55         return null;
56 }
57 public PackageBinding put(char[] key, PackageBinding value) {
58         int index = CharOperation.hashCode(key) % valueTable.length;
59         int keyLength = key.length;
60         char[] currentKey;
61         while ((currentKey = keyTable[index]) != null) {
62                 if (currentKey.length == keyLength && CharOperation.prefixEquals(currentKey, key))
63                         return valueTable[index] = value;
64                 index = (index + 1) % keyTable.length;
65         }
66         keyTable[index] = key;
67         valueTable[index] = value;
68
69         // assumes the threshold is never equal to the size of the table
70         if (++elementSize > threshold)
71                 rehash();
72         return value;
73 }
74 private void rehash() {
75         HashtableOfPackage newHashtable = new HashtableOfPackage(elementSize * 2); // double the number of expected elements
76         char[] currentKey;
77         for (int i = keyTable.length; --i >= 0;)
78                 if ((currentKey = keyTable[i]) != null)
79                         newHashtable.put(currentKey, valueTable[i]);
80
81         this.keyTable = newHashtable.keyTable;
82         this.valueTable = newHashtable.valueTable;
83         this.threshold = newHashtable.threshold;
84 }
85 public int size() {
86         return elementSize;
87 }
88 public String toString() {
89         String s = ""; //$NON-NLS-1$
90         PackageBinding pkg;
91         for (int i = 0, length = valueTable.length; i < length; i++)
92                 if ((pkg = valueTable[i]) != null)
93                         s += pkg.toString() + "\n"; //$NON-NLS-1$
94         return s;
95 }
96 }