1 22 23 package com.sosnoski.util.hashmap; 24 25 37 38 public class ObjectIntHashMap extends ObjectKeyBase 39 { 40 41 public static final int DEFAULT_NOT_FOUND = Integer.MIN_VALUE; 42 43 44 protected Object [] m_keyTable; 45 46 47 protected int[] m_valueTable; 48 49 50 protected int m_notFoundValue; 51 52 62 63 public ObjectIntHashMap(int count, double fill, int miss, Object tech) { 64 super(count, fill, Object .class, int.class, tech); 65 m_notFoundValue = miss; 66 } 67 68 75 76 public ObjectIntHashMap(int count, double fill) { 77 this(count, fill, DEFAULT_NOT_FOUND, STANDARD_HASH); 78 } 79 80 89 90 public ObjectIntHashMap(int count, Object tech) { 91 this(count, DEFAULT_FILL, DEFAULT_NOT_FOUND, tech); 92 } 93 94 100 101 public ObjectIntHashMap(int count) { 102 this(count, DEFAULT_FILL); 103 } 104 105 113 114 public ObjectIntHashMap(Object tech) { 115 this(0, DEFAULT_FILL, DEFAULT_NOT_FOUND, tech); 116 } 117 118 121 122 public ObjectIntHashMap() { 123 this(0, DEFAULT_FILL); 124 } 125 126 131 132 public ObjectIntHashMap(ObjectIntHashMap base) { 133 super(base); 134 m_notFoundValue = base.m_notFoundValue; 135 } 136 137 144 145 protected final Object [] getKeyArray() { 146 return m_keyTable; 147 } 148 149 156 157 protected final void setKeyArray(Object array) { 158 m_keyTable = (Object [])array; 159 } 160 161 168 169 protected final Object getValueArray() { 170 return m_valueTable; 171 } 172 173 180 181 protected final void setValueArray(Object array) { 182 m_valueTable = (int[])array; 183 } 184 185 195 196 protected final boolean reinsert(int slot) { 197 Object key = m_keyTable[slot]; 198 m_keyTable[slot] = null; 199 return assignSlot(key, m_valueTable[slot]) != slot; 200 } 201 202 212 213 protected void restructure(Object karray, Object varray) { 214 Object [] keys = (Object [])karray; 215 int[] values = (int[])varray; 216 for (int i = 0; i < keys.length; i++) { 217 if (keys[i] != null) { 218 assignSlot(keys[i], values[i]); 219 } 220 } 221 } 222 223 234 235 protected int assignSlot(Object key, int value) { 236 int offset = freeSlot(standardSlot(key)); 237 m_keyTable[offset] = key; 238 m_valueTable[offset] = value; 239 return offset; 240 } 241 242 251 252 public int add(Object key, int value) { 253 254 if (key == null) { 256 throw new IllegalArgumentException ("null key not supported"); 257 } else if (value == m_notFoundValue) { 258 throw new IllegalArgumentException 259 ("value matching not found return not supported"); 260 } else { 261 262 ensureCapacity(m_entryCount+1); 264 int offset = standardFind(key); 265 if (offset >= 0) { 266 267 int prior = m_valueTable[offset]; 269 m_valueTable[offset] = value; 270 return prior; 271 272 } else { 273 274 m_entryCount++; 276 offset = -offset - 1; 277 m_keyTable[offset] = key; 278 m_valueTable[offset] = value; 279 return m_notFoundValue; 280 281 } 282 } 283 } 284 285 293 294 public final boolean containsKey(Object key) { 295 return standardFind(key) >= 0; 296 } 297 298 304 305 public final int get(Object key) { 306 int slot = standardFind(key); 307 if (slot >= 0) { 308 return m_valueTable[slot]; 309 } else { 310 return m_notFoundValue; 311 } 312 } 313 314 321 322 public int remove(Object key) { 323 int slot = standardFind(key); 324 if (slot >= 0) { 325 int value = m_valueTable[slot]; 326 internalRemove(slot); 327 return value; 328 } else { 329 return m_notFoundValue; 330 } 331 } 332 333 338 339 public Object clone() { 340 return new ObjectIntHashMap(this); 341 } 342 } 343 | Popular Tags |