1 11 package org.eclipse.jdt.internal.compiler.util; 12 13 import org.eclipse.jdt.core.compiler.CharOperation; 14 import org.eclipse.jdt.internal.compiler.lookup.PackageBinding; 15 16 public final class HashtableOfPackage { 17 public char[] keyTable[]; 19 public PackageBinding valueTable[]; 20 21 public int elementSize; int threshold; 23 public HashtableOfPackage() { 24 this(3); } 26 public HashtableOfPackage(int size) { 27 this.elementSize = 0; 28 this.threshold = size; int extraRoom = (int) (size * 1.75f); 30 if (this.threshold == extraRoom) 31 extraRoom++; 32 this.keyTable = new char[extraRoom][]; 33 this.valueTable = new PackageBinding[extraRoom]; 34 } 35 public boolean containsKey(char[] key) { 36 int length = keyTable.length, 37 index = CharOperation.hashCode(key) % length; 38 int keyLength = key.length; 39 char[] currentKey; 40 while ((currentKey = keyTable[index]) != null) { 41 if (currentKey.length == keyLength && CharOperation.equals(currentKey, key)) 42 return true; 43 if (++index == length) { 44 index = 0; 45 } 46 } 47 return false; 48 } 49 public PackageBinding get(char[] key) { 50 int length = keyTable.length, 51 index = CharOperation.hashCode(key) % length; 52 int keyLength = key.length; 53 char[] currentKey; 54 while ((currentKey = keyTable[index]) != null) { 55 if (currentKey.length == keyLength && CharOperation.equals(currentKey, key)) 56 return valueTable[index]; 57 if (++index == length) { 58 index = 0; 59 } 60 } 61 return null; 62 } 63 public PackageBinding put(char[] key, PackageBinding value) { 64 int length = keyTable.length, 65 index = CharOperation.hashCode(key) % length; 66 int keyLength = key.length; 67 char[] currentKey; 68 while ((currentKey = keyTable[index]) != null) { 69 if (currentKey.length == keyLength && CharOperation.equals(currentKey, key)) 70 return valueTable[index] = value; 71 if (++index == length) { 72 index = 0; 73 } 74 } 75 keyTable[index] = key; 76 valueTable[index] = value; 77 78 if (++elementSize > threshold) 80 rehash(); 81 return value; 82 } 83 private void rehash() { 84 HashtableOfPackage newHashtable = new HashtableOfPackage(elementSize * 2); char[] currentKey; 86 for (int i = keyTable.length; --i >= 0;) 87 if ((currentKey = keyTable[i]) != null) 88 newHashtable.put(currentKey, valueTable[i]); 89 90 this.keyTable = newHashtable.keyTable; 91 this.valueTable = newHashtable.valueTable; 92 this.threshold = newHashtable.threshold; 93 } 94 public int size() { 95 return elementSize; 96 } 97 public String toString() { 98 String s = ""; PackageBinding pkg; 100 for (int i = 0, length = valueTable.length; i < length; i++) 101 if ((pkg = valueTable[i]) != null) 102 s += pkg.toString() + "\n"; return s; 104 } 105 } 106 | Popular Tags |