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 DoubleCache {
 
  14         private double keyTable[];
 
  15         private int valueTable[]; 
 
  16         private int elementSize;
 
  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.
 
  22 public DoubleCache() {
 
  26  * Constructs a new, empty hashtable with the specified initial
 
  28  * @param initialCapacity int
 
  29  *  the initial number of buckets
 
  31 public DoubleCache(int initialCapacity) {
 
  33         keyTable = new double[initialCapacity];
 
  34         valueTable = new int[initialCapacity];
 
  37  * Clears the hash table so that it has no more elements in it.
 
  40         for (int i = keyTable.length; --i >= 0;) {
 
  46 /** Returns true if the collection contains an element for the key.
 
  48  * @param key <CODE>double</CODE> the key that we are looking for
 
  51 public boolean containsKey(double key) {
 
  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)
 
  59                                 if (value1 == 0 && value2 == 0)
 
  64                 for (int i = 0, max = elementSize; i < max; i++) {
 
  65                         if (keyTable[i] == key) {
 
  72 /** Gets the object associated with the specified key in the
 
  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.
 
  78 public int get(double key) {
 
  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)
 
  86                                 if (value1 == 0 && value2 == 0)
 
  91                 for (int i = 0, max = elementSize; i < max; i++) {
 
  92                         if (keyTable[i] == key) {
 
 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.
 
 103  * @param key <CODE>double</CODE> the specified key in the hashtable
 
 104  * @param value <CODE>int</CODE> the specified element
 
 107 public int put(double key, int value) {
 
 108         if (elementSize == keyTable.length) {
 
 110                 System.arraycopy(keyTable, 0, (keyTable = new double[elementSize * 2]), 0, elementSize);
 
 111                 System.arraycopy(valueTable, 0, (valueTable = new int[elementSize * 2]), 0, elementSize);
 
 113         keyTable[elementSize] = key;
 
 114         valueTable[elementSize] = value;
 
 119  * Converts to a rather lengthy String.
 
 121  * @return String the ascii representation of the receiver
 
 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$
 
 132                         buf.append(", "); //$NON-NLS-1$
 
 135         buf.append("}"); //$NON-NLS-1$
 
 136         return buf.toString();