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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.codegen;
13 public class ObjectCache {
14 public Object keyTable[];
16 public int valueTable[];
23 * Constructs a new, empty hashtable. A default capacity is used. Note that
24 * the hashtable will automatically grow when it gets full.
26 public ObjectCache() {
31 * Constructs a new, empty hashtable with the specified initial capacity.
33 * @param initialCapacity
34 * int the initial number of buckets
36 public ObjectCache(int initialCapacity) {
38 this.threshold = (int) (initialCapacity * 0.66f);
39 this.keyTable = new Object[initialCapacity];
40 this.valueTable = new int[initialCapacity];
44 * Clears the hash table so that it has no more elements in it.
47 for (int i = keyTable.length; --i >= 0;) {
55 * Returns true if the collection contains an element for the key.
58 * key the key that we are looking for
61 public boolean containsKey(Object key) {
62 int index = hashCode(key);
63 while (keyTable[index] != null) {
64 if (keyTable[index] == key)
66 index = (index + 1) % keyTable.length;
72 * Gets the object associated with the specified key in the hashtable.
75 * <CODE>char[]</CODE> the specified key
76 * @return int the element for the key or -1 if the key is not defined in
79 public int get(Object key) {
80 int index = hashCode(key);
81 while (keyTable[index] != null) {
82 if (keyTable[index] == key)
83 return valueTable[index];
84 index = (index + 1) % keyTable.length;
90 * Return the hashcode for the key parameter
93 * net.sourceforge.phpdt.internal.compiler.lookup.MethodBinding
96 public int hashCode(Object key) {
97 return (key.hashCode() & 0x7FFFFFFF) % keyTable.length;
101 * Puts the specified element into the hashtable, using the specified key.
102 * The element may be retrieved by doing a get() with the same key. The key
103 * and the element cannot be null.
106 * <CODE>Object</CODE> the specified key in the hashtable
108 * <CODE>int</CODE> the specified element
109 * @return int the old value of the key, or -1 if it did not have one.
111 public int put(Object key, int value) {
112 int index = hashCode(key);
113 while (keyTable[index] != null) {
114 if (keyTable[index] == key)
115 return valueTable[index] = value;
116 index = (index + 1) % keyTable.length;
118 keyTable[index] = key;
119 valueTable[index] = value;
121 // assumes the threshold is never equal to the size of the table
122 if (++elementSize > threshold)
128 * Rehashes the content of the table into a bigger table. This method is
129 * called automatically when the hashtable's size exceeds the threshold.
131 private void rehash() {
132 ObjectCache newHashtable = new ObjectCache(keyTable.length * 2);
133 for (int i = keyTable.length; --i >= 0;)
134 if (keyTable[i] != null)
135 newHashtable.put(keyTable[i], valueTable[i]);
137 this.keyTable = newHashtable.keyTable;
138 this.valueTable = newHashtable.valueTable;
139 this.threshold = newHashtable.threshold;
143 * Returns the number of elements contained in the hashtable.
145 * @return <CODE>int</CODE> The size of the table
152 * Converts to a rather lengthy String.
154 * @return String the ascii representation of the receiver
156 public String toString() {
158 StringBuffer buf = new StringBuffer();
159 buf.append("{"); //$NON-NLS-1$
160 for (int i = 0; i < max; ++i) {
161 if (keyTable[i] != null) {
162 buf.append(keyTable[i]).append("->").append(valueTable[i]); //$NON-NLS-1$
165 buf.append(", "); //$NON-NLS-1$
168 buf.append("}"); //$NON-NLS-1$
169 return buf.toString();