jdk/src/share/classes/java/util/HashMap.java
author bchristi
Thu, 26 Sep 2013 11:13:34 -0700
changeset 20199 0a9d1d17b076
parent 20189 1e618f2a82d9
child 21352 0372edc9a995
permissions -rw-r--r--
8025173: HashMap.put() replacing an existing key can trigger a resize() Summary: Ensure that HashMap is not resized if we're just replacing a value Reviewed-by: alanb, martin
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
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
    28
import java.io.IOException;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
    29
import java.io.InvalidObjectException;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
    30
import java.io.Serializable;
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
    31
import java.lang.reflect.ParameterizedType;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
    32
import java.lang.reflect.Type;
18280
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
    33
import java.util.function.BiConsumer;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
    34
import java.util.function.BiFunction;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
    35
import java.util.function.Consumer;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
    36
import java.util.function.Function;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Hash table based implementation of the <tt>Map</tt> interface.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * implementation provides all of the optional map operations, and permits
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <tt>null</tt> values and the <tt>null</tt> key.  (The <tt>HashMap</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * class is roughly equivalent to <tt>Hashtable</tt>, except that it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * unsynchronized and permits nulls.)  This class makes no guarantees as to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * the order of the map; in particular, it does not guarantee that the order
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * will remain constant over time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <p>This implementation provides constant-time performance for the basic
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * operations (<tt>get</tt> and <tt>put</tt>), assuming the hash function
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * disperses the elements properly among the buckets.  Iteration over
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * collection views requires time proportional to the "capacity" of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <tt>HashMap</tt> instance (the number of buckets) plus its size (the number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * of key-value mappings).  Thus, it's very important not to set the initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * capacity too high (or the load factor too low) if iteration performance is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * important.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <p>An instance of <tt>HashMap</tt> has two parameters that affect its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * performance: <i>initial capacity</i> and <i>load factor</i>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <i>capacity</i> is the number of buckets in the hash table, and the initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * capacity is simply the capacity at the time the hash table is created.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <i>load factor</i> is a measure of how full the hash table is allowed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * get before its capacity is automatically increased.  When the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * entries in the hash table exceeds the product of the load factor and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * current capacity, the hash table is <i>rehashed</i> (that is, internal data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * structures are rebuilt) so that the hash table has approximately twice the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * number of buckets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
    67
 * <p>As a general rule, the default load factor (.75) offers a good
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
    68
 * tradeoff between time and space costs.  Higher values decrease the
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
    69
 * space overhead but increase the lookup cost (reflected in most of
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
    70
 * the operations of the <tt>HashMap</tt> class, including
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
    71
 * <tt>get</tt> and <tt>put</tt>).  The expected number of entries in
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
    72
 * the map and its load factor should be taken into account when
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
    73
 * setting its initial capacity, so as to minimize the number of
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
    74
 * rehash operations.  If the initial capacity is greater than the
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
    75
 * maximum number of entries divided by the load factor, no rehash
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
    76
 * operations will ever occur.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
    78
 * <p>If many mappings are to be stored in a <tt>HashMap</tt>
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
    79
 * instance, creating it with a sufficiently large capacity will allow
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
    80
 * the mappings to be stored more efficiently than letting it perform
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
    81
 * automatic rehashing as needed to grow the table.  Note that using
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
    82
 * many keys with the same {@code hashCode()} is a sure way to slow
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
    83
 * down performance of any hash table. To ameliorate impact, when keys
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
    84
 * are {@link Comparable}, this class may use comparison order among
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
    85
 * keys to help break ties.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <p><strong>Note that this implementation is not synchronized.</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * If multiple threads access a hash map concurrently, and at least one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * the threads modifies the map structurally, it <i>must</i> be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * synchronized externally.  (A structural modification is any operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * that adds or deletes one or more mappings; merely changing the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * associated with a key that an instance already contains is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * structural modification.)  This is typically accomplished by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * synchronizing on some object that naturally encapsulates the map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * If no such object exists, the map should be "wrapped" using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * {@link Collections#synchronizedMap Collections.synchronizedMap}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * method.  This is best done at creation time, to prevent accidental
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * unsynchronized access to the map:<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *   Map m = Collections.synchronizedMap(new HashMap(...));</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * <p>The iterators returned by all of this class's "collection view methods"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * are <i>fail-fast</i>: if the map is structurally modified at any time after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * the iterator is created, in any way except through the iterator's own
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * <tt>remove</tt> method, the iterator will throw a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * {@link ConcurrentModificationException}.  Thus, in the face of concurrent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * modification, the iterator fails quickly and cleanly, rather than risking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * arbitrary, non-deterministic behavior at an undetermined time in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * future.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * as it is, generally speaking, impossible to make any hard guarantees in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * presence of unsynchronized concurrent modification.  Fail-fast iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * Therefore, it would be wrong to write a program that depended on this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * exception for its correctness: <i>the fail-fast behavior of iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * should be used only to detect bugs.</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * <p>This class is a member of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * @param <K> the type of keys maintained by this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * @param <V> the type of mapped values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * @author  Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * @author  Arthur van Hoff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * @author  Neal Gafter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * @see     Object#hashCode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * @see     Collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * @see     Map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * @see     TreeMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * @see     Hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 */
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   137
public class HashMap<K,V> extends AbstractMap<K,V>
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   138
    implements Map<K,V>, Cloneable, Serializable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   140
    private static final long serialVersionUID = 362498820763181265L;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   141
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   142
    /*
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   143
     * Implementation notes.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   144
     *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   145
     * This map usually acts as a binned (bucketed) hash table, but
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   146
     * when bins get too large, they are transformed into bins of
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   147
     * TreeNodes, each structured similarly to those in
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   148
     * java.util.TreeMap. Most methods try to use normal bins, but
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   149
     * relay to TreeNode methods when applicable (simply by checking
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   150
     * instanceof a node).  Bins of TreeNodes may be traversed and
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   151
     * used like any others, but additionally support faster lookup
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   152
     * when overpopulated. However, since the vast majority of bins in
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   153
     * normal use are not overpopulated, checking for existence of
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   154
     * tree bins may be delayed in the course of table methods.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   155
     *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   156
     * Tree bins (i.e., bins whose elements are all TreeNodes) are
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   157
     * ordered primarily by hashCode, but in the case of ties, if two
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   158
     * elements are of the same "class C implements Comparable<C>",
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   159
     * type then their compareTo method is used for ordering. (We
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   160
     * conservatively check generic types via reflection to validate
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   161
     * this -- see method comparableClassFor).  The added complexity
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   162
     * of tree bins is worthwhile in providing worst-case O(log n)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   163
     * operations when keys either have distinct hashes or are
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   164
     * orderable, Thus, performance degrades gracefully under
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   165
     * accidental or malicious usages in which hashCode() methods
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   166
     * return values that are poorly distributed, as well as those in
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   167
     * which many keys share a hashCode, so long as they are also
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   168
     * Comparable. (If neither of these apply, we may waste about a
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   169
     * factor of two in time and space compared to taking no
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   170
     * precautions. But the only known cases stem from poor user
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   171
     * programming practices that are already so slow that this makes
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   172
     * little difference.)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   173
     *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   174
     * Because TreeNodes are about twice the size of regular nodes, we
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   175
     * use them only when bins contain enough nodes to warrant use
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   176
     * (see TREEIFY_THRESHOLD). And when they become too small (due to
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   177
     * removal or resizing) they are converted back to plain bins.  In
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   178
     * usages with well-distributed user hashCodes, tree bins are
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   179
     * rarely used.  Ideally, under random hashCodes, the frequency of
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   180
     * nodes in bins follows a Poisson distribution
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   181
     * (http://en.wikipedia.org/wiki/Poisson_distribution) with a
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   182
     * parameter of about 0.5 on average for the default resizing
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   183
     * threshold of 0.75, although with a large variance because of
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   184
     * resizing granularity. Ignoring variance, the expected
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   185
     * occurrences of list size k are (exp(-0.5) * pow(0.5, k) /
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   186
     * factorial(k)). The first values are:
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   187
     *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   188
     * 0:    0.60653066
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   189
     * 1:    0.30326533
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   190
     * 2:    0.07581633
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   191
     * 3:    0.01263606
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   192
     * 4:    0.00157952
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   193
     * 5:    0.00015795
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   194
     * 6:    0.00001316
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   195
     * 7:    0.00000094
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   196
     * 8:    0.00000006
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   197
     * more: less than 1 in ten million
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   198
     *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   199
     * The root of a tree bin is normally its first node.  However,
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   200
     * sometimes (currently only upon Iterator.remove), the root might
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   201
     * be elsewhere, but can be recovered following parent links
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   202
     * (method TreeNode.root()).
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   203
     *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   204
     * All applicable internal methods accept a hash code as an
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   205
     * argument (as normally supplied from a public method), allowing
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   206
     * them to call each other without recomputing user hashCodes.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   207
     * Most internal methods also accept a "tab" argument, that is
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   208
     * normally the current table, but may be a new or old one when
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   209
     * resizing or converting.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   210
     *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   211
     * When bin lists are treeified, split, or untreeified, we keep
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   212
     * them in the same relative access/traversal order (i.e., field
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   213
     * Node.next) to better preserve locality, and to slightly
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   214
     * simplify handling of splits and traversals that invoke
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   215
     * iterator.remove. When using comparators on insertion, to keep a
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   216
     * total ordering (or as close as is required here) across
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   217
     * rebalancings, we compare classes and identityHashCodes as
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   218
     * tie-breakers.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   219
     *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   220
     * The use and transitions among plain vs tree modes is
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   221
     * complicated by the existence of subclass LinkedHashMap. See
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   222
     * below for hook methods defined to be invoked upon insertion,
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   223
     * removal and access that allow LinkedHashMap internals to
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   224
     * otherwise remain independent of these mechanics. (This also
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   225
     * requires that a map instance be passed to some utility methods
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   226
     * that may create new nodes.)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   227
     *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   228
     * The concurrent-programming-like SSA-based coding style helps
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   229
     * avoid aliasing errors amid all of the twisty pointer operations.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   230
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * The default initial capacity - MUST be a power of two.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   235
    static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * The maximum capacity, used if a higher value is implicitly specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * by either of the constructors with arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * MUST be a power of two <= 1<<30.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    static final int MAXIMUM_CAPACITY = 1 << 30;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * The load factor used when none specified in constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    static final float DEFAULT_LOAD_FACTOR = 0.75f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    /**
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   250
     * The bin count threshold for using a tree rather than list for a
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   251
     * bin.  Bins are converted to trees when adding an element to a
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   252
     * bin with at least this many nodes. The value must be greater
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   253
     * than 2 and should be at least 8 to mesh with assumptions in
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   254
     * tree removal about conversion back to plain bins upon
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   255
     * shrinkage.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   256
     */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   257
    static final int TREEIFY_THRESHOLD = 8;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   258
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   259
    /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   260
     * The bin count threshold for untreeifying a (split) bin during a
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   261
     * resize operation. Should be less than TREEIFY_THRESHOLD, and at
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   262
     * most 6 to mesh with shrinkage detection under removal.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   263
     */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   264
    static final int UNTREEIFY_THRESHOLD = 6;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   265
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   266
    /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   267
     * The smallest table capacity for which bins may be treeified.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   268
     * (Otherwise the table is resized if too many nodes in a bin.)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   269
     * Should be at least 4 * TREEIFY_THRESHOLD to avoid conflicts
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   270
     * between resizing and treeification thresholds.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   271
     */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   272
    static final int MIN_TREEIFY_CAPACITY = 64;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   273
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   274
    /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   275
     * Basic hash bin node, used for most entries.  (See below for
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   276
     * TreeNode subclass, and in LinkedHashMap for its Entry subclass.)
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   277
     */
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   278
    static class Node<K,V> implements Map.Entry<K,V> {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   279
        final int hash;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   280
        final K key;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   281
        V value;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   282
        Node<K,V> next;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   283
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   284
        Node(int hash, K key, V value, Node<K,V> next) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   285
            this.hash = hash;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   286
            this.key = key;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   287
            this.value = value;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   288
            this.next = next;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   289
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   290
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   291
        public final K getKey()        { return key; }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   292
        public final V getValue()      { return value; }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   293
        public final String toString() { return key + "=" + value; }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   294
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   295
        public final int hashCode() {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   296
            return Objects.hashCode(key) ^ Objects.hashCode(value);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   297
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   298
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   299
        public final V setValue(V newValue) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   300
            V oldValue = value;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   301
            value = newValue;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   302
            return oldValue;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   303
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   304
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   305
        public final boolean equals(Object o) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   306
            if (o == this)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   307
                return true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   308
            if (o instanceof Map.Entry) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   309
                Map.Entry<?,?> e = (Map.Entry<?,?>)o;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   310
                if (Objects.equals(key, e.getKey()) &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   311
                    Objects.equals(value, e.getValue()))
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   312
                    return true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   313
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   314
            return false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   315
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   316
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   317
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   318
    /* ---------------- Static utilities -------------- */
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   319
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   320
    /**
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   321
     * Computes key.hashCode() and spreads (XORs) higher bits of hash
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   322
     * to lower.  Because the table uses power-of-two masking, sets of
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   323
     * hashes that vary only in bits above the current mask will
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   324
     * always collide. (Among known examples are sets of Float keys
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   325
     * holding consecutive whole numbers in small tables.)  So we
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   326
     * apply a transform that spreads the impact of higher bits
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   327
     * downward. There is a tradeoff between speed, utility, and
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   328
     * quality of bit-spreading. Because many common sets of hashes
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   329
     * are already reasonably distributed (so don't benefit from
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   330
     * spreading), and because we use trees to handle large sets of
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   331
     * collisions in bins, we just XOR some shifted bits in the
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   332
     * cheapest possible way to reduce systematic lossage, as well as
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   333
     * to incorporate impact of the highest bits that would otherwise
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   334
     * never be used in index calculations because of table bounds.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   335
     */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   336
    static final int hash(Object key) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   337
        int h;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   338
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   339
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   340
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   341
    /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   342
     * Returns x's Class if it is of the form "class C implements
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   343
     * Comparable<C>", else null.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     */
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   345
    static Class<?> comparableClassFor(Object x) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   346
        if (x instanceof Comparable) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   347
            Class<?> c; Type[] ts, as; Type t; ParameterizedType p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   348
            if ((c = x.getClass()) == String.class) // bypass checks
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   349
                return c;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   350
            if ((ts = c.getGenericInterfaces()) != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   351
                for (int i = 0; i < ts.length; ++i) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   352
                    if (((t = ts[i]) instanceof ParameterizedType) &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   353
                        ((p = (ParameterizedType)t).getRawType() ==
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   354
                         Comparable.class) &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   355
                        (as = p.getActualTypeArguments()) != null &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   356
                        as.length == 1 && as[0] == c) // type arg is c
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   357
                        return c;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   358
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   359
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   360
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   361
        return null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   362
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   363
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   364
    /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   365
     * Returns k.compareTo(x) if x matches kc (k's screened comparable
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   366
     * class), else 0.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   367
     */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   368
    @SuppressWarnings({"rawtypes","unchecked"}) // for cast to Comparable
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   369
    static int compareComparables(Class<?> kc, Object k, Object x) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   370
        return (x == null || x.getClass() != kc ? 0 :
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   371
                ((Comparable)k).compareTo(x));
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   372
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   373
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   374
    /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   375
     * Returns a power of two size for the given target capacity.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   376
     */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   377
    static final int tableSizeFor(int cap) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   378
        int n = cap - 1;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   379
        n |= n >>> 1;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   380
        n |= n >>> 2;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   381
        n |= n >>> 4;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   382
        n |= n >>> 8;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   383
        n |= n >>> 16;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   384
        return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   385
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   386
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   387
    /* ---------------- Fields -------------- */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   388
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   389
    /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   390
     * The table, initialized on first use, and resized as
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   391
     * necessary. When allocated, length is always a power of two.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   392
     * (We also tolerate length zero in some operations to allow
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   393
     * bootstrapping mechanics that are currently not needed.)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   394
     */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   395
    transient Node<K,V>[] table;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   396
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   397
    /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   398
     * Holds cached entrySet(). Note that AbstractMap fields are used
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   399
     * for keySet() and values().
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   400
     */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   401
    transient Set<Map.Entry<K,V>> entrySet;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * The number of key-value mappings contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    transient int size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * The number of times this HashMap has been structurally modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * Structural modifications are those that change the number of mappings in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * the HashMap or otherwise modify its internal structure (e.g.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * rehash).  This field is used to make iterators on Collection-views of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * the HashMap fail-fast.  (See ConcurrentModificationException).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     */
65
51fc1d79463f 6625725: (coll) modCount should not be volatile
martin
parents: 2
diff changeset
   415
    transient int modCount;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   417
    /**
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   418
     * The next size value at which to resize (capacity * load factor).
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   419
     *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   420
     * @serial
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   421
     */
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   422
    // (The javadoc description is true upon serialization.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   423
    // Additionally, if the table array has not been allocated, this
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   424
    // field holds the initial array capacity, or zero signifying
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   425
    // DEFAULT_INITIAL_CAPACITY.)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   426
    int threshold;
17939
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
    /**
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   429
     * The load factor for the hash table.
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   430
     *
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   431
     * @serial
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   432
     */
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   433
    final float loadFactor;
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   434
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   435
    /* ---------------- Public operations -------------- */
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   436
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * Constructs an empty <tt>HashMap</tt> with the specified initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * capacity and load factor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * @param  initialCapacity the initial capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * @param  loadFactor      the load factor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * @throws IllegalArgumentException if the initial capacity is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *         or the load factor is nonpositive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    public HashMap(int initialCapacity, float loadFactor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        if (initialCapacity < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            throw new IllegalArgumentException("Illegal initial capacity: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                                               initialCapacity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        if (initialCapacity > MAXIMUM_CAPACITY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            initialCapacity = MAXIMUM_CAPACITY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        if (loadFactor <= 0 || Float.isNaN(loadFactor))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            throw new IllegalArgumentException("Illegal load factor: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                                               loadFactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        this.loadFactor = loadFactor;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   456
        this.threshold = tableSizeFor(initialCapacity);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * Constructs an empty <tt>HashMap</tt> with the specified initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * capacity and the default load factor (0.75).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * @param  initialCapacity the initial capacity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * @throws IllegalArgumentException if the initial capacity is negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    public HashMap(int initialCapacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        this(initialCapacity, DEFAULT_LOAD_FACTOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * Constructs an empty <tt>HashMap</tt> with the default initial capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * (16) and the default load factor (0.75).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    public HashMap() {
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   475
        this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * Constructs a new <tt>HashMap</tt> with the same mappings as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * specified <tt>Map</tt>.  The <tt>HashMap</tt> is created with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * default load factor (0.75) and an initial capacity sufficient to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * hold the mappings in the specified <tt>Map</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * @param   m the map whose mappings are to be placed in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * @throws  NullPointerException if the specified map is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    public HashMap(Map<? extends K, ? extends V> m) {
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   488
        this.loadFactor = DEFAULT_LOAD_FACTOR;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   489
        putMapEntries(m, false);
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   490
    }
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   491
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   492
    /**
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   493
     * Implements Map.putAll and Map constructor
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   494
     *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   495
     * @param m the map
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   496
     * @param evict false when initially constructing this map, else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   497
     * true (relayed to method afterNodeInsertion).
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   498
     */
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   499
    final void putMapEntries(Map<? extends K, ? extends V> m, boolean evict) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   500
        int s = m.size();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   501
        if (s > 0) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   502
            if (table == null) { // pre-size
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   503
                float ft = ((float)s / loadFactor) + 1.0F;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   504
                int t = ((ft < (float)MAXIMUM_CAPACITY) ?
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   505
                         (int)ft : MAXIMUM_CAPACITY);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   506
                if (t > threshold)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   507
                    threshold = tableSizeFor(t);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   508
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   509
            else if (s > threshold)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   510
                resize();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   511
            for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   512
                K key = e.getKey();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   513
                V value = e.getValue();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   514
                putVal(hash(key), key, value, false, evict);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   515
            }
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   516
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * Returns the number of key-value mappings in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * @return the number of key-value mappings in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * Returns <tt>true</tt> if this map contains no key-value mappings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * @return <tt>true</tt> if this map contains no key-value mappings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        return size == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * Returns the value to which the specified key is mapped,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * or {@code null} if this map contains no mapping for the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * <p>More formally, if this map contains a mapping from a key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * {@code k} to a value {@code v} such that {@code (key==null ? k==null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * key.equals(k))}, then this method returns {@code v}; otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * it returns {@code null}.  (There can be at most one such mapping.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * <p>A return value of {@code null} does not <i>necessarily</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * indicate that the map contains no mapping for the key; it's also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * possible that the map explicitly maps the key to {@code null}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * The {@link #containsKey containsKey} operation may be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * distinguish these two cases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * @see #put(Object, Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    public V get(Object key) {
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   555
        Node<K,V> e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   556
        return (e = getNode(hash(key), key)) == null ? null : e.value;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   559
    /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   560
     * Implements Map.get and related methods
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   561
     *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   562
     * @param hash hash for key
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   563
     * @param key the key
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   564
     * @return the node, or null if none
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   565
     */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   566
    final Node<K,V> getNode(int hash, Object key) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   567
        Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   568
        if ((tab = table) != null && (n = tab.length) > 0 &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   569
            (first = tab[(n - 1) & hash]) != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   570
            if (first.hash == hash && // always check first node
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   571
                ((k = first.key) == key || (key != null && key.equals(k))))
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   572
                return first;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   573
            if ((e = first.next) != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   574
                if (first instanceof TreeNode)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   575
                    return ((TreeNode<K,V>)first).getTreeNode(hash, key);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   576
                do {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   577
                    if (e.hash == hash &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   578
                        ((k = e.key) == key || (key != null && key.equals(k))))
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   579
                        return e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   580
                } while ((e = e.next) != null);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   581
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   582
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   583
        return null;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   584
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   585
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * Returns <tt>true</tt> if this map contains a mapping for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * specified key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * @param   key   The key whose presence in this map is to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * @return <tt>true</tt> if this map contains a mapping for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    public boolean containsKey(Object key) {
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   595
        return getNode(hash(key), key) != null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * Associates the specified value with the specified key in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * If the map previously contained a mapping for the key, the old
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * value is replaced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * @param key key with which the specified value is to be associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * @param value value to be associated with the specified key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * @return the previous value associated with <tt>key</tt>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     *         <tt>null</tt> if there was no mapping for <tt>key</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     *         (A <tt>null</tt> return can also indicate that the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     *         previously associated <tt>null</tt> with <tt>key</tt>.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    public V put(K key, V value) {
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   611
        return putVal(hash(key), key, value, false, true);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   612
    }
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   613
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   614
    /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   615
     * Implements Map.put and related methods
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   616
     *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   617
     * @param hash hash for key
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   618
     * @param key the key
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   619
     * @param value the value to put
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   620
     * @param onlyIfAbsent if true, don't change existing value
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   621
     * @param evict if false, the table is in creation mode.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   622
     * @return previous value, or null if none
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   623
     */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   624
    final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   625
                   boolean evict) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   626
        Node<K,V>[] tab; Node<K,V> p; int n, i;
20199
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents: 20189
diff changeset
   627
        if ((tab = table) == null || (n = tab.length) == 0)
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   628
            n = (tab = resize()).length;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   629
        if ((p = tab[i = (n - 1) & hash]) == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   630
            tab[i] = newNode(hash, key, value, null);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   631
        else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   632
            Node<K,V> e; K k;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   633
            if (p.hash == hash &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   634
                ((k = p.key) == key || (key != null && key.equals(k))))
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   635
                e = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   636
            else if (p instanceof TreeNode)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   637
                e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   638
            else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   639
                for (int binCount = 0; ; ++binCount) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   640
                    if ((e = p.next) == null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   641
                        p.next = newNode(hash, key, value, null);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   642
                        if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   643
                            treeifyBin(tab, hash);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   644
                        break;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   645
                    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   646
                    if (e.hash == hash &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   647
                        ((k = e.key) == key || (key != null && key.equals(k))))
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   648
                        break;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   649
                    p = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   650
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   651
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   652
            if (e != null) { // existing mapping for key
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   653
                V oldValue = e.value;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   654
                if (!onlyIfAbsent || oldValue == null)
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   655
                    e.value = value;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   656
                afterNodeAccess(e);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   657
                return oldValue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        }
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   660
        ++modCount;
20199
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents: 20189
diff changeset
   661
        if (++size > threshold)
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents: 20189
diff changeset
   662
            resize();
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   663
        afterNodeInsertion(evict);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    /**
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   668
     * Initializes or doubles table size.  If null, allocates in
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   669
     * accord with initial capacity target held in field threshold.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   670
     * Otherwise, because we are using power-of-two expansion, the
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   671
     * elements from each bin must either stay at same index, or move
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   672
     * with a power of two offset in the new table.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   673
     *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   674
     * @return the table
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     */
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   676
    final Node<K,V>[] resize() {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   677
        Node<K,V>[] oldTab = table;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   678
        int oldCap = (oldTab == null) ? 0 : oldTab.length;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   679
        int oldThr = threshold;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   680
        int newCap, newThr = 0;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   681
        if (oldCap > 0) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   682
            if (oldCap >= MAXIMUM_CAPACITY) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   683
                threshold = Integer.MAX_VALUE;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   684
                return oldTab;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   685
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   686
            else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   687
                     oldCap >= DEFAULT_INITIAL_CAPACITY)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   688
                newThr = oldThr << 1; // double threshold
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        }
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   690
        else if (oldThr > 0) // initial capacity was placed in threshold
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   691
            newCap = oldThr;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   692
        else {               // zero initial threshold signifies using defaults
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   693
            newCap = DEFAULT_INITIAL_CAPACITY;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   694
            newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   695
        }
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   696
        if (newThr == 0) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   697
            float ft = (float)newCap * loadFactor;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   698
            newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   699
                      (int)ft : Integer.MAX_VALUE);
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   700
        }
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   701
        threshold = newThr;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   702
        @SuppressWarnings({"rawtypes","unchecked"})
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   703
            Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   704
        table = newTab;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   705
        if (oldTab != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   706
            for (int j = 0; j < oldCap; ++j) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   707
                Node<K,V> e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   708
                if ((e = oldTab[j]) != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   709
                    oldTab[j] = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   710
                    if (e.next == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   711
                        newTab[e.hash & (newCap - 1)] = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   712
                    else if (e instanceof TreeNode)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   713
                        ((TreeNode<K,V>)e).split(this, newTab, j, oldCap);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   714
                    else { // preserve order
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   715
                        Node<K,V> loHead = null, loTail = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   716
                        Node<K,V> hiHead = null, hiTail = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   717
                        Node<K,V> next;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   718
                        do {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   719
                            next = e.next;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   720
                            if ((e.hash & oldCap) == 0) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   721
                                if (loTail == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   722
                                    loHead = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   723
                                else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   724
                                    loTail.next = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   725
                                loTail = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   726
                            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   727
                            else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   728
                                if (hiTail == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   729
                                    hiHead = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   730
                                else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   731
                                    hiTail.next = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   732
                                hiTail = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   733
                            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   734
                        } while ((e = next) != null);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   735
                        if (loTail != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   736
                            loTail.next = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   737
                            newTab[j] = loHead;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   738
                        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   739
                        if (hiTail != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   740
                            hiTail.next = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   741
                            newTab[j + oldCap] = hiHead;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   742
                        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   743
                    }
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   744
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        }
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   747
        return newTab;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    /**
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   751
     * Replaces all linked nodes in bin at index for given hash unless
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   752
     * table is too small, in which case resizes instead.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     */
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   754
    final void treeifyBin(Node<K,V>[] tab, int hash) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   755
        int n, index; Node<K,V> e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   756
        if (tab == null || (n = tab.length) < MIN_TREEIFY_CAPACITY)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   757
            resize();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   758
        else if ((e = tab[index = (n - 1) & hash]) != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   759
            TreeNode<K,V> hd = null, tl = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   760
            do {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   761
                TreeNode<K,V> p = replacementTreeNode(e, null);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   762
                if (tl == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   763
                    hd = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   764
                else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   765
                    p.prev = tl;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   766
                    tl.next = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   767
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   768
                tl = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   769
            } while ((e = e.next) != null);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   770
            if ((tab[index] = hd) != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   771
                hd.treeify(tab);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * Copies all of the mappings from the specified map to this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * These mappings will replace any mappings that this map had for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * any of the keys currently in the specified map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * @param m mappings to be stored in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * @throws NullPointerException if the specified map is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    public void putAll(Map<? extends K, ? extends V> m) {
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   784
        putMapEntries(m, true);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   785
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * Removes the mapping for the specified key from this map if present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * @param  key key whose mapping is to be removed from the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * @return the previous value associated with <tt>key</tt>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     *         <tt>null</tt> if there was no mapping for <tt>key</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     *         (A <tt>null</tt> return can also indicate that the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     *         previously associated <tt>null</tt> with <tt>key</tt>.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    public V remove(Object key) {
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   797
        Node<K,V> e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   798
        return (e = removeNode(hash(key), key, null, false, true)) == null ?
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   799
            null : e.value;
18280
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
   800
    }
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   801
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   802
    /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   803
     * Implements Map.remove and related methods
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   804
     *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   805
     * @param hash hash for key
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   806
     * @param key the key
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   807
     * @param value the value to match if matchValue, else ignored
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   808
     * @param matchValue if true only remove if value is equal
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   809
     * @param movable if false do not move other nodes while removing
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   810
     * @return the node, or null if none
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   811
     */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   812
    final Node<K,V> removeNode(int hash, Object key, Object value,
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   813
                               boolean matchValue, boolean movable) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   814
        Node<K,V>[] tab; Node<K,V> p; int n, index;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   815
        if ((tab = table) != null && (n = tab.length) > 0 &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   816
            (p = tab[index = (n - 1) & hash]) != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   817
            Node<K,V> node = null, e; K k; V v;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   818
            if (p.hash == hash &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   819
                ((k = p.key) == key || (key != null && key.equals(k))))
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   820
                node = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   821
            else if ((e = p.next) != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   822
                if (p instanceof TreeNode)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   823
                    node = ((TreeNode<K,V>)p).getTreeNode(hash, key);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   824
                else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   825
                    do {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   826
                        if (e.hash == hash &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   827
                            ((k = e.key) == key ||
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   828
                             (key != null && key.equals(k)))) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   829
                            node = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   830
                            break;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   831
                        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   832
                        p = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   833
                    } while ((e = e.next) != null);
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   834
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   835
            }
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   836
            if (node != null && (!matchValue || (v = node.value) == value ||
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   837
                                 (value != null && value.equals(v)))) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   838
                if (node instanceof TreeNode)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   839
                    ((TreeNode<K,V>)node).removeTreeNode(this, tab, movable);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   840
                else if (node == p)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   841
                    tab[index] = node.next;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   842
                else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   843
                    p.next = node.next;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   844
                ++modCount;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   845
                --size;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   846
                afterNodeRemoval(node);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   847
                return node;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   848
            }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   849
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   850
        return null;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   851
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   852
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * Removes all of the mappings from this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * The map will be empty after this call returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    public void clear() {
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   858
        Node<K,V>[] tab;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        modCount++;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   860
        if ((tab = table) != null && size > 0) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   861
            size = 0;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   862
            for (int i = 0; i < tab.length; ++i)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   863
                tab[i] = null;
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   864
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * Returns <tt>true</tt> if this map maps one or more keys to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     * specified value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * @param value value whose presence in this map is to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * @return <tt>true</tt> if this map maps one or more keys to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     *         specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
    public boolean containsValue(Object value) {
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   876
        Node<K,V>[] tab; V v;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   877
        if ((tab = table) != null && size > 0) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   878
            for (int i = 0; i < tab.length; ++i) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   879
                for (Node<K,V> e = tab[i]; e != null; e = e.next) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   880
                    if ((v = e.value) == value ||
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   881
                        (value != null && value.equals(v)))
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   882
                        return true;
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   883
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   884
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   885
        }
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   886
        return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * Returns a {@link Set} view of the keys contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * The set is backed by the map, so changes to the map are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * reflected in the set, and vice-versa.  If the map is modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * while an iteration over the set is in progress (except through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * the iterator's own <tt>remove</tt> operation), the results of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * the iteration are undefined.  The set supports element removal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * which removes the corresponding mapping from the map, via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * operations.  It does not support the <tt>add</tt> or <tt>addAll</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * operations.
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   901
     *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   902
     * @return a set view of the keys contained in this map
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
    public Set<K> keySet() {
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   905
        Set<K> ks;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   906
        return (ks = keySet) == null ? (keySet = new KeySet()) : ks;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   909
    final class KeySet extends AbstractSet<K> {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   910
        public final int size()                 { return size; }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   911
        public final void clear()               { HashMap.this.clear(); }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   912
        public final Iterator<K> iterator()     { return new KeyIterator(); }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   913
        public final boolean contains(Object o) { return containsKey(o); }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   914
        public final boolean remove(Object key) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   915
            return removeNode(hash(key), key, null, false, true) != null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        }
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   917
        public final Spliterator<K> spliterator() {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   918
            return new KeySpliterator<K,V>(HashMap.this, 0, -1, 0, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        }
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   920
        public final void forEach(Consumer<? super K> action) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   921
            Node<K,V>[] tab;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   922
            if (action == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   923
                throw new NullPointerException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   924
            if (size > 0 && (tab = table) != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   925
                int mc = modCount;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   926
                for (int i = 0; i < tab.length; ++i) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   927
                    for (Node<K,V> e = tab[i]; e != null; e = e.next)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   928
                        action.accept(e.key);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   929
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   930
                if (modCount != mc)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   931
                    throw new ConcurrentModificationException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   932
            }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
   933
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     * Returns a {@link Collection} view of the values contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * The collection is backed by the map, so changes to the map are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * reflected in the collection, and vice-versa.  If the map is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * modified while an iteration over the collection is in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * (except through the iterator's own <tt>remove</tt> operation),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * the results of the iteration are undefined.  The collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * supports element removal, which removes the corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * mapping from the map, via the <tt>Iterator.remove</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * <tt>Collection.remove</tt>, <tt>removeAll</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * <tt>retainAll</tt> and <tt>clear</tt> operations.  It does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * support the <tt>add</tt> or <tt>addAll</tt> operations.
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   948
     *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   949
     * @return a view of the values contained in this map
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
    public Collection<V> values() {
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   952
        Collection<V> vs;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   953
        return (vs = values) == null ? (values = new Values()) : vs;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   956
    final class Values extends AbstractCollection<V> {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   957
        public final int size()                 { return size; }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   958
        public final void clear()               { HashMap.this.clear(); }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   959
        public final Iterator<V> iterator()     { return new ValueIterator(); }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   960
        public final boolean contains(Object o) { return containsValue(o); }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   961
        public final Spliterator<V> spliterator() {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   962
            return new ValueSpliterator<K,V>(HashMap.this, 0, -1, 0, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        }
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   964
        public final void forEach(Consumer<? super V> action) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   965
            Node<K,V>[] tab;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   966
            if (action == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   967
                throw new NullPointerException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   968
            if (size > 0 && (tab = table) != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   969
                int mc = modCount;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   970
                for (int i = 0; i < tab.length; ++i) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   971
                    for (Node<K,V> e = tab[i]; e != null; e = e.next)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   972
                        action.accept(e.value);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   973
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   974
                if (modCount != mc)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   975
                    throw new ConcurrentModificationException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   976
            }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
   977
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     * Returns a {@link Set} view of the mappings contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     * The set is backed by the map, so changes to the map are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     * reflected in the set, and vice-versa.  If the map is modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     * while an iteration over the set is in progress (except through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     * the iterator's own <tt>remove</tt> operation, or through the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     * <tt>setValue</tt> operation on a map entry returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     * iterator) the results of the iteration are undefined.  The set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     * supports element removal, which removes the corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * mapping from the map, via the <tt>Iterator.remove</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     * <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * <tt>clear</tt> operations.  It does not support the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * <tt>add</tt> or <tt>addAll</tt> operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     * @return a set view of the mappings contained in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
    public Set<Map.Entry<K,V>> entrySet() {
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   997
        Set<Map.Entry<K,V>> es;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   998
        return (es = entrySet) == null ? (entrySet = new EntrySet()) : es;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1001
    final class EntrySet extends AbstractSet<Map.Entry<K,V>> {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1002
        public final int size()                 { return size; }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1003
        public final void clear()               { HashMap.this.clear(); }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1004
        public final Iterator<Map.Entry<K,V>> iterator() {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1005
            return new EntryIterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        }
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1007
        public final boolean contains(Object o) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
            if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                return false;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  1010
            Map.Entry<?,?> e = (Map.Entry<?,?>) o;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1011
            Object key = e.getKey();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1012
            Node<K,V> candidate = getNode(hash(key), key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            return candidate != null && candidate.equals(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        }
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1015
        public final boolean remove(Object o) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1016
            if (o instanceof Map.Entry) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1017
                Map.Entry<?,?> e = (Map.Entry<?,?>) o;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1018
                Object key = e.getKey();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1019
                Object value = e.getValue();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1020
                return removeNode(hash(key), key, value, true, true) != null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1021
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1022
            return false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1023
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1024
        public final Spliterator<Map.Entry<K,V>> spliterator() {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1025
            return new EntrySpliterator<K,V>(HashMap.this, 0, -1, 0, 0);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1026
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1027
        public final void forEach(Consumer<? super Map.Entry<K,V>> action) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1028
            Node<K,V>[] tab;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1029
            if (action == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1030
                throw new NullPointerException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1031
            if (size > 0 && (tab = table) != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1032
                int mc = modCount;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1033
                for (int i = 0; i < tab.length; ++i) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1034
                    for (Node<K,V> e = tab[i]; e != null; e = e.next)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1035
                        action.accept(e);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1036
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1037
                if (modCount != mc)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1038
                    throw new ConcurrentModificationException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1039
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1040
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1041
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1042
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1043
    // Overrides of JDK8 Map extension methods
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1044
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1045
    public V getOrDefault(Object key, V defaultValue) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1046
        Node<K,V> e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1047
        return (e = getNode(hash(key), key)) == null ? defaultValue : e.value;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1048
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1049
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1050
    public V putIfAbsent(K key, V value) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1051
        return putVal(hash(key), key, value, true, true);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1052
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1053
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1054
    public boolean remove(Object key, Object value) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1055
        return removeNode(hash(key), key, value, true, true) != null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1056
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1057
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1058
    public boolean replace(K key, V oldValue, V newValue) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1059
        Node<K,V> e; V v;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1060
        if ((e = getNode(hash(key), key)) != null &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1061
            ((v = e.value) == oldValue || (v != null && v.equals(oldValue)))) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1062
            e.value = newValue;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1063
            afterNodeAccess(e);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1064
            return true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1065
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1066
        return false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1067
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1068
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1069
    public V replace(K key, V value) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1070
        Node<K,V> e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1071
        if ((e = getNode(hash(key), key)) != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1072
            V oldValue = e.value;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1073
            e.value = value;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1074
            afterNodeAccess(e);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1075
            return oldValue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        }
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1077
        return null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1078
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1079
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1080
    public V computeIfAbsent(K key,
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1081
                             Function<? super K, ? extends V> mappingFunction) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1082
        if (mappingFunction == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1083
            throw new NullPointerException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1084
        int hash = hash(key);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1085
        Node<K,V>[] tab; Node<K,V> first; int n, i;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1086
        int binCount = 0;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1087
        TreeNode<K,V> t = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1088
        Node<K,V> old = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1089
        if (size > threshold || (tab = table) == null ||
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1090
            (n = tab.length) == 0)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1091
            n = (tab = resize()).length;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1092
        if ((first = tab[i = (n - 1) & hash]) != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1093
            if (first instanceof TreeNode)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1094
                old = (t = (TreeNode<K,V>)first).getTreeNode(hash, key);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1095
            else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1096
                Node<K,V> e = first; K k;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1097
                do {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1098
                    if (e.hash == hash &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1099
                        ((k = e.key) == key || (key != null && key.equals(k)))) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1100
                        old = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1101
                        break;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1102
                    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1103
                    ++binCount;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1104
                } while ((e = e.next) != null);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1105
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1106
            V oldValue;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1107
            if (old != null && (oldValue = old.value) != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1108
                afterNodeAccess(old);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1109
                return oldValue;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1110
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        }
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1112
        V v = mappingFunction.apply(key);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1113
        if (old != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1114
            old.value = v;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1115
            afterNodeAccess(old);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1116
            return v;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1117
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1118
        else if (v == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1119
            return null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1120
        else if (t != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1121
            t.putTreeVal(this, tab, hash, key, v);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1122
        else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1123
            tab[i] = newNode(hash, key, v, first);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1124
            if (binCount >= TREEIFY_THRESHOLD - 1)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1125
                treeifyBin(tab, hash);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1126
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1127
        ++modCount;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1128
        ++size;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1129
        afterNodeInsertion(true);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1130
        return v;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1131
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1132
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1133
    public V computeIfPresent(K key,
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1134
                              BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
20189
1e618f2a82d9 8024331: j.u.Map.computeIfPresent() default/nondefault implementations don't throw NPE if the remappingFunction is null and the key is absent
bpb
parents: 19806
diff changeset
  1135
        if (remappingFunction == null)
1e618f2a82d9 8024331: j.u.Map.computeIfPresent() default/nondefault implementations don't throw NPE if the remappingFunction is null and the key is absent
bpb
parents: 19806
diff changeset
  1136
            throw new NullPointerException();
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1137
        Node<K,V> e; V oldValue;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1138
        int hash = hash(key);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1139
        if ((e = getNode(hash, key)) != null &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1140
            (oldValue = e.value) != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1141
            V v = remappingFunction.apply(key, oldValue);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1142
            if (v != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1143
                e.value = v;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1144
                afterNodeAccess(e);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1145
                return v;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1146
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1147
            else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1148
                removeNode(hash, key, null, false, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        }
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1150
        return null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1151
    }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1152
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1153
    public V compute(K key,
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1154
                     BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1155
        if (remappingFunction == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1156
            throw new NullPointerException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1157
        int hash = hash(key);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1158
        Node<K,V>[] tab; Node<K,V> first; int n, i;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1159
        int binCount = 0;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1160
        TreeNode<K,V> t = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1161
        Node<K,V> old = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1162
        if (size > threshold || (tab = table) == null ||
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1163
            (n = tab.length) == 0)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1164
            n = (tab = resize()).length;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1165
        if ((first = tab[i = (n - 1) & hash]) != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1166
            if (first instanceof TreeNode)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1167
                old = (t = (TreeNode<K,V>)first).getTreeNode(hash, key);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1168
            else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1169
                Node<K,V> e = first; K k;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1170
                do {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1171
                    if (e.hash == hash &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1172
                        ((k = e.key) == key || (key != null && key.equals(k)))) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1173
                        old = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1174
                        break;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1175
                    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1176
                    ++binCount;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1177
                } while ((e = e.next) != null);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1178
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1179
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1180
        V oldValue = (old == null) ? null : old.value;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1181
        V v = remappingFunction.apply(key, oldValue);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1182
        if (old != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1183
            if (v != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1184
                old.value = v;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1185
                afterNodeAccess(old);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1186
            }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1187
            else
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1188
                removeNode(hash, key, null, false, true);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1189
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1190
        else if (v != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1191
            if (t != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1192
                t.putTreeVal(this, tab, hash, key, v);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1193
            else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1194
                tab[i] = newNode(hash, key, v, first);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1195
                if (binCount >= TREEIFY_THRESHOLD - 1)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1196
                    treeifyBin(tab, hash);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1197
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1198
            ++modCount;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1199
            ++size;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1200
            afterNodeInsertion(true);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1201
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1202
        return v;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1203
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1204
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1205
    public V merge(K key, V value,
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1206
                   BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1207
        if (remappingFunction == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1208
            throw new NullPointerException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1209
        int hash = hash(key);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1210
        Node<K,V>[] tab; Node<K,V> first; int n, i;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1211
        int binCount = 0;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1212
        TreeNode<K,V> t = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1213
        Node<K,V> old = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1214
        if (size > threshold || (tab = table) == null ||
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1215
            (n = tab.length) == 0)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1216
            n = (tab = resize()).length;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1217
        if ((first = tab[i = (n - 1) & hash]) != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1218
            if (first instanceof TreeNode)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1219
                old = (t = (TreeNode<K,V>)first).getTreeNode(hash, key);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1220
            else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1221
                Node<K,V> e = first; K k;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1222
                do {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1223
                    if (e.hash == hash &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1224
                        ((k = e.key) == key || (key != null && key.equals(k)))) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1225
                        old = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1226
                        break;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1227
                    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1228
                    ++binCount;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1229
                } while ((e = e.next) != null);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1230
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1231
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1232
        if (old != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1233
            V v = remappingFunction.apply(old.value, value);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1234
            if (v != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1235
                old.value = v;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1236
                afterNodeAccess(old);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1237
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1238
            else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1239
                removeNode(hash, key, null, false, true);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1240
            return v;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1241
        }
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1242
        if (value != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1243
            if (t != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1244
                t.putTreeVal(this, tab, hash, key, value);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1245
            else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1246
                tab[i] = newNode(hash, key, value, first);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1247
                if (binCount >= TREEIFY_THRESHOLD - 1)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1248
                    treeifyBin(tab, hash);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1249
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1250
            ++modCount;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1251
            ++size;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1252
            afterNodeInsertion(true);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1253
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1254
        return value;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1255
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1256
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1257
    public void forEach(BiConsumer<? super K, ? super V> action) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1258
        Node<K,V>[] tab;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1259
        if (action == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1260
            throw new NullPointerException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1261
        if (size > 0 && (tab = table) != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1262
            int mc = modCount;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1263
            for (int i = 0; i < tab.length; ++i) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1264
                for (Node<K,V> e = tab[i]; e != null; e = e.next)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1265
                    action.accept(e.key, e.value);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1266
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1267
            if (modCount != mc)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1268
                throw new ConcurrentModificationException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1269
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1270
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1271
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1272
    public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1273
        Node<K,V>[] tab;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1274
        if (function == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1275
            throw new NullPointerException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1276
        if (size > 0 && (tab = table) != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1277
            int mc = modCount;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1278
            for (int i = 0; i < tab.length; ++i) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1279
                for (Node<K,V> e = tab[i]; e != null; e = e.next) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1280
                    e.value = function.apply(e.key, e.value);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1281
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1282
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1283
            if (modCount != mc)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1284
                throw new ConcurrentModificationException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1285
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1286
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1287
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1288
    /* ------------------------------------------------------------ */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1289
    // Cloning and serialization
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1290
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1291
    /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1292
     * Returns a shallow copy of this <tt>HashMap</tt> instance: the keys and
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1293
     * values themselves are not cloned.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1294
     *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1295
     * @return a shallow copy of this map
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1296
     */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1297
    @SuppressWarnings("unchecked")
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1298
    public Object clone() {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1299
        HashMap<K,V> result;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1300
        try {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1301
            result = (HashMap<K,V>)super.clone();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1302
        } catch (CloneNotSupportedException e) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1303
            // this shouldn't happen, since we are Cloneable
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1304
            throw new InternalError(e);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1305
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1306
        result.reinitialize();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1307
        result.putMapEntries(this, false);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1308
        return result;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1309
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1310
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1311
    // These methods are also used when serializing HashSets
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1312
    final float loadFactor() { return loadFactor; }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1313
    final int capacity() {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1314
        return (table != null) ? table.length :
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1315
            (threshold > 0) ? threshold :
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1316
            DEFAULT_INITIAL_CAPACITY;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
     * Save the state of the <tt>HashMap</tt> instance to a stream (i.e.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
     * serialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
     * @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
  1324
     *             bucket array) is emitted (int), followed by the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
     *             <i>size</i> (an int, the number of key-value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
     *             mappings), followed by the key (Object) and value (Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     *             for each key-value mapping.  The key-value mappings are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
     *             emitted in no particular order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
    private void writeObject(java.io.ObjectOutputStream s)
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1331
        throws IOException {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1332
        int buckets = capacity();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
        // Write out the threshold, loadfactor, and any hidden stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
        s.defaultWriteObject();
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1335
        s.writeInt(buckets);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
        s.writeInt(size);
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1337
        internalWriteEntries(s);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
    /**
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1341
     * Reconstitute the {@code HashMap} instance from a stream (i.e.,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
     * deserialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
    private void readObject(java.io.ObjectInputStream s)
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1345
        throws IOException, ClassNotFoundException {
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1346
        // Read in the threshold (ignored), loadfactor, and any hidden stuff
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
        s.defaultReadObject();
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1348
        reinitialize();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1349
        if (loadFactor <= 0 || Float.isNaN(loadFactor))
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1350
            throw new InvalidObjectException("Illegal load factor: " +
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1351
                                             loadFactor);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1352
        s.readInt();                // Read and ignore number of buckets
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1353
        int mappings = s.readInt(); // Read number of mappings (size)
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1354
        if (mappings < 0)
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1355
            throw new InvalidObjectException("Illegal mappings count: " +
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1356
                                             mappings);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1357
        else if (mappings > 0) { // (if zero, use defaults)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1358
            // Size the table using given load factor only if within
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1359
            // range of 0.25...4.0
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1360
            float lf = Math.min(Math.max(0.25f, loadFactor), 4.0f);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1361
            float fc = (float)mappings / lf + 1.0f;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1362
            int cap = ((fc < DEFAULT_INITIAL_CAPACITY) ?
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1363
                       DEFAULT_INITIAL_CAPACITY :
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1364
                       (fc >= MAXIMUM_CAPACITY) ?
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1365
                       MAXIMUM_CAPACITY :
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1366
                       tableSizeFor((int)fc));
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1367
            float ft = (float)cap * lf;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1368
            threshold = ((cap < MAXIMUM_CAPACITY && ft < MAXIMUM_CAPACITY) ?
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1369
                         (int)ft : Integer.MAX_VALUE);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1370
            @SuppressWarnings({"rawtypes","unchecked"})
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1371
                Node<K,V>[] tab = (Node<K,V>[])new Node[cap];
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1372
            table = tab;
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1373
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1374
            // Read the keys and values, and put the mappings in the HashMap
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1375
            for (int i = 0; i < mappings; i++) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1376
                @SuppressWarnings("unchecked")
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1377
                    K key = (K) s.readObject();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1378
                @SuppressWarnings("unchecked")
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1379
                    V value = (V) s.readObject();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1380
                putVal(hash(key), key, value, false, false);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1381
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1385
    /* ------------------------------------------------------------ */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1386
    // iterators
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1387
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1388
    abstract class HashIterator {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1389
        Node<K,V> next;        // next entry to return
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1390
        Node<K,V> current;     // current entry
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1391
        int expectedModCount;  // for fast-fail
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1392
        int index;             // current slot
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1393
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1394
        HashIterator() {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1395
            expectedModCount = modCount;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1396
            Node<K,V>[] t = table;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1397
            current = next = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1398
            index = 0;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1399
            if (t != null && size > 0) { // advance to first entry
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1400
                do {} while (index < t.length && (next = t[index++]) == null);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1401
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1402
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1403
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1404
        public final boolean hasNext() {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1405
            return next != null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1406
        }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1407
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1408
        final Node<K,V> nextNode() {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1409
            Node<K,V>[] t;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1410
            Node<K,V> e = next;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1411
            if (modCount != expectedModCount)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1412
                throw new ConcurrentModificationException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1413
            if (e == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1414
                throw new NoSuchElementException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1415
            if ((next = (current = e).next) == null && (t = table) != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1416
                do {} while (index < t.length && (next = t[index++]) == null);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1417
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1418
            return e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1419
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1420
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1421
        public final void remove() {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1422
            Node<K,V> p = current;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1423
            if (p == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1424
                throw new IllegalStateException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1425
            if (modCount != expectedModCount)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1426
                throw new ConcurrentModificationException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1427
            current = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1428
            K key = p.key;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1429
            removeNode(hash(key), key, null, false, false);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1430
            expectedModCount = modCount;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1431
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1432
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1433
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1434
    final class KeyIterator extends HashIterator
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1435
        implements Iterator<K> {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1436
        public final K next() { return nextNode().key; }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1437
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1438
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1439
    final class ValueIterator extends HashIterator
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1440
        implements Iterator<V> {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1441
        public final V next() { return nextNode().value; }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1442
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1443
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1444
    final class EntryIterator extends HashIterator
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1445
        implements Iterator<Map.Entry<K,V>> {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1446
        public final Map.Entry<K,V> next() { return nextNode(); }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1447
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1448
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1449
    /* ------------------------------------------------------------ */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1450
    // spliterators
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1451
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1452
    static class HashMapSpliterator<K,V> {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1453
        final HashMap<K,V> map;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1454
        Node<K,V> current;          // current node
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1455
        int index;                  // current index, modified on advance/split
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1456
        int fence;                  // one past last index
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1457
        int est;                    // size estimate
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1458
        int expectedModCount;       // for comodification checks
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1459
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1460
        HashMapSpliterator(HashMap<K,V> m, int origin,
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1461
                           int fence, int est,
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1462
                           int expectedModCount) {
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1463
            this.map = m;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1464
            this.index = origin;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1465
            this.fence = fence;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1466
            this.est = est;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1467
            this.expectedModCount = expectedModCount;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1468
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1469
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1470
        final int getFence() { // initialize fence and size on first use
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1471
            int hi;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1472
            if ((hi = fence) < 0) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1473
                HashMap<K,V> m = map;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1474
                est = m.size;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1475
                expectedModCount = m.modCount;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1476
                Node<K,V>[] tab = m.table;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1477
                hi = fence = (tab == null) ? 0 : tab.length;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1478
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1479
            return hi;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1480
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1481
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1482
        public final long estimateSize() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1483
            getFence(); // force init
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1484
            return (long) est;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1485
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1486
    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1487
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1488
    static final class KeySpliterator<K,V>
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1489
        extends HashMapSpliterator<K,V>
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1490
        implements Spliterator<K> {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1491
        KeySpliterator(HashMap<K,V> m, int origin, int fence, int est,
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1492
                       int expectedModCount) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1493
            super(m, origin, fence, est, expectedModCount);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1494
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1495
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1496
        public KeySpliterator<K,V> trySplit() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1497
            int hi = getFence(), lo = index, mid = (lo + hi) >>> 1;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1498
            return (lo >= mid || current != null) ? null :
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1499
                new KeySpliterator<K,V>(map, lo, index = mid, est >>>= 1,
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1500
                                        expectedModCount);
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1501
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1502
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1503
        public void forEachRemaining(Consumer<? super K> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1504
            int i, hi, mc;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1505
            if (action == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1506
                throw new NullPointerException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1507
            HashMap<K,V> m = map;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1508
            Node<K,V>[] tab = m.table;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1509
            if ((hi = fence) < 0) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1510
                mc = expectedModCount = m.modCount;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1511
                hi = fence = (tab == null) ? 0 : tab.length;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1512
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1513
            else
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1514
                mc = expectedModCount;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1515
            if (tab != null && tab.length >= hi &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1516
                (i = index) >= 0 && (i < (index = hi) || current != null)) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1517
                Node<K,V> p = current;
17949
6c33d8f2601e 8013649: HashMap spliterator tryAdvance() encounters remaining elements after forEachRemaining()
psandoz
parents: 17939
diff changeset
  1518
                current = null;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1519
                do {
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1520
                    if (p == null)
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1521
                        p = tab[i++];
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1522
                    else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1523
                        action.accept(p.key);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1524
                        p = p.next;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1525
                    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1526
                } while (p != null || i < hi);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1527
                if (m.modCount != mc)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1528
                    throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1529
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1530
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1531
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1532
        public boolean tryAdvance(Consumer<? super K> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1533
            int hi;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1534
            if (action == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1535
                throw new NullPointerException();
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1536
            Node<K,V>[] tab = map.table;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1537
            if (tab != null && tab.length >= (hi = getFence()) && index >= 0) {
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1538
                while (current != null || index < hi) {
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1539
                    if (current == null)
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1540
                        current = tab[index++];
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1541
                    else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1542
                        K k = current.key;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1543
                        current = current.next;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1544
                        action.accept(k);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1545
                        if (map.modCount != expectedModCount)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1546
                            throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1547
                        return true;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1548
                    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1549
                }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1550
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1551
            return false;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1552
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1553
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1554
        public int characteristics() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1555
            return (fence < 0 || est == map.size ? Spliterator.SIZED : 0) |
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1556
                Spliterator.DISTINCT;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1557
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1558
    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1559
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1560
    static final class ValueSpliterator<K,V>
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1561
        extends HashMapSpliterator<K,V>
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1562
        implements Spliterator<V> {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1563
        ValueSpliterator(HashMap<K,V> m, int origin, int fence, int est,
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1564
                         int expectedModCount) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1565
            super(m, origin, fence, est, expectedModCount);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1566
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1567
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1568
        public ValueSpliterator<K,V> trySplit() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1569
            int hi = getFence(), lo = index, mid = (lo + hi) >>> 1;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1570
            return (lo >= mid || current != null) ? null :
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1571
                new ValueSpliterator<K,V>(map, lo, index = mid, est >>>= 1,
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1572
                                          expectedModCount);
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1573
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1574
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1575
        public void forEachRemaining(Consumer<? super V> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1576
            int i, hi, mc;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1577
            if (action == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1578
                throw new NullPointerException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1579
            HashMap<K,V> m = map;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1580
            Node<K,V>[] tab = m.table;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1581
            if ((hi = fence) < 0) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1582
                mc = expectedModCount = m.modCount;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1583
                hi = fence = (tab == null) ? 0 : tab.length;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1584
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1585
            else
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1586
                mc = expectedModCount;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1587
            if (tab != null && tab.length >= hi &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1588
                (i = index) >= 0 && (i < (index = hi) || current != null)) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1589
                Node<K,V> p = current;
17949
6c33d8f2601e 8013649: HashMap spliterator tryAdvance() encounters remaining elements after forEachRemaining()
psandoz
parents: 17939
diff changeset
  1590
                current = null;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1591
                do {
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1592
                    if (p == null)
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1593
                        p = tab[i++];
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1594
                    else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1595
                        action.accept(p.value);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1596
                        p = p.next;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1597
                    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1598
                } while (p != null || i < hi);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1599
                if (m.modCount != mc)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1600
                    throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1601
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1602
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1603
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1604
        public boolean tryAdvance(Consumer<? super V> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1605
            int hi;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1606
            if (action == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1607
                throw new NullPointerException();
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1608
            Node<K,V>[] tab = map.table;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1609
            if (tab != null && tab.length >= (hi = getFence()) && index >= 0) {
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1610
                while (current != null || index < hi) {
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1611
                    if (current == null)
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1612
                        current = tab[index++];
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1613
                    else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1614
                        V v = current.value;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1615
                        current = current.next;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1616
                        action.accept(v);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1617
                        if (map.modCount != expectedModCount)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1618
                            throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1619
                        return true;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1620
                    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1621
                }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1622
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1623
            return false;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1624
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1625
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1626
        public int characteristics() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1627
            return (fence < 0 || est == map.size ? Spliterator.SIZED : 0);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1628
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1629
    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1630
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1631
    static final class EntrySpliterator<K,V>
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1632
        extends HashMapSpliterator<K,V>
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1633
        implements Spliterator<Map.Entry<K,V>> {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1634
        EntrySpliterator(HashMap<K,V> m, int origin, int fence, int est,
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1635
                         int expectedModCount) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1636
            super(m, origin, fence, est, expectedModCount);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1637
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1638
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1639
        public EntrySpliterator<K,V> trySplit() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1640
            int hi = getFence(), lo = index, mid = (lo + hi) >>> 1;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1641
            return (lo >= mid || current != null) ? null :
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1642
                new EntrySpliterator<K,V>(map, lo, index = mid, est >>>= 1,
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1643
                                          expectedModCount);
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1644
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1645
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1646
        public void forEachRemaining(Consumer<? super Map.Entry<K,V>> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1647
            int i, hi, mc;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1648
            if (action == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1649
                throw new NullPointerException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1650
            HashMap<K,V> m = map;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1651
            Node<K,V>[] tab = m.table;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1652
            if ((hi = fence) < 0) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1653
                mc = expectedModCount = m.modCount;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1654
                hi = fence = (tab == null) ? 0 : tab.length;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1655
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1656
            else
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1657
                mc = expectedModCount;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1658
            if (tab != null && tab.length >= hi &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1659
                (i = index) >= 0 && (i < (index = hi) || current != null)) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1660
                Node<K,V> p = current;
17949
6c33d8f2601e 8013649: HashMap spliterator tryAdvance() encounters remaining elements after forEachRemaining()
psandoz
parents: 17939
diff changeset
  1661
                current = null;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1662
                do {
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1663
                    if (p == null)
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1664
                        p = tab[i++];
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1665
                    else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1666
                        action.accept(p);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1667
                        p = p.next;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1668
                    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1669
                } while (p != null || i < hi);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1670
                if (m.modCount != mc)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1671
                    throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1672
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1673
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1674
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1675
        public boolean tryAdvance(Consumer<? super Map.Entry<K,V>> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1676
            int hi;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1677
            if (action == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1678
                throw new NullPointerException();
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1679
            Node<K,V>[] tab = map.table;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1680
            if (tab != null && tab.length >= (hi = getFence()) && index >= 0) {
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1681
                while (current != null || index < hi) {
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1682
                    if (current == null)
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1683
                        current = tab[index++];
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1684
                    else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1685
                        Node<K,V> e = current;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1686
                        current = current.next;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1687
                        action.accept(e);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1688
                        if (map.modCount != expectedModCount)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1689
                            throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1690
                        return true;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1691
                    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1692
                }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1693
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1694
            return false;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1695
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1696
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1697
        public int characteristics() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1698
            return (fence < 0 || est == map.size ? Spliterator.SIZED : 0) |
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1699
                Spliterator.DISTINCT;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1700
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1701
    }
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1702
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1703
    /* ------------------------------------------------------------ */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1704
    // LinkedHashMap support
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1705
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1706
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1707
    /*
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1708
     * The following package-protected methods are designed to be
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1709
     * overridden by LinkedHashMap, but not by any other subclass.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1710
     * Nearly all other internal methods are also package-protected
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1711
     * but are declared final, so can be used by LinkedHashMap, view
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1712
     * classes, and HashSet.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1713
     */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1714
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1715
    // Create a regular (non-tree) node
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1716
    Node<K,V> newNode(int hash, K key, V value, Node<K,V> next) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1717
        return new Node<K,V>(hash, key, value, next);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1718
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1719
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1720
    // For conversion from TreeNodes to plain nodes
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1721
    Node<K,V> replacementNode(Node<K,V> p, Node<K,V> next) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1722
        return new Node<K,V>(p.hash, p.key, p.value, next);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1723
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1724
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1725
    // Create a tree bin node
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1726
    TreeNode<K,V> newTreeNode(int hash, K key, V value, Node<K,V> next) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1727
        return new TreeNode<K,V>(hash, key, value, next);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1728
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1729
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1730
    // For treeifyBin
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1731
    TreeNode<K,V> replacementTreeNode(Node<K,V> p, Node<K,V> next) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1732
        return new TreeNode<K,V>(p.hash, p.key, p.value, next);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1733
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1734
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1735
    /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1736
     * Reset to initial default state.  Called by clone and readObject.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1737
     */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1738
    void reinitialize() {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1739
        table = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1740
        entrySet = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1741
        keySet = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1742
        values = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1743
        modCount = 0;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1744
        threshold = 0;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1745
        size = 0;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1746
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1747
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1748
    // Callbacks to allow LinkedHashMap post-actions
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1749
    void afterNodeAccess(Node<K,V> p) { }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1750
    void afterNodeInsertion(boolean evict) { }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1751
    void afterNodeRemoval(Node<K,V> p) { }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1752
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1753
    // Called only from writeObject, to ensure compatible ordering.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1754
    void internalWriteEntries(java.io.ObjectOutputStream s) throws IOException {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1755
        Node<K,V>[] tab;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1756
        if (size > 0 && (tab = table) != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1757
            for (int i = 0; i < tab.length; ++i) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1758
                for (Node<K,V> e = tab[i]; e != null; e = e.next) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1759
                    s.writeObject(e.key);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1760
                    s.writeObject(e.value);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1761
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1762
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1763
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1764
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1765
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1766
    /* ------------------------------------------------------------ */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1767
    // Tree bins
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1768
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1769
    /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1770
     * Entry for Tree bins. Extends LinkedHashMap.Entry (which in turn
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1771
     * extends Node) so can be used as extension of either regular or
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1772
     * linked node.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1773
     */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1774
    static final class TreeNode<K,V> extends LinkedHashMap.Entry<K,V> {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1775
        TreeNode<K,V> parent;  // red-black tree links
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1776
        TreeNode<K,V> left;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1777
        TreeNode<K,V> right;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1778
        TreeNode<K,V> prev;    // needed to unlink next upon deletion
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1779
        boolean red;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1780
        TreeNode(int hash, K key, V val, Node<K,V> next) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1781
            super(hash, key, val, next);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1782
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1783
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1784
        /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1785
         * Returns root of tree containing this node.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1786
         */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1787
        final TreeNode<K,V> root() {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1788
            for (TreeNode<K,V> r = this, p;;) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1789
                if ((p = r.parent) == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1790
                    return r;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1791
                r = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1792
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1793
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1794
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1795
        /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1796
         * Ensures that the given root is the first node of its bin.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1797
         */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1798
        static <K,V> void moveRootToFront(Node<K,V>[] tab, TreeNode<K,V> root) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1799
            int n;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1800
            if (root != null && tab != null && (n = tab.length) > 0) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1801
                int index = (n - 1) & root.hash;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1802
                TreeNode<K,V> first = (TreeNode<K,V>)tab[index];
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1803
                if (root != first) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1804
                    Node<K,V> rn;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1805
                    tab[index] = root;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1806
                    TreeNode<K,V> rp = root.prev;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1807
                    if ((rn = root.next) != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1808
                        ((TreeNode<K,V>)rn).prev = rp;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1809
                    if (rp != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1810
                        rp.next = rn;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1811
                    if (first != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1812
                        first.prev = root;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1813
                    root.next = first;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1814
                    root.prev = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1815
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1816
                assert checkInvariants(root);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1817
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1818
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1819
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1820
        /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1821
         * Finds the node starting at root p with the given hash and key.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1822
         * The kc argument caches comparableClassFor(key) upon first use
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1823
         * comparing keys.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1824
         */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1825
        final TreeNode<K,V> find(int h, Object k, Class<?> kc) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1826
            TreeNode<K,V> p = this;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1827
            do {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1828
                int ph, dir; K pk;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1829
                TreeNode<K,V> pl = p.left, pr = p.right, q;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1830
                if ((ph = p.hash) > h)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1831
                    p = pl;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1832
                else if (ph < h)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1833
                    p = pr;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1834
                else if ((pk = p.key) == k || (k != null && k.equals(pk)))
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1835
                    return p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1836
                else if (pl == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1837
                    p = pr;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1838
                else if (pr == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1839
                    p = pl;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1840
                else if ((kc != null ||
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1841
                          (kc = comparableClassFor(k)) != null) &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1842
                         (dir = compareComparables(kc, k, pk)) != 0)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1843
                    p = (dir < 0) ? pl : pr;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1844
                else if ((q = pr.find(h, k, kc)) != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1845
                    return q;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1846
                else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1847
                    p = pl;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1848
            } while (p != null);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1849
            return null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1850
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1851
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1852
        /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1853
         * Calls find for root node.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1854
         */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1855
        final TreeNode<K,V> getTreeNode(int h, Object k) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1856
            return ((parent != null) ? root() : this).find(h, k, null);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1857
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1858
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1859
        /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1860
         * Tie-breaking utility for ordering insertions when equal
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1861
         * hashCodes and non-comparable. We don't require a total
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1862
         * order, just a consistent insertion rule to maintain
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1863
         * equivalence across rebalancings. Tie-breaking further than
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1864
         * necessary simplifies testing a bit.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1865
         */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1866
        static int tieBreakOrder(Object a, Object b) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1867
            int d;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1868
            if (a == null || b == null ||
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1869
                (d = a.getClass().getName().
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1870
                 compareTo(b.getClass().getName())) == 0)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1871
                d = (System.identityHashCode(a) <= System.identityHashCode(b) ?
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1872
                     -1 : 1);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1873
            return d;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1874
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1875
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1876
        /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1877
         * Forms tree of the nodes linked from this node.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1878
         * @return root of tree
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1879
         */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1880
        final void treeify(Node<K,V>[] tab) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1881
            TreeNode<K,V> root = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1882
            for (TreeNode<K,V> x = this, next; x != null; x = next) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1883
                next = (TreeNode<K,V>)x.next;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1884
                x.left = x.right = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1885
                if (root == null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1886
                    x.parent = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1887
                    x.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1888
                    root = x;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1889
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1890
                else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1891
                    K k = x.key;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1892
                    int h = x.hash;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1893
                    Class<?> kc = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1894
                    for (TreeNode<K,V> p = root;;) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1895
                        int dir, ph;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1896
                        K pk = p.key;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1897
                        if ((ph = p.hash) > h)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1898
                            dir = -1;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1899
                        else if (ph < h)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1900
                            dir = 1;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1901
                        else if ((kc == null &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1902
                                  (kc = comparableClassFor(k)) == null) ||
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1903
                                 (dir = compareComparables(kc, k, pk)) == 0)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1904
                            dir = tieBreakOrder(k, pk);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1905
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1906
                        TreeNode<K,V> xp = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1907
                        if ((p = (dir <= 0) ? p.left : p.right) == null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1908
                            x.parent = xp;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1909
                            if (dir <= 0)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1910
                                xp.left = x;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1911
                            else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1912
                                xp.right = x;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1913
                            root = balanceInsertion(root, x);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1914
                            break;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1915
                        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1916
                    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1917
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1918
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1919
            moveRootToFront(tab, root);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1920
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1921
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1922
        /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1923
         * Returns a list of non-TreeNodes replacing those linked from
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1924
         * this node.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1925
         */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1926
        final Node<K,V> untreeify(HashMap<K,V> map) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1927
            Node<K,V> hd = null, tl = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1928
            for (Node<K,V> q = this; q != null; q = q.next) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1929
                Node<K,V> p = map.replacementNode(q, null);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1930
                if (tl == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1931
                    hd = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1932
                else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1933
                    tl.next = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1934
                tl = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1935
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1936
            return hd;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1937
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1938
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1939
        /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1940
         * Tree version of putVal.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1941
         */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1942
        final TreeNode<K,V> putTreeVal(HashMap<K,V> map, Node<K,V>[] tab,
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1943
                                       int h, K k, V v) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1944
            Class<?> kc = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1945
            boolean searched = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1946
            TreeNode<K,V> root = (parent != null) ? root() : this;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1947
            for (TreeNode<K,V> p = root;;) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1948
                int dir, ph; K pk;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1949
                if ((ph = p.hash) > h)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1950
                    dir = -1;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1951
                else if (ph < h)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1952
                    dir = 1;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1953
                else if ((pk = p.key) == k || (pk != null && k.equals(pk)))
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1954
                    return p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1955
                else if ((kc == null &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1956
                          (kc = comparableClassFor(k)) == null) ||
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1957
                         (dir = compareComparables(kc, k, pk)) == 0) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1958
                    if (!searched) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1959
                        TreeNode<K,V> q, ch;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1960
                        searched = true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1961
                        if (((ch = p.left) != null &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1962
                             (q = ch.find(h, k, kc)) != null) ||
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1963
                            ((ch = p.right) != null &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1964
                             (q = ch.find(h, k, kc)) != null))
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1965
                            return q;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1966
                    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1967
                    dir = tieBreakOrder(k, pk);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1968
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1969
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1970
                TreeNode<K,V> xp = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1971
                if ((p = (dir <= 0) ? p.left : p.right) == null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1972
                    Node<K,V> xpn = xp.next;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1973
                    TreeNode<K,V> x = map.newTreeNode(h, k, v, xpn);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1974
                    if (dir <= 0)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1975
                        xp.left = x;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1976
                    else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1977
                        xp.right = x;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1978
                    xp.next = x;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1979
                    x.parent = x.prev = xp;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1980
                    if (xpn != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1981
                        ((TreeNode<K,V>)xpn).prev = x;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1982
                    moveRootToFront(tab, balanceInsertion(root, x));
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1983
                    return null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1984
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1985
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1986
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1987
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1988
        /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1989
         * Removes the given node, that must be present before this call.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1990
         * This is messier than typical red-black deletion code because we
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1991
         * cannot swap the contents of an interior node with a leaf
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1992
         * successor that is pinned by "next" pointers that are accessible
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1993
         * independently during traversal. So instead we swap the tree
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1994
         * linkages. If the current tree appears to have too few nodes,
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1995
         * the bin is converted back to a plain bin. (The test triggers
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1996
         * somewhere between 2 and 6 nodes, depending on tree structure).
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1997
         */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1998
        final void removeTreeNode(HashMap<K,V> map, Node<K,V>[] tab,
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1999
                                  boolean movable) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2000
            int n;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2001
            if (tab == null || (n = tab.length) == 0)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2002
                return;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2003
            int index = (n - 1) & hash;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2004
            TreeNode<K,V> first = (TreeNode<K,V>)tab[index], root = first, rl;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2005
            TreeNode<K,V> succ = (TreeNode<K,V>)next, pred = prev;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2006
            if (pred == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2007
                tab[index] = first = succ;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2008
            else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2009
                pred.next = succ;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2010
            if (succ != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2011
                succ.prev = pred;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2012
            if (first == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2013
                return;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2014
            if (root.parent != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2015
                root = root.root();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2016
            if (root == null || root.right == null ||
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2017
                (rl = root.left) == null || rl.left == null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2018
                tab[index] = first.untreeify(map);  // too small
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2019
                return;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2020
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2021
            TreeNode<K,V> p = this, pl = left, pr = right, replacement;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2022
            if (pl != null && pr != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2023
                TreeNode<K,V> s = pr, sl;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2024
                while ((sl = s.left) != null) // find successor
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2025
                    s = sl;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2026
                boolean c = s.red; s.red = p.red; p.red = c; // swap colors
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2027
                TreeNode<K,V> sr = s.right;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2028
                TreeNode<K,V> pp = p.parent;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2029
                if (s == pr) { // p was s's direct parent
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2030
                    p.parent = s;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2031
                    s.right = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2032
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2033
                else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2034
                    TreeNode<K,V> sp = s.parent;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2035
                    if ((p.parent = sp) != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2036
                        if (s == sp.left)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2037
                            sp.left = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2038
                        else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2039
                            sp.right = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2040
                    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2041
                    if ((s.right = pr) != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2042
                        pr.parent = s;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2043
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2044
                p.left = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2045
                if ((p.right = sr) != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2046
                    sr.parent = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2047
                if ((s.left = pl) != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2048
                    pl.parent = s;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2049
                if ((s.parent = pp) == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2050
                    root = s;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2051
                else if (p == pp.left)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2052
                    pp.left = s;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2053
                else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2054
                    pp.right = s;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2055
                if (sr != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2056
                    replacement = sr;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2057
                else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2058
                    replacement = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2059
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2060
            else if (pl != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2061
                replacement = pl;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2062
            else if (pr != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2063
                replacement = pr;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2064
            else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2065
                replacement = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2066
            if (replacement != p) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2067
                TreeNode<K,V> pp = replacement.parent = p.parent;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2068
                if (pp == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2069
                    root = replacement;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2070
                else if (p == pp.left)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2071
                    pp.left = replacement;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2072
                else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2073
                    pp.right = replacement;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2074
                p.left = p.right = p.parent = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2075
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2076
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2077
            TreeNode<K,V> r = p.red ? root : balanceDeletion(root, replacement);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2078
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2079
            if (replacement == p) {  // detach
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2080
                TreeNode<K,V> pp = p.parent;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2081
                p.parent = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2082
                if (pp != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2083
                    if (p == pp.left)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2084
                        pp.left = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2085
                    else if (p == pp.right)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2086
                        pp.right = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2087
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2088
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2089
            if (movable)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2090
                moveRootToFront(tab, r);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2091
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2092
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2093
        /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2094
         * Splits nodes in a tree bin into lower and upper tree bins,
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2095
         * or untreeifies if now too small. Called only from resize;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2096
         * see above discussion about split bits and indices.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2097
         *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2098
         * @param map the map
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2099
         * @param tab the table for recording bin heads
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2100
         * @param index the index of the table being split
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2101
         * @param bit the bit of hash to split on
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2102
         */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2103
        final void split(HashMap<K,V> map, Node<K,V>[] tab, int index, int bit) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2104
            TreeNode<K,V> b = this;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2105
            // Relink into lo and hi lists, preserving order
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2106
            TreeNode<K,V> loHead = null, loTail = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2107
            TreeNode<K,V> hiHead = null, hiTail = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2108
            int lc = 0, hc = 0;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2109
            for (TreeNode<K,V> e = b, next; e != null; e = next) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2110
                next = (TreeNode<K,V>)e.next;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2111
                e.next = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2112
                if ((e.hash & bit) == 0) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2113
                    if ((e.prev = loTail) == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2114
                        loHead = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2115
                    else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2116
                        loTail.next = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2117
                    loTail = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2118
                    ++lc;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2119
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2120
                else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2121
                    if ((e.prev = hiTail) == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2122
                        hiHead = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2123
                    else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2124
                        hiTail.next = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2125
                    hiTail = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2126
                    ++hc;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2127
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2128
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2129
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2130
            if (loHead != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2131
                if (lc <= UNTREEIFY_THRESHOLD)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2132
                    tab[index] = loHead.untreeify(map);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2133
                else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2134
                    tab[index] = loHead;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2135
                    if (hiHead != null) // (else is already treeified)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2136
                        loHead.treeify(tab);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2137
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2138
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2139
            if (hiHead != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2140
                if (hc <= UNTREEIFY_THRESHOLD)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2141
                    tab[index + bit] = hiHead.untreeify(map);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2142
                else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2143
                    tab[index + bit] = hiHead;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2144
                    if (loHead != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2145
                        hiHead.treeify(tab);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2146
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2147
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2148
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2149
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2150
        /* ------------------------------------------------------------ */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2151
        // Red-black tree methods, all adapted from CLR
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2152
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2153
        static <K,V> TreeNode<K,V> rotateLeft(TreeNode<K,V> root,
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2154
                                              TreeNode<K,V> p) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2155
            TreeNode<K,V> r, pp, rl;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2156
            if (p != null && (r = p.right) != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2157
                if ((rl = p.right = r.left) != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2158
                    rl.parent = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2159
                if ((pp = r.parent = p.parent) == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2160
                    (root = r).red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2161
                else if (pp.left == p)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2162
                    pp.left = r;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2163
                else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2164
                    pp.right = r;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2165
                r.left = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2166
                p.parent = r;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2167
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2168
            return root;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2169
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2170
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2171
        static <K,V> TreeNode<K,V> rotateRight(TreeNode<K,V> root,
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2172
                                               TreeNode<K,V> p) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2173
            TreeNode<K,V> l, pp, lr;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2174
            if (p != null && (l = p.left) != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2175
                if ((lr = p.left = l.right) != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2176
                    lr.parent = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2177
                if ((pp = l.parent = p.parent) == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2178
                    (root = l).red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2179
                else if (pp.right == p)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2180
                    pp.right = l;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2181
                else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2182
                    pp.left = l;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2183
                l.right = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2184
                p.parent = l;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2185
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2186
            return root;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2187
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2188
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2189
        static <K,V> TreeNode<K,V> balanceInsertion(TreeNode<K,V> root,
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2190
                                                    TreeNode<K,V> x) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2191
            x.red = true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2192
            for (TreeNode<K,V> xp, xpp, xppl, xppr;;) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2193
                if ((xp = x.parent) == null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2194
                    x.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2195
                    return x;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2196
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2197
                else if (!xp.red || (xpp = xp.parent) == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2198
                    return root;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2199
                if (xp == (xppl = xpp.left)) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2200
                    if ((xppr = xpp.right) != null && xppr.red) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2201
                        xppr.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2202
                        xp.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2203
                        xpp.red = true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2204
                        x = xpp;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2205
                    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2206
                    else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2207
                        if (x == xp.right) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2208
                            root = rotateLeft(root, x = xp);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2209
                            xpp = (xp = x.parent) == null ? null : xp.parent;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2210
                        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2211
                        if (xp != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2212
                            xp.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2213
                            if (xpp != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2214
                                xpp.red = true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2215
                                root = rotateRight(root, xpp);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2216
                            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2217
                        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2218
                    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2219
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2220
                else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2221
                    if (xppl != null && xppl.red) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2222
                        xppl.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2223
                        xp.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2224
                        xpp.red = true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2225
                        x = xpp;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2226
                    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2227
                    else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2228
                        if (x == xp.left) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2229
                            root = rotateRight(root, x = xp);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2230
                            xpp = (xp = x.parent) == null ? null : xp.parent;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2231
                        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2232
                        if (xp != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2233
                            xp.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2234
                            if (xpp != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2235
                                xpp.red = true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2236
                                root = rotateLeft(root, xpp);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2237
                            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2238
                        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2239
                    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2240
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2241
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2242
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2243
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2244
        static <K,V> TreeNode<K,V> balanceDeletion(TreeNode<K,V> root,
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2245
                                                   TreeNode<K,V> x) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2246
            for (TreeNode<K,V> xp, xpl, xpr;;)  {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2247
                if (x == null || x == root)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2248
                    return root;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2249
                else if ((xp = x.parent) == null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2250
                    x.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2251
                    return x;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2252
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2253
                else if (x.red) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2254
                    x.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2255
                    return root;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2256
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2257
                else if ((xpl = xp.left) == x) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2258
                    if ((xpr = xp.right) != null && xpr.red) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2259
                        xpr.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2260
                        xp.red = true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2261
                        root = rotateLeft(root, xp);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2262
                        xpr = (xp = x.parent) == null ? null : xp.right;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2263
                    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2264
                    if (xpr == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2265
                        x = xp;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2266
                    else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2267
                        TreeNode<K,V> sl = xpr.left, sr = xpr.right;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2268
                        if ((sr == null || !sr.red) &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2269
                            (sl == null || !sl.red)) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2270
                            xpr.red = true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2271
                            x = xp;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2272
                        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2273
                        else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2274
                            if (sr == null || !sr.red) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2275
                                if (sl != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2276
                                    sl.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2277
                                xpr.red = true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2278
                                root = rotateRight(root, xpr);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2279
                                xpr = (xp = x.parent) == null ?
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2280
                                    null : xp.right;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2281
                            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2282
                            if (xpr != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2283
                                xpr.red = (xp == null) ? false : xp.red;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2284
                                if ((sr = xpr.right) != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2285
                                    sr.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2286
                            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2287
                            if (xp != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2288
                                xp.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2289
                                root = rotateLeft(root, xp);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2290
                            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2291
                            x = root;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2292
                        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2293
                    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2294
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2295
                else { // symmetric
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2296
                    if (xpl != null && xpl.red) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2297
                        xpl.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2298
                        xp.red = true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2299
                        root = rotateRight(root, xp);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2300
                        xpl = (xp = x.parent) == null ? null : xp.left;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2301
                    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2302
                    if (xpl == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2303
                        x = xp;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2304
                    else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2305
                        TreeNode<K,V> sl = xpl.left, sr = xpl.right;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2306
                        if ((sl == null || !sl.red) &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2307
                            (sr == null || !sr.red)) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2308
                            xpl.red = true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2309
                            x = xp;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2310
                        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2311
                        else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2312
                            if (sl == null || !sl.red) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2313
                                if (sr != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2314
                                    sr.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2315
                                xpl.red = true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2316
                                root = rotateLeft(root, xpl);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2317
                                xpl = (xp = x.parent) == null ?
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2318
                                    null : xp.left;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2319
                            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2320
                            if (xpl != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2321
                                xpl.red = (xp == null) ? false : xp.red;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2322
                                if ((sl = xpl.left) != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2323
                                    sl.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2324
                            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2325
                            if (xp != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2326
                                xp.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2327
                                root = rotateRight(root, xp);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2328
                            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2329
                            x = root;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2330
                        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2331
                    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2332
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2333
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2334
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2335
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2336
        /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2337
         * Recursive invariant check
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2338
         */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2339
        static <K,V> boolean checkInvariants(TreeNode<K,V> t) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2340
            TreeNode<K,V> tp = t.parent, tl = t.left, tr = t.right,
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2341
                tb = t.prev, tn = (TreeNode<K,V>)t.next;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2342
            if (tb != null && tb.next != t)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2343
                return false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2344
            if (tn != null && tn.prev != t)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2345
                return false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2346
            if (tp != null && t != tp.left && t != tp.right)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2347
                return false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2348
            if (tl != null && (tl.parent != t || tl.hash > t.hash))
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2349
                return false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2350
            if (tr != null && (tr.parent != t || tr.hash < t.hash))
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2351
                return false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2352
            if (t.red && tl != null && tl.red && tr != null && tr.red)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2353
                return false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2354
            if (tl != null && !checkInvariants(tl))
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2355
                return false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2356
            if (tr != null && !checkInvariants(tr))
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2357
                return false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2358
            return true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2359
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2360
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2361
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
}