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.MethodBinding;
14 import net.sourceforge.phpdt.internal.compiler.util.CharOperation;
16 public class MethodNameAndTypeCache {
17 public MethodBinding 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 MethodNameAndTypeCache() {
29 * Constructs a new, empty hashtable with the specified initial
31 * @param initialCapacity int
32 * the initial number of buckets
34 public MethodNameAndTypeCache(int initialCapacity) {
36 this.threshold = (int) (initialCapacity * 0.66f);
37 this.keyTable = new MethodBinding[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(MethodBinding key) {
56 int index = hashCode(key);
57 while (keyTable[index] != null) {
58 if (equalsForNameAndType(keyTable[index], key))
60 index = (index + 1) % keyTable.length;
65 * Returns true if the two methodBinding are consider to be equal for the name and type
68 public boolean equalsForNameAndType(MethodBinding method1, MethodBinding method2) {
69 return CharOperation.equals(method1.selector, method2.selector) && CharOperation.equals(method1.signature(), method2.signature());
71 /** Gets the object associated with the specified key in the
73 * @param key <CODE>char[]</CODE> the specified key
74 * @return int the element for the key or -1 if the key is not
75 * defined in the hash table.
77 public int get(MethodBinding key) {
78 int index = hashCode(key);
79 while (keyTable[index] != null) {
80 if (equalsForNameAndType(keyTable[index], key))
81 return valueTable[index];
82 index = (index + 1) % keyTable.length;
87 * Return the hashcode for the key parameter
89 * @param key org.eclipse.jdt.internal.compiler.lookup.MethodBinding
92 public int hashCode(MethodBinding key) {
93 return CharOperation.hashCode(key.selector) % keyTable.length;
96 * Puts the specified element into the hashtable, using the specified
97 * key. The element may be retrieved by doing a get() with the same key.
98 * The key and the element cannot be null.
100 * @param key <CODE>Object</CODE> the specified key in the hashtable
101 * @param value <CODE>int</CODE> the specified element
102 * @return int the old value of the key, or -1 if it did not have one.
104 public int put(MethodBinding key, int value) {
105 int index = hashCode(key);
106 while (keyTable[index] != null) {
107 if (equalsForNameAndType(keyTable[index], key))
108 return valueTable[index] = value;
109 index = (index + 1) % keyTable.length;
111 keyTable[index] = key;
112 valueTable[index] = value;
114 // assumes the threshold is never equal to the size of the table
115 if (++elementSize > threshold)
120 * Rehashes the content of the table into a bigger table.
121 * This method is called automatically when the hashtable's
122 * size exceeds the threshold.
124 private void rehash() {
125 MethodNameAndTypeCache newHashtable = new MethodNameAndTypeCache(keyTable.length * 2);
126 for (int i = keyTable.length; --i >= 0;)
127 if (keyTable[i] != null)
128 newHashtable.put(keyTable[i], valueTable[i]);
130 this.keyTable = newHashtable.keyTable;
131 this.valueTable = newHashtable.valueTable;
132 this.threshold = newHashtable.threshold;
135 * Returns the number of elements contained in the hashtable.
137 * @return <CODE>int</CODE> The size of the table
143 * Converts to a rather lengthy String.
145 * @return String the ascii representation of the receiver
147 public String toString() {
149 StringBuffer buf = new StringBuffer();
150 buf.append("{"); //$NON-NLS-1$
151 for (int i = 0; i < max; ++i) {
152 if (keyTable[i] != null) {
153 buf.append(keyTable[i]).append("->").append(valueTable[i]); //$NON-NLS-1$
156 buf.append(", "); //$NON-NLS-1$
159 buf.append("}"); //$NON-NLS-1$
160 return buf.toString();