Makefile fixup
[org.ibex.tool.git] / src / org / eclipse / jdt / internal / compiler / codegen / ObjectCache.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 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 org.eclipse.jdt.internal.compiler.codegen;
12
13 public class ObjectCache {
14         public Object keyTable[];
15         public int valueTable[];
16         int elementSize;
17         int threshold;
18 /**
19  * Constructs a new, empty hashtable. A default capacity is used.
20  * Note that the hashtable will automatically grow when it gets full.
21  */
22 public ObjectCache() {
23         this(13);
24 }
25 /**
26  * Constructs a new, empty hashtable with the specified initial
27  * capacity.
28  * @param initialCapacity int
29  *  the initial number of buckets
30  */
31 public ObjectCache(int initialCapacity) {
32         this.elementSize = 0;
33         this.threshold = (int) (initialCapacity * 0.66f);
34         this.keyTable = new Object[initialCapacity];
35         this.valueTable = new int[initialCapacity];
36 }
37 /**
38  * Clears the hash table so that it has no more elements in it.
39  */
40 public void clear() {
41         for (int i = keyTable.length; --i >= 0;) {
42                 keyTable[i] = null;
43                 valueTable[i] = 0;
44         }
45         elementSize = 0;
46 }
47 /** Returns true if the collection contains an element for the key.
48  *
49  * @param key char[] the key that we are looking for
50  * @return boolean
51  */
52 public boolean containsKey(Object key) {
53         int index = hashCode(key);
54         while (keyTable[index] != null) {
55                 if (keyTable[index] == key)
56                         return true;
57                 index = (index + 1) % keyTable.length;
58         }
59         return false;
60 }
61 /** Gets the object associated with the specified key in the
62  * hashtable.
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.
66  */
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;
73         }
74         return -1;
75 }
76 /**
77  * Return the hashcode for the key parameter
78  *
79  * @param key org.eclipse.jdt.internal.compiler.lookup.MethodBinding
80  * @return int
81  */
82 public int hashCode(Object key) {
83         return (key.hashCode() & 0x7FFFFFFF) % keyTable.length;
84 }
85 /**
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. 
89  * 
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.
93  */
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;
100         }
101         keyTable[index] = key;
102         valueTable[index] = value;
103
104         // assumes the threshold is never equal to the size of the table
105         if (++elementSize > threshold)
106                 rehash();
107         return value;
108 }
109 /**
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.
113  */
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]);
119
120         this.keyTable = newHashtable.keyTable;
121         this.valueTable = newHashtable.valueTable;
122         this.threshold = newHashtable.threshold;
123 }
124 /**
125  * Returns the number of elements contained in the hashtable.
126  *
127  * @return <CODE>int</CODE> The size of the table
128  */
129 public int size() {
130         return elementSize;
131 }
132 /**
133  * Converts to a rather lengthy String.
134  *
135  * @return String the ascii representation of the receiver
136  */
137 public String toString() {
138         int max = size();
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$
144                 }
145                 if (i < max) {
146                         buf.append(", "); //$NON-NLS-1$
147                 }
148         }
149         buf.append("}"); //$NON-NLS-1$
150         return buf.toString();
151 }
152 }