1 11 package org.eclipse.jdt.internal.compiler.codegen; 12 13 public class LongCache { 14 public long keyTable[]; 15 public int valueTable[]; 16 int elementSize; 17 int threshold; 18 23 public LongCache() { 24 this(13); 25 } 26 32 public LongCache(int initialCapacity) { 33 this.elementSize = 0; 34 this.threshold = (int) (initialCapacity * 0.66); 35 this.keyTable = new long[initialCapacity]; 36 this.valueTable = new int[initialCapacity]; 37 } 38 41 public void clear() { 42 for (int i = this.keyTable.length; --i >= 0;) { 43 this.keyTable[i] = 0; 44 this.valueTable[i] = 0; 45 } 46 this.elementSize = 0; 47 } 48 53 public boolean containsKey(long key) { 54 int index = hash(key), length = this.keyTable.length; 55 while ((this.keyTable[index] != 0) || ((this.keyTable[index] == 0) &&(this.valueTable[index] != 0))) { 56 if (this.keyTable[index] == key) 57 return true; 58 if (++index == length) { 59 index = 0; 60 } 61 } 62 return false; 63 } 64 69 public int hash(long key) { 70 return ((int) key & 0x7FFFFFFF) % this.keyTable.length; 71 } 72 80 public int put(long key, int value) { 81 int index = hash(key), length = this.keyTable.length; 82 while ((this.keyTable[index] != 0) || ((this.keyTable[index] == 0) && (this.valueTable[index] != 0))) { 83 if (this.keyTable[index] == key) 84 return this.valueTable[index] = value; 85 if (++index == length) { 86 index = 0; 87 } 88 } 89 this.keyTable[index] = key; 90 this.valueTable[index] = value; 91 92 if (++this.elementSize > this.threshold) { 94 rehash(); 95 } 96 return value; 97 } 98 106 public int putIfAbsent(long key, int value) { 107 int index = hash(key), length = this.keyTable.length; 108 while ((this.keyTable[index] != 0) || ((this.keyTable[index] == 0) && (this.valueTable[index] != 0))) { 109 if (this.keyTable[index] == key) 110 return this.valueTable[index]; 111 if (++index == length) { 112 index = 0; 113 } 114 } 115 this.keyTable[index] = key; 116 this.valueTable[index] = value; 117 118 if (++this.elementSize > this.threshold) { 120 rehash(); 121 } 122 return -value; } 124 129 private void rehash() { 130 LongCache newHashtable = new LongCache(this.keyTable.length * 2); 131 for (int i = this.keyTable.length; --i >= 0;) { 132 long key = this.keyTable[i]; 133 int value = this.valueTable[i]; 134 if ((key != 0) || ((key == 0) && (value != 0))) { 135 newHashtable.put(key, value); 136 } 137 } 138 this.keyTable = newHashtable.keyTable; 139 this.valueTable = newHashtable.valueTable; 140 this.threshold = newHashtable.threshold; 141 } 142 147 public int size() { 148 return this.elementSize; 149 } 150 155 public String toString() { 156 int max = size(); 157 StringBuffer buf = new StringBuffer (); 158 buf.append("{"); for (int i = 0; i < max; ++i) { 160 if ((this.keyTable[i] != 0) || ((this.keyTable[i] == 0) && (this.valueTable[i] != 0))) { 161 buf.append(this.keyTable[i]).append("->").append(this.valueTable[i]); } 163 if (i < max) { 164 buf.append(", "); } 166 } 167 buf.append("}"); return buf.toString(); 169 } 170 } 171 | Popular Tags |