1 11 package org.eclipse.jdt.internal.compiler.codegen; 12 13 import org.eclipse.jdt.core.compiler.CharOperation; 14 15 public class CharArrayCache { 16 public char[] keyTable[]; 18 public int valueTable[]; 19 int elementSize; int threshold; 21 25 public CharArrayCache() { 26 this(9); 27 } 28 34 public CharArrayCache(int initialCapacity) { 35 this.elementSize = 0; 36 this.threshold = (initialCapacity * 2) / 3; this.keyTable = new char[initialCapacity][]; 38 this.valueTable = new int[initialCapacity]; 39 } 40 43 public void clear() { 44 for (int i = this.keyTable.length; --i >= 0;) { 45 this.keyTable[i] = null; 46 this.valueTable[i] = 0; 47 } 48 this.elementSize = 0; 49 } 50 55 public boolean containsKey(char[] key) { 56 int index = hashCodeChar(key), length = this.keyTable.length; 57 while (this.keyTable[index] != null) { 58 if (CharOperation.equals(this.keyTable[index], key)) 59 return true; 60 if (++index == length) { index = 0; 62 } 63 } 64 return false; 65 } 66 72 public int get(char[] key) { 73 int index = hashCodeChar(key), length = this.keyTable.length; 74 while (this.keyTable[index] != null) { 75 if (CharOperation.equals(this.keyTable[index], key)) 76 return this.valueTable[index]; 77 if (++index == length) { index = 0; 79 } 80 } 81 return -1; 82 } 83 private int hashCodeChar(char[] val) { 84 final int length = val.length; 85 int hash = 0; 86 final int n = 3; for (int i = 0; i < length; i += n) { 88 hash += val[i]; 89 } 90 return (hash & 0x7FFFFFFF) % this.keyTable.length; 91 } 92 101 public int putIfAbsent(char[] key, int value) { 102 int index = hashCodeChar(key), length = this.keyTable.length; 103 while (this.keyTable[index] != null) { 104 if (CharOperation.equals(this.keyTable[index], key)) 105 return this.valueTable[index]; 106 if (++index == length) { index = 0; 108 } 109 } 110 this.keyTable[index] = key; 111 this.valueTable[index] = value; 112 113 if (++this.elementSize > this.threshold) 115 rehash(); 116 return -value; } 118 119 128 private int put(char[] key, int value) { 129 int index = hashCodeChar(key), length = this.keyTable.length; 130 while (this.keyTable[index] != null) { 131 if (CharOperation.equals(this.keyTable[index], key)) 132 return this.valueTable[index] = value; 133 if (++index == length) { index = 0; 135 } 136 } 137 this.keyTable[index] = key; 138 this.valueTable[index] = value; 139 140 if (++this.elementSize > this.threshold) 142 rehash(); 143 return value; 144 } 145 150 private void rehash() { 151 CharArrayCache newHashtable = new CharArrayCache(this.keyTable.length * 2); 152 for (int i = this.keyTable.length; --i >= 0;) 153 if (this.keyTable[i] != null) 154 newHashtable.put(this.keyTable[i], this.valueTable[i]); 155 156 this.keyTable = newHashtable.keyTable; 157 this.valueTable = newHashtable.valueTable; 158 this.threshold = newHashtable.threshold; 159 } 160 164 public void remove(char[] key) { 165 int index = hashCodeChar(key), length = this.keyTable.length; 166 while (this.keyTable[index] != null) { 167 if (CharOperation.equals(this.keyTable[index], key)) { 168 this.valueTable[index] = 0; 169 this.keyTable[index] = null; 170 return; 171 } 172 if (++index == length) { index = 0; 174 } 175 } 176 } 177 183 public char[] returnKeyFor(int value) { 184 for (int i = this.keyTable.length; i-- > 0;) { 185 if (this.valueTable[i] == value) { 186 return this.keyTable[i]; 187 } 188 } 189 return null; 190 } 191 196 public int size() { 197 return this.elementSize; 198 } 199 204 public String toString() { 205 int max = size(); 206 StringBuffer buf = new StringBuffer (); 207 buf.append("{"); for (int i = 0; i < max; ++i) { 209 if (this.keyTable[i] != null) { 210 buf.append(this.keyTable[i]).append("->").append(this.valueTable[i]); } 212 if (i < max) { 213 buf.append(", "); } 215 } 216 buf.append("}"); return buf.toString(); 218 } 219 } 220 | Popular Tags |