1 16 17 package org.apache.xerces.util; 18 19 20 29 public class SymbolHash { 30 31 35 36 protected int fTableSize = 101; 37 38 42 43 protected Entry[] fBuckets; 44 45 46 protected int fNum = 0; 47 48 52 53 public SymbolHash() { 54 fBuckets = new Entry[fTableSize]; 55 } 56 57 62 public SymbolHash(int size) { 63 fTableSize = size; 64 fBuckets = new Entry[fTableSize]; 65 } 66 67 71 79 public void put(Object key, Object value) { 80 int bucket = (key.hashCode() & 0x7FFFFFFF) % fTableSize; 81 Entry entry = search(key, bucket); 82 83 if (entry != null) { 85 entry.value = value; 86 } 87 else { 89 entry = new Entry(key, value, fBuckets[bucket]); 90 fBuckets[bucket] = entry; 91 fNum++; 92 } 93 } 94 95 101 public Object get(Object key) { 102 int bucket = (key.hashCode() & 0x7FFFFFFF) % fTableSize; 103 Entry entry = search(key, bucket); 104 if (entry != null) { 105 return entry.value; 106 } 107 return null; 108 } 109 110 115 public int getLength() { 116 return fNum; 117 } 118 119 126 public int getValues(Object [] elements, int from) { 127 for (int i=0, j=0; i<fTableSize && j<fNum; i++) { 128 for (Entry entry = fBuckets[i]; entry != null; entry = entry.next) { 129 elements[from+j] = entry.value; 130 j++; 131 } 132 } 133 return fNum; 134 } 135 136 139 public SymbolHash makeClone() { 140 SymbolHash newTable = new SymbolHash(fTableSize); 141 newTable.fNum = fNum; 142 for (int i = 0; i < fTableSize; i++) { 143 if (fBuckets[i] != null) 144 newTable.fBuckets[i] = fBuckets[i].makeClone(); 145 } 146 return newTable; 147 } 148 149 153 public void clear() { 154 for (int i=0; i<fTableSize; i++) { 155 fBuckets[i] = null; 156 } 157 fNum = 0; 158 } 160 protected Entry search(Object key, int bucket) { 161 for (Entry entry = fBuckets[bucket]; entry != null; entry = entry.next) { 163 if (key.equals(entry.key)) 164 return entry; 165 } 166 return null; 167 } 168 169 173 177 protected static final class Entry { 178 public Object key; 180 public Object value; 181 182 public Entry next; 183 184 public Entry() { 185 key = null; 186 value = null; 187 next = null; 188 } 189 190 public Entry(Object key, Object value, Entry next) { 191 this.key = key; 192 this.value = value; 193 this.next = next; 194 } 195 196 public Entry makeClone() { 197 Entry entry = new Entry(); 198 entry.key = key; 199 entry.value = value; 200 if (next != null) 201 entry.next = next.makeClone(); 202 return entry; 203 } 204 } 206 } 208 | Popular Tags |