1 57 58 package com.sun.org.apache.xerces.internal.util; 59 60 61 70 public class SymbolHash { 71 72 76 77 protected int fTableSize = 101; 78 79 83 84 protected Entry[] fBuckets; 85 86 87 protected int fNum = 0; 88 89 93 94 public SymbolHash() { 95 fBuckets = new Entry[fTableSize]; 96 } 97 98 103 public SymbolHash(int size) { 104 fTableSize = size; 105 fBuckets = new Entry[fTableSize]; 106 } 107 108 112 120 public void put(Object key, Object value) { 121 int bucket = (key.hashCode() & 0x7FFFFFFF) % fTableSize; 122 Entry entry = search(key, bucket); 123 124 if (entry != null) { 126 entry.value = value; 127 } 128 else { 130 entry = new Entry(key, value, fBuckets[bucket]); 131 fBuckets[bucket] = entry; 132 fNum++; 133 } 134 } 135 136 142 public Object get(Object key) { 143 int bucket = (key.hashCode() & 0x7FFFFFFF) % fTableSize; 144 Entry entry = search(key, bucket); 145 if (entry != null) { 146 return entry.value; 147 } 148 return null; 149 } 150 151 156 public int getLength() { 157 return fNum; 158 } 159 160 167 public int getValues(Object [] elements, int from) { 168 for (int i=0, j=0; i<fTableSize && j<fNum; i++) { 169 for (Entry entry = fBuckets[i]; entry != null; entry = entry.next) { 170 elements[from+j] = entry.value; 171 j++; 172 } 173 } 174 return fNum; 175 } 176 177 180 public SymbolHash makeClone() { 181 SymbolHash newTable = new SymbolHash(fTableSize); 182 newTable.fNum = fNum; 183 for (int i = 0; i < fTableSize; i++) { 184 if (fBuckets[i] != null) 185 newTable.fBuckets[i] = fBuckets[i].makeClone(); 186 } 187 return newTable; 188 } 189 190 194 public void clear() { 195 for (int i=0; i<fTableSize; i++) { 196 fBuckets[i] = null; 197 } 198 fNum = 0; 199 } 201 protected Entry search(Object key, int bucket) { 202 for (Entry entry = fBuckets[bucket]; entry != null; entry = entry.next) { 204 if (key.equals(entry.key)) 205 return entry; 206 } 207 return null; 208 } 209 210 214 218 protected static final class Entry { 219 public Object key; 221 public Object value; 222 223 public Entry next; 224 225 public Entry() { 226 key = null; 227 value = null; 228 next = null; 229 } 230 231 public Entry(Object key, Object value, Entry next) { 232 this.key = key; 233 this.value = value; 234 this.next = next; 235 } 236 237 public Entry makeClone() { 238 Entry entry = new Entry(); 239 entry.key = key; 240 entry.value = value; 241 if (next != null) 242 entry.next = next.makeClone(); 243 return entry; 244 } 245 } 247 } 249 | Popular Tags |