1 18 package org.apache.batik.dom.util; 19 20 26 public class DoublyIndexedTable { 27 28 31 protected final static int INITIAL_CAPACITY = 11; 32 33 36 protected Entry[] table; 37 38 41 protected int count; 42 43 46 public DoublyIndexedTable() { 47 table = new Entry[INITIAL_CAPACITY]; 48 } 49 50 54 public DoublyIndexedTable(int c) { 55 table = new Entry[c]; 56 } 57 58 61 public int size() { 62 return count; 63 } 64 65 69 public Object put(Object o1, Object o2, Object value) { 70 int hash = hashCode(o1, o2) & 0x7FFFFFFF; 71 int index = hash % table.length; 72 73 for (Entry e = table[index]; e != null; e = e.next) { 74 if ((e.hash == hash) && e.match(o1, o2)) { 75 Object old = e.value; 76 e.value = value; 77 return old; 78 } 79 } 80 81 int len = table.length; 83 if (count++ >= (len * 3) >>> 2) { 84 rehash(); 85 index = hash % table.length; 86 } 87 88 Entry e = new Entry(hash, o1, o2, value, table[index]); 89 table[index] = e; 90 return null; 91 } 92 93 97 public Object get(Object o1, Object o2) { 98 int hash = hashCode(o1, o2) & 0x7FFFFFFF; 99 int index = hash % table.length; 100 101 for (Entry e = table[index]; e != null; e = e.next) { 102 if ((e.hash == hash) && e.match(o1, o2)) { 103 return e.value; 104 } 105 } 106 return null; 107 } 108 109 112 protected void rehash () { 113 Entry[] oldTable = table; 114 115 table = new Entry[oldTable.length * 2 + 1]; 116 117 for (int i = oldTable.length-1; i >= 0; i--) { 118 for (Entry old = oldTable[i]; old != null;) { 119 Entry e = old; 120 old = old.next; 121 122 int index = e.hash % table.length; 123 e.next = table[index]; 124 table[index] = e; 125 } 126 } 127 } 128 129 132 protected int hashCode(Object o1, Object o2) { 133 int result = (o1 == null) ? 0 : o1.hashCode(); 134 return result ^ ((o2 == null) ? 0 : o2.hashCode()); 135 } 136 137 140 protected static class Entry { 141 144 public int hash; 145 146 149 public Object key1; 150 151 154 public Object key2; 155 156 159 public Object value; 160 161 164 public Entry next; 165 166 169 public Entry(int hash, Object key1, Object key2, Object value, Entry next) { 170 this.hash = hash; 171 this.key1 = key1; 172 this.key2 = key2; 173 this.value = value; 174 this.next = next; 175 } 176 177 180 public boolean match(Object o1, Object o2) { 181 if (key1 != null) { 182 if (!key1.equals(o1)) { 183 return false; 184 } 185 } else if (o1 != null) { 186 return false; 187 } 188 if (key2 != null) { 189 return key2.equals(o2); 190 } 191 return o2 == null; 192 } 193 } 194 } 195 | Popular Tags |