1 package org.unicode.cldr.web; 2 3 public class IntHash<T> { 4 public final static int HASH_SIZE = 2048; 5 public final static int MAX_SIZE = 768000; // 385024; 6 public final static int CHUNKSIZE = 4096; 7 public final static int INITIAL_SIZE = 10240; 8 public final static int BUCKET_COUNT = MAX_SIZE / HASH_SIZE; 9 @SuppressWarnings("unchecked") 10 private T[][] hashedIds = (T[][]) new Object[BUCKET_COUNT][]; 11 12 public void clear() { 13 for (int i = 0; i < hashedIds.length; i++) { 14 hashedIds[i] = null; 15 } 16 } 17 18 private final int idToBucket(int id) { 19 return id / HASH_SIZE; 20 } 21 22 public String stats() { 23 int filled = 0; 24 int lastbuck = 0; 25 for (int i = 0; i < BUCKET_COUNT; i++) { 26 if (hashedIds[i] != null) { 27 filled++; 28 lastbuck = i; 29 } 30 } 31 return "IntHash<" + "T" + "> Max:" + MAX_SIZE + ", HASH:" + HASH_SIZE + ", NRBUCKETS:" + filled + "/" + BUCKET_COUNT 32 + " : last bucket=" + lastbuck + ", greatest max=" + ((lastbuck + 1) * HASH_SIZE); 33 } 34 35 @SuppressWarnings("unchecked") 36 public final T put(int id, T str) { 37 try { 38 int buckid = idToBucket(id); 39 T[] bucket = hashedIds[buckid]; 40 if (bucket == null) { 41 bucket = (T[]) new Object[HASH_SIZE]; 42 hashedIds[buckid] = bucket; 43 } 44 return bucket[id % HASH_SIZE] = str; 45 } catch (ArrayIndexOutOfBoundsException aioob) { 46 if (id > MAX_SIZE) 47 throw new InternalError("Exceeded max " + MAX_SIZE + " @ " + id); 48 System.err.println("IntHash: aioob: id" + id + ", buckid" + idToBucket(id) + ", hashedIdsLen" + hashedIds.length); 49 throw aioob; 50 } 51 } 52 53 public final T get(int id) { 54 try { 55 T[] bucket = hashedIds[idToBucket(id)]; 56 if (bucket == null) 57 return null; // no bucket = no id. 58 return bucket[id % HASH_SIZE]; 59 } catch (ArrayIndexOutOfBoundsException aioob) { 60 if (id > MAX_SIZE) 61 throw new InternalError("Exceeded max " + MAX_SIZE + " @ " + id); 62 throw aioob; 63 } 64 } 65 } 66