1 11 package org.eclipse.jdt.internal.compiler.codegen; 12 13 public class FloatCache { 14 private float keyTable[]; 15 private int valueTable[]; 16 private int elementSize; 17 22 public FloatCache() { 23 this(13); 24 } 25 31 public FloatCache(int initialCapacity) { 32 this.elementSize = 0; 33 this.keyTable = new float[initialCapacity]; 34 this.valueTable = new int[initialCapacity]; 35 } 36 39 public void clear() { 40 for (int i = this.keyTable.length; --i >= 0;) { 41 this.keyTable[i] = 0.0f; 42 this.valueTable[i] = 0; 43 } 44 this.elementSize = 0; 45 } 46 51 public boolean containsKey(float key) { 52 if (key == 0.0f) { 53 for (int i = 0, max = this.elementSize; i < max; i++) { 54 if (this.keyTable[i] == 0.0f) { 55 int value1 = Float.floatToIntBits(key); 56 int value2 = Float.floatToIntBits(this.keyTable[i]); 57 if (value1 == -2147483648 && value2 == -2147483648) 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 80 public int put(float key, int value) { 81 if (this.elementSize == this.keyTable.length) { 82 System.arraycopy(this.keyTable, 0, (this.keyTable = new float[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 99 public int putIfAbsent(float key, int value) { 100 if (key == 0.0f) { 101 for (int i = 0, max = this.elementSize; i < max; i++) { 102 if (this.keyTable[i] == 0.0f) { 103 int value1 = Float.floatToIntBits(key); 104 int value2 = Float.floatToIntBits(this.keyTable[i]); 105 if (value1 == -2147483648 && value2 == -2147483648) 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 System.arraycopy(this.keyTable, 0, (this.keyTable = new float[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; } 128 133 public String toString() { 134 int max = this.elementSize; 135 StringBuffer buf = new StringBuffer (); 136 buf.append("{"); 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]); } 141 if (i < max) { 142 buf.append(", "); } 144 } 145 buf.append("}"); return buf.toString(); 147 } 148 } 149 | Popular Tags |