jdk/src/share/classes/java/util/HashMap.java
author psandoz
Wed, 04 Sep 2013 09:34:25 +0200
changeset 19806 dda89341ee2d
parent 19184 927725c23203
child 20189 1e618f2a82d9
permissions -rw-r--r--
8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black) 8012913: LinkedHashMap key/value/entry spliterators should report ORDERED Reviewed-by: mduigou, forax, bchristi, alanb Contributed-by: Doug Lea <dl@cs.oswego.edu>, Paul Sandoz <paul.sandoz@oracle.com>
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;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   627
        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
   628
            (n = tab.length) == 0)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   629
            n = (tab = resize()).length;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   630
        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
   631
            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
   632
        else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   633
            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
   634
            if (p.hash == hash &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   635
                ((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
   636
                e = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   637
            else if (p instanceof TreeNode)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   638
                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
   639
            else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   640
                for (int binCount = 0; ; ++binCount) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   641
                    if ((e = p.next) == null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   642
                        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
   643
                        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
   644
                            treeifyBin(tab, hash);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   645
                        break;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   646
                    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   647
                    if (e.hash == hash &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   648
                        ((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
   649
                        break;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   650
                    p = e;
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
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   653
            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
   654
                V oldValue = e.value;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   655
                if (!onlyIfAbsent || oldValue == null)
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   656
                    e.value = value;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   657
                afterNodeAccess(e);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   658
                return oldValue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        }
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   661
        ++modCount;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   662
        ++size;
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) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1135
        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
  1136
        int hash = hash(key);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1137
        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
  1138
            (oldValue = e.value) != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1139
            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
  1140
            if (v != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1141
                e.value = v;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1142
                afterNodeAccess(e);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1143
                return v;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1144
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1145
            else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1146
                removeNode(hash, key, null, false, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        }
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1148
        return null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1149
    }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1150
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1151
    public V compute(K key,
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1152
                     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
  1153
        if (remappingFunction == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1154
            throw new NullPointerException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1155
        int hash = hash(key);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1156
        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
  1157
        int binCount = 0;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1158
        TreeNode<K,V> t = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1159
        Node<K,V> old = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1160
        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
  1161
            (n = tab.length) == 0)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1162
            n = (tab = resize()).length;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1163
        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
  1164
            if (first instanceof TreeNode)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1165
                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
  1166
            else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1167
                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
  1168
                do {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1169
                    if (e.hash == hash &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1170
                        ((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
  1171
                        old = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1172
                        break;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1173
                    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1174
                    ++binCount;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1175
                } while ((e = e.next) != null);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1176
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1177
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1178
        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
  1179
        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
  1180
        if (old != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1181
            if (v != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1182
                old.value = v;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1183
                afterNodeAccess(old);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1184
            }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1185
            else
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1186
                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
  1187
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1188
        else if (v != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1189
            if (t != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1190
                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
  1191
            else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1192
                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
  1193
                if (binCount >= TREEIFY_THRESHOLD - 1)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1194
                    treeifyBin(tab, hash);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1195
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1196
            ++modCount;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1197
            ++size;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1198
            afterNodeInsertion(true);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1199
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1200
        return v;
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
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1203
    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
  1204
                   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
  1205
        if (remappingFunction == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1206
            throw new NullPointerException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1207
        int hash = hash(key);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1208
        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
  1209
        int binCount = 0;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1210
        TreeNode<K,V> t = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1211
        Node<K,V> old = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1212
        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
  1213
            (n = tab.length) == 0)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1214
            n = (tab = resize()).length;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1215
        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
  1216
            if (first instanceof TreeNode)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1217
                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
  1218
            else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1219
                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
  1220
                do {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1221
                    if (e.hash == hash &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1222
                        ((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
  1223
                        old = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1224
                        break;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1225
                    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1226
                    ++binCount;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1227
                } while ((e = e.next) != null);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1228
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1229
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1230
        if (old != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1231
            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
  1232
            if (v != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1233
                old.value = v;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1234
                afterNodeAccess(old);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1235
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1236
            else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1237
                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
  1238
            return v;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1239
        }
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1240
        if (value != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1241
            if (t != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1242
                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
  1243
            else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1244
                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
  1245
                if (binCount >= TREEIFY_THRESHOLD - 1)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1246
                    treeifyBin(tab, hash);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1247
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1248
            ++modCount;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1249
            ++size;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1250
            afterNodeInsertion(true);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1251
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1252
        return value;
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
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1255
    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
  1256
        Node<K,V>[] tab;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1257
        if (action == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1258
            throw new NullPointerException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1259
        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
  1260
            int mc = modCount;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1261
            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
  1262
                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
  1263
                    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
  1264
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1265
            if (modCount != mc)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1266
                throw new ConcurrentModificationException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1267
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1268
    }
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
    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
  1271
        Node<K,V>[] tab;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1272
        if (function == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1273
            throw new NullPointerException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1274
        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
  1275
            int mc = modCount;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1276
            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
  1277
                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
  1278
                    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
  1279
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1280
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1281
            if (modCount != mc)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1282
                throw new ConcurrentModificationException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1283
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1284
    }
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
    // Cloning and serialization
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
    /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1290
     * 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
  1291
     * values themselves are not cloned.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1292
     *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1293
     * @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
  1294
     */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1295
    @SuppressWarnings("unchecked")
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1296
    public Object clone() {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1297
        HashMap<K,V> result;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1298
        try {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1299
            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
  1300
        } catch (CloneNotSupportedException e) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1301
            // 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
  1302
            throw new InternalError(e);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1303
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1304
        result.reinitialize();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1305
        result.putMapEntries(this, false);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1306
        return result;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1307
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1308
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1309
    // 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
  1310
    final float loadFactor() { return loadFactor; }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1311
    final int capacity() {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1312
        return (table != null) ? table.length :
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1313
            (threshold > 0) ? threshold :
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1314
            DEFAULT_INITIAL_CAPACITY;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
     * Save the state of the <tt>HashMap</tt> instance to a stream (i.e.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
     * serialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
     * @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
  1322
     *             bucket array) is emitted (int), followed by the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
     *             <i>size</i> (an int, the number of key-value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
     *             mappings), followed by the key (Object) and value (Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
     *             for each key-value mapping.  The key-value mappings are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
     *             emitted in no particular order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
    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
  1329
        throws IOException {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1330
        int buckets = capacity();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
        // Write out the threshold, loadfactor, and any hidden stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
        s.defaultWriteObject();
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1333
        s.writeInt(buckets);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
        s.writeInt(size);
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1335
        internalWriteEntries(s);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
    /**
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1339
     * Reconstitute the {@code HashMap} instance from a stream (i.e.,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
     * deserialize it).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
    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
  1343
        throws IOException, ClassNotFoundException {
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1344
        // Read in the threshold (ignored), loadfactor, and any hidden stuff
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
        s.defaultReadObject();
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1346
        reinitialize();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1347
        if (loadFactor <= 0 || Float.isNaN(loadFactor))
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1348
            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
  1349
                                             loadFactor);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1350
        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
  1351
        int mappings = s.readInt(); // Read number of mappings (size)
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1352
        if (mappings < 0)
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1353
            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
  1354
                                             mappings);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1355
        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
  1356
            // 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
  1357
            // 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
  1358
            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
  1359
            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
  1360
            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
  1361
                       DEFAULT_INITIAL_CAPACITY :
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1362
                       (fc >= MAXIMUM_CAPACITY) ?
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1363
                       MAXIMUM_CAPACITY :
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1364
                       tableSizeFor((int)fc));
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1365
            float ft = (float)cap * lf;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1366
            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
  1367
                         (int)ft : Integer.MAX_VALUE);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1368
            @SuppressWarnings({"rawtypes","unchecked"})
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1369
                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
  1370
            table = tab;
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1371
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1372
            // 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
  1373
            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
  1374
                @SuppressWarnings("unchecked")
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1375
                    K key = (K) s.readObject();
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
                    V value = (V) s.readObject();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1378
                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
  1379
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1383
    /* ------------------------------------------------------------ */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1384
    // iterators
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
    abstract class HashIterator {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1387
        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
  1388
        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
  1389
        int expectedModCount;  // for fast-fail
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1390
        int index;             // current slot
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1391
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1392
        HashIterator() {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1393
            expectedModCount = modCount;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1394
            Node<K,V>[] t = table;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1395
            current = next = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1396
            index = 0;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1397
            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
  1398
                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
  1399
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1400
        }
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
        public final boolean hasNext() {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1403
            return next != null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1404
        }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1405
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1406
        final Node<K,V> nextNode() {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1407
            Node<K,V>[] t;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1408
            Node<K,V> e = next;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1409
            if (modCount != expectedModCount)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1410
                throw new ConcurrentModificationException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1411
            if (e == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1412
                throw new NoSuchElementException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1413
            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
  1414
                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
  1415
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1416
            return e;
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
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1419
        public final void remove() {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1420
            Node<K,V> p = current;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1421
            if (p == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1422
                throw new IllegalStateException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1423
            if (modCount != expectedModCount)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1424
                throw new ConcurrentModificationException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1425
            current = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1426
            K key = p.key;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1427
            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
  1428
            expectedModCount = modCount;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1429
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1430
    }
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
    final class KeyIterator extends HashIterator
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1433
        implements Iterator<K> {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1434
        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
  1435
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1436
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1437
    final class ValueIterator extends HashIterator
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1438
        implements Iterator<V> {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1439
        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
  1440
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1441
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1442
    final class EntryIterator extends HashIterator
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1443
        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
  1444
        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
  1445
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1446
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
    // spliterators
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1449
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1450
    static class HashMapSpliterator<K,V> {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1451
        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
  1452
        Node<K,V> current;          // current node
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1453
        int index;                  // current index, modified on advance/split
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1454
        int fence;                  // one past last index
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1455
        int est;                    // size estimate
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1456
        int expectedModCount;       // for comodification checks
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1457
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1458
        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
  1459
                           int fence, int est,
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1460
                           int expectedModCount) {
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1461
            this.map = m;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1462
            this.index = origin;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1463
            this.fence = fence;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1464
            this.est = est;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1465
            this.expectedModCount = expectedModCount;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1466
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1467
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1468
        final int getFence() { // initialize fence and size on first use
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1469
            int hi;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1470
            if ((hi = fence) < 0) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1471
                HashMap<K,V> m = map;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1472
                est = m.size;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1473
                expectedModCount = m.modCount;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1474
                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
  1475
                hi = fence = (tab == null) ? 0 : tab.length;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1476
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1477
            return hi;
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
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1480
        public final long estimateSize() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1481
            getFence(); // force init
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1482
            return (long) est;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1483
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1484
    }
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
    static final class KeySpliterator<K,V>
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1487
        extends HashMapSpliterator<K,V>
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1488
        implements Spliterator<K> {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1489
        KeySpliterator(HashMap<K,V> m, int origin, int fence, int est,
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1490
                       int expectedModCount) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1491
            super(m, origin, fence, est, expectedModCount);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1492
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1493
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1494
        public KeySpliterator<K,V> trySplit() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1495
            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
  1496
            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
  1497
                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
  1498
                                        expectedModCount);
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1499
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1500
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1501
        public void forEachRemaining(Consumer<? super K> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1502
            int i, hi, mc;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1503
            if (action == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1504
                throw new NullPointerException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1505
            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
  1506
            Node<K,V>[] tab = m.table;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1507
            if ((hi = fence) < 0) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1508
                mc = expectedModCount = m.modCount;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1509
                hi = fence = (tab == null) ? 0 : tab.length;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1510
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1511
            else
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1512
                mc = expectedModCount;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1513
            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
  1514
                (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
  1515
                Node<K,V> p = current;
17949
6c33d8f2601e 8013649: HashMap spliterator tryAdvance() encounters remaining elements after forEachRemaining()
psandoz
parents: 17939
diff changeset
  1516
                current = null;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1517
                do {
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1518
                    if (p == null)
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1519
                        p = tab[i++];
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1520
                    else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1521
                        action.accept(p.key);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1522
                        p = p.next;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1523
                    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1524
                } while (p != null || i < hi);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1525
                if (m.modCount != mc)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1526
                    throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1527
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1528
        }
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
        public boolean tryAdvance(Consumer<? super K> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1531
            int hi;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1532
            if (action == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1533
                throw new NullPointerException();
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1534
            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
  1535
            if (tab != null && tab.length >= (hi = getFence()) && index >= 0) {
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1536
                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
  1537
                    if (current == null)
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1538
                        current = tab[index++];
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1539
                    else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1540
                        K k = current.key;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1541
                        current = current.next;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1542
                        action.accept(k);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1543
                        if (map.modCount != expectedModCount)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1544
                            throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1545
                        return true;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1546
                    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1547
                }
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
            return false;
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
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1552
        public int characteristics() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1553
            return (fence < 0 || est == map.size ? Spliterator.SIZED : 0) |
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1554
                Spliterator.DISTINCT;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1555
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1556
    }
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
    static final class ValueSpliterator<K,V>
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1559
        extends HashMapSpliterator<K,V>
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1560
        implements Spliterator<V> {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1561
        ValueSpliterator(HashMap<K,V> m, int origin, int fence, int est,
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1562
                         int expectedModCount) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1563
            super(m, origin, fence, est, expectedModCount);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1564
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1565
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1566
        public ValueSpliterator<K,V> trySplit() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1567
            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
  1568
            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
  1569
                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
  1570
                                          expectedModCount);
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1571
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1572
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1573
        public void forEachRemaining(Consumer<? super V> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1574
            int i, hi, mc;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1575
            if (action == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1576
                throw new NullPointerException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1577
            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
  1578
            Node<K,V>[] tab = m.table;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1579
            if ((hi = fence) < 0) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1580
                mc = expectedModCount = m.modCount;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1581
                hi = fence = (tab == null) ? 0 : tab.length;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1582
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1583
            else
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1584
                mc = expectedModCount;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1585
            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
  1586
                (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
  1587
                Node<K,V> p = current;
17949
6c33d8f2601e 8013649: HashMap spliterator tryAdvance() encounters remaining elements after forEachRemaining()
psandoz
parents: 17939
diff changeset
  1588
                current = null;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1589
                do {
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1590
                    if (p == null)
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1591
                        p = tab[i++];
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1592
                    else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1593
                        action.accept(p.value);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1594
                        p = p.next;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1595
                    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1596
                } while (p != null || i < hi);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1597
                if (m.modCount != mc)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1598
                    throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1599
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1600
        }
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
        public boolean tryAdvance(Consumer<? super V> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1603
            int hi;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1604
            if (action == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1605
                throw new NullPointerException();
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1606
            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
  1607
            if (tab != null && tab.length >= (hi = getFence()) && index >= 0) {
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1608
                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
  1609
                    if (current == null)
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1610
                        current = tab[index++];
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1611
                    else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1612
                        V v = current.value;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1613
                        current = current.next;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1614
                        action.accept(v);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1615
                        if (map.modCount != expectedModCount)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1616
                            throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1617
                        return true;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1618
                    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1619
                }
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
            return false;
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
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1624
        public int characteristics() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1625
            return (fence < 0 || est == map.size ? Spliterator.SIZED : 0);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1626
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1627
    }
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
    static final class EntrySpliterator<K,V>
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1630
        extends HashMapSpliterator<K,V>
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1631
        implements Spliterator<Map.Entry<K,V>> {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1632
        EntrySpliterator(HashMap<K,V> m, int origin, int fence, int est,
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1633
                         int expectedModCount) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1634
            super(m, origin, fence, est, expectedModCount);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1635
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1636
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1637
        public EntrySpliterator<K,V> trySplit() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1638
            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
  1639
            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
  1640
                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
  1641
                                          expectedModCount);
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1642
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1643
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1644
        public void forEachRemaining(Consumer<? super Map.Entry<K,V>> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1645
            int i, hi, mc;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1646
            if (action == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1647
                throw new NullPointerException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1648
            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
  1649
            Node<K,V>[] tab = m.table;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1650
            if ((hi = fence) < 0) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1651
                mc = expectedModCount = m.modCount;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1652
                hi = fence = (tab == null) ? 0 : tab.length;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1653
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1654
            else
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1655
                mc = expectedModCount;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1656
            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
  1657
                (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
  1658
                Node<K,V> p = current;
17949
6c33d8f2601e 8013649: HashMap spliterator tryAdvance() encounters remaining elements after forEachRemaining()
psandoz
parents: 17939
diff changeset
  1659
                current = null;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1660
                do {
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1661
                    if (p == null)
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1662
                        p = tab[i++];
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1663
                    else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1664
                        action.accept(p);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1665
                        p = p.next;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1666
                    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1667
                } while (p != null || i < hi);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1668
                if (m.modCount != mc)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1669
                    throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1670
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1671
        }
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
        public boolean tryAdvance(Consumer<? super Map.Entry<K,V>> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1674
            int hi;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1675
            if (action == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1676
                throw new NullPointerException();
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1677
            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
  1678
            if (tab != null && tab.length >= (hi = getFence()) && index >= 0) {
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1679
                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
  1680
                    if (current == null)
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1681
                        current = tab[index++];
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1682
                    else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1683
                        Node<K,V> e = current;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1684
                        current = current.next;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1685
                        action.accept(e);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1686
                        if (map.modCount != expectedModCount)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1687
                            throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1688
                        return true;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1689
                    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1690
                }
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
            return false;
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
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1695
        public int characteristics() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1696
            return (fence < 0 || est == map.size ? Spliterator.SIZED : 0) |
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1697
                Spliterator.DISTINCT;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1698
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1699
    }
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1700
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1701
    /* ------------------------------------------------------------ */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1702
    // LinkedHashMap support
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
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
     * 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
  1707
     * 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
  1708
     * 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
  1709
     * 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
  1710
     * classes, and HashSet.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1711
     */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1712
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1713
    // 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
  1714
    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
  1715
        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
  1716
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1717
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1718
    // 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
  1719
    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
  1720
        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
  1721
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1722
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1723
    // Create a tree bin node
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1724
    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
  1725
        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
  1726
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1727
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1728
    // For treeifyBin
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1729
    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
  1730
        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
  1731
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1732
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
     * 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
  1735
     */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1736
    void reinitialize() {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1737
        table = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1738
        entrySet = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1739
        keySet = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1740
        values = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1741
        modCount = 0;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1742
        threshold = 0;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1743
        size = 0;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1744
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1745
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1746
    // 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
  1747
    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
  1748
    void afterNodeInsertion(boolean evict) { }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1749
    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
  1750
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1751
    // 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
  1752
    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
  1753
        Node<K,V>[] tab;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1754
        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
  1755
            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
  1756
                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
  1757
                    s.writeObject(e.key);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1758
                    s.writeObject(e.value);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1759
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1760
            }
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
    // Tree bins
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
    /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1768
     * 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
  1769
     * 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
  1770
     * linked node.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1771
     */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1772
    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
  1773
        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
  1774
        TreeNode<K,V> left;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1775
        TreeNode<K,V> right;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1776
        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
  1777
        boolean red;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1778
        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
  1779
            super(hash, key, val, next);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1780
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1781
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
         * 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
  1784
         */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1785
        final TreeNode<K,V> root() {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1786
            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
  1787
                if ((p = r.parent) == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1788
                    return r;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1789
                r = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1790
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1791
        }
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
         * 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
  1795
         */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1796
        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
  1797
            int n;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1798
            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
  1799
                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
  1800
                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
  1801
                if (root != first) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1802
                    Node<K,V> rn;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1803
                    tab[index] = root;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1804
                    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
  1805
                    if ((rn = root.next) != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1806
                        ((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
  1807
                    if (rp != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1808
                        rp.next = rn;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1809
                    if (first != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1810
                        first.prev = root;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1811
                    root.next = first;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1812
                    root.prev = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1813
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1814
                assert checkInvariants(root);
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
        }
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
         * 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
  1820
         * 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
  1821
         * comparing keys.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1822
         */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1823
        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
  1824
            TreeNode<K,V> p = this;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1825
            do {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1826
                int ph, dir; K pk;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1827
                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
  1828
                if ((ph = p.hash) > h)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1829
                    p = pl;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1830
                else if (ph < h)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1831
                    p = pr;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1832
                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
  1833
                    return p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1834
                else if (pl == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1835
                    p = pr;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1836
                else if (pr == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1837
                    p = pl;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1838
                else if ((kc != null ||
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1839
                          (kc = comparableClassFor(k)) != null) &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1840
                         (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
  1841
                    p = (dir < 0) ? pl : pr;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1842
                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
  1843
                    return q;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1844
                else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1845
                    p = pl;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1846
            } while (p != null);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1847
            return null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1848
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1849
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
         * Calls find for root node.
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
        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
  1854
            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
  1855
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1856
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
         * 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
  1859
         * 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
  1860
         * 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
  1861
         * 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
  1862
         * necessary simplifies testing a bit.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1863
         */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1864
        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
  1865
            int d;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1866
            if (a == null || b == null ||
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1867
                (d = a.getClass().getName().
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1868
                 compareTo(b.getClass().getName())) == 0)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1869
                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
  1870
                     -1 : 1);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1871
            return d;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1872
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1873
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
         * 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
  1876
         * @return root of tree
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1877
         */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1878
        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
  1879
            TreeNode<K,V> root = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1880
            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
  1881
                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
  1882
                x.left = x.right = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1883
                if (root == null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1884
                    x.parent = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1885
                    x.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1886
                    root = x;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1887
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1888
                else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1889
                    K k = x.key;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1890
                    int h = x.hash;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1891
                    Class<?> kc = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1892
                    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
  1893
                        int dir, ph;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1894
                        K pk = p.key;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1895
                        if ((ph = p.hash) > h)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1896
                            dir = -1;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1897
                        else if (ph < 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 ((kc == null &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1900
                                  (kc = comparableClassFor(k)) == null) ||
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1901
                                 (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
  1902
                            dir = tieBreakOrder(k, pk);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1903
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1904
                        TreeNode<K,V> xp = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1905
                        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
  1906
                            x.parent = xp;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1907
                            if (dir <= 0)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1908
                                xp.left = x;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1909
                            else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1910
                                xp.right = x;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1911
                            root = balanceInsertion(root, x);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1912
                            break;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1913
                        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1914
                    }
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
            moveRootToFront(tab, root);
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
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
         * 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
  1922
         * this node.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1923
         */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1924
        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
  1925
            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
  1926
            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
  1927
                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
  1928
                if (tl == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1929
                    hd = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1930
                else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1931
                    tl.next = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1932
                tl = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1933
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1934
            return hd;
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
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
         * Tree version of putVal.
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
        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
  1941
                                       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
  1942
            Class<?> kc = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1943
            boolean searched = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1944
            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
  1945
            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
  1946
                int dir, ph; K pk;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1947
                if ((ph = p.hash) > h)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1948
                    dir = -1;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1949
                else if (ph < 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 ((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
  1952
                    return p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1953
                else if ((kc == null &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1954
                          (kc = comparableClassFor(k)) == null) ||
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1955
                         (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
  1956
                    if (!searched) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1957
                        TreeNode<K,V> q, ch;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1958
                        searched = true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1959
                        if (((ch = p.left) != null &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1960
                             (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
  1961
                            ((ch = p.right) != 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
                            return q;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1964
                    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1965
                    dir = tieBreakOrder(k, pk);
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
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1968
                TreeNode<K,V> xp = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1969
                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
  1970
                    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
  1971
                    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
  1972
                    if (dir <= 0)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1973
                        xp.left = x;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1974
                    else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1975
                        xp.right = x;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1976
                    xp.next = x;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1977
                    x.parent = x.prev = xp;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1978
                    if (xpn != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1979
                        ((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
  1980
                    moveRootToFront(tab, balanceInsertion(root, x));
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1981
                    return null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1982
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1983
            }
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
         * 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
  1988
         * 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
  1989
         * 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
  1990
         * 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
  1991
         * 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
  1992
         * 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
  1993
         * 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
  1994
         * 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
  1995
         */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1996
        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
  1997
                                  boolean movable) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1998
            int n;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1999
            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
  2000
                return;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2001
            int index = (n - 1) & hash;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2002
            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
  2003
            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
  2004
            if (pred == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2005
                tab[index] = first = succ;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2006
            else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2007
                pred.next = succ;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2008
            if (succ != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2009
                succ.prev = pred;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2010
            if (first == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2011
                return;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2012
            if (root.parent != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2013
                root = root.root();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2014
            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
  2015
                (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
  2016
                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
  2017
                return;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2018
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2019
            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
  2020
            if (pl != null && pr != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2021
                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
  2022
                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
  2023
                    s = sl;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2024
                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
  2025
                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
  2026
                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
  2027
                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
  2028
                    p.parent = s;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2029
                    s.right = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2030
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2031
                else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2032
                    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
  2033
                    if ((p.parent = sp) != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2034
                        if (s == sp.left)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2035
                            sp.left = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2036
                        else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2037
                            sp.right = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2038
                    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2039
                    if ((s.right = pr) != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2040
                        pr.parent = s;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2041
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2042
                p.left = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2043
                if ((p.right = sr) != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2044
                    sr.parent = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2045
                if ((s.left = pl) != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2046
                    pl.parent = s;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2047
                if ((s.parent = pp) == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2048
                    root = s;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2049
                else if (p == pp.left)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2050
                    pp.left = s;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2051
                else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2052
                    pp.right = s;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2053
                if (sr != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2054
                    replacement = sr;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2055
                else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2056
                    replacement = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2057
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2058
            else if (pl != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2059
                replacement = pl;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2060
            else if (pr != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2061
                replacement = pr;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2062
            else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2063
                replacement = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2064
            if (replacement != p) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2065
                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
  2066
                if (pp == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2067
                    root = replacement;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2068
                else if (p == pp.left)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2069
                    pp.left = replacement;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2070
                else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2071
                    pp.right = replacement;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2072
                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
  2073
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2074
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2075
            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
  2076
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2077
            if (replacement == p) {  // detach
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2078
                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
  2079
                p.parent = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2080
                if (pp != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2081
                    if (p == pp.left)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2082
                        pp.left = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2083
                    else if (p == pp.right)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2084
                        pp.right = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2085
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2086
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2087
            if (movable)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2088
                moveRootToFront(tab, r);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2089
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2090
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
         * 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
  2093
         * 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
  2094
         * 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
  2095
         *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2096
         * @param map the map
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2097
         * @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
  2098
         * @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
  2099
         * @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
  2100
         */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2101
        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
  2102
            TreeNode<K,V> b = this;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2103
            // 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
  2104
            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
  2105
            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
  2106
            int lc = 0, hc = 0;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2107
            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
  2108
                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
  2109
                e.next = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2110
                if ((e.hash & bit) == 0) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2111
                    if ((e.prev = loTail) == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2112
                        loHead = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2113
                    else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2114
                        loTail.next = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2115
                    loTail = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2116
                    ++lc;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2117
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2118
                else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2119
                    if ((e.prev = hiTail) == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2120
                        hiHead = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2121
                    else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2122
                        hiTail.next = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2123
                    hiTail = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2124
                    ++hc;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2125
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2126
            }
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
            if (loHead != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2129
                if (lc <= UNTREEIFY_THRESHOLD)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2130
                    tab[index] = loHead.untreeify(map);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2131
                else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2132
                    tab[index] = loHead;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2133
                    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
  2134
                        loHead.treeify(tab);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2135
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2136
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2137
            if (hiHead != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2138
                if (hc <= UNTREEIFY_THRESHOLD)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2139
                    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
  2140
                else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2141
                    tab[index + bit] = hiHead;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2142
                    if (loHead != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2143
                        hiHead.treeify(tab);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2144
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2145
            }
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
        // 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
  2150
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2151
        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
  2152
                                              TreeNode<K,V> p) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2153
            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
  2154
            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
  2155
                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
  2156
                    rl.parent = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2157
                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
  2158
                    (root = r).red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2159
                else if (pp.left == p)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2160
                    pp.left = r;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2161
                else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2162
                    pp.right = r;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2163
                r.left = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2164
                p.parent = r;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2165
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2166
            return root;
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
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2169
        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
  2170
                                               TreeNode<K,V> p) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2171
            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
  2172
            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
  2173
                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
  2174
                    lr.parent = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2175
                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
  2176
                    (root = l).red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2177
                else if (pp.right == p)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2178
                    pp.right = l;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2179
                else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2180
                    pp.left = l;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2181
                l.right = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2182
                p.parent = l;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2183
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2184
            return root;
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
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2187
        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
  2188
                                                    TreeNode<K,V> x) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2189
            x.red = true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2190
            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
  2191
                if ((xp = x.parent) == null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2192
                    x.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2193
                    return x;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2194
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2195
                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
  2196
                    return root;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2197
                if (xp == (xppl = xpp.left)) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2198
                    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
  2199
                        xppr.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2200
                        xp.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2201
                        xpp.red = true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2202
                        x = xpp;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2203
                    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2204
                    else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2205
                        if (x == xp.right) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2206
                            root = rotateLeft(root, x = xp);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2207
                            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
  2208
                        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2209
                        if (xp != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2210
                            xp.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2211
                            if (xpp != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2212
                                xpp.red = true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2213
                                root = rotateRight(root, xpp);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2214
                            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2215
                        }
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
                else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2219
                    if (xppl != null && xppl.red) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2220
                        xppl.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2221
                        xp.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2222
                        xpp.red = true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2223
                        x = xpp;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2224
                    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2225
                    else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2226
                        if (x == xp.left) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2227
                            root = rotateRight(root, x = xp);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2228
                            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
  2229
                        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2230
                        if (xp != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2231
                            xp.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2232
                            if (xpp != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2233
                                xpp.red = true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2234
                                root = rotateLeft(root, xpp);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2235
                            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2236
                        }
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
        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
  2243
                                                   TreeNode<K,V> x) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2244
            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
  2245
                if (x == null || x == root)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2246
                    return root;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2247
                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
  2248
                    x.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2249
                    return x;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2250
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2251
                else if (x.red) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2252
                    x.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2253
                    return root;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2254
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2255
                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
  2256
                    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
  2257
                        xpr.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2258
                        xp.red = true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2259
                        root = rotateLeft(root, xp);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2260
                        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
  2261
                    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2262
                    if (xpr == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2263
                        x = xp;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2264
                    else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2265
                        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
  2266
                        if ((sr == null || !sr.red) &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2267
                            (sl == null || !sl.red)) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2268
                            xpr.red = true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2269
                            x = xp;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2270
                        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2271
                        else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2272
                            if (sr == null || !sr.red) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2273
                                if (sl != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2274
                                    sl.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2275
                                xpr.red = true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2276
                                root = rotateRight(root, xpr);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2277
                                xpr = (xp = x.parent) == null ?
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2278
                                    null : xp.right;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2279
                            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2280
                            if (xpr != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2281
                                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
  2282
                                if ((sr = xpr.right) != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2283
                                    sr.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2284
                            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2285
                            if (xp != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2286
                                xp.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2287
                                root = rotateLeft(root, xp);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2288
                            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2289
                            x = root;
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
                    }
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
                else { // symmetric
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2294
                    if (xpl != null && xpl.red) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2295
                        xpl.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2296
                        xp.red = true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2297
                        root = rotateRight(root, xp);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2298
                        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
  2299
                    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2300
                    if (xpl == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2301
                        x = xp;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2302
                    else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2303
                        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
  2304
                        if ((sl == null || !sl.red) &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2305
                            (sr == null || !sr.red)) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2306
                            xpl.red = true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2307
                            x = xp;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2308
                        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2309
                        else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2310
                            if (sl == null || !sl.red) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2311
                                if (sr != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2312
                                    sr.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2313
                                xpl.red = true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2314
                                root = rotateLeft(root, xpl);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2315
                                xpl = (xp = x.parent) == null ?
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2316
                                    null : xp.left;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2317
                            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2318
                            if (xpl != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2319
                                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
  2320
                                if ((sl = xpl.left) != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2321
                                    sl.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2322
                            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2323
                            if (xp != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2324
                                xp.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2325
                                root = rotateRight(root, xp);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2326
                            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2327
                            x = root;
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
                    }
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
         * Recursive invariant check
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
        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
  2338
            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
  2339
                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
  2340
            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
  2341
                return false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2342
            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
  2343
                return false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2344
            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
  2345
                return false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2346
            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
  2347
                return false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2348
            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
  2349
                return false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2350
            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
  2351
                return false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2352
            if (tl != null && !checkInvariants(tl))
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 (tr != null && !checkInvariants(tr))
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
            return true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2357
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2358
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2359
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
}