1 7 8 package com.sun.corba.se.impl.orbutil; 9 import org.omg.CORBA.INTERNAL ; 10 import org.omg.CORBA.CompletionStatus ; 11 12 import com.sun.corba.se.spi.logging.CORBALogDomains; 13 import com.sun.corba.se.spi.orb.ORB; 14 15 import com.sun.corba.se.impl.logging.ORBUtilSystemException; 16 17 public class CacheTable { 18 class Entry { 19 java.lang.Object key; 20 int val; 21 Entry next; Entry rnext; public Entry(java.lang.Object k, int v) { 24 key = k; 25 val = v; 26 next = null; 27 rnext = null; 28 } 29 } 30 private boolean noReverseMap; 31 static final int INITIAL_SIZE = 16; 33 static final int MAX_SIZE = 1 << 30; 34 int size; 35 int entryCount; 36 private Entry [] map; 37 private Entry [] rmap; 38 39 private ORB orb; 40 private ORBUtilSystemException wrapper; 41 42 private CacheTable() {} 43 public CacheTable(ORB orb, boolean u) { 44 this.orb = orb; 46 wrapper = ORBUtilSystemException.get(orb, 47 CORBALogDomains.RPC_ENCODING); 48 noReverseMap = u; 49 size = INITIAL_SIZE; 50 entryCount = 0; 51 initTables(); 52 } 53 private void initTables() { 54 map = new Entry[size]; 55 rmap = noReverseMap ? null : new Entry[size]; 56 } 57 private void grow() { 58 if (size == MAX_SIZE) 59 return; 60 Entry [] oldMap = map; 61 int oldSize = size; 62 size <<= 1; 63 initTables(); 64 for (int i = 0; i < oldSize; i++) { 66 for (Entry e = oldMap[i]; e != null; e = e.next) 67 put_table(e.key, e.val); 68 } 69 } 70 private int moduloTableSize(int h) { 71 h += ~(h << 9); 74 h ^= (h >>> 14); 75 h += (h << 4); 76 h ^= (h >>> 10); 77 return h & (size - 1); 78 } 79 private int hash(java.lang.Object key) { 80 return moduloTableSize(System.identityHashCode(key)); 81 } 82 private int hash(int val) { 83 return moduloTableSize(val); 84 } 85 public final void put(java.lang.Object key, int val) { 86 if (put_table(key, val)) { 87 entryCount++; 88 if (entryCount > size * 3 / 4) 89 grow(); 90 } 91 } 92 private boolean put_table(java.lang.Object key, int val) { 93 int index = hash(key); 94 for (Entry e = map[index]; e != null; e = e.next) { 95 if (e.key == key) { 96 if (e.val != val) { 97 throw wrapper.duplicateIndirectionOffset(); 98 } 99 return false; 102 } 103 } 104 Entry newEntry = new Entry(key, val); 107 newEntry.next = map[index]; 108 map[index] = newEntry; 109 if (!noReverseMap) { 110 int rindex = hash(val); 111 newEntry.rnext = rmap[rindex]; 112 rmap[rindex] = newEntry; 113 } 114 return true; 115 } 116 public final boolean containsKey(java.lang.Object key) { 117 return (getVal(key) != -1); 118 } 119 public final int getVal(java.lang.Object key) { 120 int index = hash(key); 121 for (Entry e = map[index]; e != null; e = e.next) { 122 if (e.key == key) 123 return e.val; 124 } 125 return -1; 126 } 127 public final boolean containsVal(int val) { 128 return (getKey(val) != null); 129 } 130 public final boolean containsOrderedVal(int val) { 131 return containsVal(val); 132 } 133 public final java.lang.Object getKey(int val) { 134 int index = hash(val); 135 for (Entry e = rmap[index]; e != null; e = e.rnext) { 136 if (e.val == val) 137 return e.key; 138 } 139 return null; 140 } 141 public void done() { 142 map = null; 143 rmap = null; 144 } 145 } 146 | Popular Tags |