Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 15th, 2012  |  syntax: None  |  size: 1.07 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Mark an empty space into an array without using null
  2. K[] keys;
  3. V[] values;
  4.        
  5. class Node {
  6.     K key;
  7.     V value;
  8. }
  9.  
  10. Node[] nodes;
  11.        
  12. private static final Object BLANK = new Object();
  13.        
  14. K[] keys;
  15. V[] values;
  16. boolean[] hasValue;
  17.        
  18. private boolean isNullMapped = false;
  19. private V nullValue = null;
  20.  
  21. public put(K key, V value)
  22. {
  23.   if (key == null) { nullValue = value; }
  24.   ...
  25. }
  26.        
  27. private static class KeyWrapper<K>
  28. {
  29.   public K key;
  30. }
  31.        
  32. private static final Object NULL_KEY = "NULL";
  33. K[] keys;
  34. V[] values;
  35.  
  36. private int find(K key) {
  37.   for (int i = 0; i < keys.length; i++) {
  38.     if (keys[i] == key) {
  39.       return i;
  40.     }
  41.   }
  42.   return -1;
  43. }
  44.  
  45. public V put(K key, V value) {
  46.   V old = null;
  47.   if (key != null) {
  48.     int i = find(key);
  49.     if (i >= 0) {
  50.       old = values[i];
  51.       values[i] = value;
  52.     } else {
  53.       // ...
  54.     }
  55.   } else {
  56.     return put((K) NULL_KEY, value);
  57.   }
  58.   return old;
  59. }
  60.  
  61. public V get(K key) {
  62.   if (key != null) {
  63.     int i = find(key);
  64.     if (i >= 0) {
  65.       return values[i];
  66.     }
  67.     return null;
  68.   } else {
  69.     return (get((K) NULL_KEY));
  70.   }
  71. }