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 IntStringHashMap extends PrimitiveKeyBase 43 { 44 45 protected int[] m_keyTable; 46 47 48 protected String [] m_valueTable; 49 50 56 57 public IntStringHashMap(int count, double fill) { 58 super(count, fill, int.class, String .class); 59 } 60 61 67 68 public IntStringHashMap(int count) { 69 this(count, DEFAULT_FILL); 70 } 71 72 75 76 public IntStringHashMap() { 77 this(0, DEFAULT_FILL); 78 } 79 80 85 86 public IntStringHashMap(IntStringHashMap base) { 87 super(base); 88 } 89 90 97 98 protected final Object getKeyArray() { 99 return m_keyTable; 100 } 101 102 109 110 protected final void setKeyArray(Object array) { 111 m_keyTable = (int[])array; 112 } 113 114 121 122 protected final Object getValueArray() { 123 return m_valueTable; 124 } 125 126 133 134 protected final void setValueArray(Object array) { 135 m_valueTable = (String [])array; 136 } 137 138 147 148 protected final boolean reinsert(int slot) { 149 m_flagTable[slot] = false; 150 return assignSlot(m_keyTable[slot], m_valueTable[slot]) != slot; 151 } 152 153 164 165 protected void restructure(boolean[] flags, Object karray, Object varray) { 166 int[] keys = (int[])karray; 167 String [] values = (String [])varray; 168 for (int i = 0; i < flags.length; i++) { 169 if (flags[i]) { 170 assignSlot(keys[i], values[i]); 171 } 172 } 173 } 174 175 181 182 protected final int computeSlot(int key) { 183 return (key * KEY_MULTIPLIER & Integer.MAX_VALUE) % m_flagTable.length; 184 } 185 186 197 198 protected int assignSlot(int key, String value) { 199 int offset = freeSlot(computeSlot(key)); 200 m_flagTable[offset] = true; 201 m_keyTable[offset] = key; 202 m_valueTable[offset] = value; 203 return offset; 204 } 205 206 215 216 public String add(int key, String value) { 217 ensureCapacity(m_entryCount+1); 218 int offset = internalFind(key); 219 if (offset >= 0) { 220 String prior = m_valueTable[offset]; 221 m_valueTable[offset] = value; 222 return prior; 223 } else { 224 m_entryCount++; 225 offset = -offset - 1; 226 m_flagTable[offset] = true; 227 m_keyTable[offset] = key; 228 m_valueTable[offset] = value; 229 return null; 230 } 231 } 232 233 240 241 protected final int internalFind(int key) { 242 int slot = computeSlot(key); 243 while (m_flagTable[slot]) { 244 if (key == m_keyTable[slot]) { 245 return slot; 246 } 247 slot = stepSlot(slot); 248 } 249 return -slot - 1; 250 } 251 252 260 261 public final boolean containsKey(int key) { 262 return internalFind(key) >= 0; 263 } 264 265 271 272 public final String get(int key) { 273 int slot = internalFind(key); 274 if (slot >= 0) { 275 return m_valueTable[slot]; 276 } else { 277 return null; 278 } 279 } 280 281 288 289 public String remove(int key) { 290 int slot = internalFind(key); 291 if (slot >= 0) { 292 String value = m_valueTable[slot]; 293 m_flagTable[slot] = false; 294 m_entryCount--; 295 while (m_flagTable[(slot = stepSlot(slot))]) { 296 reinsert(slot); 297 } 298 return value; 299 } else { 300 return null; 301 } 302 } 303 304 312 313 public final Iterator valueIterator() { 314 return SparseArrayIterator.buildIterator(m_valueTable); 315 } 316 317 322 323 public Object clone() { 324 return new IntStringHashMap(this); 325 } 326 } 327 | Popular Tags |