Makefile fixup
[org.ibex.tool.git] / repo / org.ibex.tool / src / org / eclipse / jdt / internal / compiler / codegen / DoubleCache.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 DoubleCache {
14         private double keyTable[];
15         private int valueTable[]; 
16         private int elementSize;
17 /**
18  * Constructs a new, empty hashtable. A default capacity and
19  * load factor is used. Note that the hashtable will automatically 
20  * grow when it gets full.
21  */
22 public DoubleCache() {
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 DoubleCache(int initialCapacity) {
32         elementSize = 0;
33         keyTable = new double[initialCapacity];
34         valueTable = new int[initialCapacity];
35 }
36 /**
37  * Clears the hash table so that it has no more elements in it.
38  */
39 public void clear() {
40         for (int i = keyTable.length; --i >= 0;) {
41                 keyTable[i] = 0.0;
42                 valueTable[i] = 0;
43         }
44         elementSize = 0;
45 }
46 /** Returns true if the collection contains an element for the key.
47  *
48  * @param key <CODE>double</CODE> the key that we are looking for
49  * @return boolean
50  */
51 public boolean containsKey(double key) {
52         if (key == 0.0) {
53                 for (int i = 0, max = elementSize; i < max; i++) {
54                         if (keyTable[i] == 0.0) {
55                                 long value1 = Double.doubleToLongBits(key);
56                                 long value2 = Double.doubleToLongBits(keyTable[i]);
57                                 if (value1 == -9223372036854775808L && value2 == -9223372036854775808L)
58                                         return true;
59                                 if (value1 == 0 && value2 == 0)
60                                         return true;
61                         }
62                 }
63         } else {
64                 for (int i = 0, max = elementSize; i < max; i++) {
65                         if (keyTable[i] == key) {
66                                 return true;
67                         }
68                 }
69         }
70         return false;
71 }
72 /** Gets the object associated with the specified key in the
73  * hashtable.
74  * @param key <CODE>double</CODE> the specified key
75  * @return int the element for the key or -1 if the key is not
76  *  defined in the hash table.
77  */
78 public int get(double key) {
79         if (key == 0.0) {
80                 for (int i = 0, max = elementSize; i < max; i++) {
81                         if (keyTable[i] == 0.0) {
82                                 long value1 = Double.doubleToLongBits(key);
83                                 long value2 = Double.doubleToLongBits(keyTable[i]);
84                                 if (value1 == -9223372036854775808L && value2 == -9223372036854775808L)
85                                         return valueTable[i];
86                                 if (value1 == 0 && value2 == 0)
87                                         return valueTable[i];
88                         }
89                 }
90         } else {
91                 for (int i = 0, max = elementSize; i < max; i++) {
92                         if (keyTable[i] == key) {
93                                 return valueTable[i];
94                         }
95                 }
96         }
97         return -1;
98 }
99 /**
100  * Puts the specified element into the hashtable, using the specified
101  * key.  The element may be retrieved by doing a get() with the same key.
102  * 
103  * @param key <CODE>double</CODE> the specified key in the hashtable
104  * @param value <CODE>int</CODE> the specified element
105  * @return int value
106  */
107 public int put(double key, int value) {
108         if (elementSize == keyTable.length) {
109                 // resize
110                 System.arraycopy(keyTable, 0, (keyTable = new double[elementSize * 2]), 0, elementSize);
111                 System.arraycopy(valueTable, 0, (valueTable = new int[elementSize * 2]), 0, elementSize);
112         }
113         keyTable[elementSize] = key;
114         valueTable[elementSize] = value;
115         elementSize++;
116         return value;
117 }
118 /**
119  * Converts to a rather lengthy String.
120  *
121  * @return String the ascii representation of the receiver
122  */
123 public String toString() {
124         int max = elementSize;
125         StringBuffer buf = new StringBuffer();
126         buf.append("{"); //$NON-NLS-1$
127         for (int i = 0; i < max; ++i) {
128                 if ((keyTable[i] != 0) || ((keyTable[i] == 0) &&(valueTable[i] != 0))) {
129                         buf.append(keyTable[i]).append("->").append(valueTable[i]); //$NON-NLS-1$
130                 }
131                 if (i < max) {
132                         buf.append(", "); //$NON-NLS-1$
133                 }
134         }
135         buf.append("}"); //$NON-NLS-1$
136         return buf.toString();
137 }
138 }