1 /* IdentityHashMap.java -- a class providing a hashtable data structure,
2 mapping Object --> Object, which uses object identity for hashing.
3 Copyright (C) 2001, 2002, 2004, 2005 Free Software Foundation, Inc.
5 This file is part of GNU Classpath.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 exception statement from your version. */
41 import java
.io
.IOException
;
42 import java
.io
.ObjectInputStream
;
43 import java
.io
.ObjectOutputStream
;
44 import java
.io
.Serializable
;
47 * This class provides a hashtable-backed implementation of the
48 * Map interface, but uses object identity to do its hashing. In fact,
49 * it uses object identity for comparing values, as well. It uses a
50 * linear-probe hash table, which may have faster performance
51 * than the chaining employed by HashMap.
54 * <em>WARNING: This is not a general purpose map. Because it uses
55 * System.identityHashCode and ==, instead of hashCode and equals, for
56 * comparison, it violated Map's general contract, and may cause
57 * undefined behavior when compared to other maps which are not
58 * IdentityHashMaps. This is designed only for the rare cases when
59 * identity semantics are needed.</em> An example use is
60 * topology-preserving graph transformations, such as deep cloning,
61 * or as proxy object mapping such as in debugging.
64 * This map permits <code>null</code> keys and values, and does not
65 * guarantee that elements will stay in the same order over time. The
66 * basic operations (<code>get</code> and <code>put</code>) take
67 * constant time, provided System.identityHashCode is decent. You can
68 * tune the behavior by specifying the expected maximum size. As more
69 * elements are added, the map may need to allocate a larger table,
70 * which can be expensive.
73 * This implementation is unsynchronized. If you want multi-thread
74 * access to be consistent, you must synchronize it, perhaps by using
75 * <code>Collections.synchronizedMap(new IdentityHashMap(...));</code>.
76 * The iterators are <i>fail-fast</i>, meaning that a structural modification
77 * made to the map outside of an iterator's remove method cause the
78 * iterator, and in the case of the entrySet, the Map.Entry, to
79 * fail with a {@link ConcurrentModificationException}.
81 * @author Tom Tromey (tromey@redhat.com)
82 * @author Eric Blake (ebb9@email.byu.edu)
83 * @see System#identityHashCode(Object)
91 * @status updated to 1.4
93 public class IdentityHashMap
extends AbstractMap
94 implements Map
, Serializable
, Cloneable
96 /** The default capacity. */
97 private static final int DEFAULT_CAPACITY
= 21;
100 * This object is used to mark deleted items. Package visible for use by
103 static final Object tombstone
= new Object();
106 * This object is used to mark empty slots. We need this because
107 * using null is ambiguous. Package visible for use by nested classes.
109 static final Object emptyslot
= new Object();
112 * Compatible with JDK 1.4.
114 private static final long serialVersionUID
= 8188218128353913216L;
117 * The number of mappings in the table. Package visible for use by nested
124 * The table itself. Package visible for use by nested classes.
126 transient Object
[] table
;
129 * The number of structural modifications made so far. Package visible for
130 * use by nested classes.
132 transient int modCount
;
135 * The cache for {@link #entrySet()}.
137 private transient Set entries
;
140 * The threshold for rehashing, which is 75% of (table.length / 2).
142 private transient int threshold
;
145 * Create a new IdentityHashMap with the default capacity (21 entries).
147 public IdentityHashMap()
149 this(DEFAULT_CAPACITY
);
153 * Create a new IdentityHashMap with the indicated number of
154 * entries. If the number of elements added to this hash map
155 * exceeds this maximum, the map will grow itself; however, that
156 * incurs a performance penalty.
158 * @param max initial size
159 * @throws IllegalArgumentException if max is negative
161 public IdentityHashMap(int max
)
164 throw new IllegalArgumentException();
165 // Need at least two slots, or hash() will break.
168 table
= new Object
[max
<< 1];
169 Arrays
.fill(table
, emptyslot
);
170 threshold
= (max
>> 2) * 3;
174 * Create a new IdentityHashMap whose contents are taken from the
177 * @param m The map whose elements are to be put in this map
178 * @throws NullPointerException if m is null
180 public IdentityHashMap(Map m
)
182 this(Math
.max(m
.size() << 1, DEFAULT_CAPACITY
));
187 * Remove all mappings from this map.
194 Arrays
.fill(table
, emptyslot
);
200 * Creates a shallow copy where keys and values are not cloned.
202 public Object
clone()
206 IdentityHashMap copy
= (IdentityHashMap
) super.clone();
207 copy
.table
= (Object
[]) table
.clone();
208 copy
.entries
= null; // invalidate the cache
211 catch (CloneNotSupportedException e
)
219 * Tests whether the specified key is in this map. Unlike normal Maps,
220 * this test uses <code>entry == key</code> instead of
221 * <code>entry == null ? key == null : entry.equals(key)</code>.
223 * @param key the key to look for
224 * @return true if the key is contained in the map
225 * @see #containsValue(Object)
228 public boolean containsKey(Object key
)
230 return key
== table
[hash(key
)];
234 * Returns true if this HashMap contains the value. Unlike normal maps,
235 * this test uses <code>entry == value</code> instead of
236 * <code>entry == null ? value == null : entry.equals(value)</code>.
238 * @param value the value to search for in this HashMap
239 * @return true if at least one key maps to the value
240 * @see #containsKey(Object)
242 public boolean containsValue(Object value
)
244 for (int i
= table
.length
- 1; i
> 0; i
-= 2)
245 if (table
[i
] == value
)
251 * Returns a "set view" of this Map's entries. The set is backed by
252 * the Map, so changes in one show up in the other. The set supports
253 * element removal, but not element addition.
256 * <em>The semantics of this set, and of its contained entries, are
257 * different from the contract of Set and Map.Entry in order to make
258 * IdentityHashMap work. This means that while you can compare these
259 * objects between IdentityHashMaps, comparing them with regular sets
260 * or entries is likely to have undefined behavior.</em> The entries
261 * in this set are reference-based, rather than the normal object
262 * equality. Therefore, <code>e1.equals(e2)</code> returns
263 * <code>e1.getKey() == e2.getKey() && e1.getValue() == e2.getValue()</code>,
264 * and <code>e.hashCode()</code> returns
265 * <code>System.identityHashCode(e.getKey()) ^
266 * System.identityHashCode(e.getValue())</code>.
269 * Note that the iterators for all three views, from keySet(), entrySet(),
270 * and values(), traverse the Map in the same sequence.
272 * @return a set view of the entries
277 public Set
entrySet()
280 entries
= new AbstractSet()
287 public Iterator
iterator()
289 return new IdentityIterator(ENTRIES
);
294 IdentityHashMap
.this.clear();
297 public boolean contains(Object o
)
299 if (! (o
instanceof Map
.Entry
))
301 Map
.Entry m
= (Map
.Entry
) o
;
302 return m
.getValue() == table
[hash(m
.getKey()) + 1];
305 public int hashCode()
307 return IdentityHashMap
.this.hashCode();
310 public boolean remove(Object o
)
312 if (! (o
instanceof Map
.Entry
))
314 Object key
= ((Map
.Entry
) o
).getKey();
320 table
[h
] = tombstone
;
321 table
[h
+ 1] = tombstone
;
331 * Compares two maps for equality. This returns true only if both maps
332 * have the same reference-identity comparisons. While this returns
333 * <code>this.entrySet().equals(m.entrySet())</code> as specified by Map,
334 * this will not work with normal maps, since the entry set compares
335 * with == instead of .equals.
337 * @param o the object to compare to
338 * @return true if it is equal
340 public boolean equals(Object o
)
342 // Why did Sun specify this one? The superclass does the right thing.
343 return super.equals(o
);
347 * Return the value in this Map associated with the supplied key, or
348 * <code>null</code> if the key maps to nothing.
350 * <p>NOTE: Since the value could also be null, you must use
351 * containsKey to see if this key actually maps to something.
352 * Unlike normal maps, this tests for the key with <code>entry ==
353 * key</code> instead of <code>entry == null ? key == null :
354 * entry.equals(key)</code>.
356 * @param key the key for which to fetch an associated value
357 * @return what the key maps to, if present
358 * @see #put(Object, Object)
359 * @see #containsKey(Object)
361 public Object
get(Object key
)
364 return table
[h
] == key ? table
[h
+ 1] : null;
368 * Returns the hashcode of this map. This guarantees that two
369 * IdentityHashMaps that compare with equals() will have the same hash code,
370 * but may break with comparison to normal maps since it uses
371 * System.identityHashCode() instead of hashCode().
373 * @return the hash code
375 public int hashCode()
378 for (int i
= table
.length
- 2; i
>= 0; i
-= 2)
380 Object key
= table
[i
];
381 if (key
== emptyslot
|| key
== tombstone
)
383 hash
+= (System
.identityHashCode(key
)
384 ^ System
.identityHashCode(table
[i
+ 1]));
390 * Returns true if there are no key-value mappings currently in this Map
391 * @return <code>size() == 0</code>
393 public boolean isEmpty()
399 * Returns a "set view" of this Map's keys. The set is backed by the
400 * Map, so changes in one show up in the other. The set supports
401 * element removal, but not element addition.
404 * <em>The semantics of this set are different from the contract of Set
405 * in order to make IdentityHashMap work. This means that while you can
406 * compare these objects between IdentityHashMaps, comparing them with
407 * regular sets is likely to have undefined behavior.</em> The hashCode
408 * of the set is the sum of the identity hash codes, instead of the
409 * regular hashCodes, and equality is determined by reference instead
410 * of by the equals method.
413 * @return a set view of the keys
420 keys
= new AbstractSet()
427 public Iterator
iterator()
429 return new IdentityIterator(KEYS
);
434 IdentityHashMap
.this.clear();
437 public boolean contains(Object o
)
439 return containsKey(o
);
442 public int hashCode()
445 for (int i
= table
.length
- 2; i
>= 0; i
-= 2)
447 Object key
= table
[i
];
448 if (key
== emptyslot
|| key
== tombstone
)
450 hash
+= System
.identityHashCode(key
);
456 public boolean remove(Object o
)
463 table
[h
] = tombstone
;
464 table
[h
+ 1] = tombstone
;
474 * Puts the supplied value into the Map, mapped by the supplied key.
475 * The value may be retrieved by any object which <code>equals()</code>
476 * this key. NOTE: Since the prior value could also be null, you must
477 * first use containsKey if you want to see if you are replacing the
478 * key's mapping. Unlike normal maps, this tests for the key
479 * with <code>entry == key</code> instead of
480 * <code>entry == null ? key == null : entry.equals(key)</code>.
482 * @param key the key used to locate the value
483 * @param value the value to be stored in the HashMap
484 * @return the prior mapping of the key, or null if there was none
487 public Object
put(Object key
, Object value
)
489 // Rehash if the load factor is too high.
490 if (size
> threshold
)
492 Object
[] old
= table
;
493 // This isn't necessarily prime, but it is an odd number of key/value
494 // slots, which has a higher probability of fewer collisions.
495 table
= new Object
[(old
.length
* 2) + 2];
496 Arrays
.fill(table
, emptyslot
);
498 threshold
= (table
.length
>>> 3) * 3;
500 for (int i
= old
.length
- 2; i
>= 0; i
-= 2)
502 Object oldkey
= old
[i
];
503 if (oldkey
!= tombstone
&& oldkey
!= emptyslot
)
504 // Just use put. This isn't very efficient, but it is ok.
505 put(oldkey
, old
[i
+ 1]);
512 Object r
= table
[h
+ 1];
513 table
[h
+ 1] = value
;
517 // At this point, we add a new mapping.
521 table
[h
+ 1] = value
;
526 * Copies all of the mappings from the specified map to this. If a key
527 * is already in this map, its value is replaced.
529 * @param m the map to copy
530 * @throws NullPointerException if m is null
532 public void putAll(Map m
)
534 // Why did Sun specify this one? The superclass does the right thing.
539 * Removes from the HashMap and returns the value which is mapped by
540 * the supplied key. If the key maps to nothing, then the HashMap
541 * remains unchanged, and <code>null</code> is returned.
543 * NOTE: Since the value could also be null, you must use
544 * containsKey to see if you are actually removing a mapping.
545 * Unlike normal maps, this tests for the key with <code>entry ==
546 * key</code> instead of <code>entry == null ? key == null :
547 * entry.equals(key)</code>.
549 * @param key the key used to locate the value to remove
550 * @return whatever the key mapped to, if present
552 public Object
remove(Object key
)
559 Object r
= table
[h
+ 1];
560 table
[h
] = tombstone
;
561 table
[h
+ 1] = tombstone
;
568 * Returns the number of kay-value mappings currently in this Map
577 * Returns a "collection view" (or "bag view") of this Map's values.
578 * The collection is backed by the Map, so changes in one show up
579 * in the other. The collection supports element removal, but not element
583 * <em>The semantics of this set are different from the contract of
584 * Collection in order to make IdentityHashMap work. This means that
585 * while you can compare these objects between IdentityHashMaps, comparing
586 * them with regular sets is likely to have undefined behavior.</em>
587 * Likewise, contains and remove go by == instead of equals().
590 * @return a bag view of the values
594 public Collection
values()
597 values
= new AbstractCollection()
604 public Iterator
iterator()
606 return new IdentityIterator(VALUES
);
611 IdentityHashMap
.this.clear();
614 public boolean remove(Object o
)
616 for (int i
= table
.length
- 1; i
> 0; i
-= 2)
620 table
[i
- 1] = tombstone
;
621 table
[i
] = tombstone
;
632 * Helper method which computes the hash code, then traverses the table
633 * until it finds the key, or the spot where the key would go.
635 * @param key the key to check
636 * @return the index where the key belongs
637 * @see #IdentityHashMap(int)
638 * @see #put(Object, Object)
640 // Package visible for use by nested classes.
643 // Implementation note: it is feasible for the table to have no
644 // emptyslots, if it is full with entries and tombstones, so we must
645 // remember where we started. If we encounter the key or an emptyslot,
646 // we are done. If we encounter a tombstone, the key may still be in
647 // the array. If we don't encounter the key, we use the first emptyslot
648 // or tombstone we encountered as the location where the key would go.
649 // By requiring at least 2 key/value slots, and rehashing at 75%
650 // capacity, we guarantee that there will always be either an emptyslot
651 // or a tombstone somewhere in the table.
652 int h
= Math
.abs(System
.identityHashCode(key
) % (table
.length
>> 1)) << 1;
660 if (table
[h
] == emptyslot
)
662 if (table
[h
] == tombstone
&& del
< 0)
666 h
= table
.length
- 2;
670 return del
< 0 ? h
: del
;
674 * This class allows parameterized iteration over IdentityHashMaps. Based
675 * on its construction, it returns the key or value of a mapping, or
676 * creates the appropriate Map.Entry object with the correct fail-fast
677 * semantics and identity comparisons.
679 * @author Tom Tromey (tromey@redhat.com)
680 * @author Eric Blake (ebb9@email.byu.edu)
682 private class IdentityIterator
implements Iterator
685 * The type of this Iterator: {@link #KEYS}, {@link #VALUES},
686 * or {@link #ENTRIES}.
689 /** The number of modifications to the backing Map that we know about. */
690 int knownMod
= modCount
;
691 /** The number of elements remaining to be returned by next(). */
693 /** Location in the table. */
694 int loc
= table
.length
;
697 * Construct a new Iterator with the supplied type.
698 * @param type {@link #KEYS}, {@link #VALUES}, or {@link #ENTRIES}
700 IdentityIterator(int type
)
706 * Returns true if the Iterator has more elements.
707 * @return true if there are more elements
708 * @throws ConcurrentModificationException if the Map was modified
710 public boolean hasNext()
712 if (knownMod
!= modCount
)
713 throw new ConcurrentModificationException();
718 * Returns the next element in the Iterator's sequential view.
719 * @return the next element
720 * @throws ConcurrentModificationException if the Map was modified
721 * @throws NoSuchElementException if there is none
725 if (knownMod
!= modCount
)
726 throw new ConcurrentModificationException();
728 throw new NoSuchElementException();
737 while (key
== emptyslot
|| key
== tombstone
);
739 return type
== KEYS ? key
: (type
== VALUES ? table
[loc
+ 1]
740 : new IdentityEntry(loc
));
744 * Removes from the backing Map the last element which was fetched
745 * with the <code>next()</code> method.
747 * @throws ConcurrentModificationException if the Map was modified
748 * @throws IllegalStateException if called when there is no last element
752 if (knownMod
!= modCount
)
753 throw new ConcurrentModificationException();
754 if (loc
== table
.length
|| table
[loc
] == tombstone
)
755 throw new IllegalStateException();
758 table
[loc
] = tombstone
;
759 table
[loc
+ 1] = tombstone
;
762 } // class IdentityIterator
765 * This class provides Map.Entry objects for IdentityHashMaps. The entry
766 * is fail-fast, and will throw a ConcurrentModificationException if
767 * the underlying map is modified, or if remove is called on the iterator
768 * that generated this object. It is identity based, so it violates
769 * the general contract of Map.Entry, and is probably unsuitable for
770 * comparison to normal maps; but it works among other IdentityHashMaps.
772 * @author Eric Blake (ebb9@email.byu.edu)
774 private final class IdentityEntry
implements Map
.Entry
776 /** The location of this entry. */
778 /** The number of modifications to the backing Map that we know about. */
779 final int knownMod
= modCount
;
782 * Constructs the Entry.
784 * @param loc the location of this entry in table
786 IdentityEntry(int loc
)
792 * Compares the specified object with this entry, using identity
793 * semantics. Note that this can lead to undefined results with
794 * Entry objects created by normal maps.
796 * @param o the object to compare
797 * @return true if it is equal
798 * @throws ConcurrentModificationException if the entry was invalidated
799 * by modifying the Map or calling Iterator.remove()
801 public boolean equals(Object o
)
803 if (knownMod
!= modCount
|| table
[loc
] == tombstone
)
804 throw new ConcurrentModificationException();
805 if (! (o
instanceof Map
.Entry
))
807 Map
.Entry e
= (Map
.Entry
) o
;
808 return table
[loc
] == e
.getKey() && table
[loc
+ 1] == e
.getValue();
812 * Returns the key of this entry.
815 * @throws ConcurrentModificationException if the entry was invalidated
816 * by modifying the Map or calling Iterator.remove()
818 public Object
getKey()
820 if (knownMod
!= modCount
|| table
[loc
] == tombstone
)
821 throw new ConcurrentModificationException();
826 * Returns the value of this entry.
829 * @throws ConcurrentModificationException if the entry was invalidated
830 * by modifying the Map or calling Iterator.remove()
832 public Object
getValue()
834 if (knownMod
!= modCount
|| table
[loc
] == tombstone
)
835 throw new ConcurrentModificationException();
836 return table
[loc
+ 1];
840 * Returns the hashcode of the entry, using identity semantics.
841 * Note that this can lead to undefined results with Entry objects
842 * created by normal maps.
844 * @return the hash code
845 * @throws ConcurrentModificationException if the entry was invalidated
846 * by modifying the Map or calling Iterator.remove()
848 public int hashCode()
850 if (knownMod
!= modCount
|| table
[loc
] == tombstone
)
851 throw new ConcurrentModificationException();
852 return (System
.identityHashCode(table
[loc
])
853 ^ System
.identityHashCode(table
[loc
+ 1]));
857 * Replaces the value of this mapping, and returns the old value.
859 * @param value the new value
860 * @return the old value
861 * @throws ConcurrentModificationException if the entry was invalidated
862 * by modifying the Map or calling Iterator.remove()
864 public Object
setValue(Object value
)
866 if (knownMod
!= modCount
|| table
[loc
] == tombstone
)
867 throw new ConcurrentModificationException();
868 Object r
= table
[loc
+ 1];
869 table
[loc
+ 1] = value
;
874 * This provides a string representation of the entry. It is of the form
875 * "key=value", where string concatenation is used on key and value.
877 * @return the string representation
878 * @throws ConcurrentModificationException if the entry was invalidated
879 * by modifying the Map or calling Iterator.remove()
881 public String
toString()
883 if (knownMod
!= modCount
|| table
[loc
] == tombstone
)
884 throw new ConcurrentModificationException();
885 return table
[loc
] + "=" + table
[loc
+ 1];
887 } // class IdentityEntry
890 * Reads the object from a serial stream.
892 * @param s the stream to read from
893 * @throws ClassNotFoundException if the underlying stream fails
894 * @throws IOException if the underlying stream fails
895 * @serialData expects the size (int), followed by that many key (Object)
896 * and value (Object) pairs, with the pairs in no particular
899 private void readObject(ObjectInputStream s
)
900 throws IOException
, ClassNotFoundException
902 s
.defaultReadObject();
904 int num
= s
.readInt();
905 table
= new Object
[Math
.max(num
<< 1, DEFAULT_CAPACITY
) << 1];
906 // Read key/value pairs.
908 put(s
.readObject(), s
.readObject());
912 * Writes the object to a serial stream.
914 * @param s the stream to write to
915 * @throws IOException if the underlying stream fails
916 * @serialData outputs the size (int), followed by that many key (Object)
917 * and value (Object) pairs, with the pairs in no particular
920 private void writeObject(ObjectOutputStream s
)
923 s
.defaultWriteObject();
925 for (int i
= table
.length
- 2; i
>= 0; i
-= 2)
927 Object key
= table
[i
];
928 if (key
!= tombstone
&& key
!= emptyslot
)
931 s
.writeObject(table
[i
+ 1]);