src/java.base/share/classes/java/util/IdentityHashMap.java
author mchung
Tue, 06 Nov 2018 10:01:16 -0800
changeset 52427 3c6aa484536c
parent 49433 b6671a111395
child 57956 e0b8b019d2f5
permissions -rw-r--r--
8211122: Reduce the number of internal classes made accessible to jdk.unsupported Reviewed-by: alanb, dfuchs, kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
49433
b6671a111395 8199465: {@docRoot} references need to be updated to reflect new module/package structure
jjg
parents: 47423
diff changeset
     2
 * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 494
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 494
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 494
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 494
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 494
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
    27
15997
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
    28
import java.lang.reflect.Array;
18280
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
    29
import java.util.function.BiConsumer;
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
    30
import java.util.function.BiFunction;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
    31
import java.util.function.Consumer;
52427
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 49433
diff changeset
    32
import jdk.internal.access.SharedSecrets;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    35
 * This class implements the {@code Map} interface with a hash table, using
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * reference-equality in place of object-equality when comparing keys (and
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    37
 * values).  In other words, in an {@code IdentityHashMap}, two keys
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    38
 * {@code k1} and {@code k2} are considered equal if and only if
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    39
 * {@code (k1==k2)}.  (In normal {@code Map} implementations (like
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    40
 * {@code HashMap}) two keys {@code k1} and {@code k2} are considered equal
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    41
 * if and only if {@code (k1==null ? k2==null : k1.equals(k2))}.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    43
 * <p><b>This class is <i>not</i> a general-purpose {@code Map}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    44
 * implementation!  While this class implements the {@code Map} interface, it
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    45
 * intentionally violates {@code Map's} general contract, which mandates the
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    46
 * use of the {@code equals} method when comparing objects.  This class is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * designed for use only in the rare cases wherein reference-equality
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * semantics are required.</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p>A typical use of this class is <i>topology-preserving object graph
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * transformations</i>, such as serialization or deep-copying.  To perform such
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * a transformation, a program must maintain a "node table" that keeps track
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * of all the object references that have already been processed.  The node
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * table must not equate distinct objects even if they happen to be equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * Another typical use of this class is to maintain <i>proxy objects</i>.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * example, a debugging facility might wish to maintain a proxy object for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * each object in the program being debugged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <p>This class provides all of the optional map operations, and permits
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    60
 * {@code null} values and the {@code null} key.  This class makes no
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * guarantees as to the order of the map; in particular, it does not guarantee
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * that the order will remain constant over time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <p>This class provides constant-time performance for the basic
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
    65
 * operations ({@code get} and {@code put}), assuming the system
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * identity hash function ({@link System#identityHashCode(Object)})
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * disperses elements properly among the buckets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * <p>This class has one tuning parameter (which affects performance but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * semantics): <i>expected maximum size</i>.  This parameter is the maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * number of key-value mappings that the map is expected to hold.  Internally,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * this parameter is used to determine the number of buckets initially
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * comprising the hash table.  The precise relationship between the expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * maximum size and the number of buckets is unspecified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <p>If the size of the map (the number of key-value mappings) sufficiently
25413
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
    77
 * exceeds the expected maximum size, the number of buckets is increased.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * Increasing the number of buckets ("rehashing") may be fairly expensive, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * it pays to create identity hash maps with a sufficiently large expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * maximum size.  On the other hand, iteration over collection views requires
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * time proportional to the number of buckets in the hash table, so it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * pays not to set the expected maximum size too high if you are especially
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * concerned with iteration performance or memory usage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * <p><strong>Note that this implementation is not synchronized.</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * If multiple threads access an identity hash map concurrently, and at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * least one of the threads modifies the map structurally, it <i>must</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * be synchronized externally.  (A structural modification is any operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * that adds or deletes one or more mappings; merely changing the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * associated with a key that an instance already contains is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * structural modification.)  This is typically accomplished by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * synchronizing on some object that naturally encapsulates the map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * If no such object exists, the map should be "wrapped" using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * {@link Collections#synchronizedMap Collections.synchronizedMap}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * method.  This is best done at creation time, to prevent accidental
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * unsynchronized access to the map:<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *   Map m = Collections.synchronizedMap(new IdentityHashMap(...));</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   100
 * <p>The iterators returned by the {@code iterator} method of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * collections returned by all of this class's "collection view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * methods" are <i>fail-fast</i>: if the map is structurally modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * at any time after the iterator is created, in any way except
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   104
 * through the iterator's own {@code remove} method, the iterator
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * will throw a {@link ConcurrentModificationException}.  Thus, in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * face of concurrent modification, the iterator fails quickly and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * cleanly, rather than risking arbitrary, non-deterministic behavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * at an undetermined time in the future.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * as it is, generally speaking, impossible to make any hard guarantees in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * presence of unsynchronized concurrent modification.  Fail-fast iterators
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   113
 * throw {@code ConcurrentModificationException} on a best-effort basis.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * Therefore, it would be wrong to write a program that depended on this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * exception for its correctness: <i>fail-fast iterators should be used only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * to detect bugs.</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * <p>Implementation note: This is a simple <i>linear-probe</i> hash table,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * as described for example in texts by Sedgewick and Knuth.  The array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * alternates holding keys and values.  (This has better locality for large
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * tables than does using separate arrays.)  For many JRE implementations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * and operation mixes, this class will yield better performance than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * {@link HashMap} (which uses <i>chaining</i> rather than linear-probing).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * <p>This class is a member of the
49433
b6671a111395 8199465: {@docRoot} references need to be updated to reflect new module/package structure
jjg
parents: 47423
diff changeset
   126
 * <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * @see     System#identityHashCode(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * @see     Object#hashCode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * @see     Collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * @see     Map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * @see     HashMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * @see     TreeMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * @author  Doug Lea and Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * @since   1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
public class IdentityHashMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    extends AbstractMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    implements Map<K,V>, java.io.Serializable, Cloneable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * The initial capacity used by the no-args constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * MUST be a power of two.  The value 32 corresponds to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * (specified) expected maximum size of 21, given a load factor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * of 2/3.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    private static final int DEFAULT_CAPACITY = 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * The minimum capacity, used if a lower value is implicitly specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * by either of the constructors with arguments.  The value 4 corresponds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * to an expected maximum size of 2, given a load factor of 2/3.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * MUST be a power of two.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    private static final int MINIMUM_CAPACITY = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * The maximum capacity, used if a higher value is implicitly specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * by either of the constructors with arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * MUST be a power of two <= 1<<29.
25413
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   163
     *
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   164
     * In fact, the map can hold no more than MAXIMUM_CAPACITY-1 items
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   165
     * because it has to have at least one slot with the key == null
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   166
     * in order to avoid infinite loops in get(), put(), remove()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    private static final int MAXIMUM_CAPACITY = 1 << 29;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * The table, resized as necessary. Length MUST always be a power of two.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
   173
    transient Object[] table; // non-private to simplify nested class access
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * The number of key-value mappings contained in this identity hash map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
   180
    int size;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * The number of modifications, to support fast-fail iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
   185
    transient int modCount;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Value representing null keys inside tables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
   190
    static final Object NULL_KEY = new Object();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Use NULL_KEY for key if it is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    private static Object maskNull(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        return (key == null ? NULL_KEY : key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * Returns internal representation of null key back to caller as null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     */
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
   202
    static final Object unmaskNull(Object key) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return (key == NULL_KEY ? null : key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * Constructs a new, empty identity hash map with a default expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * maximum size (21).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public IdentityHashMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        init(DEFAULT_CAPACITY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * Constructs a new, empty map with the specified expected maximum size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * Putting more than the expected number of key-value mappings into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * the map may cause the internal data structure to grow, which may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * somewhat time-consuming.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @param expectedMaxSize the expected maximum size of the map
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   221
     * @throws IllegalArgumentException if {@code expectedMaxSize} is negative
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    public IdentityHashMap(int expectedMaxSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        if (expectedMaxSize < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            throw new IllegalArgumentException("expectedMaxSize is negative: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                                               + expectedMaxSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        init(capacity(expectedMaxSize));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /**
25413
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   231
     * Returns the appropriate capacity for the given expected maximum size.
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   232
     * Returns the smallest power of two between MINIMUM_CAPACITY and
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   233
     * MAXIMUM_CAPACITY, inclusive, that is greater than (3 *
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   234
     * expectedMaxSize)/2, if such a number exists.  Otherwise returns
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   235
     * MAXIMUM_CAPACITY.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
25413
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   237
    private static int capacity(int expectedMaxSize) {
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   238
        // assert expectedMaxSize >= 0;
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   239
        return
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   240
            (expectedMaxSize > MAXIMUM_CAPACITY / 3) ? MAXIMUM_CAPACITY :
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   241
            (expectedMaxSize <= 2 * MINIMUM_CAPACITY / 3) ? MINIMUM_CAPACITY :
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   242
            Integer.highestOneBit(expectedMaxSize + (expectedMaxSize << 1));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * Initializes object to be an empty map with the specified initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * capacity, which is assumed to be a power of two between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * MINIMUM_CAPACITY and MAXIMUM_CAPACITY inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    private void init(int initCapacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        // assert (initCapacity & -initCapacity) == initCapacity; // power of 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        // assert initCapacity >= MINIMUM_CAPACITY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        // assert initCapacity <= MAXIMUM_CAPACITY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        table = new Object[2 * initCapacity];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * Constructs a new identity hash map containing the keys-value mappings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * in the specified map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @param m the map whose mappings are to be placed into this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @throws NullPointerException if the specified map is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    public IdentityHashMap(Map<? extends K, ? extends V> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        // Allow for a bit of growth
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        this((int) ((1 + m.size()) * 1.1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        putAll(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * Returns the number of key-value mappings in this identity hash map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @return the number of key-value mappings in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   281
     * Returns {@code true} if this identity hash map contains no key-value
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * mappings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   284
     * @return {@code true} if this identity hash map contains no key-value
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *         mappings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        return size == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * Returns index for Object x.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    private static int hash(Object x, int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        int h = System.identityHashCode(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        // Multiply by -127, and left-shift to use least bit as part of hash
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        return ((h << 1) - (h << 8)) & (length - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * Circularly traverses table of size len.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    private static int nextKeyIndex(int i, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        return (i + 2 < len ? i + 2 : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * Returns the value to which the specified key is mapped,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * or {@code null} if this map contains no mapping for the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * <p>More formally, if this map contains a mapping from a key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * {@code k} to a value {@code v} such that {@code (key == k)},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * then this method returns {@code v}; otherwise it returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * {@code null}.  (There can be at most one such mapping.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * <p>A return value of {@code null} does not <i>necessarily</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * indicate that the map contains no mapping for the key; it's also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * possible that the map explicitly maps the key to {@code null}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * The {@link #containsKey containsKey} operation may be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * distinguish these two cases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * @see #put(Object, Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   324
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    public V get(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        Object k = maskNull(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        Object[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        int len = tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        int i = hash(k, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            Object item = tab[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            if (item == k)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                return (V) tab[i + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            if (item == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            i = nextKeyIndex(i, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * Tests whether the specified object reference is a key in this identity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * hash map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @param   key   possible key
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   345
     * @return  {@code true} if the specified object reference is a key
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *          in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @see     #containsValue(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    public boolean containsKey(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        Object k = maskNull(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        Object[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        int len = tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        int i = hash(k, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            Object item = tab[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            if (item == k)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            if (item == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            i = nextKeyIndex(i, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * Tests whether the specified object reference is a value in this identity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * hash map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @param value value whose presence in this map is to be tested
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   369
     * @return {@code true} if this map maps one or more keys to the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     *         specified object reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * @see     #containsKey(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    public boolean containsValue(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        Object[] tab = table;
494
320ce398f07e 6691215: (coll) IdentityHashMap.containsValue(null) returns true when null value not present
martin
parents: 65
diff changeset
   375
        for (int i = 1; i < tab.length; i += 2)
320ce398f07e 6691215: (coll) IdentityHashMap.containsValue(null) returns true when null value not present
martin
parents: 65
diff changeset
   376
            if (tab[i] == value && tab[i - 1] != null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * Tests if the specified key-value mapping is in the map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * @param   key   possible key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * @param   value possible value
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   387
     * @return  {@code true} if and only if the specified key-value
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *          mapping is in the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    private boolean containsMapping(Object key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        Object k = maskNull(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        Object[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        int len = tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        int i = hash(k, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            Object item = tab[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            if (item == k)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                return tab[i + 1] == value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            if (item == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            i = nextKeyIndex(i, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * Associates the specified value with the specified key in this identity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * hash map.  If the map previously contained a mapping for the key, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * old value is replaced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * @param key the key with which the specified value is to be associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * @param value the value to be associated with the specified key
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   412
     * @return the previous value associated with {@code key}, or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   413
     *         {@code null} if there was no mapping for {@code key}.
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   414
     *         (A {@code null} return can also indicate that the map
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   415
     *         previously associated {@code null} with {@code key}.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * @see     Object#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @see     #get(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * @see     #containsKey(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    public V put(K key, V value) {
25413
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   421
        final Object k = maskNull(key);
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   422
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   423
        retryAfterResize: for (;;) {
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   424
            final Object[] tab = table;
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   425
            final int len = tab.length;
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   426
            int i = hash(k, len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
25413
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   428
            for (Object item; (item = tab[i]) != null;
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   429
                 i = nextKeyIndex(i, len)) {
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   430
                if (item == k) {
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   431
                    @SuppressWarnings("unchecked")
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   432
                        V oldValue = (V) tab[i + 1];
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   433
                    tab[i + 1] = value;
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   434
                    return oldValue;
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   435
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            }
25413
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   437
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   438
            final int s = size + 1;
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   439
            // Use optimized form of 3 * s.
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   440
            // Next capacity is len, 2 * current capacity.
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   441
            if (s + (s << 1) > len && resize(len))
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   442
                continue retryAfterResize;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
25413
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   444
            modCount++;
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   445
            tab[i] = k;
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   446
            tab[i + 1] = value;
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   447
            size = s;
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   448
            return null;
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   449
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    /**
25413
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   453
     * Resizes the table if necessary to hold given capacity.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * @param newCapacity the new capacity, must be a power of two.
25413
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   456
     * @return whether a resize did in fact take place
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     */
25413
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   458
    private boolean resize(int newCapacity) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        // assert (newCapacity & -newCapacity) == newCapacity; // power of 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        int newLength = newCapacity * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        Object[] oldTable = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        int oldLength = oldTable.length;
25413
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   464
        if (oldLength == 2 * MAXIMUM_CAPACITY) { // can't expand any further
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   465
            if (size == MAXIMUM_CAPACITY - 1)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                throw new IllegalStateException("Capacity exhausted.");
25413
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   467
            return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        if (oldLength >= newLength)
25413
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   470
            return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        Object[] newTable = new Object[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        for (int j = 0; j < oldLength; j += 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            Object key = oldTable[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            if (key != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                Object value = oldTable[j+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                oldTable[j] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                oldTable[j+1] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                int i = hash(key, newLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                while (newTable[i] != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                    i = nextKeyIndex(i, newLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                newTable[i] = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                newTable[i + 1] = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        table = newTable;
25413
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   488
        return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * Copies all of the mappings from the specified map to this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * These mappings will replace any mappings that this map had for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * any of the keys currently in the specified map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * @param m mappings to be stored in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * @throws NullPointerException if the specified map is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    public void putAll(Map<? extends K, ? extends V> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        int n = m.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        if (n == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            return;
25413
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   503
        if (n > size)
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
   504
            resize(capacity(n)); // conservatively pre-expand
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        for (Entry<? extends K, ? extends V> e : m.entrySet())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            put(e.getKey(), e.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * Removes the mapping for this key from this map if present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * @param key key whose mapping is to be removed from the map
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   514
     * @return the previous value associated with {@code key}, or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   515
     *         {@code null} if there was no mapping for {@code key}.
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   516
     *         (A {@code null} return can also indicate that the map
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   517
     *         previously associated {@code null} with {@code key}.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    public V remove(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        Object k = maskNull(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        Object[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        int len = tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        int i = hash(k, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            Object item = tab[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            if (item == k) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                size--;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   530
                @SuppressWarnings("unchecked")
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   531
                    V oldValue = (V) tab[i + 1];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                tab[i + 1] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                tab[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                closeDeletion(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            if (item == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            i = nextKeyIndex(i, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * Removes the specified key-value mapping from the map if it is present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * @param   key   possible key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * @param   value possible value
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   548
     * @return  {@code true} if and only if the specified key-value
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     *          mapping was in the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    private boolean removeMapping(Object key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        Object k = maskNull(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        Object[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        int len = tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        int i = hash(k, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            Object item = tab[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            if (item == k) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                if (tab[i + 1] != value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                size--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                tab[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                tab[i + 1] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                closeDeletion(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            if (item == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            i = nextKeyIndex(i, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * Rehash all possibly-colliding entries following a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * deletion. This preserves the linear-probe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * collision properties required by get, put, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * @param d the index of a newly empty deleted slot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    private void closeDeletion(int d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        // Adapted from Knuth Section 6.4 Algorithm R
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        Object[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        int len = tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        // Look for items to swap into newly vacated slot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        // starting at index immediately following deletion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        // and continuing until a null slot is seen, indicating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        // the end of a run of possibly-colliding keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        Object item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        for (int i = nextKeyIndex(d, len); (item = tab[i]) != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
             i = nextKeyIndex(i, len) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            // The following test triggers if the item at slot i (which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            // hashes to be at slot r) should take the spot vacated by d.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            // If so, we swap it in, and then continue with d now at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            // newly vacated i.  This process will terminate when we hit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            // the null slot at the end of this run.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            // The test is messy because we are using a circular table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            int r = hash(item, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            if ((i < r && (r <= d || d <= i)) || (r <= d && d <= i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                tab[d] = item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                tab[d + 1] = tab[i + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                tab[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                tab[i + 1] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                d = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * Removes all of the mappings from this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * The map will be empty after this call returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        Object[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        for (int i = 0; i < tab.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            tab[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * Compares the specified object with this map for equality.  Returns
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   625
     * {@code true} if the given object is also a map and the two maps
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * represent identical object-reference mappings.  More formally, this
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   627
     * map is equal to another map {@code m} if and only if
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   628
     * {@code this.entrySet().equals(m.entrySet())}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * <p><b>Owing to the reference-equality-based semantics of this map it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * possible that the symmetry and transitivity requirements of the
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   632
     * {@code Object.equals} contract may be violated if this map is compared
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   633
     * to a normal map.  However, the {@code Object.equals} contract is
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   634
     * guaranteed to hold among {@code IdentityHashMap} instances.</b>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * @param  o object to be compared for equality with this map
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   637
     * @return {@code true} if the specified object is equal to this map
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * @see Object#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
    public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        if (o == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        } else if (o instanceof IdentityHashMap) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   644
            IdentityHashMap<?,?> m = (IdentityHashMap<?,?>) o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            if (m.size() != size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            Object[] tab = m.table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            for (int i = 0; i < tab.length; i+=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                Object k = tab[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                if (k != null && !containsMapping(k, tab[i + 1]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        } else if (o instanceof Map) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   656
            Map<?,?> m = (Map<?,?>)o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            return entrySet().equals(m.entrySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            return false;  // o is not a Map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * Returns the hash code value for this map.  The hash code of a map is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * defined to be the sum of the hash codes of each entry in the map's
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   666
     * {@code entrySet()} view.  This ensures that {@code m1.equals(m2)}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   667
     * implies that {@code m1.hashCode()==m2.hashCode()} for any two
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   668
     * {@code IdentityHashMap} instances {@code m1} and {@code m2}, as
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * required by the general contract of {@link Object#hashCode}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * <p><b>Owing to the reference-equality-based semantics of the
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   672
     * {@code Map.Entry} instances in the set returned by this map's
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   673
     * {@code entrySet} method, it is possible that the contractual
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   674
     * requirement of {@code Object.hashCode} mentioned in the previous
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * paragraph will be violated if one of the two objects being compared is
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   676
     * an {@code IdentityHashMap} instance and the other is a normal map.</b>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * @return the hash code value for this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * @see Object#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * @see #equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        int result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        Object[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        for (int i = 0; i < tab.length; i +=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            Object key = tab[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            if (key != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                Object k = unmaskNull(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                result += System.identityHashCode(k) ^
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                          System.identityHashCode(tab[i + 1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * Returns a shallow copy of this identity hash map: the keys and values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * themselves are not cloned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * @return a shallow copy of this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        try {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   704
            IdentityHashMap<?,?> m = (IdentityHashMap<?,?>) super.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            m.entrySet = null;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   706
            m.table = table.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            return m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        } catch (CloneNotSupportedException e) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 9275
diff changeset
   709
            throw new InternalError(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    private abstract class IdentityHashMapIterator<T> implements Iterator<T> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        int index = (size != 0 ? 0 : table.length); // current slot.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        int expectedModCount = modCount; // to support fast-fail
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        int lastReturnedIndex = -1;      // to allow remove()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        boolean indexValid; // To avoid unnecessary next computation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        Object[] traversalTable = table; // reference to main table or copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            Object[] tab = traversalTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            for (int i = index; i < tab.length; i+=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                Object key = tab[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                if (key != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                    index = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                    return indexValid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            index = tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        protected int nextIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            if (modCount != expectedModCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            if (!indexValid && !hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            indexValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            lastReturnedIndex = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            index += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            return lastReturnedIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            if (lastReturnedIndex == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            if (modCount != expectedModCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                throw new ConcurrentModificationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            expectedModCount = ++modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            int deletedSlot = lastReturnedIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            lastReturnedIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            // back up index to revisit new contents after deletion
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            index = deletedSlot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            indexValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            // Removal code proceeds as in closeDeletion except that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            // it must catch the rare case where an element already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            // seen is swapped into a vacant slot that will be later
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            // traversed by this iterator. We cannot allow future
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            // next() calls to return it again.  The likelihood of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            // this occurring under 2/3 load factor is very slim, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            // when it does happen, we must make a copy of the rest of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            // the table to use for the rest of the traversal. Since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            // this can only happen when we are near the end of the table,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            // even in these rare cases, this is not very expensive in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            // time or space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            Object[] tab = traversalTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            int len = tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            int d = deletedSlot;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   774
            Object key = tab[d];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            tab[d] = null;        // vacate the slot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            tab[d + 1] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            // If traversing a copy, remove in real table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            // We can skip gap-closure on copy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            if (tab != IdentityHashMap.this.table) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                IdentityHashMap.this.remove(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                expectedModCount = modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
58
55e9cd9ade0b 6612102: (coll) IdentityHashMap.iterator().remove() might decrement size twice
martin
parents: 51
diff changeset
   786
            size--;
55e9cd9ade0b 6612102: (coll) IdentityHashMap.iterator().remove() might decrement size twice
martin
parents: 51
diff changeset
   787
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            Object item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            for (int i = nextKeyIndex(d, len); (item = tab[i]) != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                 i = nextKeyIndex(i, len)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                int r = hash(item, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                // See closeDeletion for explanation of this conditional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                if ((i < r && (r <= d || d <= i)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                    (r <= d && d <= i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                    // If we are about to swap an already-seen element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                    // into a slot that may later be returned by next(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                    // then clone the rest of table for use in future
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                    // next() calls. It is OK that our copy will have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                    // a gap in the "wrong" place, since it will never
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                    // be used for searching anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                    if (i < deletedSlot && d >= deletedSlot &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                        traversalTable == IdentityHashMap.this.table) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                        int remaining = len - deletedSlot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                        Object[] newTable = new Object[remaining];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                        System.arraycopy(tab, deletedSlot,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                                         newTable, 0, remaining);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                        traversalTable = newTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                        index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                    tab[d] = item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                    tab[d + 1] = tab[i + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                    tab[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                    tab[i + 1] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                    d = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    private class KeyIterator extends IdentityHashMapIterator<K> {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   824
        @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        public K next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            return (K) unmaskNull(traversalTable[nextIndex()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    private class ValueIterator extends IdentityHashMapIterator<V> {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   831
        @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        public V next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            return (V) traversalTable[nextIndex() + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    private class EntryIterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        extends IdentityHashMapIterator<Map.Entry<K,V>>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    {
23746
ce60f7b62312 8035284: Remove redundant null initialization
mduigou
parents: 22078
diff changeset
   840
        private Entry lastReturnedEntry;
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   841
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        public Map.Entry<K,V> next() {
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   843
            lastReturnedEntry = new Entry(nextIndex());
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   844
            return lastReturnedEntry;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   847
        public void remove() {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   848
            lastReturnedIndex =
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   849
                ((null == lastReturnedEntry) ? -1 : lastReturnedEntry.index);
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   850
            super.remove();
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   851
            lastReturnedEntry.index = lastReturnedIndex;
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   852
            lastReturnedEntry = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   855
        private class Entry implements Map.Entry<K,V> {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   856
            private int index;
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   857
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   858
            private Entry(int index) {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   859
                this.index = index;
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   860
            }
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   861
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   862
            @SuppressWarnings("unchecked")
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   863
            public K getKey() {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   864
                checkIndexForEntryUse();
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   865
                return (K) unmaskNull(traversalTable[index]);
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   866
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   868
            @SuppressWarnings("unchecked")
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   869
            public V getValue() {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   870
                checkIndexForEntryUse();
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   871
                return (V) traversalTable[index+1];
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   872
            }
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   873
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   874
            @SuppressWarnings("unchecked")
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   875
            public V setValue(V value) {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   876
                checkIndexForEntryUse();
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   877
                V oldValue = (V) traversalTable[index+1];
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   878
                traversalTable[index+1] = value;
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   879
                // if shadowing, force into main table
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   880
                if (traversalTable != IdentityHashMap.this.table)
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   881
                    put((K) traversalTable[index], value);
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   882
                return oldValue;
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   883
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   885
            public boolean equals(Object o) {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   886
                if (index < 0)
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   887
                    return super.equals(o);
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   888
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   889
                if (!(o instanceof Map.Entry))
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   890
                    return false;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
   891
                Map.Entry<?,?> e = (Map.Entry<?,?>)o;
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   892
                return (e.getKey() == unmaskNull(traversalTable[index]) &&
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   893
                       e.getValue() == traversalTable[index+1]);
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   894
            }
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   895
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   896
            public int hashCode() {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   897
                if (lastReturnedIndex < 0)
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   898
                    return super.hashCode();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   900
                return (System.identityHashCode(unmaskNull(traversalTable[index])) ^
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   901
                       System.identityHashCode(traversalTable[index+1]));
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   902
            }
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   903
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   904
            public String toString() {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   905
                if (index < 0)
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   906
                    return super.toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   908
                return (unmaskNull(traversalTable[index]) + "="
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   909
                        + traversalTable[index+1]);
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   910
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
9235
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   912
            private void checkIndexForEntryUse() {
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   913
                if (index < 0)
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   914
                    throw new IllegalStateException("Entry was removed");
ddd556c97e6c 6312706: Map entrySet iterators should return different entries on each call to next()
mduigou
parents: 7803
diff changeset
   915
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
    // Views
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * This field is initialized to contain an instance of the entry set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * view the first time this view is requested.  The view is stateless,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * so there's no reason to create more than one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     */
23746
ce60f7b62312 8035284: Remove redundant null initialization
mduigou
parents: 22078
diff changeset
   926
    private transient Set<Map.Entry<K,V>> entrySet;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * Returns an identity-based set view of the keys contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * The set is backed by the map, so changes to the map are reflected in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * the set, and vice-versa.  If the map is modified while an iteration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * over the set is in progress, the results of the iteration are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * undefined.  The set supports element removal, which removes the
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   934
     * corresponding mapping from the map, via the {@code Iterator.remove},
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   935
     * {@code Set.remove}, {@code removeAll}, {@code retainAll}, and
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   936
     * {@code clear} methods.  It does not support the {@code add} or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   937
     * {@code addAll} methods.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * <p><b>While the object returned by this method implements the
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   940
     * {@code Set} interface, it does <i>not</i> obey {@code Set's} general
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * contract.  Like its backing map, the set returned by this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * defines element equality as reference-equality rather than
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   943
     * object-equality.  This affects the behavior of its {@code contains},
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   944
     * {@code remove}, {@code containsAll}, {@code equals}, and
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   945
     * {@code hashCode} methods.</b>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   947
     * <p><b>The {@code equals} method of the returned set returns {@code true}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * only if the specified object is a set containing exactly the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * object references as the returned set.  The symmetry and transitivity
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   950
     * requirements of the {@code Object.equals} contract may be violated if
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     * the set returned by this method is compared to a normal set.  However,
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   952
     * the {@code Object.equals} contract is guaranteed to hold among sets
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     * returned by this method.</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   955
     * <p>The {@code hashCode} method of the returned set returns the sum of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * the <i>identity hashcodes</i> of the elements in the set, rather than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * the sum of their hashcodes.  This is mandated by the change in the
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   958
     * semantics of the {@code equals} method, in order to enforce the
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
   959
     * general contract of the {@code Object.hashCode} method among sets
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     * returned by this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * @return an identity-based set view of the keys contained in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * @see Object#equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * @see System#identityHashCode(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
    public Set<K> keySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        Set<K> ks = keySet;
34712
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   968
        if (ks == null) {
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   969
            ks = new KeySet();
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   970
            keySet = ks;
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   971
        }
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   972
        return ks;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
    private class KeySet extends AbstractSet<K> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        public Iterator<K> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            return new KeyIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            return containsKey(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            int oldSize = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            IdentityHashMap.this.remove(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
            return size != oldSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
         * Must revert from AbstractSet's impl to AbstractCollection's, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
         * the former contains an optimization that results in incorrect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
         * behavior when c is a smaller "normal" (non-identity-based) Set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        public boolean removeAll(Collection<?> c) {
19855
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 19208
diff changeset
   996
            Objects.requireNonNull(c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            boolean modified = false;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   998
            for (Iterator<K> i = iterator(); i.hasNext(); ) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                if (c.contains(i.next())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                    i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                    modified = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            return modified;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            IdentityHashMap.this.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            int result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            for (K key : this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                result += System.identityHashCode(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        }
15997
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1015
        public Object[] toArray() {
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1016
            return toArray(new Object[0]);
15997
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1017
        }
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1018
        @SuppressWarnings("unchecked")
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1019
        public <T> T[] toArray(T[] a) {
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1020
            int expectedModCount = modCount;
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1021
            int size = size();
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1022
            if (a.length < size)
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1023
                a = (T[]) Array.newInstance(a.getClass().getComponentType(), size);
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1024
            Object[] tab = table;
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1025
            int ti = 0;
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1026
            for (int si = 0; si < tab.length; si += 2) {
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1027
                Object key;
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1028
                if ((key = tab[si]) != null) { // key present ?
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1029
                    // more elements than expected -> concurrent modification from other thread
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1030
                    if (ti >= size) {
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1031
                        throw new ConcurrentModificationException();
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1032
                    }
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1033
                    a[ti++] = (T) unmaskNull(key); // unmask key
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1034
                }
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1035
            }
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1036
            // fewer elements than expected or concurrent modification from other thread detected
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1037
            if (ti < size || expectedModCount != modCount) {
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1038
                throw new ConcurrentModificationException();
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1039
            }
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1040
            // final null marker as per spec
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1041
            if (ti < a.length) {
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1042
                a[ti] = null;
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1043
            }
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1044
            return a;
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1045
        }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1046
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1047
        public Spliterator<K> spliterator() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1048
            return new KeySpliterator<>(IdentityHashMap.this, 0, -1, 0, 0);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1049
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * Returns a {@link Collection} view of the values contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * The collection is backed by the map, so changes to the map are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * reflected in the collection, and vice-versa.  If the map is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * modified while an iteration over the collection is in progress,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * the results of the iteration are undefined.  The collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * supports element removal, which removes the corresponding
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
  1059
     * mapping from the map, via the {@code Iterator.remove},
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
  1060
     * {@code Collection.remove}, {@code removeAll},
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
  1061
     * {@code retainAll} and {@code clear} methods.  It does not
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
  1062
     * support the {@code add} or {@code addAll} methods.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     * <p><b>While the object returned by this method implements the
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
  1065
     * {@code Collection} interface, it does <i>not</i> obey
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
  1066
     * {@code Collection's} general contract.  Like its backing map,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * the collection returned by this method defines element equality as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * reference-equality rather than object-equality.  This affects the
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
  1069
     * behavior of its {@code contains}, {@code remove} and
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
  1070
     * {@code containsAll} methods.</b>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
    public Collection<V> values() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        Collection<V> vs = values;
34712
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
  1074
        if (vs == null) {
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
  1075
            vs = new Values();
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
  1076
            values = vs;
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
  1077
        }
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
  1078
        return vs;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
    private class Values extends AbstractCollection<V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        public Iterator<V> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
            return new ValueIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
            return containsValue(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        public boolean remove(Object o) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1092
            for (Iterator<V> i = iterator(); i.hasNext(); ) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
                if (i.next() == o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
                    i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
            IdentityHashMap.this.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        }
15997
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1103
        public Object[] toArray() {
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1104
            return toArray(new Object[0]);
15997
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1105
        }
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1106
        @SuppressWarnings("unchecked")
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1107
        public <T> T[] toArray(T[] a) {
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1108
            int expectedModCount = modCount;
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1109
            int size = size();
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1110
            if (a.length < size)
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1111
                a = (T[]) Array.newInstance(a.getClass().getComponentType(), size);
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1112
            Object[] tab = table;
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1113
            int ti = 0;
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1114
            for (int si = 0; si < tab.length; si += 2) {
16042
0bf6469a1cfb 8008785: IdentityHashMap.values().toArray(V[]) broken by JDK-8008167
mduigou
parents: 15997
diff changeset
  1115
                if (tab[si] != null) { // key present ?
15997
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1116
                    // more elements than expected -> concurrent modification from other thread
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1117
                    if (ti >= size) {
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1118
                        throw new ConcurrentModificationException();
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1119
                    }
16042
0bf6469a1cfb 8008785: IdentityHashMap.values().toArray(V[]) broken by JDK-8008167
mduigou
parents: 15997
diff changeset
  1120
                    a[ti++] = (T) tab[si+1]; // copy value
15997
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1121
                }
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1122
            }
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1123
            // fewer elements than expected or concurrent modification from other thread detected
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1124
            if (ti < size || expectedModCount != modCount) {
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1125
                throw new ConcurrentModificationException();
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1126
            }
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1127
            // final null marker as per spec
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1128
            if (ti < a.length) {
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1129
                a[ti] = null;
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1130
            }
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1131
            return a;
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1132
        }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1133
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1134
        public Spliterator<V> spliterator() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1135
            return new ValueSpliterator<>(IdentityHashMap.this, 0, -1, 0, 0);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1136
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     * Returns a {@link Set} view of the mappings contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     * Each element in the returned set is a reference-equality-based
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
  1142
     * {@code Map.Entry}.  The set is backed by the map, so changes
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * to the map are reflected in the set, and vice-versa.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * map is modified while an iteration over the set is in progress,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     * the results of the iteration are undefined.  The set supports
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     * element removal, which removes the corresponding mapping from
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
  1147
     * the map, via the {@code Iterator.remove}, {@code Set.remove},
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
  1148
     * {@code removeAll}, {@code retainAll} and {@code clear}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
  1149
     * methods.  It does not support the {@code add} or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
  1150
     * {@code addAll} methods.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
  1152
     * <p>Like the backing map, the {@code Map.Entry} objects in the set
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     * returned by this method define key and value equality as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     * reference-equality rather than object-equality.  This affects the
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
  1155
     * behavior of the {@code equals} and {@code hashCode} methods of these
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
  1156
     * {@code Map.Entry} objects.  A reference-equality based {@code Map.Entry
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
  1157
     * e} is equal to an object {@code o} if and only if {@code o} is a
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
  1158
     * {@code Map.Entry} and {@code e.getKey()==o.getKey() &&
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
  1159
     * e.getValue()==o.getValue()}.  To accommodate these equals
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
  1160
     * semantics, the {@code hashCode} method returns
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
  1161
     * {@code System.identityHashCode(e.getKey()) ^
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
  1162
     * System.identityHashCode(e.getValue())}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     * <p><b>Owing to the reference-equality-based semantics of the
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
  1165
     * {@code Map.Entry} instances in the set returned by this method,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     * it is possible that the symmetry and transitivity requirements of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     * the {@link Object#equals(Object)} contract may be violated if any of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     * the entries in the set is compared to a normal map entry, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     * the set returned by this method is compared to a set of normal map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * entries (such as would be returned by a call to this method on a normal
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
  1171
     * map).  However, the {@code Object.equals} contract is guaranteed to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * hold among identity-based map entries, and among sets of such entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     * </b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     * @return a set view of the identity-mappings contained in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
    public Set<Map.Entry<K,V>> entrySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        Set<Map.Entry<K,V>> es = entrySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        if (es != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
            return es;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
            return entrySet = new EntrySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
    private class EntrySet extends AbstractSet<Map.Entry<K,V>> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        public Iterator<Map.Entry<K,V>> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
            return new EntryIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
            if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
                return false;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
  1192
            Map.Entry<?,?> entry = (Map.Entry<?,?>)o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
            return containsMapping(entry.getKey(), entry.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
            if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
                return false;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
  1198
            Map.Entry<?,?> entry = (Map.Entry<?,?>)o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
            return removeMapping(entry.getKey(), entry.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
            IdentityHashMap.this.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
         * Must revert from AbstractSet's impl to AbstractCollection's, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
         * the former contains an optimization that results in incorrect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
         * behavior when c is a smaller "normal" (non-identity-based) Set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
        public boolean removeAll(Collection<?> c) {
19855
bfe130545fe0 8021591: Additional explicit null checks
mduigou
parents: 19208
diff changeset
  1213
            Objects.requireNonNull(c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
            boolean modified = false;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1215
            for (Iterator<Map.Entry<K,V>> i = iterator(); i.hasNext(); ) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
                if (c.contains(i.next())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
                    i.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
                    modified = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
            return modified;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        public Object[] toArray() {
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1225
            return toArray(new Object[0]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
        @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        public <T> T[] toArray(T[] a) {
15997
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1230
            int expectedModCount = modCount;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
            int size = size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
            if (a.length < size)
15997
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1233
                a = (T[]) Array.newInstance(a.getClass().getComponentType(), size);
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1234
            Object[] tab = table;
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1235
            int ti = 0;
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1236
            for (int si = 0; si < tab.length; si += 2) {
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1237
                Object key;
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1238
                if ((key = tab[si]) != null) { // key present ?
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1239
                    // more elements than expected -> concurrent modification from other thread
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1240
                    if (ti >= size) {
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1241
                        throw new ConcurrentModificationException();
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1242
                    }
21655
55f32ae4f920 8028229: Fix more raw types lint warning in core libraries
darcy
parents: 19855
diff changeset
  1243
                    a[ti++] = (T) new AbstractMap.SimpleEntry<>(unmaskNull(key), tab[si + 1]);
15997
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1244
                }
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1245
            }
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1246
            // fewer elements than expected or concurrent modification from other thread detected
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1247
            if (ti < size || expectedModCount != modCount) {
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1248
                throw new ConcurrentModificationException();
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1249
            }
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1250
            // final null marker as per spec
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1251
            if (ti < a.length) {
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1252
                a[ti] = null;
39590b63ec4c 8008167: IdentityHashMap.[keySet|values|entrySet].toArray speed-up
mduigou
parents: 14342
diff changeset
  1253
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
        }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1256
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1257
        public Spliterator<Map.Entry<K,V>> spliterator() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1258
            return new EntrySpliterator<>(IdentityHashMap.this, 0, -1, 0, 0);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1259
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
    private static final long serialVersionUID = 8188218128353913216L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
  1266
     * Saves the state of the {@code IdentityHashMap} instance to a stream
25413
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
  1267
     * (i.e., serializes it).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     * @serialData The <i>size</i> of the HashMap (the number of key-value
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
  1270
     *          mappings) ({@code int}), followed by the key (Object) and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     *          value (Object) for each key-value mapping represented by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     *          IdentityHashMap.  The key-value mappings are emitted in no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     *          particular order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
    private void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        throws java.io.IOException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
        // Write out and any hidden stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
        // Write out size (number of Mappings)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        s.writeInt(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
        // Write out keys and values (alternating)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
        Object[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
        for (int i = 0; i < tab.length; i += 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
            Object key = tab[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
            if (key != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
                s.writeObject(unmaskNull(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
                s.writeObject(tab[i + 1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 25859
diff changeset
  1295
     * Reconstitutes the {@code IdentityHashMap} instance from a stream (i.e.,
25413
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
  1296
     * deserializes it).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
        throws java.io.IOException, ClassNotFoundException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
        // Read in any hidden stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        // Read in size (number of Mappings)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
        int size = s.readInt();
25413
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
  1305
        if (size < 0)
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
  1306
            throw new java.io.StreamCorruptedException
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
  1307
                ("Illegal mappings count: " + size);
47423
4fc2a4a29f3d 8174109: Better queuing priorities
smarks
parents: 47216
diff changeset
  1308
        int cap = capacity(size);
4fc2a4a29f3d 8174109: Better queuing priorities
smarks
parents: 47216
diff changeset
  1309
        SharedSecrets.getJavaObjectInputStreamAccess().checkArray(s, Object[].class, cap);
4fc2a4a29f3d 8174109: Better queuing priorities
smarks
parents: 47216
diff changeset
  1310
        init(cap);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
        // Read the keys and values, and put the mappings in the table
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
        for (int i=0; i<size; i++) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
  1314
            @SuppressWarnings("unchecked")
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
  1315
                K key = (K) s.readObject();
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
  1316
            @SuppressWarnings("unchecked")
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
  1317
                V value = (V) s.readObject();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
            putForCreate(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
     * The put method for readObject.  It does not resize the table,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
     * update modCount, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
    private void putForCreate(K key, V value)
25413
bb827716b9b9 6904367: (coll) IdentityHashMap is resized before exceeding the expected maximum size
igerasim
parents: 23746
diff changeset
  1327
        throws java.io.StreamCorruptedException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
    {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 10419
diff changeset
  1329
        Object k = maskNull(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
        Object[] tab = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
        int len = tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
        int i = hash(k, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
        Object item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
        while ( (item = tab[i]) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
            if (item == k)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
                throw new java.io.StreamCorruptedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
            i = nextKeyIndex(i, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
        tab[i] = k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
        tab[i + 1] = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
    }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1343
19208
1e3d351eba80 8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents: 18280
diff changeset
  1344
    @SuppressWarnings("unchecked")
18280
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1345
    @Override
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1346
    public void forEach(BiConsumer<? super K, ? super V> action) {
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1347
        Objects.requireNonNull(action);
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1348
        int expectedModCount = modCount;
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1349
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1350
        Object[] t = table;
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1351
        for (int index = 0; index < t.length; index += 2) {
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1352
            Object k = t[index];
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1353
            if (k != null) {
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1354
                action.accept((K) unmaskNull(k), (V) t[index + 1]);
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1355
            }
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1356
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1357
            if (modCount != expectedModCount) {
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1358
                throw new ConcurrentModificationException();
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1359
            }
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1360
        }
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1361
    }
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1362
19208
1e3d351eba80 8022412: Fixed warnings in java.util root, except for HashMap
lagergren
parents: 18280
diff changeset
  1363
    @SuppressWarnings("unchecked")
18280
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1364
    @Override
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1365
    public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1366
        Objects.requireNonNull(function);
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1367
        int expectedModCount = modCount;
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1368
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1369
        Object[] t = table;
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1370
        for (int index = 0; index < t.length; index += 2) {
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1371
            Object k = t[index];
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1372
            if (k != null) {
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1373
                t[index + 1] = function.apply((K) unmaskNull(k), (V) t[index + 1]);
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1374
            }
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1375
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1376
            if (modCount != expectedModCount) {
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1377
                throw new ConcurrentModificationException();
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1378
            }
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1379
        }
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1380
    }
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 17168
diff changeset
  1381
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1382
    /**
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1383
     * Similar form as array-based Spliterators, but skips blank elements,
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1384
     * and guestimates size as decreasing by half per split.
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1385
     */
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1386
    static class IdentityHashMapSpliterator<K,V> {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1387
        final IdentityHashMap<K,V> map;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1388
        int index;             // current index, modified on advance/split
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1389
        int fence;             // -1 until first use; then one past last index
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1390
        int est;               // size estimate
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1391
        int expectedModCount;  // initialized when fence set
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1392
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1393
        IdentityHashMapSpliterator(IdentityHashMap<K,V> map, int origin,
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1394
                                   int fence, int est, int expectedModCount) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1395
            this.map = map;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1396
            this.index = origin;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1397
            this.fence = fence;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1398
            this.est = est;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1399
            this.expectedModCount = expectedModCount;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1400
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1401
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1402
        final int getFence() { // initialize fence and size on first use
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1403
            int hi;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1404
            if ((hi = fence) < 0) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1405
                est = map.size;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1406
                expectedModCount = map.modCount;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1407
                hi = fence = map.table.length;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1408
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1409
            return hi;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1410
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1411
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1412
        public final long estimateSize() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1413
            getFence(); // force init
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1414
            return (long) est;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1415
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1416
    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1417
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1418
    static final class KeySpliterator<K,V>
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1419
        extends IdentityHashMapSpliterator<K,V>
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1420
        implements Spliterator<K> {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1421
        KeySpliterator(IdentityHashMap<K,V> map, int origin, int fence, int est,
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1422
                       int expectedModCount) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1423
            super(map, origin, fence, est, expectedModCount);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1424
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1425
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1426
        public KeySpliterator<K,V> trySplit() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1427
            int hi = getFence(), lo = index, mid = ((lo + hi) >>> 1) & ~1;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1428
            return (lo >= mid) ? null :
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21655
diff changeset
  1429
                new KeySpliterator<>(map, lo, index = mid, est >>>= 1,
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21655
diff changeset
  1430
                                     expectedModCount);
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1431
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1432
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1433
        @SuppressWarnings("unchecked")
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1434
        public void forEachRemaining(Consumer<? super K> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1435
            if (action == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1436
                throw new NullPointerException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1437
            int i, hi, mc; Object key;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1438
            IdentityHashMap<K,V> m; Object[] a;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1439
            if ((m = map) != null && (a = m.table) != null &&
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1440
                (i = index) >= 0 && (index = hi = getFence()) <= a.length) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1441
                for (; i < hi; i += 2) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1442
                    if ((key = a[i]) != null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1443
                        action.accept((K)unmaskNull(key));
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1444
                }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1445
                if (m.modCount == expectedModCount)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1446
                    return;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1447
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1448
            throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1449
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1450
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1451
        @SuppressWarnings("unchecked")
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1452
        public boolean tryAdvance(Consumer<? super K> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1453
            if (action == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1454
                throw new NullPointerException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1455
            Object[] a = map.table;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1456
            int hi = getFence();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1457
            while (index < hi) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1458
                Object key = a[index];
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1459
                index += 2;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1460
                if (key != null) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1461
                    action.accept((K)unmaskNull(key));
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1462
                    if (map.modCount != expectedModCount)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1463
                        throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1464
                    return true;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1465
                }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1466
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1467
            return false;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1468
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1469
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1470
        public int characteristics() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1471
            return (fence < 0 || est == map.size ? SIZED : 0) | Spliterator.DISTINCT;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1472
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1473
    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1474
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1475
    static final class ValueSpliterator<K,V>
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1476
        extends IdentityHashMapSpliterator<K,V>
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1477
        implements Spliterator<V> {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1478
        ValueSpliterator(IdentityHashMap<K,V> m, int origin, int fence, int est,
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1479
                         int expectedModCount) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1480
            super(m, origin, fence, est, expectedModCount);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1481
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1482
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1483
        public ValueSpliterator<K,V> trySplit() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1484
            int hi = getFence(), lo = index, mid = ((lo + hi) >>> 1) & ~1;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1485
            return (lo >= mid) ? null :
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21655
diff changeset
  1486
                new ValueSpliterator<>(map, lo, index = mid, est >>>= 1,
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21655
diff changeset
  1487
                                       expectedModCount);
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1488
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1489
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1490
        public void forEachRemaining(Consumer<? super V> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1491
            if (action == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1492
                throw new NullPointerException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1493
            int i, hi, mc;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1494
            IdentityHashMap<K,V> m; Object[] a;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1495
            if ((m = map) != null && (a = m.table) != null &&
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1496
                (i = index) >= 0 && (index = hi = getFence()) <= a.length) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1497
                for (; i < hi; i += 2) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1498
                    if (a[i] != null) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1499
                        @SuppressWarnings("unchecked") V v = (V)a[i+1];
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1500
                        action.accept(v);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1501
                    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1502
                }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1503
                if (m.modCount == expectedModCount)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1504
                    return;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1505
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1506
            throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1507
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1508
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1509
        public boolean tryAdvance(Consumer<? super V> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1510
            if (action == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1511
                throw new NullPointerException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1512
            Object[] a = map.table;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1513
            int hi = getFence();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1514
            while (index < hi) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1515
                Object key = a[index];
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1516
                @SuppressWarnings("unchecked") V v = (V)a[index+1];
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1517
                index += 2;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1518
                if (key != null) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1519
                    action.accept(v);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1520
                    if (map.modCount != expectedModCount)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1521
                        throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1522
                    return true;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1523
                }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1524
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1525
            return false;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1526
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1527
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1528
        public int characteristics() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1529
            return (fence < 0 || est == map.size ? SIZED : 0);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1530
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1531
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1532
    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1533
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1534
    static final class EntrySpliterator<K,V>
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1535
        extends IdentityHashMapSpliterator<K,V>
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1536
        implements Spliterator<Map.Entry<K,V>> {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1537
        EntrySpliterator(IdentityHashMap<K,V> m, int origin, int fence, int est,
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1538
                         int expectedModCount) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1539
            super(m, origin, fence, est, expectedModCount);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1540
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1541
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1542
        public EntrySpliterator<K,V> trySplit() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1543
            int hi = getFence(), lo = index, mid = ((lo + hi) >>> 1) & ~1;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1544
            return (lo >= mid) ? null :
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21655
diff changeset
  1545
                new EntrySpliterator<>(map, lo, index = mid, est >>>= 1,
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21655
diff changeset
  1546
                                       expectedModCount);
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1547
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1548
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1549
        public void forEachRemaining(Consumer<? super Map.Entry<K, V>> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1550
            if (action == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1551
                throw new NullPointerException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1552
            int i, hi, mc;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1553
            IdentityHashMap<K,V> m; Object[] a;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1554
            if ((m = map) != null && (a = m.table) != null &&
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1555
                (i = index) >= 0 && (index = hi = getFence()) <= a.length) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1556
                for (; i < hi; i += 2) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1557
                    Object key = a[i];
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1558
                    if (key != null) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1559
                        @SuppressWarnings("unchecked") K k =
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1560
                            (K)unmaskNull(key);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1561
                        @SuppressWarnings("unchecked") V v = (V)a[i+1];
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1562
                        action.accept
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21655
diff changeset
  1563
                            (new AbstractMap.SimpleImmutableEntry<>(k, v));
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1564
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1565
                    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1566
                }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1567
                if (m.modCount == expectedModCount)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1568
                    return;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1569
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1570
            throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1571
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1572
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1573
        public boolean tryAdvance(Consumer<? super Map.Entry<K,V>> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1574
            if (action == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1575
                throw new NullPointerException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1576
            Object[] a = map.table;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1577
            int hi = getFence();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1578
            while (index < hi) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1579
                Object key = a[index];
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1580
                @SuppressWarnings("unchecked") V v = (V)a[index+1];
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1581
                index += 2;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1582
                if (key != null) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1583
                    @SuppressWarnings("unchecked") K k =
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1584
                        (K)unmaskNull(key);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1585
                    action.accept
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21655
diff changeset
  1586
                        (new AbstractMap.SimpleImmutableEntry<>(k, v));
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1587
                    if (map.modCount != expectedModCount)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1588
                        throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1589
                    return true;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1590
                }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1591
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1592
            return false;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1593
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1594
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1595
        public int characteristics() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1596
            return (fence < 0 || est == map.size ? SIZED : 0) | Spliterator.DISTINCT;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1597
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1598
    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16042
diff changeset
  1599
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
}