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 public class LongCache {
14 public long keyTable[];
15 public int valueTable[];
19 * Constructs a new, empty hashtable. A default capacity and
20 * load factor is used. Note that the hashtable will automatically
21 * grow when it gets full.
27 * Constructs a new, empty hashtable with the specified initial
29 * @param initialCapacity int
30 * the initial number of buckets
32 public LongCache(int initialCapacity) {
34 threshold = (int) (initialCapacity * 0.66);
35 keyTable = new long[initialCapacity];
36 valueTable = new int[initialCapacity];
39 * Clears the hash table so that it has no more elements in it.
42 for (int i = keyTable.length; --i >= 0;) {
48 /** Returns true if the collection contains an element for the key.
50 * @param key <CODE>long</CODE> the key that we are looking for
53 public boolean containsKey(long key) {
54 int index = hash(key);
55 while ((keyTable[index] != 0) || ((keyTable[index] == 0) &&(valueTable[index] != 0))) {
56 if (keyTable[index] == key)
58 index = (index + 1) % keyTable.length;
62 /** Gets the object associated with the specified key in the
64 * @param key <CODE>long</CODE> the specified key
65 * @return int the element for the key or -1 if the key is not
66 * defined in the hash table.
68 public int get(long key) {
69 int index = hash(key);
70 while ((keyTable[index] != 0) || ((keyTable[index] == 0) &&(valueTable[index] != 0))) {
71 if (keyTable[index] == key)
72 return valueTable[index];
73 index = (index + 1) % keyTable.length;
78 * Return a hashcode for the value of the key parameter.
80 * @return int the hash code corresponding to the key value
82 public int hash(long key) {
83 return ((int) key & 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.
89 * @param key <CODE>long</CODE> the specified key in the hashtable
90 * @param value <CODE>int</CODE> the specified element
93 public int put(long key, int value) {
94 int index = hash(key);
95 while ((keyTable[index] != 0) || ((keyTable[index] == 0) && (valueTable[index] != 0))) {
96 if (keyTable[index] == key)
97 return valueTable[index] = value;
98 index = (index + 1) % keyTable.length;
100 keyTable[index] = key;
101 valueTable[index] = value;
103 // assumes the threshold is never equal to the size of the table
104 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 LongCache newHashtable = new LongCache(keyTable.length * 2);
116 for (int i = keyTable.length; --i >= 0;) {
117 long key = keyTable[i];
118 int value = valueTable[i];
119 if ((key != 0) || ((key == 0) && (value != 0))) {
120 newHashtable.put(key, value);
123 this.keyTable = newHashtable.keyTable;
124 this.valueTable = newHashtable.valueTable;
125 this.threshold = newHashtable.threshold;
128 * Returns the number of elements contained in the hashtable.
130 * @return <CODE>int</CODE> The size of the table
136 * Converts to a rather lengthy String.
138 * @return String the ascii representation of the receiver
140 public String toString() {
142 StringBuffer buf = new StringBuffer();
143 buf.append("{"); //$NON-NLS-1$
144 for (int i = 0; i < max; ++i) {
145 if ((keyTable[i] != 0) || ((keyTable[i] == 0) && (valueTable[i] != 0))) {
146 buf.append(keyTable[i]).append("->").append(valueTable[i]); //$NON-NLS-1$
149 buf.append(", "); //$NON-NLS-1$
152 buf.append("}"); //$NON-NLS-1$
153 return buf.toString();