jdk/src/share/classes/java/util/HashMap.java
author psandoz
Fri, 31 May 2013 10:53:19 +0200
changeset 17949 6c33d8f2601e
parent 17939 bd750ec19d82
child 18166 a24e00a7c5ae
permissions -rw-r--r--
8013649: HashMap spliterator tryAdvance() encounters remaining elements after forEachRemaining() Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
     2
 * Copyright (c) 1997, 2013, 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: 4110
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: 4110
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: 4110
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
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;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
    27
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
    29
import java.lang.reflect.ParameterizedType;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
    30
import java.lang.reflect.Type;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
    31
import java.util.function.Consumer;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
    32
import java.util.function.BiFunction;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
    33
import java.util.function.Function;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Hash table based implementation of the <tt>Map</tt> interface.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * implementation provides all of the optional map operations, and permits
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <tt>null</tt> values and the <tt>null</tt> key.  (The <tt>HashMap</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * class is roughly equivalent to <tt>Hashtable</tt>, except that it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * unsynchronized and permits nulls.)  This class makes no guarantees as to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * the order of the map; in particular, it does not guarantee that the order
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * will remain constant over time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <p>This implementation provides constant-time performance for the basic
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * operations (<tt>get</tt> and <tt>put</tt>), assuming the hash function
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * disperses the elements properly among the buckets.  Iteration over
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * collection views requires time proportional to the "capacity" of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <tt>HashMap</tt> instance (the number of buckets) plus its size (the number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * of key-value mappings).  Thus, it's very important not to set the initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * capacity too high (or the load factor too low) if iteration performance is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * important.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <p>An instance of <tt>HashMap</tt> has two parameters that affect its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * performance: <i>initial capacity</i> and <i>load factor</i>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <i>capacity</i> is the number of buckets in the hash table, and the initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * capacity is simply the capacity at the time the hash table is created.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <i>load factor</i> is a measure of how full the hash table is allowed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * get before its capacity is automatically increased.  When the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * entries in the hash table exceeds the product of the load factor and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * current capacity, the hash table is <i>rehashed</i> (that is, internal data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * structures are rebuilt) so that the hash table has approximately twice the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * number of buckets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <p>As a general rule, the default load factor (.75) offers a good tradeoff
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * between time and space costs.  Higher values decrease the space overhead
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * but increase the lookup cost (reflected in most of the operations of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <tt>HashMap</tt> class, including <tt>get</tt> and <tt>put</tt>).  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * expected number of entries in the map and its load factor should be taken
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * into account when setting its initial capacity, so as to minimize the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * number of rehash operations.  If the initial capacity is greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * than the maximum number of entries divided by the load factor, no
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * rehash operations will ever occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * <p>If many mappings are to be stored in a <tt>HashMap</tt> instance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * creating it with a sufficiently large capacity will allow the mappings to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * be stored more efficiently than letting it perform automatic rehashing as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * needed to grow the table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * <p><strong>Note that this implementation is not synchronized.</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * If multiple threads access a hash map concurrently, and at least one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * the threads modifies the map structurally, it <i>must</i> be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * synchronized externally.  (A structural modification is any operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * that adds or deletes one or more mappings; merely changing the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * associated with a key that an instance already contains is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * structural modification.)  This is typically accomplished by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * synchronizing on some object that naturally encapsulates the map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * If no such object exists, the map should be "wrapped" using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * {@link Collections#synchronizedMap Collections.synchronizedMap}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * method.  This is best done at creation time, to prevent accidental
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * unsynchronized access to the map:<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *   Map m = Collections.synchronizedMap(new HashMap(...));</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * <p>The iterators returned by all of this class's "collection view methods"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * are <i>fail-fast</i>: if the map is structurally modified at any time after
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * the iterator is created, in any way except through the iterator's own
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * <tt>remove</tt> method, the iterator will throw a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * {@link ConcurrentModificationException}.  Thus, in the face of concurrent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * modification, the iterator fails quickly and cleanly, rather than risking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * arbitrary, non-deterministic behavior at an undetermined time in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * future.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * as it is, generally speaking, impossible to make any hard guarantees in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * presence of unsynchronized concurrent modification.  Fail-fast iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * Therefore, it would be wrong to write a program that depended on this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * exception for its correctness: <i>the fail-fast behavior of iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * should be used only to detect bugs.</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * <p>This class is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * @param <K> the type of keys maintained by this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * @param <V> the type of mapped values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * @author  Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * @author  Arthur van Hoff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * @author  Neal Gafter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * @see     Object#hashCode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * @see     Collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * @see     Map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * @see     TreeMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * @see     Hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
public class HashMap<K,V>
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   131
        extends AbstractMap<K,V>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    implements Map<K,V>, Cloneable, Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * The default initial capacity - MUST be a power of two.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   138
    static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * The maximum capacity, used if a higher value is implicitly specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * by either of the constructors with arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * MUST be a power of two <= 1<<30.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    static final int MAXIMUM_CAPACITY = 1 << 30;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * The load factor used when none specified in constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    static final float DEFAULT_LOAD_FACTOR = 0.75f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   153
     * An empty table instance to share when the table is not inflated.
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   154
     */
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   155
    static final Object[] EMPTY_TABLE = {};
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   156
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   157
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * The table, resized as necessary. Length MUST Always be a power of two.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   160
    transient Object[] table = EMPTY_TABLE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * The number of key-value mappings contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    transient int size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * The next size value at which to resize (capacity * load factor).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   171
    // If table == EMPTY_TABLE then this is the initial capacity at which the
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   172
    // table will be created when inflated.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    int threshold;
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 load factor for the hash table.
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
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    final float loadFactor;
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 times this HashMap has been structurally modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Structural modifications are those that change the number of mappings in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * the HashMap or otherwise modify its internal structure (e.g.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * rehash).  This field is used to make iterators on Collection-views of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * the HashMap fail-fast.  (See ConcurrentModificationException).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
65
51fc1d79463f 6625725: (coll) modCount should not be volatile
martin
parents: 2
diff changeset
   189
    transient int modCount;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   191
    /**
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   192
     * Holds values which can't be initialized until after VM is booted.
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   193
     */
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   194
    private static class Holder {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   195
        static final sun.misc.Unsafe UNSAFE;
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   196
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   197
        /**
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   198
         * Offset of "final" hashSeed field we must set in
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   199
         * readObject() method.
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   200
         */
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   201
        static final long HASHSEED_OFFSET;
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   202
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   203
        static final boolean USE_HASHSEED;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   204
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   205
        static {
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   206
            String hashSeedProp = java.security.AccessController.doPrivileged(
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   207
                    new sun.security.action.GetPropertyAction(
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   208
                        "jdk.map.useRandomSeed"));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   209
            boolean localBool = (null != hashSeedProp)
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   210
                    ? Boolean.parseBoolean(hashSeedProp) : false;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   211
            USE_HASHSEED = localBool;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   212
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   213
            if (USE_HASHSEED) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   214
                try {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   215
                    UNSAFE = sun.misc.Unsafe.getUnsafe();
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   216
                    HASHSEED_OFFSET = UNSAFE.objectFieldOffset(
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   217
                        HashMap.class.getDeclaredField("hashSeed"));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   218
                } catch (NoSuchFieldException | SecurityException e) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   219
                    throw new InternalError("Failed to record hashSeed offset", e);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   220
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   221
            } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   222
                UNSAFE = null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   223
                HASHSEED_OFFSET = 0;
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   224
            }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   225
        }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   226
    }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   227
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   228
    /*
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   229
     * A randomizing value associated with this instance that is applied to
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   230
     * hash code of keys to make hash collisions harder to find.
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   231
     *
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   232
     * Non-final so it can be set lazily, but be sure not to set more than once.
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   233
     */
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   234
    transient final int hashSeed;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   235
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   236
    /*
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   237
     * TreeBin/TreeNode code from CHM doesn't handle the null key.  Store the
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   238
     * null key entry here.
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   239
     */
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   240
    transient Entry<K,V> nullKeyEntry = null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   241
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   242
    /*
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   243
     * In order to improve performance under high hash-collision conditions,
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   244
     * HashMap will switch to storing a bin's entries in a balanced tree
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   245
     * (TreeBin) instead of a linked-list once the number of entries in the bin
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   246
     * passes a certain threshold (TreeBin.TREE_THRESHOLD), if at least one of
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   247
     * the keys in the bin implements Comparable.  This technique is borrowed
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   248
     * from ConcurrentHashMap.
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   249
     */
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   250
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   251
    /*
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   252
     * Code based on CHMv8
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   253
     *
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   254
     * Node type for TreeBin
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   255
     */
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   256
    final static class TreeNode<K,V> {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   257
        TreeNode parent;  // red-black tree links
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   258
        TreeNode left;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   259
        TreeNode right;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   260
        TreeNode prev;    // needed to unlink next upon deletion
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   261
        boolean red;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   262
        final HashMap.Entry<K,V> entry;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   263
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   264
        TreeNode(HashMap.Entry<K,V> entry, Object next, TreeNode parent) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   265
            this.entry = entry;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   266
            this.entry.next = next;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   267
            this.parent = parent;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   268
        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   269
    }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   270
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   271
    /**
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   272
     * Returns a Class for the given object of the form "class C
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   273
     * implements Comparable<C>", if one exists, else null.  See the TreeBin
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   274
     * docs, below, for explanation.
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   275
     */
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   276
    static Class<?> comparableClassFor(Object x) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   277
        Class<?> c, s, cmpc; Type[] ts, as; Type t; ParameterizedType p;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   278
        if ((c = x.getClass()) == String.class) // bypass checks
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   279
            return c;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   280
        if ((cmpc = Comparable.class).isAssignableFrom(c)) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   281
            while (cmpc.isAssignableFrom(s = c.getSuperclass()))
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   282
                c = s; // find topmost comparable class
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   283
            if ((ts  = c.getGenericInterfaces()) != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   284
                for (int i = 0; i < ts.length; ++i) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   285
                    if (((t = ts[i]) instanceof ParameterizedType) &&
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   286
                        ((p = (ParameterizedType)t).getRawType() == cmpc) &&
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   287
                        (as = p.getActualTypeArguments()) != null &&
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   288
                        as.length == 1 && as[0] == c) // type arg is c
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   289
                        return c;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   290
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   291
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   292
        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   293
        return null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   294
    }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   295
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   296
    /*
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   297
     * Code based on CHMv8
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   298
     *
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   299
     * A specialized form of red-black tree for use in bins
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   300
     * whose size exceeds a threshold.
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   301
     *
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   302
     * TreeBins use a special form of comparison for search and
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   303
     * related operations (which is the main reason we cannot use
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   304
     * existing collections such as TreeMaps). TreeBins contain
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   305
     * Comparable elements, but may contain others, as well as
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   306
     * elements that are Comparable but not necessarily Comparable<T>
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   307
     * for the same T, so we cannot invoke compareTo among them. To
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   308
     * handle this, the tree is ordered primarily by hash value, then
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   309
     * by Comparable.compareTo order if applicable.  On lookup at a
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   310
     * node, if elements are not comparable or compare as 0 then both
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   311
     * left and right children may need to be searched in the case of
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   312
     * tied hash values. (This corresponds to the full list search
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   313
     * that would be necessary if all elements were non-Comparable and
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   314
     * had tied hashes.)  The red-black balancing code is updated from
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   315
     * pre-jdk-collections
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   316
     * (http://gee.cs.oswego.edu/dl/classes/collections/RBCell.java)
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   317
     * based in turn on Cormen, Leiserson, and Rivest "Introduction to
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   318
     * Algorithms" (CLR).
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   319
     */
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   320
    final class TreeBin {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   321
        /*
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   322
         * The bin count threshold for using a tree rather than list for a bin. The
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   323
         * value reflects the approximate break-even point for using tree-based
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   324
         * operations.
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   325
         */
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   326
        static final int TREE_THRESHOLD = 16;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   327
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   328
        TreeNode<K,V> root;  // root of tree
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   329
        TreeNode<K,V> first; // head of next-pointer list
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   330
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   331
        /*
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   332
         * Split a TreeBin into lo and hi parts and install in given table.
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   333
         *
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   334
         * Existing Entrys are re-used, which maintains the before/after links for
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   335
         * LinkedHashMap.Entry.
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   336
         *
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   337
         * No check for Comparable, though this is the same as CHM.
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   338
         */
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   339
        final void splitTreeBin(Object[] newTable, int i, TreeBin loTree, TreeBin hiTree) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   340
            TreeBin oldTree = this;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   341
            int bit = newTable.length >>> 1;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   342
            int loCount = 0, hiCount = 0;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   343
            TreeNode<K,V> e = oldTree.first;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   344
            TreeNode<K,V> next;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   345
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   346
            // This method is called when the table has just increased capacity,
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   347
            // so indexFor() is now taking one additional bit of hash into
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   348
            // account ("bit").  Entries in this TreeBin now belong in one of
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   349
            // two bins, "i" or "i+bit", depending on if the new top bit of the
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   350
            // hash is set.  The trees for the two bins are loTree and hiTree.
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   351
            // If either tree ends up containing fewer than TREE_THRESHOLD
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   352
            // entries, it is converted back to a linked list.
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   353
            while (e != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   354
                // Save entry.next - it will get overwritten in putTreeNode()
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   355
                next = (TreeNode<K,V>)e.entry.next;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   356
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   357
                int h = e.entry.hash;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   358
                K k = (K) e.entry.key;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   359
                V v = e.entry.value;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   360
                if ((h & bit) == 0) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   361
                    ++loCount;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   362
                    // Re-using e.entry
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   363
                    loTree.putTreeNode(h, k, v, e.entry);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   364
                } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   365
                    ++hiCount;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   366
                    hiTree.putTreeNode(h, k, v, e.entry);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   367
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   368
                // Iterate using the saved 'next'
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   369
                e = next;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   370
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   371
            if (loCount < TREE_THRESHOLD) { // too small, convert back to list
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   372
                HashMap.Entry loEntry = null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   373
                TreeNode<K,V> p = loTree.first;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   374
                while (p != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   375
                    @SuppressWarnings("unchecked")
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   376
                    TreeNode<K,V> savedNext = (TreeNode<K,V>) p.entry.next;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   377
                    p.entry.next = loEntry;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   378
                    loEntry = p.entry;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   379
                    p = savedNext;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   380
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   381
                // assert newTable[i] == null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   382
                newTable[i] = loEntry;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   383
            } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   384
                // assert newTable[i] == null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   385
                newTable[i] = loTree;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   386
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   387
            if (hiCount < TREE_THRESHOLD) { // too small, convert back to list
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   388
                HashMap.Entry hiEntry = null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   389
                TreeNode<K,V> p = hiTree.first;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   390
                while (p != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   391
                    @SuppressWarnings("unchecked")
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   392
                    TreeNode<K,V> savedNext = (TreeNode<K,V>) p.entry.next;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   393
                    p.entry.next = hiEntry;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   394
                    hiEntry = p.entry;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   395
                    p = savedNext;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   396
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   397
                // assert newTable[i + bit] == null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   398
                newTable[i + bit] = hiEntry;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   399
            } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   400
                // assert newTable[i + bit] == null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   401
                newTable[i + bit] = hiTree;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   402
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   403
        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   404
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   405
        /*
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   406
         * Popuplate the TreeBin with entries from the linked list e
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   407
         *
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   408
         * Assumes 'this' is a new/empty TreeBin
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   409
         *
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   410
         * Note: no check for Comparable
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   411
         * Note: I believe this changes iteration order
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   412
         */
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   413
        @SuppressWarnings("unchecked")
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   414
        void populate(HashMap.Entry e) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   415
            // assert root == null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   416
            // assert first == null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   417
            HashMap.Entry next;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   418
            while (e != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   419
                // Save entry.next - it will get overwritten in putTreeNode()
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   420
                next = (HashMap.Entry)e.next;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   421
                // Re-using Entry e will maintain before/after in LinkedHM
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   422
                putTreeNode(e.hash, (K)e.key, (V)e.value, e);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   423
                // Iterate using the saved 'next'
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   424
                e = next;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   425
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   426
        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   427
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   428
        /**
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   429
         * Copied from CHMv8
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   430
         * From CLR
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   431
         */
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   432
        private void rotateLeft(TreeNode p) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   433
            if (p != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   434
                TreeNode r = p.right, pp, rl;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   435
                if ((rl = p.right = r.left) != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   436
                    rl.parent = p;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   437
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   438
                if ((pp = r.parent = p.parent) == null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   439
                    root = r;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   440
                } else if (pp.left == p) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   441
                    pp.left = r;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   442
                } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   443
                    pp.right = r;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   444
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   445
                r.left = p;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   446
                p.parent = r;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   447
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   448
        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   449
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   450
        /**
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   451
         * Copied from CHMv8
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   452
         * From CLR
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   453
         */
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   454
        private void rotateRight(TreeNode p) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   455
            if (p != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   456
                TreeNode l = p.left, pp, lr;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   457
                if ((lr = p.left = l.right) != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   458
                    lr.parent = p;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   459
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   460
                if ((pp = l.parent = p.parent) == null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   461
                    root = l;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   462
                } else if (pp.right == p) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   463
                    pp.right = l;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   464
                } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   465
                    pp.left = l;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   466
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   467
                l.right = p;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   468
                p.parent = l;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   469
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   470
        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   471
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   472
        /**
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   473
         * Returns the TreeNode (or null if not found) for the given
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   474
         * key.  A front-end for recursive version.
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   475
         */
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   476
        final TreeNode getTreeNode(int h, K k) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   477
            return getTreeNode(h, k, root, comparableClassFor(k));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   478
        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   479
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   480
        /**
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   481
         * Returns the TreeNode (or null if not found) for the given key
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   482
         * starting at given root.
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   483
         */
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   484
        @SuppressWarnings("unchecked")
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   485
        final TreeNode getTreeNode (int h, K k, TreeNode p, Class<?> cc) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   486
            // assert k != null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   487
            while (p != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   488
                int dir, ph;  Object pk;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   489
                if ((ph = p.entry.hash) != h)
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   490
                    dir = (h < ph) ? -1 : 1;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   491
                else if ((pk = p.entry.key) == k || k.equals(pk))
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   492
                    return p;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   493
                else if (cc == null || comparableClassFor(pk) != cc ||
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   494
                         (dir = ((Comparable<Object>)k).compareTo(pk)) == 0) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   495
                    // assert pk != null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   496
                    TreeNode r, pl, pr; // check both sides
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   497
                    if ((pr = p.right) != null &&
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   498
                        (r = getTreeNode(h, k, pr, cc)) != null)
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   499
                        return r;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   500
                    else if ((pl = p.left) != null)
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   501
                        dir = -1;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   502
                    else // nothing there
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   503
                        break;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   504
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   505
                p = (dir > 0) ? p.right : p.left;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   506
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   507
            return null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   508
        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   509
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   510
        /*
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   511
         * Finds or adds a node.
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   512
         *
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   513
         * 'entry' should be used to recycle an existing Entry (e.g. in the case
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   514
         * of converting a linked-list bin to a TreeBin).
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   515
         * If entry is null, a new Entry will be created for the new TreeNode
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   516
         *
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   517
         * @return the TreeNode containing the mapping, or null if a new
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   518
         * TreeNode was added
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   519
         */
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   520
        @SuppressWarnings("unchecked")
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   521
        TreeNode putTreeNode(int h, K k, V v, HashMap.Entry<K,V> entry) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   522
            // assert k != null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   523
            //if (entry != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   524
                // assert h == entry.hash;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   525
                // assert k == entry.key;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   526
                // assert v == entry.value;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   527
            // }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   528
            Class<?> cc = comparableClassFor(k);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   529
            TreeNode pp = root, p = null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   530
            int dir = 0;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   531
            while (pp != null) { // find existing node or leaf to insert at
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   532
                int ph;  Object pk;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   533
                p = pp;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   534
                if ((ph = p.entry.hash) != h)
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   535
                    dir = (h < ph) ? -1 : 1;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   536
                else if ((pk = p.entry.key) == k || k.equals(pk))
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   537
                    return p;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   538
                else if (cc == null || comparableClassFor(pk) != cc ||
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   539
                         (dir = ((Comparable<Object>)k).compareTo(pk)) == 0) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   540
                    TreeNode r, pr;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   541
                    if ((pr = p.right) != null &&
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   542
                        (r = getTreeNode(h, k, pr, cc)) != null)
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   543
                        return r;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   544
                    else // continue left
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   545
                        dir = -1;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   546
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   547
                pp = (dir > 0) ? p.right : p.left;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   548
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   549
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   550
            // Didn't find the mapping in the tree, so add it
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   551
            TreeNode f = first;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   552
            TreeNode x;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   553
            if (entry != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   554
                x = new TreeNode(entry, f, p);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   555
            } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   556
                x = new TreeNode(newEntry(h, k, v, null), f, p);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   557
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   558
            first = x;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   559
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   560
            if (p == null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   561
                root = x;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   562
            } else { // attach and rebalance; adapted from CLR
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   563
                TreeNode xp, xpp;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   564
                if (f != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   565
                    f.prev = x;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   566
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   567
                if (dir <= 0) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   568
                    p.left = x;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   569
                } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   570
                    p.right = x;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   571
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   572
                x.red = true;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   573
                while (x != null && (xp = x.parent) != null && xp.red
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   574
                        && (xpp = xp.parent) != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   575
                    TreeNode xppl = xpp.left;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   576
                    if (xp == xppl) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   577
                        TreeNode y = xpp.right;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   578
                        if (y != null && y.red) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   579
                            y.red = false;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   580
                            xp.red = false;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   581
                            xpp.red = true;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   582
                            x = xpp;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   583
                        } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   584
                            if (x == xp.right) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   585
                                rotateLeft(x = xp);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   586
                                xpp = (xp = x.parent) == null ? null : xp.parent;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   587
                            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   588
                            if (xp != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   589
                                xp.red = false;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   590
                                if (xpp != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   591
                                    xpp.red = true;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   592
                                    rotateRight(xpp);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   593
                                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   594
                            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   595
                        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   596
                    } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   597
                        TreeNode y = xppl;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   598
                        if (y != null && y.red) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   599
                            y.red = false;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   600
                            xp.red = false;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   601
                            xpp.red = true;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   602
                            x = xpp;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   603
                        } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   604
                            if (x == xp.left) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   605
                                rotateRight(x = xp);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   606
                                xpp = (xp = x.parent) == null ? null : xp.parent;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   607
                            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   608
                            if (xp != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   609
                                xp.red = false;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   610
                                if (xpp != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   611
                                    xpp.red = true;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   612
                                    rotateLeft(xpp);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   613
                                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   614
                            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   615
                        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   616
                    }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   617
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   618
                TreeNode r = root;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   619
                if (r != null && r.red) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   620
                    r.red = false;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   621
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   622
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   623
            return null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   624
        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   625
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   626
        /*
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   627
         * From CHMv8
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   628
         *
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   629
         * Removes the given node, that must be present before this
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   630
         * call.  This is messier than typical red-black deletion code
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   631
         * because we cannot swap the contents of an interior node
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   632
         * with a leaf successor that is pinned by "next" pointers
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   633
         * that are accessible independently of lock. So instead we
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   634
         * swap the tree linkages.
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   635
         */
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   636
        final void deleteTreeNode(TreeNode p) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   637
            TreeNode next = (TreeNode) p.entry.next; // unlink traversal pointers
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   638
            TreeNode pred = p.prev;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   639
            if (pred == null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   640
                first = next;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   641
            } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   642
                pred.entry.next = next;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   643
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   644
            if (next != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   645
                next.prev = pred;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   646
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   647
            TreeNode replacement;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   648
            TreeNode pl = p.left;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   649
            TreeNode pr = p.right;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   650
            if (pl != null && pr != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   651
                TreeNode s = pr, sl;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   652
                while ((sl = s.left) != null) // find successor
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   653
                {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   654
                    s = sl;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   655
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   656
                boolean c = s.red;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   657
                s.red = p.red;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   658
                p.red = c; // swap colors
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   659
                TreeNode sr = s.right;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   660
                TreeNode pp = p.parent;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   661
                if (s == pr) { // p was s's direct parent
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   662
                    p.parent = s;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   663
                    s.right = p;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   664
                } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   665
                    TreeNode sp = s.parent;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   666
                    if ((p.parent = sp) != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   667
                        if (s == sp.left) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   668
                            sp.left = p;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   669
                        } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   670
                            sp.right = p;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   671
                        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   672
                    }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   673
                    if ((s.right = pr) != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   674
                        pr.parent = s;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   675
                    }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   676
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   677
                p.left = null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   678
                if ((p.right = sr) != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   679
                    sr.parent = p;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   680
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   681
                if ((s.left = pl) != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   682
                    pl.parent = s;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   683
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   684
                if ((s.parent = pp) == null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   685
                    root = s;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   686
                } else if (p == pp.left) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   687
                    pp.left = s;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   688
                } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   689
                    pp.right = s;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   690
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   691
                replacement = sr;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   692
            } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   693
                replacement = (pl != null) ? pl : pr;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   694
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   695
            TreeNode pp = p.parent;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   696
            if (replacement == null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   697
                if (pp == null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   698
                    root = null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   699
                    return;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   700
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   701
                replacement = p;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   702
            } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   703
                replacement.parent = pp;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   704
                if (pp == null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   705
                    root = replacement;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   706
                } else if (p == pp.left) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   707
                    pp.left = replacement;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   708
                } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   709
                    pp.right = replacement;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   710
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   711
                p.left = p.right = p.parent = null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   712
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   713
            if (!p.red) { // rebalance, from CLR
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   714
                TreeNode x = replacement;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   715
                while (x != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   716
                    TreeNode xp, xpl;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   717
                    if (x.red || (xp = x.parent) == null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   718
                        x.red = false;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   719
                        break;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   720
                    }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   721
                    if (x == (xpl = xp.left)) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   722
                        TreeNode sib = xp.right;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   723
                        if (sib != null && sib.red) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   724
                            sib.red = false;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   725
                            xp.red = true;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   726
                            rotateLeft(xp);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   727
                            sib = (xp = x.parent) == null ? null : xp.right;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   728
                        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   729
                        if (sib == null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   730
                            x = xp;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   731
                        } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   732
                            TreeNode sl = sib.left, sr = sib.right;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   733
                            if ((sr == null || !sr.red)
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   734
                                    && (sl == null || !sl.red)) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   735
                                sib.red = true;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   736
                                x = xp;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   737
                            } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   738
                                if (sr == null || !sr.red) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   739
                                    if (sl != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   740
                                        sl.red = false;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   741
                                    }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   742
                                    sib.red = true;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   743
                                    rotateRight(sib);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   744
                                    sib = (xp = x.parent) == null ?
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   745
                                        null : xp.right;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   746
                                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   747
                                if (sib != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   748
                                    sib.red = (xp == null) ? false : xp.red;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   749
                                    if ((sr = sib.right) != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   750
                                        sr.red = false;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   751
                                    }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   752
                                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   753
                                if (xp != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   754
                                    xp.red = false;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   755
                                    rotateLeft(xp);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   756
                                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   757
                                x = root;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   758
                            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   759
                        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   760
                    } else { // symmetric
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   761
                        TreeNode sib = xpl;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   762
                        if (sib != null && sib.red) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   763
                            sib.red = false;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   764
                            xp.red = true;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   765
                            rotateRight(xp);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   766
                            sib = (xp = x.parent) == null ? null : xp.left;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   767
                        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   768
                        if (sib == null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   769
                            x = xp;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   770
                        } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   771
                            TreeNode sl = sib.left, sr = sib.right;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   772
                            if ((sl == null || !sl.red)
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   773
                                    && (sr == null || !sr.red)) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   774
                                sib.red = true;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   775
                                x = xp;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   776
                            } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   777
                                if (sl == null || !sl.red) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   778
                                    if (sr != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   779
                                        sr.red = false;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   780
                                    }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   781
                                    sib.red = true;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   782
                                    rotateLeft(sib);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   783
                                    sib = (xp = x.parent) == null ?
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   784
                                        null : xp.left;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   785
                                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   786
                                if (sib != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   787
                                    sib.red = (xp == null) ? false : xp.red;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   788
                                    if ((sl = sib.left) != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   789
                                        sl.red = false;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   790
                                    }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   791
                                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   792
                                if (xp != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   793
                                    xp.red = false;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   794
                                    rotateRight(xp);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   795
                                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   796
                                x = root;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   797
                            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   798
                        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   799
                    }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   800
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   801
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   802
            if (p == replacement && (pp = p.parent) != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   803
                if (p == pp.left) // detach pointers
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   804
                {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   805
                    pp.left = null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   806
                } else if (p == pp.right) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   807
                    pp.right = null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   808
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   809
                p.parent = null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   810
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   811
        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   812
    }
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   813
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * Constructs an empty <tt>HashMap</tt> with the specified initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * capacity and load factor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * @param  initialCapacity the initial capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * @param  loadFactor      the load factor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * @throws IllegalArgumentException if the initial capacity is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     *         or the load factor is nonpositive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    public HashMap(int initialCapacity, float loadFactor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        if (initialCapacity < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            throw new IllegalArgumentException("Illegal initial capacity: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                                               initialCapacity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        if (initialCapacity > MAXIMUM_CAPACITY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            initialCapacity = MAXIMUM_CAPACITY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        if (loadFactor <= 0 || Float.isNaN(loadFactor))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            throw new IllegalArgumentException("Illegal load factor: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                                               loadFactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        this.loadFactor = loadFactor;
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   833
        threshold = initialCapacity;
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   834
        hashSeed = initHashSeed();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * Constructs an empty <tt>HashMap</tt> with the specified initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * capacity and the default load factor (0.75).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * @param  initialCapacity the initial capacity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * @throws IllegalArgumentException if the initial capacity is negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
    public HashMap(int initialCapacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        this(initialCapacity, DEFAULT_LOAD_FACTOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * Constructs an empty <tt>HashMap</tt> with the default initial capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * (16) and the default load factor (0.75).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    public HashMap() {
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   854
        this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * Constructs a new <tt>HashMap</tt> with the same mappings as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * specified <tt>Map</tt>.  The <tt>HashMap</tt> is created with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * default load factor (0.75) and an initial capacity sufficient to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * hold the mappings in the specified <tt>Map</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * @param   m the map whose mappings are to be placed in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * @throws  NullPointerException if the specified map is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    public HashMap(Map<? extends K, ? extends V> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1,
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   868
                DEFAULT_INITIAL_CAPACITY), DEFAULT_LOAD_FACTOR);
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   869
        inflateTable(threshold);
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   870
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        putAllForCreate(m);
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   872
        // assert size == m.size();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   875
    private static int roundUpToPowerOf2(int number) {
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   876
        // assert number >= 0 : "number must be non-negative";
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   877
        int rounded = number >= MAXIMUM_CAPACITY
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   878
                ? MAXIMUM_CAPACITY
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   879
                : (rounded = Integer.highestOneBit(number)) != 0
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   880
                    ? (Integer.bitCount(number) > 1) ? rounded << 1 : rounded
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   881
                    : 1;
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   882
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   883
        return rounded;
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   884
    }
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   885
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   886
    /**
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   887
     * Inflates the table.
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   888
     */
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   889
    private void inflateTable(int toSize) {
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   890
        // Find a power of 2 >= toSize
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   891
        int capacity = roundUpToPowerOf2(toSize);
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   892
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   893
        threshold = (int) Math.min(capacity * loadFactor, MAXIMUM_CAPACITY + 1);
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   894
        table = new Object[capacity];
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   895
    }
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   896
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
    // internal utilities
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * Initialization hook for subclasses. This method is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * in all constructors and pseudo-constructors (clone, readObject)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     * after HashMap has been initialized but before any entries have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     * been inserted.  (In the absence of this method, readObject would
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * require explicit knowledge of subclasses.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    void init() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
    /**
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   910
     * Return an initial value for the hashSeed, or 0 if the random seed is not
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   911
     * enabled.
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   912
     */
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   913
    final int initHashSeed() {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   914
        if (sun.misc.VM.isBooted() && Holder.USE_HASHSEED) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   915
            return sun.misc.Hashing.randomHashSeed(this);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   916
        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   917
        return 0;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   918
    }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   919
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   920
    /**
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   921
     * Retrieve object hash code and applies a supplemental hash function to the
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   922
     * result hash, which defends against poor quality hash functions. This is
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   923
     * critical because HashMap uses power-of-two length hash tables, that
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * otherwise encounter collisions for hashCodes that do not differ
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   925
     * in lower bits.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     */
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   927
    final int hash(Object k) {
13018
92c86cea72a8 7173919: Minor optimization of hashing methods
mduigou
parents: 12883
diff changeset
   928
        int  h = hashSeed ^ k.hashCode();
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   929
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        // This function ensures that hashCodes that differ only by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        // constant multiples at each bit position have a bounded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        // number of collisions (approximately 8 at default load factor).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        h ^= (h >>> 20) ^ (h >>> 12);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        return h ^ (h >>> 7) ^ (h >>> 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * Returns index for hash code h.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
    static int indexFor(int h, int length) {
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   941
        // assert Integer.bitCount(length) == 1 : "length must be a non-zero power of 2";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        return h & (length-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * Returns the number of key-value mappings in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * @return the number of key-value mappings in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     * Returns <tt>true</tt> if this map contains no key-value mappings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * @return <tt>true</tt> if this map contains no key-value mappings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        return size == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * Returns the value to which the specified key is mapped,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * or {@code null} if this map contains no mapping for the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * <p>More formally, if this map contains a mapping from a key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * {@code k} to a value {@code v} such that {@code (key==null ? k==null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * key.equals(k))}, then this method returns {@code v}; otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     * it returns {@code null}.  (There can be at most one such mapping.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     * <p>A return value of {@code null} does not <i>necessarily</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * indicate that the map contains no mapping for the key; it's also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     * possible that the map explicitly maps the key to {@code null}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * The {@link #containsKey containsKey} operation may be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * distinguish these two cases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * @see #put(Object, Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
   980
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
    public V get(Object key) {
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   982
        Entry<K,V> entry = getEntry(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   984
        return null == entry ? null : entry.getValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   987
    @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   988
    public V getOrDefault(Object key, V defaultValue) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   989
        Entry<K,V> entry = getEntry(key);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   990
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   991
        return (entry == null) ? defaultValue : entry.getValue();
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   992
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   993
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     * Returns <tt>true</tt> if this map contains a mapping for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * specified key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     * @param   key   The key whose presence in this map is to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     * @return <tt>true</tt> if this map contains a mapping for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     * key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
    public boolean containsKey(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        return getEntry(key) != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * Returns the entry associated with the specified key in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * HashMap.  Returns null if the HashMap contains no mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     * for the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  1011
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
    final Entry<K,V> getEntry(Object key) {
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1013
        if (isEmpty()) {
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1014
            return null;
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1015
        }
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1016
        if (key == null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1017
            return nullKeyEntry;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1018
        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1019
        int hash = hash(key);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1020
        int bin = indexFor(hash, table.length);
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1021
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1022
        if (table[bin] instanceof Entry) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1023
            Entry<K,V> e = (Entry<K,V>) table[bin];
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1024
            for (; e != null; e = (Entry<K,V>)e.next) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1025
                Object k;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1026
                if (e.hash == hash &&
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1027
                    ((k = e.key) == key || key.equals(k))) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1028
                    return e;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1029
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1030
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1031
        } else if (table[bin] != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1032
            TreeBin e = (TreeBin)table[bin];
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1033
            TreeNode p = e.getTreeNode(hash, (K)key);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1034
            if (p != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1035
                // assert p.entry.hash == hash && p.entry.key.equals(key);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1036
                return (Entry<K,V>)p.entry;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1037
            } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1038
                return null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1039
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1044
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     * Associates the specified value with the specified key in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     * If the map previously contained a mapping for the key, the old
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     * value is replaced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     * @param key key with which the specified value is to be associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     * @param value value to be associated with the specified key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * @return the previous value associated with <tt>key</tt>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     *         <tt>null</tt> if there was no mapping for <tt>key</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     *         (A <tt>null</tt> return can also indicate that the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     *         previously associated <tt>null</tt> with <tt>key</tt>.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     */
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1057
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
    public V put(K key, V value) {
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1059
        if (table == EMPTY_TABLE) {
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1060
            inflateTable(threshold);
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1061
        }
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1062
       if (key == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
            return putForNullKey(value);
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1064
        int hash = hash(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        int i = indexFor(hash, table.length);
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1066
        boolean checkIfNeedTree = false; // Might we convert bin to a TreeBin?
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1067
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1068
        if (table[i] instanceof Entry) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1069
            // Bin contains ordinary Entries.  Search for key in the linked list
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1070
            // of entries, counting the number of entries.  Only check for
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1071
            // TreeBin conversion if the list size is >= TREE_THRESHOLD.
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1072
            // (The conversion still may not happen if the table gets resized.)
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1073
            int listSize = 0;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1074
            Entry<K,V> e = (Entry<K,V>) table[i];
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1075
            for (; e != null; e = (Entry<K,V>)e.next) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1076
                Object k;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1077
                if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1078
                    V oldValue = e.value;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1079
                    e.value = value;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1080
                    e.recordAccess(this);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1081
                    return oldValue;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1082
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1083
                listSize++;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1084
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1085
            // Didn't find, so fall through and call addEntry() to add the
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1086
            // Entry and check for TreeBin conversion.
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1087
            checkIfNeedTree = listSize >= TreeBin.TREE_THRESHOLD;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1088
        } else if (table[i] != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1089
            TreeBin e = (TreeBin)table[i];
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1090
            TreeNode p = e.putTreeNode(hash, key, value, null);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1091
            if (p == null) { // putTreeNode() added a new node
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1092
                modCount++;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1093
                size++;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1094
                if (size >= threshold) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1095
                    resize(2 * table.length);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1096
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1097
                return null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1098
            } else { // putTreeNode() found an existing node
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1099
                Entry<K,V> pEntry = (Entry<K,V>)p.entry;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1100
                V oldVal = pEntry.value;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1101
                pEntry.value = value;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1102
                pEntry.recordAccess(this);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1103
                return oldVal;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        modCount++;
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1107
        addEntry(hash, key, value, i, checkIfNeedTree);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     * Offloaded version of put for null keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
    private V putForNullKey(V value) {
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1115
        if (nullKeyEntry != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1116
            V oldValue = nullKeyEntry.value;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1117
            nullKeyEntry.value = value;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1118
            nullKeyEntry.recordAccess(this);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1119
            return oldValue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        modCount++;
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1122
        size++; // newEntry() skips size++
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1123
        nullKeyEntry = newEntry(0, null, value, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1127
    private void putForCreateNullKey(V value) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1128
        // Look for preexisting entry for key.  This will never happen for
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1129
        // clone or deserialize.  It will only happen for construction if the
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1130
        // input Map is a sorted map whose ordering is inconsistent w/ equals.
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1131
        if (nullKeyEntry != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1132
            nullKeyEntry.value = value;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1133
        } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1134
            nullKeyEntry = newEntry(0, null, value, null);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1135
            size++;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1136
        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1137
    }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1138
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1139
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     * This method is used instead of put by constructors and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     * pseudoconstructors (clone, readObject).  It does not resize the table,
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1143
     * check for comodification, etc, though it will convert bins to TreeBins
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1144
     * as needed.  It calls createEntry rather than addEntry.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     */
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1146
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
    private void putForCreate(K key, V value) {
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1148
        if (null == key) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1149
            putForCreateNullKey(value);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1150
            return;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1151
        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1152
        int hash = hash(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        int i = indexFor(hash, table.length);
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1154
        boolean checkIfNeedTree = false; // Might we convert bin to a TreeBin?
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
         * Look for preexisting entry for key.  This will never happen for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
         * clone or deserialize.  It will only happen for construction if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
         * input Map is a sorted map whose ordering is inconsistent w/ equals.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
         */
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1161
        if (table[i] instanceof Entry) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1162
            int listSize = 0;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1163
            Entry<K,V> e = (Entry<K,V>) table[i];
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1164
            for (; e != null; e = (Entry<K,V>)e.next) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1165
                Object k;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1166
                if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1167
                    e.value = value;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1168
                    return;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1169
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1170
                listSize++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
            }
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1172
            // Didn't find, fall through to createEntry().
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1173
            // Check for conversion to TreeBin done via createEntry().
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1174
            checkIfNeedTree = listSize >= TreeBin.TREE_THRESHOLD;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1175
        } else if (table[i] != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1176
            TreeBin e = (TreeBin)table[i];
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1177
            TreeNode p = e.putTreeNode(hash, key, value, null);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1178
            if (p != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1179
                p.entry.setValue(value); // Found an existing node, set value
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1180
            } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1181
                size++; // Added a new TreeNode, so update size
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1182
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1183
            // don't need modCount++/check for resize - just return
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1184
            return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1187
        createEntry(hash, key, value, i, checkIfNeedTree);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
    private void putAllForCreate(Map<? extends K, ? extends V> m) {
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 715
diff changeset
  1191
        for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
            putForCreate(e.getKey(), e.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
     * Rehashes the contents of this map into a new array with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
     * larger capacity.  This method is called automatically when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     * number of keys in this map reaches its threshold.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     * If current capacity is MAXIMUM_CAPACITY, this method does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     * resize the map, but sets threshold to Integer.MAX_VALUE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     * This has the effect of preventing future calls.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     * @param newCapacity the new capacity, MUST be a power of two;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     *        must be greater than current capacity unless current
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     *        capacity is MAXIMUM_CAPACITY (in which case value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     *        is irrelevant).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
    void resize(int newCapacity) {
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1210
        Object[] oldTable = table;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        int oldCapacity = oldTable.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
        if (oldCapacity == MAXIMUM_CAPACITY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
            threshold = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1217
        Object[] newTable = new Object[newCapacity];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        transfer(newTable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        table = newTable;
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1220
        threshold = (int)Math.min(newCapacity * loadFactor, MAXIMUM_CAPACITY + 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     * Transfers all entries from current table to newTable.
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1225
     *
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1226
     * Assumes newTable is larger than table
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
     */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  1228
    @SuppressWarnings("unchecked")
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1229
    void transfer(Object[] newTable) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1230
        Object[] src = table;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1231
        // assert newTable.length > src.length : "newTable.length(" +
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1232
        //   newTable.length + ") expected to be > src.length("+src.length+")";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
        int newCapacity = newTable.length;
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1234
        for (int j = 0; j < src.length; j++) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1235
             if (src[j] instanceof Entry) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1236
                // Assume: since wasn't TreeBin before, won't need TreeBin now
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1237
                Entry<K,V> e = (Entry<K,V>) src[j];
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1238
                while (null != e) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1239
                    Entry<K,V> next = (Entry<K,V>)e.next;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1240
                    int i = indexFor(e.hash, newCapacity);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1241
                    e.next = (Entry<K,V>) newTable[i];
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1242
                    newTable[i] = e;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1243
                    e = next;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1244
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1245
            } else if (src[j] != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1246
                TreeBin e = (TreeBin) src[j];
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1247
                TreeBin loTree = new TreeBin();
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1248
                TreeBin hiTree = new TreeBin();
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1249
                e.splitTreeBin(newTable, j, loTree, hiTree);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
        }
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1252
        Arrays.fill(table, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     * Copies all of the mappings from the specified map to this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     * These mappings will replace any mappings that this map had for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     * any of the keys currently in the specified map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
     * @param m mappings to be stored in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     * @throws NullPointerException if the specified map is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
    public void putAll(Map<? extends K, ? extends V> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        int numKeysToBeAdded = m.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        if (numKeysToBeAdded == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1268
        if (table == EMPTY_TABLE) {
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1269
            inflateTable((int) Math.max(numKeysToBeAdded * loadFactor, threshold));
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1270
        }
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1271
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
         * Expand the map if the map if the number of mappings to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
         * is greater than or equal to threshold.  This is conservative; the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
         * obvious condition is (m.size() + size) >= threshold, but this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
         * condition could result in a map with twice the appropriate capacity,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
         * if the keys to be added overlap with the keys already in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
         * By using the conservative calculation, we subject ourself
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
         * to at most one extra resize.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
         */
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1281
        if (numKeysToBeAdded > threshold && table.length < MAXIMUM_CAPACITY) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1282
            resize(table.length * 2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
4110
ac033ba6ede4 6865582: jsr166y - jsr166 maintenance update
dl
parents: 715
diff changeset
  1285
        for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
            put(e.getKey(), e.getValue());
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1287
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
     * Removes the mapping for the specified key from this map if present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
     * @param  key key whose mapping is to be removed from the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
     * @return the previous value associated with <tt>key</tt>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     *         <tt>null</tt> if there was no mapping for <tt>key</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     *         (A <tt>null</tt> return can also indicate that the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
     *         previously associated <tt>null</tt> with <tt>key</tt>.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
    public V remove(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
        Entry<K,V> e = removeEntryForKey(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
        return (e == null ? null : e.value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1303
    // optimized implementations of default methods in Map
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1304
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1305
    @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1306
    public V putIfAbsent(K key, V value) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1307
        if (table == EMPTY_TABLE) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1308
            inflateTable(threshold);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1309
        }
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1310
        if (key == null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1311
            if (nullKeyEntry == null || nullKeyEntry.value == null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1312
                putForNullKey(value);
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1313
                return null;
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1314
            } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1315
                return nullKeyEntry.value;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1316
            }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1317
        }
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1318
        int hash = hash(key);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1319
        int i = indexFor(hash, table.length);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1320
        boolean checkIfNeedTree = false; // Might we convert bin to a TreeBin?
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1321
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1322
        if (table[i] instanceof Entry) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1323
            int listSize = 0;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1324
            Entry<K,V> e = (Entry<K,V>) table[i];
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1325
            for (; e != null; e = (Entry<K,V>)e.next) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1326
                if (e.hash == hash && Objects.equals(e.key, key)) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1327
                    if (e.value != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1328
                        return e.value;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1329
                    }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1330
                    e.value = value;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1331
                    e.recordAccess(this);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1332
                    return null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1333
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1334
                listSize++;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1335
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1336
            // Didn't find, so fall through and call addEntry() to add the
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1337
            // Entry and check for TreeBin conversion.
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1338
            checkIfNeedTree = listSize >= TreeBin.TREE_THRESHOLD;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1339
        } else if (table[i] != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1340
            TreeBin e = (TreeBin)table[i];
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1341
            TreeNode p = e.putTreeNode(hash, key, value, null);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1342
            if (p == null) { // not found, putTreeNode() added a new node
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1343
                modCount++;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1344
                size++;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1345
                if (size >= threshold) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1346
                    resize(2 * table.length);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1347
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1348
                return null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1349
            } else { // putTreeNode() found an existing node
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1350
                Entry<K,V> pEntry = (Entry<K,V>)p.entry;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1351
                V oldVal = pEntry.value;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1352
                if (oldVal == null) { // only replace if maps to null
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1353
                    pEntry.value = value;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1354
                    pEntry.recordAccess(this);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1355
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1356
                return oldVal;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1357
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1358
        }
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1359
        modCount++;
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1360
        addEntry(hash, key, value, i, checkIfNeedTree);
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1361
        return null;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1362
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1363
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1364
    @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1365
    public boolean remove(Object key, Object value) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1366
        if (isEmpty()) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1367
            return false;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1368
        }
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1369
        if (key == null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1370
            if (nullKeyEntry != null &&
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1371
                 Objects.equals(nullKeyEntry.value, value)) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1372
                removeNullKey();
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1373
                return true;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1374
            }
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1375
            return false;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1376
        }
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1377
        int hash = hash(key);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1378
        int i = indexFor(hash, table.length);
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1379
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1380
        if (table[i] instanceof Entry) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1381
            @SuppressWarnings("unchecked")
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1382
            Entry<K,V> prev = (Entry<K,V>) table[i];
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1383
            Entry<K,V> e = prev;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1384
            while (e != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1385
                @SuppressWarnings("unchecked")
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1386
                Entry<K,V> next = (Entry<K,V>) e.next;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1387
                if (e.hash == hash && Objects.equals(e.key, key)) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1388
                    if (!Objects.equals(e.value, value)) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1389
                        return false;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1390
                    }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1391
                    modCount++;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1392
                    size--;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1393
                    if (prev == e)
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1394
                        table[i] = next;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1395
                    else
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1396
                        prev.next = next;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1397
                    e.recordRemoval(this);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1398
                    return true;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1399
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1400
                prev = e;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1401
                e = next;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1402
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1403
        } else if (table[i] != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1404
            TreeBin tb = ((TreeBin) table[i]);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1405
            TreeNode p = tb.getTreeNode(hash, (K)key);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1406
            if (p != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1407
                Entry<K,V> pEntry = (Entry<K,V>)p.entry;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1408
                // assert pEntry.key.equals(key);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1409
                if (Objects.equals(pEntry.value, value)) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1410
                    modCount++;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1411
                    size--;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1412
                    tb.deleteTreeNode(p);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1413
                    pEntry.recordRemoval(this);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1414
                    if (tb.root == null || tb.first == null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1415
                        // assert tb.root == null && tb.first == null :
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1416
                        //         "TreeBin.first and root should both be null";
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1417
                        // TreeBin is now empty, we should blank this bin
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1418
                        table[i] = null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1419
                    }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1420
                    return true;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1421
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1422
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1423
        }
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1424
        return false;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1425
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1426
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1427
    @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1428
    public boolean replace(K key, V oldValue, V newValue) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1429
        if (isEmpty()) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1430
            return false;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1431
        }
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1432
        if (key == null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1433
            if (nullKeyEntry != null &&
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1434
                 Objects.equals(nullKeyEntry.value, oldValue)) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1435
                putForNullKey(newValue);
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1436
                return true;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1437
            }
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1438
            return false;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1439
        }
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1440
        int hash = hash(key);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1441
        int i = indexFor(hash, table.length);
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1442
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1443
        if (table[i] instanceof Entry) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1444
            @SuppressWarnings("unchecked")
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1445
            Entry<K,V> e = (Entry<K,V>) table[i];
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1446
            for (; e != null; e = (Entry<K,V>)e.next) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1447
                if (e.hash == hash && Objects.equals(e.key, key) && Objects.equals(e.value, oldValue)) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1448
                    e.value = newValue;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1449
                    e.recordAccess(this);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1450
                    return true;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1451
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1452
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1453
            return false;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1454
        } else if (table[i] != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1455
            TreeBin tb = ((TreeBin) table[i]);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1456
            TreeNode p = tb.getTreeNode(hash, key);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1457
            if (p != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1458
                Entry<K,V> pEntry = (Entry<K,V>)p.entry;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1459
                // assert pEntry.key.equals(key);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1460
                if (Objects.equals(pEntry.value, oldValue)) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1461
                    pEntry.value = newValue;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1462
                    pEntry.recordAccess(this);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1463
                    return true;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1464
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1465
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1466
        }
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1467
        return false;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1468
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1469
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1470
   @Override
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1471
    public V replace(K key, V value) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1472
        if (isEmpty()) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1473
            return null;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1474
        }
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1475
        if (key == null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1476
            if (nullKeyEntry != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1477
                return putForNullKey(value);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1478
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1479
            return null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1480
        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1481
        int hash = hash(key);
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1482
        int i = indexFor(hash, table.length);
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1483
        if (table[i] instanceof Entry) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1484
            @SuppressWarnings("unchecked")
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1485
            Entry<K,V> e = (Entry<K,V>)table[i];
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1486
            for (; e != null; e = (Entry<K,V>)e.next) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1487
                if (e.hash == hash && Objects.equals(e.key, key)) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1488
                    V oldValue = e.value;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1489
                    e.value = value;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1490
                    e.recordAccess(this);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1491
                    return oldValue;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1492
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1493
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1494
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1495
            return null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1496
        } else if (table[i] != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1497
            TreeBin tb = ((TreeBin) table[i]);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1498
            TreeNode p = tb.getTreeNode(hash, key);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1499
            if (p != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1500
                Entry<K,V> pEntry = (Entry<K,V>)p.entry;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1501
                // assert pEntry.key.equals(key);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1502
                V oldValue = pEntry.value;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1503
                pEntry.value = value;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1504
                pEntry.recordAccess(this);
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1505
                return oldValue;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1506
            }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1507
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1508
        return null;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1509
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1510
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1511
    @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1512
    public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1513
        if (table == EMPTY_TABLE) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1514
            inflateTable(threshold);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1515
        }
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1516
        if (key == null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1517
            if (nullKeyEntry == null || nullKeyEntry.value == null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1518
                V newValue = mappingFunction.apply(key);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1519
                if (newValue != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1520
                    putForNullKey(newValue);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1521
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1522
                return newValue;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1523
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1524
            return nullKeyEntry.value;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1525
        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1526
        int hash = hash(key);
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1527
        int i = indexFor(hash, table.length);
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1528
        boolean checkIfNeedTree = false; // Might we convert bin to a TreeBin?
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1529
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1530
        if (table[i] instanceof Entry) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1531
            int listSize = 0;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1532
            @SuppressWarnings("unchecked")
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1533
            Entry<K,V> e = (Entry<K,V>)table[i];
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1534
            for (; e != null; e = (Entry<K,V>)e.next) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1535
                if (e.hash == hash && Objects.equals(e.key, key)) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1536
                    V oldValue = e.value;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1537
                    if (oldValue == null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1538
                        V newValue = mappingFunction.apply(key);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1539
                        if (newValue != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1540
                            e.value = newValue;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1541
                            e.recordAccess(this);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1542
                        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1543
                        return newValue;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1544
                    }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1545
                    return oldValue;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1546
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1547
                listSize++;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1548
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1549
            // Didn't find, fall through to call the mapping function
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1550
            checkIfNeedTree = listSize >= TreeBin.TREE_THRESHOLD;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1551
        } else if (table[i] != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1552
            TreeBin e = (TreeBin)table[i];
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1553
            V value = mappingFunction.apply(key);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1554
            if (value == null) { // Return the existing value, if any
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1555
                TreeNode p = e.getTreeNode(hash, key);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1556
                if (p != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1557
                    return (V) p.entry.value;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1558
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1559
                return null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1560
            } else { // Put the new value into the Tree, if absent
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1561
                TreeNode p = e.putTreeNode(hash, key, value, null);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1562
                if (p == null) { // not found, new node was added
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1563
                    modCount++;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1564
                    size++;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1565
                    if (size >= threshold) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1566
                        resize(2 * table.length);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1567
                    }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1568
                    return value;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1569
                } else { // putTreeNode() found an existing node
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1570
                    Entry<K,V> pEntry = (Entry<K,V>)p.entry;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1571
                    V oldVal = pEntry.value;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1572
                    if (oldVal == null) { // only replace if maps to null
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1573
                        pEntry.value = value;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1574
                        pEntry.recordAccess(this);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1575
                        return value;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1576
                    }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1577
                    return oldVal;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1578
                }
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1579
            }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1580
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1581
        V newValue = mappingFunction.apply(key);
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1582
        if (newValue != null) { // add Entry and check for TreeBin conversion
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1583
            modCount++;
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1584
            addEntry(hash, key, newValue, i, checkIfNeedTree);
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1585
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1586
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1587
        return newValue;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1588
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1589
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1590
    @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1591
    public V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1592
        if (isEmpty()) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1593
            return null;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1594
        }
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1595
        if (key == null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1596
            V oldValue;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1597
            if (nullKeyEntry != null && (oldValue = nullKeyEntry.value) != null) {
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1598
                V newValue = remappingFunction.apply(key, oldValue);
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1599
                if (newValue != null ) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1600
                    putForNullKey(newValue);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1601
                    return newValue;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1602
                } else {
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1603
                    removeNullKey();
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1604
                }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1605
            }
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1606
            return null;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1607
        }
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1608
        int hash = hash(key);
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1609
        int i = indexFor(hash, table.length);
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1610
        if (table[i] instanceof Entry) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1611
            @SuppressWarnings("unchecked")
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1612
            Entry<K,V> prev = (Entry<K,V>)table[i];
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1613
            Entry<K,V> e = prev;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1614
            while (e != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1615
                Entry<K,V> next = (Entry<K,V>)e.next;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1616
                if (e.hash == hash && Objects.equals(e.key, key)) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1617
                    V oldValue = e.value;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1618
                    if (oldValue == null)
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1619
                        break;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1620
                    V newValue = remappingFunction.apply(key, oldValue);
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1621
                    if (newValue == null) {
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1622
                        modCount++;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1623
                        size--;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1624
                        if (prev == e)
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1625
                            table[i] = next;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1626
                        else
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1627
                            prev.next = next;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1628
                        e.recordRemoval(this);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1629
                    } else {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1630
                        e.value = newValue;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1631
                        e.recordAccess(this);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1632
                    }
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1633
                    return newValue;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1634
                }
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1635
                prev = e;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1636
                e = next;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1637
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1638
        } else if (table[i] != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1639
            TreeBin tb = (TreeBin)table[i];
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1640
            TreeNode p = tb.getTreeNode(hash, key);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1641
            if (p != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1642
                Entry<K,V> pEntry = (Entry<K,V>)p.entry;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1643
                // assert pEntry.key.equals(key);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1644
                V oldValue = pEntry.value;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1645
                if (oldValue != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1646
                    V newValue = remappingFunction.apply(key, oldValue);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1647
                    if (newValue == null) { // remove mapping
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1648
                        modCount++;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1649
                        size--;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1650
                        tb.deleteTreeNode(p);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1651
                        pEntry.recordRemoval(this);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1652
                        if (tb.root == null || tb.first == null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1653
                            // assert tb.root == null && tb.first == null :
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1654
                            //     "TreeBin.first and root should both be null";
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1655
                            // TreeBin is now empty, we should blank this bin
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1656
                            table[i] = null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1657
                        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1658
                    } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1659
                        pEntry.value = newValue;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1660
                        pEntry.recordAccess(this);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1661
                    }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1662
                    return newValue;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1663
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1664
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1665
        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1666
        return null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1667
    }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1668
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1669
    @Override
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1670
    public V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1671
        if (table == EMPTY_TABLE) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1672
            inflateTable(threshold);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1673
        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1674
        if (key == null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1675
            V oldValue = nullKeyEntry == null ? null : nullKeyEntry.value;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1676
            V newValue = remappingFunction.apply(key, oldValue);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1677
            if (newValue != oldValue) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1678
                if (newValue == null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1679
                    removeNullKey();
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1680
                } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1681
                    putForNullKey(newValue);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1682
                }
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1683
            }
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1684
            return newValue;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1685
        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1686
        int hash = hash(key);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1687
        int i = indexFor(hash, table.length);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1688
        boolean checkIfNeedTree = false; // Might we convert bin to a TreeBin?
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1689
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1690
        if (table[i] instanceof Entry) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1691
            int listSize = 0;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1692
            @SuppressWarnings("unchecked")
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1693
            Entry<K,V> prev = (Entry<K,V>)table[i];
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1694
            Entry<K,V> e = prev;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1695
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1696
            while (e != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1697
                Entry<K,V> next = (Entry<K,V>)e.next;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1698
                if (e.hash == hash && Objects.equals(e.key, key)) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1699
                    V oldValue = e.value;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1700
                    V newValue = remappingFunction.apply(key, oldValue);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1701
                    if (newValue != oldValue) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1702
                        if (newValue == null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1703
                            modCount++;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1704
                            size--;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1705
                            if (prev == e)
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1706
                                table[i] = next;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1707
                            else
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1708
                                prev.next = next;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1709
                            e.recordRemoval(this);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1710
                        } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1711
                            e.value = newValue;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1712
                            e.recordAccess(this);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1713
                        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1714
                    }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1715
                    return newValue;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1716
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1717
                prev = e;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1718
                e = next;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1719
                listSize++;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1720
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1721
            checkIfNeedTree = listSize >= TreeBin.TREE_THRESHOLD;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1722
        } else if (table[i] != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1723
            TreeBin tb = (TreeBin)table[i];
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1724
            TreeNode p = tb.getTreeNode(hash, key);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1725
            V oldValue = p == null ? null : (V)p.entry.value;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1726
            V newValue = remappingFunction.apply(key, oldValue);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1727
            if (newValue != oldValue) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1728
                if (newValue == null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1729
                    Entry<K,V> pEntry = (Entry<K,V>)p.entry;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1730
                    modCount++;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1731
                    size--;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1732
                    tb.deleteTreeNode(p);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1733
                    pEntry.recordRemoval(this);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1734
                    if (tb.root == null || tb.first == null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1735
                        // assert tb.root == null && tb.first == null :
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1736
                        //         "TreeBin.first and root should both be null";
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1737
                        // TreeBin is now empty, we should blank this bin
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1738
                        table[i] = null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1739
                    }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1740
                } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1741
                    if (p != null) { // just update the value
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1742
                        Entry<K,V> pEntry = (Entry<K,V>)p.entry;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1743
                        pEntry.value = newValue;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1744
                        pEntry.recordAccess(this);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1745
                    } else { // need to put new node
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1746
                        p = tb.putTreeNode(hash, key, newValue, null);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1747
                        // assert p == null; // should have added a new node
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1748
                        modCount++;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1749
                        size++;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1750
                        if (size >= threshold) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1751
                            resize(2 * table.length);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1752
                        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1753
                    }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1754
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1755
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1756
            return newValue;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1757
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1758
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1759
        V newValue = remappingFunction.apply(key, null);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1760
        if (newValue != null) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1761
            modCount++;
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1762
            addEntry(hash, key, newValue, i, checkIfNeedTree);
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1763
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1764
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1765
        return newValue;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1766
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1767
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1768
    @Override
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1769
    public V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1770
        if (table == EMPTY_TABLE) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1771
            inflateTable(threshold);
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1772
        }
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1773
        if (key == null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1774
            V oldValue = nullKeyEntry == null ? null : nullKeyEntry.value;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1775
            V newValue = oldValue == null ? value : remappingFunction.apply(oldValue, value);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1776
            if (newValue != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1777
                putForNullKey(newValue);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1778
            } else if (nullKeyEntry != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1779
                removeNullKey();
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1780
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1781
            return newValue;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1782
        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1783
        int hash = hash(key);
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1784
        int i = indexFor(hash, table.length);
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1785
        boolean checkIfNeedTree = false; // Might we convert bin to a TreeBin?
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1786
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1787
        if (table[i] instanceof Entry) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1788
            int listSize = 0;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1789
            @SuppressWarnings("unchecked")
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1790
            Entry<K,V> prev = (Entry<K,V>)table[i];
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1791
            Entry<K,V> e = prev;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1792
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1793
            while (e != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1794
                Entry<K,V> next = (Entry<K,V>)e.next;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1795
                if (e.hash == hash && Objects.equals(e.key, key)) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1796
                    V oldValue = e.value;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1797
                    V newValue = (oldValue == null) ? value :
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1798
                                 remappingFunction.apply(oldValue, value);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1799
                    if (newValue == null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1800
                        modCount++;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1801
                        size--;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1802
                        if (prev == e)
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1803
                            table[i] = next;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1804
                        else
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1805
                            prev.next = next;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1806
                        e.recordRemoval(this);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1807
                    } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1808
                        e.value = newValue;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1809
                        e.recordAccess(this);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1810
                    }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1811
                    return newValue;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1812
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1813
                prev = e;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1814
                e = next;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1815
                listSize++;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1816
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1817
            // Didn't find, so fall through and (maybe) call addEntry() to add
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1818
            // the Entry and check for TreeBin conversion.
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1819
            checkIfNeedTree = listSize >= TreeBin.TREE_THRESHOLD;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1820
        } else if (table[i] != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1821
            TreeBin tb = (TreeBin)table[i];
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1822
            TreeNode p = tb.getTreeNode(hash, key);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1823
            V oldValue = p == null ? null : (V)p.entry.value;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1824
            V newValue = (oldValue == null) ? value :
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1825
                         remappingFunction.apply(oldValue, value);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1826
            if (newValue == null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1827
                if (p != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1828
                    Entry<K,V> pEntry = (Entry<K,V>)p.entry;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1829
                    modCount++;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1830
                    size--;
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1831
                    tb.deleteTreeNode(p);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1832
                    pEntry.recordRemoval(this);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1833
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1834
                    if (tb.root == null || tb.first == null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1835
                        // assert tb.root == null && tb.first == null :
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1836
                        //         "TreeBin.first and root should both be null";
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1837
                        // TreeBin is now empty, we should blank this bin
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1838
                        table[i] = null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1839
                    }
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1840
                }
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1841
                return null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1842
            } else if (newValue != oldValue) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1843
                if (p != null) { // just update the value
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1844
                    Entry<K,V> pEntry = (Entry<K,V>)p.entry;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1845
                    pEntry.value = newValue;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1846
                    pEntry.recordAccess(this);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1847
                } else { // need to put new node
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1848
                    p = tb.putTreeNode(hash, key, newValue, null);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1849
                    // assert p == null; // should have added a new node
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1850
                    modCount++;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1851
                    size++;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1852
                    if (size >= threshold) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1853
                        resize(2 * table.length);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1854
                    }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1855
                }
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1856
            }
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1857
            return newValue;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1858
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1859
        if (value != null) {
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1860
            modCount++;
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1861
            addEntry(hash, key, value, i, checkIfNeedTree);
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1862
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1863
        return value;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1864
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1865
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1866
    // end of optimized implementations of default methods in Map
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
  1867
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
     * Removes and returns the entry associated with the specified key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
     * in the HashMap.  Returns null if the HashMap contains no mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
     * for this key.
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1872
     *
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1873
     * We don't bother converting TreeBins back to Entry lists if the bin falls
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1874
     * back below TREE_THRESHOLD, but we do clear bins when removing the last
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1875
     * TreeNode in a TreeBin.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
    final Entry<K,V> removeEntryForKey(Object key) {
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1878
        if (isEmpty()) {
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1879
            return null;
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1880
        }
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1881
        if (key == null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1882
            if (nullKeyEntry != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1883
                return removeNullKey();
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1884
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1885
            return null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1886
        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1887
        int hash = hash(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
        int i = indexFor(hash, table.length);
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1889
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1890
        if (table[i] instanceof Entry) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1891
            @SuppressWarnings("unchecked")
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  1892
            Entry<K,V> prev = (Entry<K,V>)table[i];
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1893
            Entry<K,V> e = prev;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1895
            while (e != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1896
                @SuppressWarnings("unchecked")
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1897
                Entry<K,V> next = (Entry<K,V>) e.next;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1898
                if (e.hash == hash && Objects.equals(e.key, key)) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1899
                    modCount++;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1900
                    size--;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1901
                    if (prev == e)
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1902
                        table[i] = next;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1903
                    else
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1904
                        prev.next = next;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1905
                    e.recordRemoval(this);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1906
                    return e;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1907
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1908
                prev = e;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1909
                e = next;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1910
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1911
        } else if (table[i] != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1912
            TreeBin tb = ((TreeBin) table[i]);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1913
            TreeNode p = tb.getTreeNode(hash, (K)key);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1914
            if (p != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1915
                Entry<K,V> pEntry = (Entry<K,V>)p.entry;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1916
                // assert pEntry.key.equals(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
                modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
                size--;
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1919
                tb.deleteTreeNode(p);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1920
                pEntry.recordRemoval(this);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1921
                if (tb.root == null || tb.first == null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1922
                    // assert tb.root == null && tb.first == null :
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1923
                    //             "TreeBin.first and root should both be null";
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1924
                    // TreeBin is now empty, we should blank this bin
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1925
                    table[i] = null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1926
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1927
                return pEntry;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
        }
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1930
        return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
    /**
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1934
     * Special version of remove for EntrySet using {@code Map.Entry.equals()}
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1935
     * for matching.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
    final Entry<K,V> removeMapping(Object o) {
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1938
        if (isEmpty() || !(o instanceof Map.Entry))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  1941
        Map.Entry<?,?> entry = (Map.Entry<?,?>) o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
        Object key = entry.getKey();
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1943
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1944
        if (key == null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1945
            if (entry.equals(nullKeyEntry)) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1946
                return removeNullKey();
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1947
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1948
            return null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1949
        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1950
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1951
        int hash = hash(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
        int i = indexFor(hash, table.length);
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1953
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1954
        if (table[i] instanceof Entry) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1955
            @SuppressWarnings("unchecked")
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1956
                Entry<K,V> prev = (Entry<K,V>)table[i];
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1957
            Entry<K,V> e = prev;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1959
            while (e != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1960
                @SuppressWarnings("unchecked")
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1961
                Entry<K,V> next = (Entry<K,V>)e.next;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1962
                if (e.hash == hash && e.equals(entry)) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1963
                    modCount++;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1964
                    size--;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1965
                    if (prev == e)
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1966
                        table[i] = next;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1967
                    else
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1968
                        prev.next = next;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1969
                    e.recordRemoval(this);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1970
                    return e;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1971
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1972
                prev = e;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1973
                e = next;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1974
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1975
        } else if (table[i] != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1976
            TreeBin tb = ((TreeBin) table[i]);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1977
            TreeNode p = tb.getTreeNode(hash, (K)key);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1978
            if (p != null && p.entry.equals(entry)) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1979
                @SuppressWarnings("unchecked")
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1980
                Entry<K,V> pEntry = (Entry<K,V>)p.entry;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1981
                // assert pEntry.key.equals(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
                modCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
                size--;
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1984
                tb.deleteTreeNode(p);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1985
                pEntry.recordRemoval(this);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1986
                if (tb.root == null || tb.first == null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1987
                    // assert tb.root == null && tb.first == null :
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1988
                    //             "TreeBin.first and root should both be null";
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1989
                    // TreeBin is now empty, we should blank this bin
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1990
                    table[i] = null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1991
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1992
                return pEntry;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
        }
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1995
        return null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1996
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1998
    /*
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  1999
     * Remove the mapping for the null key, and update internal accounting
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2000
     * (size, modcount, recordRemoval, etc).
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2001
     *
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2002
     * Assumes nullKeyEntry is non-null.
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2003
     */
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2004
    private Entry<K,V> removeNullKey() {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2005
        // assert nullKeyEntry != null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2006
        Entry<K,V> retVal = nullKeyEntry;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2007
        modCount++;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2008
        size--;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2009
        retVal.recordRemoval(this);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2010
        nullKeyEntry = null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2011
        return retVal;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
     * Removes all of the mappings from this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
     * The map will be empty after this call returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
    public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
        modCount++;
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2020
        if (nullKeyEntry != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2021
            nullKeyEntry = null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2022
        }
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  2023
        Arrays.fill(table, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
        size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
     * Returns <tt>true</tt> if this map maps one or more keys to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
     * specified value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
     * @param value value whose presence in this map is to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
     * @return <tt>true</tt> if this map maps one or more keys to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
     *         specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
    public boolean containsValue(Object value) {
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2036
        if (value == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
            return containsNullValue();
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2038
        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2039
        Object[] tab = table;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2040
        for (int i = 0; i < tab.length; i++) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2041
            if (tab[i] instanceof Entry) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2042
                Entry<?,?> e = (Entry<?,?>)tab[i];
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2043
                for (; e != null; e = (Entry<?,?>)e.next) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2044
                    if (value.equals(e.value)) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2045
                        return true;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2046
                    }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2047
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2048
            } else if (tab[i] != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2049
                TreeBin e = (TreeBin)tab[i];
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2050
                TreeNode p = e.first;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2051
                for (; p != null; p = (TreeNode) p.entry.next) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2052
                    if (value == p.entry.value || value.equals(p.entry.value)) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2053
                        return true;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2054
                    }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2055
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2056
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2057
        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2058
        // Didn't find value in table - could be in nullKeyEntry
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2059
        return (nullKeyEntry != null && (value == nullKeyEntry.value ||
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2060
                                         value.equals(nullKeyEntry.value)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
     * Special-case code for containsValue with null argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
    private boolean containsNullValue() {
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2067
        Object[] tab = table;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2068
        for (int i = 0; i < tab.length; i++) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2069
            if (tab[i] instanceof Entry) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2070
                Entry<K,V> e = (Entry<K,V>)tab[i];
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2071
                for (; e != null; e = (Entry<K,V>)e.next) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2072
                    if (e.value == null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2073
                        return true;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2074
                    }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2075
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2076
            } else if (tab[i] != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2077
                TreeBin e = (TreeBin)tab[i];
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2078
                TreeNode p = e.first;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2079
                for (; p != null; p = (TreeNode) p.entry.next) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2080
                    if (p.entry.value == null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2081
                        return true;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2082
                    }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2083
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2084
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2085
        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2086
        // Didn't find value in table - could be in nullKeyEntry
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2087
        return (nullKeyEntry != null && nullKeyEntry.value == null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
     * Returns a shallow copy of this <tt>HashMap</tt> instance: the keys and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
     * values themselves are not cloned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
     * @return a shallow copy of this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
     */
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  2096
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
        HashMap<K,V> result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
            result = (HashMap<K,V>)super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
        } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
            // assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
        }
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  2104
        if (result.table != EMPTY_TABLE) {
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  2105
            result.inflateTable(Math.min(
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  2106
                (int) Math.min(
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  2107
                    size * Math.min(1 / loadFactor, 4.0f),
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  2108
                    // we have limits...
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  2109
                    HashMap.MAXIMUM_CAPACITY),
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  2110
                table.length));
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  2111
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
        result.entrySet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
        result.modCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
        result.size = 0;
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2115
        result.nullKeyEntry = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
        result.init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
        result.putAllForCreate(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
    static class Entry<K,V> implements Map.Entry<K,V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
        final K key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
        V value;
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2125
        Object next; // an Entry, or a TreeNode
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
        final int hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
         * Creates new entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
         */
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2131
        Entry(int h, K k, V v, Object n) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
            value = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
            next = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
            key = k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
            hash = h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
        public final K getKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
            return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
        public final V getValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
            return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
        public final V setValue(V newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
            V oldValue = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
            value = newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
            return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
        public final boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
            if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
                return false;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  2155
            Map.Entry<?,?> e = (Map.Entry<?,?>)o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
            Object k1 = getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
            Object k2 = e.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
            if (k1 == k2 || (k1 != null && k1.equals(k2))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
                Object v1 = getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
                Object v2 = e.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
                if (v1 == v2 || (v1 != null && v1.equals(v2)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
                    return true;
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2163
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
        public final int hashCode() {
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  2168
            return Objects.hashCode(getKey()) ^ Objects.hashCode(getValue());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
        public final String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
            return getKey() + "=" + getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
         * This method is invoked whenever the value in an entry is
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2177
         * overwritten for a key that's already in the HashMap.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
        void recordAccess(HashMap<K,V> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
         * This method is invoked whenever the entry is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
         * removed from the table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
        void recordRemoval(HashMap<K,V> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2190
    void addEntry(int hash, K key, V value, int bucketIndex) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2191
        addEntry(hash, key, value, bucketIndex, true);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2192
    }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2193
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
     * Adds a new entry with the specified key, value and hash code to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
     * the specified bucket.  It is the responsibility of this
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2197
     * method to resize the table if appropriate.  The new entry is then
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2198
     * created by calling createEntry().
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
     * Subclass overrides this to alter the behavior of put method.
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2201
     *
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2202
     * If checkIfNeedTree is false, it is known that this bucket will not need
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2203
     * to be converted to a TreeBin, so don't bothering checking.
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2204
     *
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2205
     * Assumes key is not null.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
     */
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2207
    void addEntry(int hash, K key, V value, int bucketIndex, boolean checkIfNeedTree) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2208
        // assert key != null;
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  2209
        if ((size >= threshold) && (null != table[bucketIndex])) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
            resize(2 * table.length);
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2211
            hash = hash(key);
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  2212
            bucketIndex = indexFor(hash, table.length);
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  2213
        }
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2214
        createEntry(hash, key, value, bucketIndex, checkIfNeedTree);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
    /**
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2218
     * Called by addEntry(), and also used when creating entries
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
     * as part of Map construction or "pseudo-construction" (cloning,
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2220
     * deserialization).  This version does not check for resizing of the table.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
     *
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2222
     * This method is responsible for converting a bucket to a TreeBin once
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2223
     * TREE_THRESHOLD is reached. However if checkIfNeedTree is false, it is known
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2224
     * that this bucket will not need to be converted to a TreeBin, so don't
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2225
     * bother checking.  The new entry is constructed by calling newEntry().
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2226
     *
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2227
     * Assumes key is not null.
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2228
     *
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2229
     * Note: buckets already converted to a TreeBin don't call this method, but
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2230
     * instead call TreeBin.putTreeNode() to create new entries.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
     */
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2232
    void createEntry(int hash, K key, V value, int bucketIndex, boolean checkIfNeedTree) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2233
        // assert key != null;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  2234
        @SuppressWarnings("unchecked")
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  2235
            Entry<K,V> e = (Entry<K,V>)table[bucketIndex];
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2236
        table[bucketIndex] = newEntry(hash, key, value, e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
        size++;
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2238
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2239
        if (checkIfNeedTree) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2240
            int listSize = 0;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2241
            for (e = (Entry<K,V>) table[bucketIndex]; e != null; e = (Entry<K,V>)e.next) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2242
                listSize++;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2243
                if (listSize >= TreeBin.TREE_THRESHOLD) { // Convert to TreeBin
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2244
                    if (comparableClassFor(key) != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2245
                        TreeBin t = new TreeBin();
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2246
                        t.populate((Entry)table[bucketIndex]);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2247
                        table[bucketIndex] = t;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2248
                    }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2249
                    break;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2250
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2251
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2252
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2255
    /*
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2256
     * Factory method to create a new Entry object.
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2257
     */
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2258
    Entry<K,V> newEntry(int hash, K key, V value, Object next) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2259
        return new HashMap.Entry<>(hash, key, value, next);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2260
    }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2261
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2262
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
    private abstract class HashIterator<E> implements Iterator<E> {
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2264
        Object next;            // next entry to return, an Entry or a TreeNode
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
        int expectedModCount;   // For fast-fail
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
        int index;              // current slot
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2267
        Object current;         // current entry, an Entry or a TreeNode
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
        HashIterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
            expectedModCount = modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
            if (size > 0) { // advance to first entry
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2272
                if (nullKeyEntry != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2273
                    // assert nullKeyEntry.next == null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2274
                    // This works with nextEntry(): nullKeyEntry isa Entry, and
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2275
                    // e.next will be null, so we'll hit the findNextBin() call.
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2276
                    next = nullKeyEntry;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2277
                } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2278
                    findNextBin();
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2279
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
        public final boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
            return next != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  2287
        @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
        final Entry<K,V> nextEntry() {
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2289
            if (modCount != expectedModCount) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
                throw new ConcurrentModificationException();
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2291
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2292
            Object e = next;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2293
            Entry<K,V> retVal;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2294
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
            if (e == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2298
            if (e instanceof Entry) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2299
                retVal = (Entry<K,V>)e;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2300
                next = ((Entry<K,V>)e).next;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2301
            } else { // TreeBin
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2302
                retVal = (Entry<K,V>)((TreeNode)e).entry;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2303
                next = retVal.next;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2304
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2305
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2306
            if (next == null) { // Move to next bin
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2307
                findNextBin();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
            current = e;
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2310
            return retVal;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
        public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
            if (current == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
                throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
            if (modCount != expectedModCount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
                throw new ConcurrentModificationException();
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2318
            K k;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2319
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2320
            if (current instanceof Entry) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2321
                k = ((Entry<K,V>)current).key;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2322
            } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2323
                k = ((Entry<K,V>)((TreeNode)current).entry).key;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2324
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2325
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
            current = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
            HashMap.this.removeEntryForKey(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
            expectedModCount = modCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
        }
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2330
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2331
        /*
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2332
         * Set 'next' to the first entry of the next non-empty bin in the table
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2333
         */
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2334
        private void findNextBin() {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2335
            // assert next == null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2336
            Object[] t = table;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2337
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2338
            while (index < t.length && (next = t[index++]) == null)
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2339
                ;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2340
            if (next instanceof HashMap.TreeBin) { // Point to the first TreeNode
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2341
                next = ((TreeBin) next).first;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2342
                // assert next != null; // There should be no empty TreeBins
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2343
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2344
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
    private final class ValueIterator extends HashIterator<V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
        public V next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
            return nextEntry().value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
    private final class KeyIterator extends HashIterator<K> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
        public K next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
            return nextEntry().getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
    private final class EntryIterator extends HashIterator<Map.Entry<K,V>> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
        public Map.Entry<K,V> next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
            return nextEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
    // Subclass overrides these to alter behavior of views' iterator() method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
    Iterator<K> newKeyIterator()   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
        return new KeyIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
    Iterator<V> newValueIterator()   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
        return new ValueIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
    Iterator<Map.Entry<K,V>> newEntryIterator()   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
        return new EntryIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
    // Views
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
    private transient Set<Map.Entry<K,V>> entrySet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
     * Returns a {@link Set} view of the keys contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
     * The set is backed by the map, so changes to the map are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
     * reflected in the set, and vice-versa.  If the map is modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
     * while an iteration over the set is in progress (except through
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
     * the iterator's own <tt>remove</tt> operation), the results of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
     * the iteration are undefined.  The set supports element removal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
     * which removes the corresponding mapping from the map, via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
     * <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
     * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
     * operations.  It does not support the <tt>add</tt> or <tt>addAll</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
     * operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
    public Set<K> keySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
        Set<K> ks = keySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
        return (ks != null ? ks : (keySet = new KeySet()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
    private final class KeySet extends AbstractSet<K> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
        public Iterator<K> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
            return newKeyIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
            return containsKey(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
        public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
            return HashMap.this.removeEntryForKey(o) != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
            HashMap.this.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
        }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2415
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2416
        public Spliterator<K> spliterator() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2417
            if (HashMap.this.getClass() == HashMap.class)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2418
                return new KeySpliterator<K,V>(HashMap.this, 0, -1, 0, 0);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2419
            else
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2420
                return Spliterators.spliterator
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2421
                        (this, Spliterator.SIZED | Spliterator.DISTINCT);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2422
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
     * Returns a {@link Collection} view of the values contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
     * The collection is backed by the map, so changes to the map are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
     * reflected in the collection, and vice-versa.  If the map is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
     * modified while an iteration over the collection is in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
     * (except through the iterator's own <tt>remove</tt> operation),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
     * the results of the iteration are undefined.  The collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
     * supports element removal, which removes the corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
     * mapping from the map, via the <tt>Iterator.remove</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
     * <tt>Collection.remove</tt>, <tt>removeAll</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
     * <tt>retainAll</tt> and <tt>clear</tt> operations.  It does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
     * support the <tt>add</tt> or <tt>addAll</tt> operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
    public Collection<V> values() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
        Collection<V> vs = values;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
        return (vs != null ? vs : (values = new Values()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
    private final class Values extends AbstractCollection<V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
        public Iterator<V> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
            return newValueIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
            return containsValue(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
            HashMap.this.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
        }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2456
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2457
        public Spliterator<V> spliterator() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2458
            if (HashMap.this.getClass() == HashMap.class)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2459
                return new ValueSpliterator<K,V>(HashMap.this, 0, -1, 0, 0);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2460
            else
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2461
                return Spliterators.spliterator
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2462
                        (this, Spliterator.SIZED);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2463
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
     * Returns a {@link Set} view of the mappings contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
     * The set is backed by the map, so changes to the map are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
     * reflected in the set, and vice-versa.  If the map is modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
     * while an iteration over the set is in progress (except through
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
     * the iterator's own <tt>remove</tt> operation, or through the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
     * <tt>setValue</tt> operation on a map entry returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
     * iterator) the results of the iteration are undefined.  The set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
     * supports element removal, which removes the corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
     * mapping from the map, via the <tt>Iterator.remove</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
     * <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
     * <tt>clear</tt> operations.  It does not support the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
     * <tt>add</tt> or <tt>addAll</tt> operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
     * @return a set view of the mappings contained in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
    public Set<Map.Entry<K,V>> entrySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
        return entrySet0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
    private Set<Map.Entry<K,V>> entrySet0() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
        Set<Map.Entry<K,V>> es = entrySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
        return es != null ? es : (entrySet = new EntrySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
    private final class EntrySet extends AbstractSet<Map.Entry<K,V>> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
        public Iterator<Map.Entry<K,V>> iterator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
            return newEntryIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
            if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
                return false;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  2498
            Map.Entry<?,?> e = (Map.Entry<?,?>) o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
            Entry<K,V> candidate = getEntry(e.getKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
            return candidate != null && candidate.equals(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
        public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
            return removeMapping(o) != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
            HashMap.this.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
        }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2511
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2512
        public Spliterator<Map.Entry<K,V>> spliterator() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2513
            if (HashMap.this.getClass() == HashMap.class)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2514
                return new EntrySpliterator<K,V>(HashMap.this, 0, -1, 0, 0);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2515
            else
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2516
                return Spliterators.spliterator
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2517
                        (this, Spliterator.SIZED | Spliterator.DISTINCT);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2518
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
     * Save the state of the <tt>HashMap</tt> instance to a stream (i.e.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
     * serialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
     * @serialData The <i>capacity</i> of the HashMap (the length of the
16725
9e7cfcc4ff6a 8011199: Backout changeset JDK-7143928 (2b34a1eb3153)
mduigou
parents: 16723
diff changeset
  2526
     *             bucket array) is emitted (int), followed by the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
     *             <i>size</i> (an int, the number of key-value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
     *             mappings), followed by the key (Object) and value (Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
     *             for each key-value mapping.  The key-value mappings are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
     *             emitted in no particular order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
    private void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
        // Write out the threshold, loadfactor, and any hidden stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
        // Write out number of buckets
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  2539
        if (table==EMPTY_TABLE) {
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  2540
            s.writeInt(roundUpToPowerOf2(threshold));
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  2541
        } else {
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2542
            s.writeInt(table.length);
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  2543
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
        // Write out size (number of Mappings)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
        s.writeInt(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
        // Write out keys and values (alternating)
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  2549
        if (size > 0) {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  2550
            for(Map.Entry<K,V> e : entrySet0()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
                s.writeObject(e.getKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
                s.writeObject(e.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
    private static final long serialVersionUID = 362498820763181265L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
    /**
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  2560
     * Reconstitute the {@code HashMap} instance from a stream (i.e.,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
     * deserialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
         throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
    {
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  2566
        // Read in the threshold (ignored), loadfactor, and any hidden stuff
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
        s.defaultReadObject();
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  2568
        if (loadFactor <= 0 || Float.isNaN(loadFactor)) {
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  2569
            throw new InvalidObjectException("Illegal load factor: " +
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  2570
                                               loadFactor);
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  2571
        }
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  2572
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  2573
        // set other fields that need values
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2574
        if (Holder.USE_HASHSEED) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2575
            Holder.UNSAFE.putIntVolatile(this, Holder.HASHSEED_OFFSET,
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2576
                    sun.misc.Hashing.randomHashSeed(this));
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2577
        }
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  2578
        table = EMPTY_TABLE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  2580
        // Read in number of buckets
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  2581
        s.readInt(); // ignored.
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  2582
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  2583
        // Read number of mappings
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  2584
        int mappings = s.readInt();
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  2585
        if (mappings < 0)
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  2586
            throw new InvalidObjectException("Illegal mappings count: " +
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  2587
                                               mappings);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  2589
        // capacity chosen by number of mappings and desired load (if >= 0.25)
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  2590
        int capacity = (int) Math.min(
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2591
                mappings * Math.min(1 / loadFactor, 4.0f),
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2592
                // we have limits...
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2593
                HashMap.MAXIMUM_CAPACITY);
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  2594
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  2595
        // allocate the bucket array;
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  2596
        if (mappings > 0) {
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  2597
            inflateTable(capacity);
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  2598
        } else {
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  2599
            threshold = capacity;
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  2600
        }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  2601
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
        init();  // Give subclass a chance to do its thing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
        // Read the keys and values, and put the mappings in the HashMap
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  2605
        for (int i=0; i<mappings; i++) {
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  2606
            @SuppressWarnings("unchecked")
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2607
            K key = (K) s.readObject();
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  2608
            @SuppressWarnings("unchecked")
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2609
            V value = (V) s.readObject();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
            putForCreate(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
    // These methods are used when serializing HashSets
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
    int   capacity()     { return table.length; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
    float loadFactor()   { return loadFactor;   }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2617
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2618
    /**
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2619
     * Standin until HM overhaul; based loosely on Weak and Identity HM.
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2620
     */
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2621
    static class HashMapSpliterator<K,V> {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2622
        final HashMap<K,V> map;
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2623
        Object current;             // current node, can be Entry or TreeNode
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2624
        int index;                  // current index, modified on advance/split
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2625
        int fence;                  // one past last index
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2626
        int est;                    // size estimate
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2627
        int expectedModCount;       // for comodification checks
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2628
        boolean acceptedNull;       // Have we accepted the null key?
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2629
                                    // Without this, we can't distinguish
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2630
                                    // between being at the very beginning (and
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2631
                                    // needing to accept null), or being at the
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2632
                                    // end of the list in bin 0.  In both cases,
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2633
                                    // current == null && index == 0.
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2634
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2635
        HashMapSpliterator(HashMap<K,V> m, int origin,
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2636
                               int fence, int est,
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2637
                               int expectedModCount) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2638
            this.map = m;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2639
            this.index = origin;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2640
            this.fence = fence;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2641
            this.est = est;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2642
            this.expectedModCount = expectedModCount;
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2643
            this.acceptedNull = false;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2644
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2645
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2646
        final int getFence() { // initialize fence and size on first use
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2647
            int hi;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2648
            if ((hi = fence) < 0) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2649
                HashMap<K,V> m = map;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2650
                est = m.size;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2651
                expectedModCount = m.modCount;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2652
                hi = fence = m.table.length;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2653
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2654
            return hi;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2655
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2656
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2657
        public final long estimateSize() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2658
            getFence(); // force init
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2659
            return (long) est;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2660
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2661
    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2662
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2663
    static final class KeySpliterator<K,V>
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2664
        extends HashMapSpliterator<K,V>
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2665
        implements Spliterator<K> {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2666
        KeySpliterator(HashMap<K,V> m, int origin, int fence, int est,
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2667
                       int expectedModCount) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2668
            super(m, origin, fence, est, expectedModCount);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2669
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2670
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2671
        public KeySpliterator<K,V> trySplit() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2672
            int hi = getFence(), lo = index, mid = (lo + hi) >>> 1;
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2673
            if (lo >= mid || current != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2674
                return null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2675
            } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2676
                KeySpliterator<K,V> retVal = new KeySpliterator<K,V>(map, lo,
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2677
                                     index = mid, est >>>= 1, expectedModCount);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2678
                // Only 'this' Spliterator chould check for null.
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2679
                retVal.acceptedNull = true;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2680
                return retVal;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2681
            }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2682
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2683
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2684
        @SuppressWarnings("unchecked")
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2685
        public void forEachRemaining(Consumer<? super K> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2686
            int i, hi, mc;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2687
            if (action == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2688
                throw new NullPointerException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2689
            HashMap<K,V> m = map;
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2690
            Object[] tab = m.table;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2691
            if ((hi = fence) < 0) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2692
                mc = expectedModCount = m.modCount;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2693
                hi = fence = tab.length;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2694
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2695
            else
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2696
                mc = expectedModCount;
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2697
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2698
            if (!acceptedNull) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2699
                acceptedNull = true;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2700
                if (m.nullKeyEntry != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2701
                    action.accept(m.nullKeyEntry.key);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2702
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2703
            }
17949
6c33d8f2601e 8013649: HashMap spliterator tryAdvance() encounters remaining elements after forEachRemaining()
psandoz
parents: 17939
diff changeset
  2704
            if (tab.length >= hi && (i = index) >= 0 &&
6c33d8f2601e 8013649: HashMap spliterator tryAdvance() encounters remaining elements after forEachRemaining()
psandoz
parents: 17939
diff changeset
  2705
                (i < (index = hi) || current != null)) {
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2706
                Object p = current;
17949
6c33d8f2601e 8013649: HashMap spliterator tryAdvance() encounters remaining elements after forEachRemaining()
psandoz
parents: 17939
diff changeset
  2707
                current = null;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2708
                do {
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2709
                    if (p == null) {
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2710
                        p = tab[i++];
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2711
                        if (p instanceof HashMap.TreeBin) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2712
                            p = ((HashMap.TreeBin)p).first;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2713
                        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2714
                    } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2715
                        HashMap.Entry<K,V> entry;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2716
                        if (p instanceof HashMap.Entry) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2717
                            entry = (HashMap.Entry<K,V>)p;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2718
                        } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2719
                            entry = (HashMap.Entry<K,V>)((TreeNode)p).entry;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2720
                        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2721
                        action.accept(entry.key);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2722
                        p = entry.next;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2723
                    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2724
                } while (p != null || i < hi);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2725
                if (m.modCount != mc)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2726
                    throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2727
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2728
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2729
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2730
        @SuppressWarnings("unchecked")
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2731
        public boolean tryAdvance(Consumer<? super K> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2732
            int hi;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2733
            if (action == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2734
                throw new NullPointerException();
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2735
            Object[] tab = map.table;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2736
            hi = getFence();
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2737
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2738
            if (!acceptedNull) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2739
                acceptedNull = true;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2740
                if (map.nullKeyEntry != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2741
                    action.accept(map.nullKeyEntry.key);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2742
                    if (map.modCount != expectedModCount)
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2743
                        throw new ConcurrentModificationException();
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2744
                    return true;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2745
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2746
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2747
            if (tab.length >= hi && index >= 0) {
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2748
                while (current != null || index < hi) {
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2749
                    if (current == null) {
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2750
                        current = tab[index++];
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2751
                        if (current instanceof HashMap.TreeBin) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2752
                            current = ((HashMap.TreeBin)current).first;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2753
                        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2754
                    } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2755
                        HashMap.Entry<K,V> entry;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2756
                        if (current instanceof HashMap.Entry) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2757
                            entry = (HashMap.Entry<K,V>)current;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2758
                        } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2759
                            entry = (HashMap.Entry<K,V>)((TreeNode)current).entry;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2760
                        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2761
                        K k = entry.key;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2762
                        current = entry.next;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2763
                        action.accept(k);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2764
                        if (map.modCount != expectedModCount)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2765
                            throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2766
                        return true;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2767
                    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2768
                }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2769
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2770
            return false;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2771
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2772
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2773
        public int characteristics() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2774
            return (fence < 0 || est == map.size ? Spliterator.SIZED : 0) |
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2775
                Spliterator.DISTINCT;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2776
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2777
    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2778
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2779
    static final class ValueSpliterator<K,V>
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2780
        extends HashMapSpliterator<K,V>
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2781
        implements Spliterator<V> {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2782
        ValueSpliterator(HashMap<K,V> m, int origin, int fence, int est,
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2783
                         int expectedModCount) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2784
            super(m, origin, fence, est, expectedModCount);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2785
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2786
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2787
        public ValueSpliterator<K,V> trySplit() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2788
            int hi = getFence(), lo = index, mid = (lo + hi) >>> 1;
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2789
            if (lo >= mid || current != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2790
                return null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2791
            } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2792
                ValueSpliterator<K,V> retVal = new ValueSpliterator<K,V>(map,
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2793
                                 lo, index = mid, est >>>= 1, expectedModCount);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2794
                // Only 'this' Spliterator chould check for null.
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2795
                retVal.acceptedNull = true;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2796
                return retVal;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2797
            }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2798
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2799
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2800
        @SuppressWarnings("unchecked")
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2801
        public void forEachRemaining(Consumer<? super V> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2802
            int i, hi, mc;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2803
            if (action == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2804
                throw new NullPointerException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2805
            HashMap<K,V> m = map;
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2806
            Object[] tab = m.table;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2807
            if ((hi = fence) < 0) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2808
                mc = expectedModCount = m.modCount;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2809
                hi = fence = tab.length;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2810
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2811
            else
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2812
                mc = expectedModCount;
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2813
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2814
            if (!acceptedNull) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2815
                acceptedNull = true;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2816
                if (m.nullKeyEntry != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2817
                    action.accept(m.nullKeyEntry.value);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2818
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2819
            }
17949
6c33d8f2601e 8013649: HashMap spliterator tryAdvance() encounters remaining elements after forEachRemaining()
psandoz
parents: 17939
diff changeset
  2820
            if (tab.length >= hi && (i = index) >= 0 &&
6c33d8f2601e 8013649: HashMap spliterator tryAdvance() encounters remaining elements after forEachRemaining()
psandoz
parents: 17939
diff changeset
  2821
                (i < (index = hi) || current != null)) {
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2822
                Object p = current;
17949
6c33d8f2601e 8013649: HashMap spliterator tryAdvance() encounters remaining elements after forEachRemaining()
psandoz
parents: 17939
diff changeset
  2823
                current = null;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2824
                do {
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2825
                    if (p == null) {
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2826
                        p = tab[i++];
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2827
                        if (p instanceof HashMap.TreeBin) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2828
                            p = ((HashMap.TreeBin)p).first;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2829
                        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2830
                    } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2831
                        HashMap.Entry<K,V> entry;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2832
                        if (p instanceof HashMap.Entry) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2833
                            entry = (HashMap.Entry<K,V>)p;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2834
                        } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2835
                            entry = (HashMap.Entry<K,V>)((TreeNode)p).entry;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2836
                        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2837
                        action.accept(entry.value);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2838
                        p = entry.next;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2839
                    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2840
                } while (p != null || i < hi);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2841
                if (m.modCount != mc)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2842
                    throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2843
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2844
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2845
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2846
        @SuppressWarnings("unchecked")
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2847
        public boolean tryAdvance(Consumer<? super V> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2848
            int hi;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2849
            if (action == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2850
                throw new NullPointerException();
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2851
            Object[] tab = map.table;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2852
            hi = getFence();
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2853
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2854
            if (!acceptedNull) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2855
                acceptedNull = true;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2856
                if (map.nullKeyEntry != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2857
                    action.accept(map.nullKeyEntry.value);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2858
                    if (map.modCount != expectedModCount)
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2859
                        throw new ConcurrentModificationException();
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2860
                    return true;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2861
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2862
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2863
            if (tab.length >= hi && index >= 0) {
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2864
                while (current != null || index < hi) {
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2865
                    if (current == null) {
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2866
                        current = tab[index++];
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2867
                        if (current instanceof HashMap.TreeBin) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2868
                            current = ((HashMap.TreeBin)current).first;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2869
                        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2870
                    } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2871
                        HashMap.Entry<K,V> entry;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2872
                        if (current instanceof HashMap.Entry) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2873
                            entry = (Entry<K,V>)current;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2874
                        } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2875
                            entry = (Entry<K,V>)((TreeNode)current).entry;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2876
                        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2877
                        V v = entry.value;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2878
                        current = entry.next;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2879
                        action.accept(v);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2880
                        if (map.modCount != expectedModCount)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2881
                            throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2882
                        return true;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2883
                    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2884
                }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2885
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2886
            return false;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2887
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2888
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2889
        public int characteristics() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2890
            return (fence < 0 || est == map.size ? Spliterator.SIZED : 0);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2891
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2892
    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2893
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2894
    static final class EntrySpliterator<K,V>
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2895
        extends HashMapSpliterator<K,V>
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2896
        implements Spliterator<Map.Entry<K,V>> {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2897
        EntrySpliterator(HashMap<K,V> m, int origin, int fence, int est,
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2898
                         int expectedModCount) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2899
            super(m, origin, fence, est, expectedModCount);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2900
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2901
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2902
        public EntrySpliterator<K,V> trySplit() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2903
            int hi = getFence(), lo = index, mid = (lo + hi) >>> 1;
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2904
            if (lo >= mid || current != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2905
                return null;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2906
            } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2907
                EntrySpliterator<K,V> retVal = new EntrySpliterator<K,V>(map,
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2908
                                 lo, index = mid, est >>>= 1, expectedModCount);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2909
                // Only 'this' Spliterator chould check for null.
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2910
                retVal.acceptedNull = true;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2911
                return retVal;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2912
            }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2913
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2914
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2915
        @SuppressWarnings("unchecked")
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2916
        public void forEachRemaining(Consumer<? super Map.Entry<K,V>> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2917
            int i, hi, mc;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2918
            if (action == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2919
                throw new NullPointerException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2920
            HashMap<K,V> m = map;
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2921
            Object[] tab = m.table;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2922
            if ((hi = fence) < 0) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2923
                mc = expectedModCount = m.modCount;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2924
                hi = fence = tab.length;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2925
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2926
            else
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2927
                mc = expectedModCount;
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2928
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2929
            if (!acceptedNull) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2930
                acceptedNull = true;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2931
                if (m.nullKeyEntry != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2932
                    action.accept(m.nullKeyEntry);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2933
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2934
            }
17949
6c33d8f2601e 8013649: HashMap spliterator tryAdvance() encounters remaining elements after forEachRemaining()
psandoz
parents: 17939
diff changeset
  2935
            if (tab.length >= hi && (i = index) >= 0 &&
6c33d8f2601e 8013649: HashMap spliterator tryAdvance() encounters remaining elements after forEachRemaining()
psandoz
parents: 17939
diff changeset
  2936
                (i < (index = hi) || current != null)) {
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2937
                Object p = current;
17949
6c33d8f2601e 8013649: HashMap spliterator tryAdvance() encounters remaining elements after forEachRemaining()
psandoz
parents: 17939
diff changeset
  2938
                current = null;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2939
                do {
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2940
                    if (p == null) {
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2941
                        p = tab[i++];
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2942
                        if (p instanceof HashMap.TreeBin) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2943
                            p = ((HashMap.TreeBin)p).first;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2944
                        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2945
                    } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2946
                        HashMap.Entry<K,V> entry;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2947
                        if (p instanceof HashMap.Entry) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2948
                            entry = (HashMap.Entry<K,V>)p;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2949
                        } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2950
                            entry = (HashMap.Entry<K,V>)((TreeNode)p).entry;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2951
                        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2952
                        action.accept(entry);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2953
                        p = entry.next;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2954
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2955
                    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2956
                } while (p != null || i < hi);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2957
                if (m.modCount != mc)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2958
                    throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2959
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2960
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2961
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2962
        @SuppressWarnings("unchecked")
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2963
        public boolean tryAdvance(Consumer<? super Map.Entry<K,V>> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2964
            int hi;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2965
            if (action == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2966
                throw new NullPointerException();
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2967
            Object[] tab = map.table;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2968
            hi = getFence();
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2969
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2970
            if (!acceptedNull) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2971
                acceptedNull = true;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2972
                if (map.nullKeyEntry != null) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2973
                    action.accept(map.nullKeyEntry);
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2974
                    if (map.modCount != expectedModCount)
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2975
                        throw new ConcurrentModificationException();
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2976
                    return true;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2977
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2978
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2979
            if (tab.length >= hi && index >= 0) {
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2980
                while (current != null || index < hi) {
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2981
                    if (current == null) {
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2982
                        current = tab[index++];
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2983
                        if (current instanceof HashMap.TreeBin) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2984
                            current = ((HashMap.TreeBin)current).first;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2985
                        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2986
                    } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2987
                        HashMap.Entry<K,V> e;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2988
                        if (current instanceof HashMap.Entry) {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2989
                            e = (Entry<K,V>)current;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2990
                        } else {
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2991
                            e = (Entry<K,V>)((TreeNode)current).entry;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2992
                        }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
  2993
                        current = e.next;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2994
                        action.accept(e);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2995
                        if (map.modCount != expectedModCount)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2996
                            throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2997
                        return true;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2998
                    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  2999
                }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  3000
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  3001
            return false;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  3002
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  3003
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  3004
        public int characteristics() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  3005
            return (fence < 0 || est == map.size ? Spliterator.SIZED : 0) |
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  3006
                Spliterator.DISTINCT;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  3007
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  3008
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3009
}