Makefile fixup
[org.ibex.tool.git] / repo / org.ibex.tool / src / org / eclipse / jdt / internal / compiler / codegen / LongCache.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 LongCache {
14         public long keyTable[];
15         public int valueTable[]; 
16         int elementSize;
17         int threshold;
18 /**
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.
22  */
23 public LongCache() {
24         this(13);
25 }
26 /**
27  * Constructs a new, empty hashtable with the specified initial
28  * capacity.
29  * @param initialCapacity int
30  *  the initial number of buckets
31  */
32 public LongCache(int initialCapacity) {
33         elementSize = 0;
34         threshold = (int) (initialCapacity * 0.66);
35         keyTable = new long[initialCapacity];
36         valueTable = new int[initialCapacity];
37 }
38 /**
39  * Clears the hash table so that it has no more elements in it.
40  */
41 public void clear() {
42         for (int i = keyTable.length; --i >= 0;) {
43                 keyTable[i] = 0;
44                 valueTable[i] = 0;
45         }
46         elementSize = 0;
47 }
48 /** Returns true if the collection contains an element for the key.
49  *
50  * @param key <CODE>long</CODE> the key that we are looking for
51  * @return boolean
52  */
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)
57                         return true;
58                 index = (index + 1) % keyTable.length;
59         }
60         return false;
61 }
62 /** Gets the object associated with the specified key in the
63  * hashtable.
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.
67  */
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;
74         }
75         return -1;
76 }
77 /**
78  * Return a hashcode for the value of the key parameter.
79  * @param key long
80  * @return int the hash code corresponding to the key value
81  */
82 public int hash(long key) {
83         return ((int) key & 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  * 
89  * @param key <CODE>long</CODE> the specified key in the hashtable
90  * @param value <CODE>int</CODE> the specified element
91  * @return int value
92  */
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;
99         }
100         keyTable[index] = key;
101         valueTable[index] = value;
102
103         // assumes the threshold is never equal to the size of the table
104         if (++elementSize > threshold) {
105                 rehash();
106         }
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         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);
121                 }
122         }
123         this.keyTable = newHashtable.keyTable;
124         this.valueTable = newHashtable.valueTable;
125         this.threshold = newHashtable.threshold;
126 }
127 /**
128  * Returns the number of elements contained in the hashtable.
129  *
130  * @return <CODE>int</CODE> The size of the table
131  */
132 public int size() {
133         return elementSize;
134 }
135 /**
136  * Converts to a rather lengthy String.
137  *
138  * @return String the ascii representation of the receiver
139  */
140 public String toString() {
141         int max = size();
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$
147                 }
148                 if (i < max) {
149                         buf.append(", "); //$NON-NLS-1$
150                 }
151         }
152         buf.append("}"); //$NON-NLS-1$
153         return buf.toString();
154 }
155 }