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[];
15 public int valueTable[];
19 * Constructs a new, empty hashtable. A default capacity is used.
20 * Note that the hashtable will automatically grow when it gets full.
22 public ObjectCache() {
26 * Constructs a new, empty hashtable with the specified initial
28 * @param initialCapacity int
29 * the initial number of buckets
31 public ObjectCache(int initialCapacity) {
33 this.threshold = (int) (initialCapacity * 0.66f);
34 this.keyTable = new Object[initialCapacity];
35 this.valueTable = new int[initialCapacity];
38 * Clears the hash table so that it has no more elements in it.
41 for (int i = keyTable.length; --i >= 0;) {
47 /** Returns true if the collection contains an element for the key.
49 * @param char[] key the key that we are looking for
52 public boolean containsKey(Object key) {
53 int index = hashCode(key);
54 while (keyTable[index] != null) {
55 if (keyTable[index] == key)
57 index = (index + 1) % keyTable.length;
61 /** Gets the object associated with the specified key in the
63 * @param key <CODE>char[]</CODE> the specified key
64 * @return int the element for the key or -1 if the key is not
65 * defined in the hash table.
67 public int get(Object key) {
68 int index = hashCode(key);
69 while (keyTable[index] != null) {
70 if (keyTable[index] == key)
71 return valueTable[index];
72 index = (index + 1) % keyTable.length;
77 * Return the hashcode for the key parameter
79 * @param key net.sourceforge.phpdt.internal.compiler.lookup.MethodBinding
82 public int hashCode(Object key) {
83 return (key.hashCode() & 0x7FFFFFFF) % keyTable.length;
86 * Puts the specified element into the hashtable, using the specified
87 * key. The element may be retrieved by doing a get() with the same key.
88 * The key and the element cannot be null.
90 * @param key <CODE>Object</CODE> the specified key in the hashtable
91 * @param value <CODE>int</CODE> the specified element
92 * @return int the old value of the key, or -1 if it did not have one.
94 public int put(Object key, int value) {
95 int index = hashCode(key);
96 while (keyTable[index] != null) {
97 if (keyTable[index] == key)
98 return valueTable[index] = value;
99 index = (index + 1) % keyTable.length;
101 keyTable[index] = key;
102 valueTable[index] = value;
104 // assumes the threshold is never equal to the size of the table
105 if (++elementSize > threshold)
110 * Rehashes the content of the table into a bigger table.
111 * This method is called automatically when the hashtable's
112 * size exceeds the threshold.
114 private void rehash() {
115 ObjectCache newHashtable = new ObjectCache(keyTable.length * 2);
116 for (int i = keyTable.length; --i >= 0;)
117 if (keyTable[i] != null)
118 newHashtable.put(keyTable[i], valueTable[i]);
120 this.keyTable = newHashtable.keyTable;
121 this.valueTable = newHashtable.valueTable;
122 this.threshold = newHashtable.threshold;
125 * Returns the number of elements contained in the hashtable.
127 * @return <CODE>int</CODE> The size of the table
133 * Converts to a rather lengthy String.
135 * @return String the ascii representation of the receiver
137 public String toString() {
139 StringBuffer buf = new StringBuffer();
140 buf.append("{"); //$NON-NLS-1$
141 for (int i = 0; i < max; ++i) {
142 if (keyTable[i] != null) {
143 buf.append(keyTable[i]).append("->").append(valueTable[i]); //$NON-NLS-1$
146 buf.append(", "); //$NON-NLS-1$
149 buf.append("}"); //$NON-NLS-1$
150 return buf.toString();