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
9 * IBM Corporation - initial API and implementation
10 ******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.codegen;
13 import net.sourceforge.phpdt.internal.compiler.lookup.FieldBinding;
14 import net.sourceforge.phpdt.internal.compiler.util.CharOperation;
16 public class FieldNameAndTypeCache {
17 public FieldBinding keyTable[];
18 public int valueTable[];
22 * Constructs a new, empty hashtable. A default capacity is used.
23 * Note that the hashtable will automatically grow when it gets full.
25 public FieldNameAndTypeCache() {
29 * Constructs a new, empty hashtable with the specified initial
31 * @param initialCapacity int
32 * the initial number of buckets
34 public FieldNameAndTypeCache(int initialCapacity) {
36 this.threshold = (int) (initialCapacity * 0.66f);
37 this.keyTable = new FieldBinding[initialCapacity];
38 this.valueTable = new int[initialCapacity];
41 * Clears the hash table so that it has no more elements in it.
44 for (int i = keyTable.length; --i >= 0;) {
50 /** Returns true if the collection contains an element for the key.
52 * @param char[] key the key that we are looking for
55 public boolean containsKey(FieldBinding key) {
56 int index = hashCode(key);
57 while (keyTable[index] != null) {
58 if (equalsForNameAndType(keyTable[index], key))
60 index = (index + 1) % keyTable.length;
65 * Return true if the two field binding are consider like equals.
67 public boolean equalsForNameAndType(FieldBinding field1, FieldBinding field2) {
68 return ((field1.type == field2.type) && CharOperation.equals(field1.name, field2.name));
70 /** Gets the object associated with the specified key in the
72 * @param key <CODE>char[]</CODE> the specified key
73 * @return int the element for the key or -1 if the key is not
74 * defined in the hash table.
76 public int get(FieldBinding key) {
77 int index = hashCode(key);
78 while (keyTable[index] != null) {
79 if (equalsForNameAndType(keyTable[index], key))
80 return valueTable[index];
81 index = (index + 1) % keyTable.length;
86 * Return the hashcode for the key parameter
88 * @param key org.eclipse.jdt.internal.compiler.lookup.MethodBinding
91 public int hashCode(FieldBinding key) {
92 return ((CharOperation.hashCode(key.name) + key.type.hashCode()) & 0x7FFFFFFF) % keyTable.length;
95 * Puts the specified element into the hashtable, using the specified
96 * key. The element may be retrieved by doing a get() with the same key.
97 * The key and the element cannot be null.
99 * @param key <CODE>Object</CODE> the specified key in the hashtable
100 * @param value <CODE>int</CODE> the specified element
101 * @return int the old value of the key, or -1 if it did not have one.
103 public int put(FieldBinding key, int value) {
104 int index = hashCode(key);
105 while (keyTable[index] != null) {
106 if (equalsForNameAndType(keyTable[index], key))
107 return valueTable[index] = value;
108 index = (index + 1) % keyTable.length;
110 keyTable[index] = key;
111 valueTable[index] = value;
113 // assumes the threshold is never equal to the size of the table
114 if (++elementSize > threshold)
119 * Rehashes the content of the table into a bigger table.
120 * This method is called automatically when the hashtable's
121 * size exceeds the threshold.
123 private void rehash() {
124 FieldNameAndTypeCache newHashtable = new FieldNameAndTypeCache(keyTable.length * 2);
125 for (int i = keyTable.length; --i >= 0;)
126 if (keyTable[i] != null)
127 newHashtable.put(keyTable[i], valueTable[i]);
129 this.keyTable = newHashtable.keyTable;
130 this.valueTable = newHashtable.valueTable;
131 this.threshold = newHashtable.threshold;
134 * Returns the number of elements contained in the hashtable.
136 * @return <CODE>int</CODE> The size of the table
142 * Converts to a rather lengthy String.
144 * @return String the ascii representation of the receiver
146 public String toString() {
148 StringBuffer buf = new StringBuffer();
149 buf.append("{"); //$NON-NLS-1$
150 for (int i = 0; i < max; ++i) {
151 if (keyTable[i] != null) {
152 buf.append(keyTable[i]).append("->").append(valueTable[i]); //$NON-NLS-1$
155 buf.append(", "); //$NON-NLS-1$
158 buf.append("}"); //$NON-NLS-1$
159 return buf.toString();