001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.compiler.codegen;
011:
012: public class FloatCache {
013: private float keyTable[];
014: private int valueTable[];
015: private int elementSize;
016:
017: /**
018: * Constructs a new, empty hashtable. A default capacity and
019: * load factor is used. Note that the hashtable will automatically
020: * grow when it gets full.
021: */
022: public FloatCache() {
023: this (13);
024: }
025:
026: /**
027: * Constructs a new, empty hashtable with the specified initial
028: * capacity.
029: * @param initialCapacity int
030: * the initial number of buckets
031: */
032: public FloatCache(int initialCapacity) {
033: this .elementSize = 0;
034: this .keyTable = new float[initialCapacity];
035: this .valueTable = new int[initialCapacity];
036: }
037:
038: /**
039: * Clears the hash table so that it has no more elements in it.
040: */
041: public void clear() {
042: for (int i = this .keyTable.length; --i >= 0;) {
043: this .keyTable[i] = 0.0f;
044: this .valueTable[i] = 0;
045: }
046: this .elementSize = 0;
047: }
048:
049: /** Returns true if the collection contains an element for the key.
050: *
051: * @param key <CODE>float</CODE> the key that we are looking for
052: * @return boolean
053: */
054: public boolean containsKey(float key) {
055: if (key == 0.0f) {
056: for (int i = 0, max = this .elementSize; i < max; i++) {
057: if (this .keyTable[i] == 0.0f) {
058: int value1 = Float.floatToIntBits(key);
059: int value2 = Float.floatToIntBits(this .keyTable[i]);
060: if (value1 == -2147483648 && value2 == -2147483648)
061: return true;
062: if (value1 == 0 && value2 == 0)
063: return true;
064: }
065: }
066: } else {
067: for (int i = 0, max = this .elementSize; i < max; i++) {
068: if (this .keyTable[i] == key) {
069: return true;
070: }
071: }
072: }
073: return false;
074: }
075:
076: /**
077: * Puts the specified element into the hashtable, using the specified
078: * key. The element may be retrieved by doing a get() with the same key.
079: *
080: * @param key <CODE>float</CODE> the specified key in the hashtable
081: * @param value <CODE>int</CODE> the specified element
082: * @return int value
083: */
084: public int put(float key, int value) {
085: if (this .elementSize == this .keyTable.length) {
086: // resize
087: System.arraycopy(this .keyTable, 0,
088: (this .keyTable = new float[this .elementSize * 2]),
089: 0, this .elementSize);
090: System.arraycopy(this .valueTable, 0,
091: (this .valueTable = new int[this .elementSize * 2]),
092: 0, this .elementSize);
093: }
094: this .keyTable[this .elementSize] = key;
095: this .valueTable[this .elementSize] = value;
096: this .elementSize++;
097: return value;
098: }
099:
100: /**
101: * Puts the specified element into the hashtable, using the specified
102: * key. The element may be retrieved by doing a get() with the same key.
103: *
104: * @param key <CODE>float</CODE> the specified key in the hashtable
105: * @param value <CODE>int</CODE> the specified element
106: * @return int value
107: */
108: public int putIfAbsent(float key, int value) {
109: if (key == 0.0f) {
110: for (int i = 0, max = this .elementSize; i < max; i++) {
111: if (this .keyTable[i] == 0.0f) {
112: int value1 = Float.floatToIntBits(key);
113: int value2 = Float.floatToIntBits(this .keyTable[i]);
114: if (value1 == -2147483648 && value2 == -2147483648)
115: return this .valueTable[i];
116: if (value1 == 0 && value2 == 0)
117: return this .valueTable[i];
118: }
119: }
120: } else {
121: for (int i = 0, max = this .elementSize; i < max; i++) {
122: if (this .keyTable[i] == key) {
123: return this .valueTable[i];
124: }
125: }
126: }
127: if (this .elementSize == this .keyTable.length) {
128: // resize
129: System.arraycopy(this .keyTable, 0,
130: (this .keyTable = new float[this .elementSize * 2]),
131: 0, this .elementSize);
132: System.arraycopy(this .valueTable, 0,
133: (this .valueTable = new int[this .elementSize * 2]),
134: 0, this .elementSize);
135: }
136: this .keyTable[this .elementSize] = key;
137: this .valueTable[this .elementSize] = value;
138: this .elementSize++;
139: return -value; // negative when added, assumes value is > 0
140: }
141:
142: /**
143: * Converts to a rather lengthy String.
144: *
145: * @return String the ascii representation of the receiver
146: */
147: public String toString() {
148: int max = this .elementSize;
149: StringBuffer buf = new StringBuffer();
150: buf.append("{"); //$NON-NLS-1$
151: for (int i = 0; i < max; ++i) {
152: if ((this .keyTable[i] != 0)
153: || ((this .keyTable[i] == 0) && (this .valueTable[i] != 0))) {
154: buf.append(this .keyTable[i])
155: .append("->").append(this .valueTable[i]); //$NON-NLS-1$
156: }
157: if (i < max) {
158: buf.append(", "); //$NON-NLS-1$
159: }
160: }
161: buf.append("}"); //$NON-NLS-1$
162: return buf.toString();
163: }
164: }
|