KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > codegen > DoubleCache


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-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     this.elementSize = 0;
33     this.keyTable = new double[initialCapacity];
34     this.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 = this.keyTable.length; --i >= 0;) {
41         this.keyTable[i] = 0.0;
42         this.valueTable[i] = 0;
43     }
44     this.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 = this.elementSize; i < max; i++) {
54             if (this.keyTable[i] == 0.0) {
55                 long value1 = Double.doubleToLongBits(key);
56                 long value2 = Double.doubleToLongBits(this.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 = this.elementSize; i < max; i++) {
65             if (this.keyTable[i] == key) {
66                 return true;
67             }
68         }
69     }
70     return false;
71 }
72 /**
73  * Puts the specified element into the hashtable, using the specified
74  * key. The element may be retrieved by doing a get() with the same key.
75  *
76  * @param key <CODE>double</CODE> the specified key in the hashtable
77  * @param value <CODE>int</CODE> the specified element
78  * @return int value
79  */

80 public int put(double key, int value) {
81     if (this.elementSize == this.keyTable.length) {
82         // resize
83
System.arraycopy(this.keyTable, 0, (this.keyTable = new double[this.elementSize * 2]), 0, this.elementSize);
84         System.arraycopy(this.valueTable, 0, (this.valueTable = new int[this.elementSize * 2]), 0, this.elementSize);
85     }
86     this.keyTable[this.elementSize] = key;
87     this.valueTable[this.elementSize] = value;
88     this.elementSize++;
89     return value;
90 }
91 /**
92  * Puts the specified element into the hashtable, using the specified
93  * key. The element may be retrieved by doing a get() with the same key.
94  *
95  * @param key <CODE>double</CODE> the specified key in the hashtable
96  * @param value <CODE>int</CODE> the specified element
97  * @return int value
98  */

99 public int putIfAbsent(double key, int value) {
100     if (key == 0.0) {
101         for (int i = 0, max = this.elementSize; i < max; i++) {
102             if (this.keyTable[i] == 0.0) {
103                 long value1 = Double.doubleToLongBits(key);
104                 long value2 = Double.doubleToLongBits(this.keyTable[i]);
105                 if (value1 == -9223372036854775808L && value2 == -9223372036854775808L)
106                     return this.valueTable[i];
107                 if (value1 == 0 && value2 == 0)
108                     return this.valueTable[i];
109             }
110         }
111     } else {
112         for (int i = 0, max = this.elementSize; i < max; i++) {
113             if (this.keyTable[i] == key) {
114                 return this.valueTable[i];
115             }
116         }
117     }
118     if (this.elementSize == this.keyTable.length) {
119         // resize
120
System.arraycopy(this.keyTable, 0, (this.keyTable = new double[this.elementSize * 2]), 0, this.elementSize);
121         System.arraycopy(this.valueTable, 0, (this.valueTable = new int[this.elementSize * 2]), 0, this.elementSize);
122     }
123     this.keyTable[this.elementSize] = key;
124     this.valueTable[this.elementSize] = value;
125     this.elementSize++;
126     return -value; // negative when added, assumes value is > 0
127
}
128 /**
129  * Converts to a rather lengthy String.
130  *
131  * @return String the ascii representation of the receiver
132  */

133 public String JavaDoc toString() {
134     int max = this.elementSize;
135     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
136     buf.append("{"); //$NON-NLS-1$
137
for (int i = 0; i < max; ++i) {
138         if ((this.keyTable[i] != 0) || ((this.keyTable[i] == 0) &&(this.valueTable[i] != 0))) {
139             buf.append(this.keyTable[i]).append("->").append(this.valueTable[i]); //$NON-NLS-1$
140
}
141         if (i < max) {
142             buf.append(", "); //$NON-NLS-1$
143
}
144     }
145     buf.append("}"); //$NON-NLS-1$
146
return buf.toString();
147 }
148 }
149
Popular Tags