src/share/classes/sun/util/PreHashedMap.java

Print this page
rev 5028 : 7126277: alternative hashing


 109     protected PreHashedMap(int rows, int size, int shift, int mask) {
 110         this.rows = rows;
 111         this.size = size;
 112         this.shift = shift;
 113         this.mask = mask;
 114         this.ht = new Object[rows];
 115         init(ht);
 116     }
 117 
 118     /**
 119      * Initializes this map.
 120      *
 121      * <p> This method must construct the map's hash chains and store them into
 122      * the appropriate elements of the given hash-table row array.
 123      *
 124      * @param rows
 125      *        The row array to be initialized
 126      */
 127     protected abstract void init(Object[] ht);
 128 
 129     // @SuppressWarnings("unchecked")
 130     private V toV(Object x) {
 131         return (V)x;
 132     }
 133 
 134     public V get(Object k) {
 135         int h = (k.hashCode() >> shift) & mask;
 136         Object[] a = (Object[])ht[h];
 137         if (a == null) return null;
 138         for (;;) {
 139             if (a[0].equals(k))
 140                 return toV(a[1]);
 141             if (a.length < 3)
 142                 return null;
 143             a = (Object[])a[2];
 144         }
 145     }
 146 
 147     /**
 148      * @throws UnsupportedOperationException
 149      *         If the given key is not part of this map's initial key set


 237             public Iterator<Map.Entry<String,V>> iterator() {
 238                 return new Iterator<Map.Entry<String,V>>() {
 239                     final Iterator<String> i = keySet().iterator();
 240 
 241                     public boolean hasNext() {
 242                         return i.hasNext();
 243                     }
 244 
 245                     public Map.Entry<String,V> next() {
 246                         return new Map.Entry<String,V>() {
 247                             String k = i.next();
 248                             public String getKey() { return k; }
 249                             public V getValue() { return get(k); }
 250                             public int hashCode() {
 251                                 V v = get(k);
 252                                 return (k.hashCode()
 253                                         + (v == null
 254                                            ? 0
 255                                            : v.hashCode()));
 256                             }

 257                             public boolean equals(Object ob) {
 258                                 if (ob == this)
 259                                     return true;
 260                                 if (!(ob instanceof Map.Entry))
 261                                     return false;
 262                                 Map.Entry<String,V> that
 263                                     = (Map.Entry<String,V>)ob;
 264                                 return ((this.getKey() == null
 265                                          ? that.getKey() == null
 266                                          : this.getKey()
 267                                                .equals(that.getKey()))
 268                                         &&
 269                                         (this.getValue() == null
 270                                          ? that.getValue() == null
 271                                          : this.getValue()
 272                                                .equals(that.getValue())));
 273                             }
 274                             public V setValue(V v) {
 275                                 throw new UnsupportedOperationException();
 276                             }


 109     protected PreHashedMap(int rows, int size, int shift, int mask) {
 110         this.rows = rows;
 111         this.size = size;
 112         this.shift = shift;
 113         this.mask = mask;
 114         this.ht = new Object[rows];
 115         init(ht);
 116     }
 117 
 118     /**
 119      * Initializes this map.
 120      *
 121      * <p> This method must construct the map's hash chains and store them into
 122      * the appropriate elements of the given hash-table row array.
 123      *
 124      * @param rows
 125      *        The row array to be initialized
 126      */
 127     protected abstract void init(Object[] ht);
 128 
 129     @SuppressWarnings("unchecked")
 130     private V toV(Object x) {
 131         return (V)x;
 132     }
 133 
 134     public V get(Object k) {
 135         int h = (k.hashCode() >> shift) & mask;
 136         Object[] a = (Object[])ht[h];
 137         if (a == null) return null;
 138         for (;;) {
 139             if (a[0].equals(k))
 140                 return toV(a[1]);
 141             if (a.length < 3)
 142                 return null;
 143             a = (Object[])a[2];
 144         }
 145     }
 146 
 147     /**
 148      * @throws UnsupportedOperationException
 149      *         If the given key is not part of this map's initial key set


 237             public Iterator<Map.Entry<String,V>> iterator() {
 238                 return new Iterator<Map.Entry<String,V>>() {
 239                     final Iterator<String> i = keySet().iterator();
 240 
 241                     public boolean hasNext() {
 242                         return i.hasNext();
 243                     }
 244 
 245                     public Map.Entry<String,V> next() {
 246                         return new Map.Entry<String,V>() {
 247                             String k = i.next();
 248                             public String getKey() { return k; }
 249                             public V getValue() { return get(k); }
 250                             public int hashCode() {
 251                                 V v = get(k);
 252                                 return (k.hashCode()
 253                                         + (v == null
 254                                            ? 0
 255                                            : v.hashCode()));
 256                             }
 257                             @SuppressWarnings("unchecked")
 258                             public boolean equals(Object ob) {
 259                                 if (ob == this)
 260                                     return true;
 261                                 if (!(ob instanceof Map.Entry))
 262                                     return false;
 263                                 Map.Entry<String,V> that
 264                                     = (Map.Entry<String,V>)ob;
 265                                 return ((this.getKey() == null
 266                                          ? that.getKey() == null
 267                                          : this.getKey()
 268                                                .equals(that.getKey()))
 269                                         &&
 270                                         (this.getValue() == null
 271                                          ? that.getValue() == null
 272                                          : this.getValue()
 273                                                .equals(that.getValue())));
 274                             }
 275                             public V setValue(V v) {
 276                                 throw new UnsupportedOperationException();
 277                             }