1 22 23 package com.sosnoski.util.hashmap; 24 25 import java.util.Iterator ; 26 27 import com.sosnoski.util.SparseArrayIterator; 28 29 41 42 public class ObjectObjectHashMap extends ObjectKeyBase 43 { 44 45 protected Object [] m_keyTable; 46 47 48 protected Object [] m_valueTable; 49 50 59 60 public ObjectObjectHashMap(int count, double fill, Object tech) { 61 super(count, fill, Object .class, Object .class, tech); 62 } 63 64 71 72 public ObjectObjectHashMap(int count, double fill) { 73 this(count, fill, STANDARD_HASH); 74 } 75 76 85 86 public ObjectObjectHashMap(int count, Object tech) { 87 this(count, DEFAULT_FILL, tech); 88 } 89 90 96 97 public ObjectObjectHashMap(int count) { 98 this(count, DEFAULT_FILL); 99 } 100 101 109 110 public ObjectObjectHashMap(Object tech) { 111 this(0, DEFAULT_FILL, tech); 112 } 113 114 117 118 public ObjectObjectHashMap() { 119 this(0, DEFAULT_FILL); 120 } 121 122 127 128 public ObjectObjectHashMap(ObjectObjectHashMap base) { 129 super(base); 130 } 131 132 139 140 protected final Object [] getKeyArray() { 141 return m_keyTable; 142 } 143 144 151 152 protected final void setKeyArray(Object array) { 153 m_keyTable = (Object [])array; 154 } 155 156 163 164 protected final Object getValueArray() { 165 return m_valueTable; 166 } 167 168 175 176 protected final void setValueArray(Object array) { 177 m_valueTable = (Object [])array; 178 } 179 180 190 191 protected final boolean reinsert(int slot) { 192 Object key = m_keyTable[slot]; 193 m_keyTable[slot] = null; 194 return assignSlot(key, m_valueTable[slot]) != slot; 195 } 196 197 207 208 protected void restructure(Object karray, Object varray) { 209 Object [] keys = (Object [])karray; 210 Object [] values = (Object [])varray; 211 for (int i = 0; i < keys.length; i++) { 212 if (keys[i] != null) { 213 assignSlot(keys[i], values[i]); 214 } 215 } 216 } 217 218 229 230 protected int assignSlot(Object key, Object value) { 231 int offset = freeSlot(standardSlot(key)); 232 m_keyTable[offset] = key; 233 m_valueTable[offset] = value; 234 return offset; 235 } 236 237 246 247 public Object add(Object key, Object value) { 248 249 if (key == null) { 251 throw new IllegalArgumentException ("null key not supported"); 252 } else if (value == null) { 253 throw new IllegalArgumentException ("null value not supported"); 254 } else { 255 256 ensureCapacity(m_entryCount+1); 258 int offset = standardFind(key); 259 if (offset >= 0) { 260 261 Object prior = m_valueTable[offset]; 263 m_valueTable[offset] = value; 264 return prior; 265 266 } else { 267 268 m_entryCount++; 270 offset = -offset - 1; 271 m_keyTable[offset] = key; 272 m_valueTable[offset] = value; 273 return null; 274 275 } 276 } 277 } 278 279 287 288 public final boolean containsKey(Object key) { 289 return standardFind(key) >= 0; 290 } 291 292 298 299 public final Object get(Object key) { 300 int slot = standardFind(key); 301 if (slot >= 0) { 302 return m_valueTable[slot]; 303 } else { 304 return null; 305 } 306 } 307 308 316 317 public Object remove(Object key) { 318 int slot = standardFind(key); 319 if (slot >= 0) { 320 Object value = m_valueTable[slot]; 321 internalRemove(slot); 322 return value; 323 } else { 324 return null; 325 } 326 } 327 328 336 337 public final Iterator valueIterator() { 338 return SparseArrayIterator.buildIterator(m_valueTable); 339 } 340 341 346 347 public Object clone() { 348 return new ObjectObjectHashMap(this); 349 } 350 } 351 | Popular Tags |