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