src/java.base/share/classes/java/util/HashMap.java
author smarks
Wed, 02 Aug 2017 10:34:35 -0700
changeset 47423 4fc2a4a29f3d
parent 47304 3f5f9bc0bdc2
child 49433 b6671a111395
permissions -rw-r--r--
8174109: Better queuing priorities Reviewed-by: chegar, dfuchs, rriggs, alanb, robm, rhalade, jeff, ahgross
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
47423
4fc2a4a29f3d 8174109: Better queuing priorities
smarks
parents: 47304
diff changeset
     2
 * Copyright (c) 1997, 2017, 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;
47423
4fc2a4a29f3d 8174109: Better queuing priorities
smarks
parents: 47304
diff changeset
    37
import jdk.internal.misc.SharedSecrets;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
    40
 * Hash table based implementation of the {@code Map} interface.  This
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * implementation provides all of the optional map operations, and permits
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
    42
 * {@code null} values and the {@code null} key.  (The {@code HashMap}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
    43
 * class is roughly equivalent to {@code Hashtable}, except that it is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * unsynchronized and permits nulls.)  This class makes no guarantees as to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * the order of the map; in particular, it does not guarantee that the order
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * will remain constant over time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <p>This implementation provides constant-time performance for the basic
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
    49
 * operations ({@code get} and {@code put}), assuming the hash function
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * disperses the elements properly among the buckets.  Iteration over
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * collection views requires time proportional to the "capacity" of the
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
    52
 * {@code HashMap} instance (the number of buckets) plus its size (the number
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * of key-value mappings).  Thus, it's very important not to set the initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * capacity too high (or the load factor too low) if iteration performance is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * important.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
    57
 * <p>An instance of {@code HashMap} has two parameters that affect its
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * performance: <i>initial capacity</i> and <i>load factor</i>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <i>capacity</i> is the number of buckets in the hash table, and the initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * capacity is simply the capacity at the time the hash table is created.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <i>load factor</i> is a measure of how full the hash table is allowed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * get before its capacity is automatically increased.  When the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * entries in the hash table exceeds the product of the load factor and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * current capacity, the hash table is <i>rehashed</i> (that is, internal data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * structures are rebuilt) so that the hash table has approximately twice the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * number of buckets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
    68
 * <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
    69
 * 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
    70
 * space overhead but increase the lookup cost (reflected in most of
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
    71
 * the operations of the {@code HashMap} class, including
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
    72
 * {@code get} and {@code put}).  The expected number of entries in
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
    73
 * 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
    74
 * 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
    75
 * 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
    76
 * 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
    77
 * operations will ever occur.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
    79
 * <p>If many mappings are to be stored in a {@code HashMap}
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
    80
 * 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
    81
 * 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
    82
 * 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
    83
 * 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
    84
 * 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
    85
 * 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
    86
 * keys to help break ties.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * <p><strong>Note that this implementation is not synchronized.</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * If multiple threads access a hash map concurrently, and at least one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * the threads modifies the map structurally, it <i>must</i> be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * synchronized externally.  (A structural modification is any operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * that adds or deletes one or more mappings; merely changing the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * associated with a key that an instance already contains is not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * structural modification.)  This is typically accomplished by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * synchronizing on some object that naturally encapsulates the map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * If no such object exists, the map should be "wrapped" using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * {@link Collections#synchronizedMap Collections.synchronizedMap}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * method.  This is best done at creation time, to prevent accidental
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * unsynchronized access to the map:<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *   Map m = Collections.synchronizedMap(new HashMap(...));</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * <p>The iterators returned by all of this class's "collection view methods"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * are <i>fail-fast</i>: if the map is structurally modified at any time after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * the iterator is created, in any way except through the iterator's own
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   106
 * {@code remove} method, the iterator will throw a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * {@link ConcurrentModificationException}.  Thus, in the face of concurrent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * modification, the iterator fails quickly and cleanly, rather than risking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * arbitrary, non-deterministic behavior at an undetermined time in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * future.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * as it is, generally speaking, impossible to make any hard guarantees in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * presence of unsynchronized concurrent modification.  Fail-fast iterators
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   115
 * throw {@code ConcurrentModificationException} on a best-effort basis.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * Therefore, it would be wrong to write a program that depended on this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * exception for its correctness: <i>the fail-fast behavior of iterators
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * should be used only to detect bugs.</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * <p>This class is a member of the
44743
f0bbd698c486 8177789: fix collections framework links to point to java.util package doc
smarks
parents: 42361
diff changeset
   121
 * <a href="{@docRoot}/java/util/package-summary.html#CollectionsFramework">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * @param <K> the type of keys maintained by this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * @param <V> the type of mapped values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * @author  Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * @author  Arthur van Hoff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * @author  Neal Gafter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * @see     Object#hashCode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * @see     Collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * @see     Map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * @see     TreeMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * @see     Hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 */
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   138
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
   139
    implements Map<K,V>, Cloneable, Serializable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   141
    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
   142
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   143
    /*
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   144
     * Implementation notes.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   145
     *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   146
     * 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
   147
     * 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
   148
     * 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
   149
     * 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
   150
     * 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
   151
     * 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
   152
     * 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
   153
     * 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
   154
     * 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
   155
     * 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
   156
     *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   157
     * 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
   158
     * 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
   159
     * 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
   160
     * 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
   161
     * 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
   162
     * 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
   163
     * 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
   164
     * 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
   165
     * 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
   166
     * 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
   167
     * 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
   168
     * 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
   169
     * 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
   170
     * 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
   171
     * 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
   172
     * 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
   173
     * little difference.)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   174
     *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   175
     * 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
   176
     * 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
   177
     * (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
   178
     * 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
   179
     * 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
   180
     * 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
   181
     * 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
   182
     * (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
   183
     * 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
   184
     * 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
   185
     * 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
   186
     * 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
   187
     * 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
   188
     *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   189
     * 0:    0.60653066
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   190
     * 1:    0.30326533
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   191
     * 2:    0.07581633
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   192
     * 3:    0.01263606
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   193
     * 4:    0.00157952
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   194
     * 5:    0.00015795
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   195
     * 6:    0.00001316
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   196
     * 7:    0.00000094
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   197
     * 8:    0.00000006
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   198
     * 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
   199
     *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   200
     * 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
   201
     * 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
   202
     * 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
   203
     * (method TreeNode.root()).
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   204
     *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   205
     * 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
   206
     * 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
   207
     * 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
   208
     * 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
   209
     * 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
   210
     * resizing or converting.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   211
     *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   212
     * 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
   213
     * 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
   214
     * 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
   215
     * 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
   216
     * 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
   217
     * 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
   218
     * 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
   219
     * tie-breakers.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   220
     *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   221
     * 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
   222
     * 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
   223
     * 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
   224
     * 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
   225
     * 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
   226
     * 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
   227
     * that may create new nodes.)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   228
     *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   229
     * 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
   230
     * 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
   231
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * The default initial capacity - MUST be a power of two.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   236
    static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * The maximum capacity, used if a higher value is implicitly specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * by either of the constructors with arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * MUST be a power of two <= 1<<30.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    static final int MAXIMUM_CAPACITY = 1 << 30;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * The load factor used when none specified in constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    static final float DEFAULT_LOAD_FACTOR = 0.75f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   251
     * 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
   252
     * 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
   253
     * 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
   254
     * 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
   255
     * 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
   256
     * shrinkage.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   257
     */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   258
    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
   259
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   260
    /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   261
     * 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
   262
     * 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
   263
     * 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
   264
     */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   265
    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
   266
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   267
    /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   268
     * 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
   269
     * (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
   270
     * 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
   271
     * between resizing and treeification thresholds.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   272
     */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   273
    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
   274
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   275
    /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   276
     * 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
   277
     * TreeNode subclass, and in LinkedHashMap for its Entry subclass.)
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   278
     */
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   279
    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
   280
        final int hash;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   281
        final K key;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   282
        V value;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   283
        Node<K,V> next;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   284
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   285
        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
   286
            this.hash = hash;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   287
            this.key = key;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   288
            this.value = value;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   289
            this.next = next;
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
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   292
        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
   293
        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
   294
        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
   295
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   296
        public final int hashCode() {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   297
            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
   298
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   299
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   300
        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
   301
            V oldValue = value;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   302
            value = newValue;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   303
            return oldValue;
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
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   306
        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
   307
            if (o == this)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   308
                return true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   309
            if (o instanceof Map.Entry) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   310
                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
   311
                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
   312
                    Objects.equals(value, e.getValue()))
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   313
                    return true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   314
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   315
            return false;
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
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   319
    /* ---------------- Static utilities -------------- */
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   320
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   321
    /**
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   322
     * 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
   323
     * 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
   324
     * 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
   325
     * 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
   326
     * 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
   327
     * 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
   328
     * 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
   329
     * 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
   330
     * 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
   331
     * 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
   332
     * 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
   333
     * 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
   334
     * 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
   335
     * 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
   336
     */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   337
    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
   338
        int h;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   339
        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
   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
    /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   343
     * 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
   344
     * Comparable<C>", else null.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     */
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   346
    static Class<?> comparableClassFor(Object x) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   347
        if (x instanceof Comparable) {
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 22055
diff changeset
   348
            Class<?> c; Type[] ts, as; ParameterizedType p;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   349
            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
   350
                return c;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   351
            if ((ts = c.getGenericInterfaces()) != null) {
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 22055
diff changeset
   352
                for (Type t : ts) {
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 22055
diff changeset
   353
                    if ((t instanceof ParameterizedType) &&
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 22055
diff changeset
   354
                        ((p = (ParameterizedType) t).getRawType() ==
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   355
                         Comparable.class) &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   356
                        (as = p.getActualTypeArguments()) != null &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   357
                        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
   358
                        return c;
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
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   362
        return null;
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
    /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   366
     * 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
   367
     * class), else 0.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   368
     */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   369
    @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
   370
    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
   371
        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
   372
                ((Comparable)k).compareTo(x));
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
    /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   376
     * 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
   377
     */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   378
    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
   379
        int n = cap - 1;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   380
        n |= n >>> 1;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   381
        n |= n >>> 2;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   382
        n |= n >>> 4;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   383
        n |= n >>> 8;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   384
        n |= n >>> 16;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   385
        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
   386
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   387
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   388
    /* ---------------- Fields -------------- */
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
    /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   391
     * 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
   392
     * 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
   393
     * (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
   394
     * 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
   395
     */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   396
    transient Node<K,V>[] table;
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
    /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   399
     * 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
   400
     * for keySet() and values().
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   401
     */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   402
    transient Set<Map.Entry<K,V>> entrySet;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * The number of key-value mappings contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    transient int size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * The number of times this HashMap has been structurally modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * Structural modifications are those that change the number of mappings in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * the HashMap or otherwise modify its internal structure (e.g.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * rehash).  This field is used to make iterators on Collection-views of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * the HashMap fail-fast.  (See ConcurrentModificationException).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     */
65
51fc1d79463f 6625725: (coll) modCount should not be volatile
martin
parents: 2
diff changeset
   416
    transient int modCount;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   418
    /**
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   419
     * 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
   420
     *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   421
     * @serial
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   422
     */
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   423
    // (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
   424
    // 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
   425
    // 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
   426
    // DEFAULT_INITIAL_CAPACITY.)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   427
    int threshold;
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   428
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   429
    /**
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   430
     * The load factor for the hash table.
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   431
     *
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   432
     * @serial
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   433
     */
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   434
    final float loadFactor;
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   435
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   436
    /* ---------------- Public operations -------------- */
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
   437
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   439
     * Constructs an empty {@code HashMap} with the specified initial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * capacity and load factor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * @param  initialCapacity the initial capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * @param  loadFactor      the load factor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * @throws IllegalArgumentException if the initial capacity is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     *         or the load factor is nonpositive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    public HashMap(int initialCapacity, float loadFactor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        if (initialCapacity < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            throw new IllegalArgumentException("Illegal initial capacity: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                                               initialCapacity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        if (initialCapacity > MAXIMUM_CAPACITY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            initialCapacity = MAXIMUM_CAPACITY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        if (loadFactor <= 0 || Float.isNaN(loadFactor))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            throw new IllegalArgumentException("Illegal load factor: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                                               loadFactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        this.loadFactor = loadFactor;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   457
        this.threshold = tableSizeFor(initialCapacity);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   461
     * Constructs an empty {@code HashMap} with the specified initial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * capacity and the default load factor (0.75).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * @param  initialCapacity the initial capacity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * @throws IllegalArgumentException if the initial capacity is negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    public HashMap(int initialCapacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        this(initialCapacity, DEFAULT_LOAD_FACTOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   472
     * Constructs an empty {@code HashMap} with the default initial capacity
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * (16) and the default load factor (0.75).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    public HashMap() {
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   476
        this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   480
     * Constructs a new {@code HashMap} with the same mappings as the
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   481
     * specified {@code Map}.  The {@code HashMap} is created with
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * default load factor (0.75) and an initial capacity sufficient to
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   483
     * hold the mappings in the specified {@code Map}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * @param   m the map whose mappings are to be placed in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * @throws  NullPointerException if the specified map is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    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
   489
        this.loadFactor = DEFAULT_LOAD_FACTOR;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   490
        putMapEntries(m, false);
16855
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
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
   493
    /**
47304
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents: 47216
diff changeset
   494
     * Implements Map.putAll and Map constructor.
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   495
     *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   496
     * @param m the map
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   497
     * @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
   498
     * true (relayed to method afterNodeInsertion).
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   499
     */
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   500
    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
   501
        int s = m.size();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   502
        if (s > 0) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   503
            if (table == null) { // pre-size
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   504
                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
   505
                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
   506
                         (int)ft : MAXIMUM_CAPACITY);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   507
                if (t > threshold)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   508
                    threshold = tableSizeFor(t);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   509
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   510
            else if (s > threshold)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   511
                resize();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   512
            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
   513
                K key = e.getKey();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   514
                V value = e.getValue();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   515
                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
   516
            }
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   517
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * Returns the number of key-value mappings in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * @return the number of key-value mappings in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   530
     * Returns {@code true} if this map contains no key-value mappings.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   532
     * @return {@code true} if this map contains no key-value mappings
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        return size == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * Returns the value to which the specified key is mapped,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * or {@code null} if this map contains no mapping for the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * <p>More formally, if this map contains a mapping from a key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * {@code k} to a value {@code v} such that {@code (key==null ? k==null :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * key.equals(k))}, then this method returns {@code v}; otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * it returns {@code null}.  (There can be at most one such mapping.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * <p>A return value of {@code null} does not <i>necessarily</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * indicate that the map contains no mapping for the key; it's also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * possible that the map explicitly maps the key to {@code null}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * The {@link #containsKey containsKey} operation may be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * distinguish these two cases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * @see #put(Object, Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    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
   556
        Node<K,V> e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   557
        return (e = getNode(hash(key), key)) == null ? null : e.value;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   560
    /**
47304
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents: 47216
diff changeset
   561
     * Implements Map.get and related methods.
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   562
     *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   563
     * @param hash hash for key
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   564
     * @param key the key
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   565
     * @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
   566
     */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   567
    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
   568
        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
   569
        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
   570
            (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
   571
            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
   572
                ((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
   573
                return first;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   574
            if ((e = first.next) != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   575
                if (first instanceof TreeNode)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   576
                    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
   577
                do {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   578
                    if (e.hash == hash &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   579
                        ((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
   580
                        return e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   581
                } while ((e = e.next) != null);
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
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   584
        return null;
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   585
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   586
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   588
     * Returns {@code true} if this map contains a mapping for the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * specified key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * @param   key   The key whose presence in this map is to be tested
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   592
     * @return {@code true} if this map contains a mapping for the specified
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    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
   596
        return getNode(hash(key), key) != null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * Associates the specified value with the specified key in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * If the map previously contained a mapping for the key, the old
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * value is replaced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * @param key key with which the specified value is to be associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * @param value value to be associated with the specified key
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   606
     * @return the previous value associated with {@code key}, or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   607
     *         {@code null} if there was no mapping for {@code key}.
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   608
     *         (A {@code null} return can also indicate that the map
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   609
     *         previously associated {@code null} with {@code key}.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    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
   612
        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
   613
    }
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   614
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   615
    /**
47304
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents: 47216
diff changeset
   616
     * Implements Map.put and related methods.
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   617
     *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   618
     * @param hash hash for key
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   619
     * @param key the key
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   620
     * @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
   621
     * @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
   622
     * @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
   623
     * @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
   624
     */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   625
    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
   626
                   boolean evict) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   627
        Node<K,V>[] tab; Node<K,V> p; int n, i;
20199
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents: 20189
diff changeset
   628
        if ((tab = table) == null || (n = tab.length) == 0)
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   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;
20199
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents: 20189
diff changeset
   662
        if (++size > threshold)
0a9d1d17b076 8025173: HashMap.put() replacing an existing key can trigger a resize()
bchristi
parents: 20189
diff changeset
   663
            resize();
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   664
        afterNodeInsertion(evict);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    /**
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   669
     * 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
   670
     * 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
   671
     * 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
   672
     * 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
   673
     * 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
   674
     *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   675
     * @return the table
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     */
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   677
    final Node<K,V>[] resize() {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   678
        Node<K,V>[] oldTab = table;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   679
        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
   680
        int oldThr = threshold;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   681
        int newCap, newThr = 0;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   682
        if (oldCap > 0) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   683
            if (oldCap >= MAXIMUM_CAPACITY) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   684
                threshold = Integer.MAX_VALUE;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   685
                return oldTab;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   686
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   687
            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
   688
                     oldCap >= DEFAULT_INITIAL_CAPACITY)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   689
                newThr = oldThr << 1; // double threshold
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        }
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   691
        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
   692
            newCap = oldThr;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   693
        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
   694
            newCap = DEFAULT_INITIAL_CAPACITY;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   695
            newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   696
        }
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   697
        if (newThr == 0) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   698
            float ft = (float)newCap * loadFactor;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   699
            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
   700
                      (int)ft : Integer.MAX_VALUE);
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   701
        }
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   702
        threshold = newThr;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   703
        @SuppressWarnings({"rawtypes","unchecked"})
47304
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents: 47216
diff changeset
   704
        Node<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   705
        table = newTab;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   706
        if (oldTab != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   707
            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
   708
                Node<K,V> e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   709
                if ((e = oldTab[j]) != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   710
                    oldTab[j] = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   711
                    if (e.next == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   712
                        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
   713
                    else if (e instanceof TreeNode)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   714
                        ((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
   715
                    else { // preserve order
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   716
                        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
   717
                        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
   718
                        Node<K,V> next;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   719
                        do {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   720
                            next = e.next;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   721
                            if ((e.hash & oldCap) == 0) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   722
                                if (loTail == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   723
                                    loHead = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   724
                                else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   725
                                    loTail.next = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   726
                                loTail = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   727
                            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   728
                            else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   729
                                if (hiTail == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   730
                                    hiHead = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   731
                                else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   732
                                    hiTail.next = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   733
                                hiTail = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   734
                            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   735
                        } while ((e = next) != null);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   736
                        if (loTail != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   737
                            loTail.next = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   738
                            newTab[j] = loHead;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   739
                        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   740
                        if (hiTail != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   741
                            hiTail.next = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   742
                            newTab[j + oldCap] = hiHead;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   743
                        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   744
                    }
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   745
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        }
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   748
        return newTab;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    /**
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   752
     * 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
   753
     * table is too small, in which case resizes instead.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     */
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   755
    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
   756
        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
   757
        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
   758
            resize();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   759
        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
   760
            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
   761
            do {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   762
                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
   763
                if (tl == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   764
                    hd = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   765
                else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   766
                    p.prev = tl;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   767
                    tl.next = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   768
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   769
                tl = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   770
            } while ((e = e.next) != null);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   771
            if ((tab[index] = hd) != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   772
                hd.treeify(tab);
2
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * Copies all of the mappings from the specified map to this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * These mappings will replace any mappings that this map had for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * any of the keys currently in the specified map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * @param m mappings to be stored in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * @throws NullPointerException if the specified map is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
    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
   785
        putMapEntries(m, true);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   786
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * Removes the mapping for the specified key from this map if present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * @param  key key whose mapping is to be removed from the map
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   792
     * @return the previous value associated with {@code key}, or
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   793
     *         {@code null} if there was no mapping for {@code key}.
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   794
     *         (A {@code null} return can also indicate that the map
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   795
     *         previously associated {@code null} with {@code key}.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
    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
   798
        Node<K,V> e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   799
        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
   800
            null : e.value;
18280
6c3c0ff49eb5 8016446: Improve forEach/replaceAll for Map, HashMap, Hashtable, IdentityHashMap, WeakHashMap, TreeMap, ConcurrentMap
mduigou
parents: 18166
diff changeset
   801
    }
16867
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   802
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   803
    /**
47304
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents: 47216
diff changeset
   804
     * Implements Map.remove and related methods.
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   805
     *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   806
     * @param hash hash for key
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   807
     * @param key the key
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   808
     * @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
   809
     * @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
   810
     * @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
   811
     * @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
   812
     */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   813
    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
   814
                               boolean matchValue, boolean movable) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   815
        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
   816
        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
   817
            (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
   818
            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
   819
            if (p.hash == hash &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   820
                ((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
   821
                node = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   822
            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
   823
                if (p instanceof TreeNode)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   824
                    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
   825
                else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   826
                    do {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   827
                        if (e.hash == hash &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   828
                            ((k = e.key) == key ||
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   829
                             (key != null && key.equals(k)))) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   830
                            node = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   831
                            break;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   832
                        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   833
                        p = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   834
                    } while ((e = e.next) != null);
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   835
                }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   836
            }
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   837
            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
   838
                                 (value != null && value.equals(v)))) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   839
                if (node instanceof TreeNode)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   840
                    ((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
   841
                else if (node == p)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   842
                    tab[index] = node.next;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   843
                else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   844
                    p.next = node.next;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   845
                ++modCount;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   846
                --size;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   847
                afterNodeRemoval(node);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   848
                return node;
16867
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
        }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   851
        return null;
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   852
    }
76499721c6c1 8004518: Add in-place operations to Map
mduigou
parents: 16855
diff changeset
   853
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * Removes all of the mappings from this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * The map will be empty after this call returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    public void clear() {
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   859
        Node<K,V>[] tab;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        modCount++;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   861
        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
   862
            size = 0;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   863
            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
   864
                tab[i] = null;
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   865
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   869
     * Returns {@code true} if this map maps one or more keys to the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * specified value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * @param value value whose presence in this map is to be tested
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   873
     * @return {@code true} if this map maps one or more keys to the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     *         specified value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
    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
   877
        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
   878
        if ((tab = table) != null && size > 0) {
47304
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents: 47216
diff changeset
   879
            for (Node<K,V> e : tab) {
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 22055
diff changeset
   880
                for (; e != null; e = e.next) {
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   881
                    if ((v = e.value) == value ||
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   882
                        (value != null && value.equals(v)))
17939
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   883
                        return true;
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
            }
bd750ec19d82 8005698: Handle Frequent HashMap Collisions with Balanced Trees
bchristi
parents: 17168
diff changeset
   886
        }
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   887
        return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * Returns a {@link Set} view of the keys contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * The set is backed by the map, so changes to the map are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * reflected in the set, and vice-versa.  If the map is modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * while an iteration over the set is in progress (except through
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   895
     * the iterator's own {@code remove} operation), the results of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * the iteration are undefined.  The set supports element removal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * which removes the corresponding mapping from the map, via the
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   898
     * {@code Iterator.remove}, {@code Set.remove},
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   899
     * {@code removeAll}, {@code retainAll}, and {@code clear}
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   900
     * operations.  It does not support the {@code add} or {@code addAll}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * operations.
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   902
     *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   903
     * @return a set view of the keys contained in this map
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
    public Set<K> keySet() {
34712
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   906
        Set<K> ks = keySet;
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   907
        if (ks == null) {
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   908
            ks = new KeySet();
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   909
            keySet = ks;
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   910
        }
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   911
        return ks;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   914
    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
   915
        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
   916
        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
   917
        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
   918
        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
   919
        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
   920
            return removeNode(hash(key), key, null, false, true) != null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        }
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   922
        public final Spliterator<K> spliterator() {
21352
0372edc9a995 8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents: 20199
diff changeset
   923
            return new KeySpliterator<>(HashMap.this, 0, -1, 0, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        }
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   925
        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
   926
            Node<K,V>[] tab;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   927
            if (action == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   928
                throw new NullPointerException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   929
            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
   930
                int mc = modCount;
47304
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents: 47216
diff changeset
   931
                for (Node<K,V> e : tab) {
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 22055
diff changeset
   932
                    for (; e != null; e = e.next)
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   933
                        action.accept(e.key);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   934
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   935
                if (modCount != mc)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   936
                    throw new ConcurrentModificationException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   937
            }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
   938
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * Returns a {@link Collection} view of the values contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * The collection is backed by the map, so changes to the map are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * reflected in the collection, and vice-versa.  If the map is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * modified while an iteration over the collection is in progress
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   946
     * (except through the iterator's own {@code remove} operation),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * the results of the iteration are undefined.  The collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * supports element removal, which removes the corresponding
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   949
     * mapping from the map, via the {@code Iterator.remove},
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   950
     * {@code Collection.remove}, {@code removeAll},
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   951
     * {@code retainAll} and {@code clear} operations.  It does not
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   952
     * support the {@code add} or {@code addAll} operations.
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   953
     *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   954
     * @return a view of the values contained in this map
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
    public Collection<V> values() {
34712
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   957
        Collection<V> vs = values;
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   958
        if (vs == null) {
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   959
            vs = new Values();
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   960
            values = vs;
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   961
        }
b183cfd1ce17 8145539: (coll) AbstractMap.keySet and .values should not be volatile
shade
parents: 32108
diff changeset
   962
        return vs;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   965
    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
   966
        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
   967
        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
   968
        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
   969
        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
   970
        public final Spliterator<V> spliterator() {
21352
0372edc9a995 8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents: 20199
diff changeset
   971
            return new ValueSpliterator<>(HashMap.this, 0, -1, 0, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        }
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   973
        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
   974
            Node<K,V>[] tab;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   975
            if (action == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   976
                throw new NullPointerException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   977
            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
   978
                int mc = modCount;
47304
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents: 47216
diff changeset
   979
                for (Node<K,V> e : tab) {
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 22055
diff changeset
   980
                    for (; e != null; e = e.next)
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   981
                        action.accept(e.value);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   982
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   983
                if (modCount != mc)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   984
                    throw new ConcurrentModificationException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
   985
            }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
   986
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     * Returns a {@link Set} view of the mappings contained in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * The set is backed by the map, so changes to the map are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * reflected in the set, and vice-versa.  If the map is modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * while an iteration over the set is in progress (except through
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   994
     * the iterator's own {@code remove} operation, or through the
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   995
     * {@code setValue} operation on a map entry returned by the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * iterator) the results of the iteration are undefined.  The set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     * supports element removal, which removes the corresponding
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   998
     * mapping from the map, via the {@code Iterator.remove},
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
   999
     * {@code Set.remove}, {@code removeAll}, {@code retainAll} and
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
  1000
     * {@code clear} operations.  It does not support the
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
  1001
     * {@code add} or {@code addAll} operations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * @return a set view of the mappings contained in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
    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
  1006
        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
  1007
        return (es = entrySet) == null ? (entrySet = new EntrySet()) : es;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1010
    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
  1011
        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
  1012
        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
  1013
        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
  1014
            return new EntryIterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        }
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1016
        public final boolean contains(Object o) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                return false;
12448
b95438b17098 7157893: Warnings Cleanup in java.util.*
khazra
parents: 9035
diff changeset
  1019
            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
  1020
            Object key = e.getKey();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1021
            Node<K,V> candidate = getNode(hash(key), key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            return candidate != null && candidate.equals(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        }
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1024
        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
  1025
            if (o instanceof Map.Entry) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1026
                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
  1027
                Object key = e.getKey();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1028
                Object value = e.getValue();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1029
                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
  1030
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1031
            return false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1032
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1033
        public final Spliterator<Map.Entry<K,V>> spliterator() {
21352
0372edc9a995 8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents: 20199
diff changeset
  1034
            return new EntrySpliterator<>(HashMap.this, 0, -1, 0, 0);
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1035
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1036
        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
  1037
            Node<K,V>[] tab;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1038
            if (action == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1039
                throw new NullPointerException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1040
            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
  1041
                int mc = modCount;
47304
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents: 47216
diff changeset
  1042
                for (Node<K,V> e : tab) {
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 22055
diff changeset
  1043
                    for (; e != null; e = e.next)
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1044
                        action.accept(e);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1045
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1046
                if (modCount != mc)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1047
                    throw new ConcurrentModificationException();
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
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1051
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1052
    // 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
  1053
21352
0372edc9a995 8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents: 20199
diff changeset
  1054
    @Override
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1055
    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
  1056
        Node<K,V> e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1057
        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
  1058
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1059
21352
0372edc9a995 8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents: 20199
diff changeset
  1060
    @Override
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1061
    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
  1062
        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
  1063
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1064
21352
0372edc9a995 8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents: 20199
diff changeset
  1065
    @Override
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1066
    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
  1067
        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
  1068
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1069
21352
0372edc9a995 8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents: 20199
diff changeset
  1070
    @Override
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1071
    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
  1072
        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
  1073
        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
  1074
            ((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
  1075
            e.value = newValue;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1076
            afterNodeAccess(e);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1077
            return true;
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
        return false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1080
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1081
21352
0372edc9a995 8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents: 20199
diff changeset
  1082
    @Override
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1083
    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
  1084
        Node<K,V> e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1085
        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
  1086
            V oldValue = e.value;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1087
            e.value = value;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1088
            afterNodeAccess(e);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1089
            return oldValue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        }
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1091
        return null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1092
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1093
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1094
    /**
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1095
     * {@inheritDoc}
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1096
     *
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1097
     * <p>This method will, on a best-effort basis, throw a
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1098
     * {@link ConcurrentModificationException} if it is detected that the
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1099
     * mapping function modifies this map during computation.
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1100
     *
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1101
     * @throws ConcurrentModificationException if it is detected that the
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1102
     * mapping function modified this map
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1103
     */
21352
0372edc9a995 8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents: 20199
diff changeset
  1104
    @Override
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1105
    public V computeIfAbsent(K key,
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1106
                             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
  1107
        if (mappingFunction == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1108
            throw new NullPointerException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1109
        int hash = hash(key);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1110
        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
  1111
        int binCount = 0;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1112
        TreeNode<K,V> t = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1113
        Node<K,V> old = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1114
        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
  1115
            (n = tab.length) == 0)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1116
            n = (tab = resize()).length;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1117
        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
  1118
            if (first instanceof TreeNode)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1119
                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
  1120
            else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1121
                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
  1122
                do {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1123
                    if (e.hash == hash &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1124
                        ((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
  1125
                        old = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1126
                        break;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1127
                    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1128
                    ++binCount;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1129
                } while ((e = e.next) != null);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1130
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1131
            V oldValue;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1132
            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
  1133
                afterNodeAccess(old);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1134
                return oldValue;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1135
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        }
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1137
        int mc = modCount;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1138
        V v = mappingFunction.apply(key);
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1139
        if (mc != modCount) { throw new ConcurrentModificationException(); }
22054
503bc3781dfe 8030016: HashMap.computeIfAbsent generates spurious access event
mduigou
parents: 21352
diff changeset
  1140
        if (v == null) {
503bc3781dfe 8030016: HashMap.computeIfAbsent generates spurious access event
mduigou
parents: 21352
diff changeset
  1141
            return null;
503bc3781dfe 8030016: HashMap.computeIfAbsent generates spurious access event
mduigou
parents: 21352
diff changeset
  1142
        } else if (old != null) {
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1143
            old.value = v;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1144
            afterNodeAccess(old);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1145
            return v;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1146
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1147
        else if (t != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1148
            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
  1149
        else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1150
            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
  1151
            if (binCount >= TREEIFY_THRESHOLD - 1)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1152
                treeifyBin(tab, hash);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1153
        }
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1154
        modCount = mc + 1;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1155
        ++size;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1156
        afterNodeInsertion(true);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1157
        return v;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1158
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1159
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1160
    /**
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1161
     * {@inheritDoc}
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1162
     *
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1163
     * <p>This method will, on a best-effort basis, throw a
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1164
     * {@link ConcurrentModificationException} if it is detected that the
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1165
     * remapping function modifies this map during computation.
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1166
     *
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1167
     * @throws ConcurrentModificationException if it is detected that the
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1168
     * remapping function modified this map
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1169
     */
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1170
    @Override
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1171
    public V computeIfPresent(K key,
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1172
                              BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
20189
1e618f2a82d9 8024331: j.u.Map.computeIfPresent() default/nondefault implementations don't throw NPE if the remappingFunction is null and the key is absent
bpb
parents: 19806
diff changeset
  1173
        if (remappingFunction == null)
1e618f2a82d9 8024331: j.u.Map.computeIfPresent() default/nondefault implementations don't throw NPE if the remappingFunction is null and the key is absent
bpb
parents: 19806
diff changeset
  1174
            throw new NullPointerException();
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1175
        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
  1176
        int hash = hash(key);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1177
        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
  1178
            (oldValue = e.value) != null) {
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1179
            int mc = modCount;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1180
            V v = remappingFunction.apply(key, oldValue);
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1181
            if (mc != modCount) { throw new ConcurrentModificationException(); }
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1182
            if (v != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1183
                e.value = v;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1184
                afterNodeAccess(e);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1185
                return v;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1186
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1187
            else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1188
                removeNode(hash, key, null, false, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        }
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1190
        return null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1191
    }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1192
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1193
    /**
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1194
     * {@inheritDoc}
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1195
     *
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1196
     * <p>This method will, on a best-effort basis, throw a
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1197
     * {@link ConcurrentModificationException} if it is detected that the
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1198
     * remapping function modifies this map during computation.
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1199
     *
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1200
     * @throws ConcurrentModificationException if it is detected that the
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1201
     * remapping function modified this map
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1202
     */
21352
0372edc9a995 8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents: 20199
diff changeset
  1203
    @Override
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1204
    public V compute(K key,
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1205
                     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
  1206
        if (remappingFunction == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1207
            throw new NullPointerException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1208
        int hash = hash(key);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1209
        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
  1210
        int binCount = 0;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1211
        TreeNode<K,V> t = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1212
        Node<K,V> old = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1213
        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
  1214
            (n = tab.length) == 0)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1215
            n = (tab = resize()).length;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1216
        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
  1217
            if (first instanceof TreeNode)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1218
                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
  1219
            else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1220
                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
  1221
                do {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1222
                    if (e.hash == hash &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1223
                        ((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
  1224
                        old = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1225
                        break;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1226
                    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1227
                    ++binCount;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1228
                } while ((e = e.next) != null);
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
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1231
        V oldValue = (old == null) ? null : old.value;
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1232
        int mc = modCount;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1233
        V v = remappingFunction.apply(key, oldValue);
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1234
        if (mc != modCount) { throw new ConcurrentModificationException(); }
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1235
        if (old != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1236
            if (v != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1237
                old.value = v;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1238
                afterNodeAccess(old);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1239
            }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1240
            else
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1241
                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
  1242
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1243
        else if (v != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1244
            if (t != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1245
                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
  1246
            else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1247
                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
  1248
                if (binCount >= TREEIFY_THRESHOLD - 1)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1249
                    treeifyBin(tab, hash);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1250
            }
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1251
            modCount = mc + 1;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1252
            ++size;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1253
            afterNodeInsertion(true);
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
        return v;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1256
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1257
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1258
    /**
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1259
     * {@inheritDoc}
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1260
     *
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1261
     * <p>This method will, on a best-effort basis, throw a
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1262
     * {@link ConcurrentModificationException} if it is detected that the
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1263
     * remapping function modifies this map during computation.
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1264
     *
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1265
     * @throws ConcurrentModificationException if it is detected that the
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1266
     * remapping function modified this map
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1267
     */
21352
0372edc9a995 8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents: 20199
diff changeset
  1268
    @Override
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1269
    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
  1270
                   BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
22055
d9836bf9992a 8029055: Map.merge implementations should refuse null value param
mduigou
parents: 22054
diff changeset
  1271
        if (value == null)
d9836bf9992a 8029055: Map.merge implementations should refuse null value param
mduigou
parents: 22054
diff changeset
  1272
            throw new NullPointerException();
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1273
        if (remappingFunction == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1274
            throw new NullPointerException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1275
        int hash = hash(key);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1276
        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
  1277
        int binCount = 0;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1278
        TreeNode<K,V> t = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1279
        Node<K,V> old = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1280
        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
  1281
            (n = tab.length) == 0)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1282
            n = (tab = resize()).length;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1283
        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
  1284
            if (first instanceof TreeNode)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1285
                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
  1286
            else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1287
                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
  1288
                do {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1289
                    if (e.hash == hash &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1290
                        ((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
  1291
                        old = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1292
                        break;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1293
                    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1294
                    ++binCount;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1295
                } while ((e = e.next) != null);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1296
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1297
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1298
        if (old != null) {
21352
0372edc9a995 8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents: 20199
diff changeset
  1299
            V v;
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1300
            if (old.value != null) {
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1301
                int mc = modCount;
21352
0372edc9a995 8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents: 20199
diff changeset
  1302
                v = remappingFunction.apply(old.value, value);
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1303
                if (mc != modCount) {
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1304
                    throw new ConcurrentModificationException();
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1305
                }
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1306
            } else {
21352
0372edc9a995 8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents: 20199
diff changeset
  1307
                v = value;
29743
981893a47bec 8071667: HashMap.computeIfAbsent() adds entry that HashMap.get() does not find.
bchristi
parents: 25859
diff changeset
  1308
            }
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1309
            if (v != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1310
                old.value = v;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1311
                afterNodeAccess(old);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1312
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1313
            else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1314
                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
  1315
            return v;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1316
        }
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1317
        if (value != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1318
            if (t != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1319
                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
  1320
            else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1321
                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
  1322
                if (binCount >= TREEIFY_THRESHOLD - 1)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1323
                    treeifyBin(tab, hash);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1324
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1325
            ++modCount;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1326
            ++size;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1327
            afterNodeInsertion(true);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1328
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1329
        return value;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1330
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1331
21352
0372edc9a995 8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents: 20199
diff changeset
  1332
    @Override
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1333
    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
  1334
        Node<K,V>[] tab;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1335
        if (action == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1336
            throw new NullPointerException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1337
        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
  1338
            int mc = modCount;
47304
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents: 47216
diff changeset
  1339
            for (Node<K,V> e : tab) {
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 22055
diff changeset
  1340
                for (; e != null; e = e.next)
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1341
                    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
  1342
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1343
            if (modCount != mc)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1344
                throw new ConcurrentModificationException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1345
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1346
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1347
21352
0372edc9a995 8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents: 20199
diff changeset
  1348
    @Override
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1349
    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
  1350
        Node<K,V>[] tab;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1351
        if (function == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1352
            throw new NullPointerException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1353
        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
  1354
            int mc = modCount;
47304
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents: 47216
diff changeset
  1355
            for (Node<K,V> e : tab) {
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 22055
diff changeset
  1356
                for (; e != null; e = e.next) {
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1357
                    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
  1358
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1359
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1360
            if (modCount != mc)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1361
                throw new ConcurrentModificationException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1362
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1363
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1364
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1365
    /* ------------------------------------------------------------ */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1366
    // Cloning and serialization
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1367
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1368
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 29743
diff changeset
  1369
     * Returns a shallow copy of this {@code HashMap} instance: the keys and
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1370
     * values themselves are not cloned.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1371
     *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1372
     * @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
  1373
     */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1374
    @SuppressWarnings("unchecked")
21352
0372edc9a995 8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents: 20199
diff changeset
  1375
    @Override
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1376
    public Object clone() {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1377
        HashMap<K,V> result;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1378
        try {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1379
            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
  1380
        } catch (CloneNotSupportedException e) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1381
            // 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
  1382
            throw new InternalError(e);
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
        result.reinitialize();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1385
        result.putMapEntries(this, false);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1386
        return result;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1387
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1388
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1389
    // 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
  1390
    final float loadFactor() { return loadFactor; }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1391
    final int capacity() {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1392
        return (table != null) ? table.length :
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1393
            (threshold > 0) ? threshold :
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1394
            DEFAULT_INITIAL_CAPACITY;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
    /**
47304
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents: 47216
diff changeset
  1398
     * Saves this map to a stream (that is, serializes it).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
     *
47304
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents: 47216
diff changeset
  1400
     * @param s the stream
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents: 47216
diff changeset
  1401
     * @throws IOException if an I/O error occurs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
     * @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
  1403
     *             bucket array) is emitted (int), followed by the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
     *             <i>size</i> (an int, the number of key-value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
     *             mappings), followed by the key (Object) and value (Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
     *             for each key-value mapping.  The key-value mappings are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
     *             emitted in no particular order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
    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
  1410
        throws IOException {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1411
        int buckets = capacity();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
        // Write out the threshold, loadfactor, and any hidden stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
        s.defaultWriteObject();
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1414
        s.writeInt(buckets);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
        s.writeInt(size);
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1416
        internalWriteEntries(s);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
    /**
47304
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents: 47216
diff changeset
  1420
     * Reconstitutes this map from a stream (that is, deserializes it).
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents: 47216
diff changeset
  1421
     * @param s the stream
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents: 47216
diff changeset
  1422
     * @throws ClassNotFoundException if the class of a serialized object
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents: 47216
diff changeset
  1423
     *         could not be found
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents: 47216
diff changeset
  1424
     * @throws IOException if an I/O error occurs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
    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
  1427
        throws IOException, ClassNotFoundException {
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1428
        // Read in the threshold (ignored), loadfactor, and any hidden stuff
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
        s.defaultReadObject();
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1430
        reinitialize();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1431
        if (loadFactor <= 0 || Float.isNaN(loadFactor))
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1432
            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
  1433
                                             loadFactor);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1434
        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
  1435
        int mappings = s.readInt(); // Read number of mappings (size)
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1436
        if (mappings < 0)
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 12448
diff changeset
  1437
            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
  1438
                                             mappings);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1439
        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
  1440
            // 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
  1441
            // 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
  1442
            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
  1443
            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
  1444
            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
  1445
                       DEFAULT_INITIAL_CAPACITY :
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1446
                       (fc >= MAXIMUM_CAPACITY) ?
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1447
                       MAXIMUM_CAPACITY :
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1448
                       tableSizeFor((int)fc));
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1449
            float ft = (float)cap * lf;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1450
            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
  1451
                         (int)ft : Integer.MAX_VALUE);
47423
4fc2a4a29f3d 8174109: Better queuing priorities
smarks
parents: 47304
diff changeset
  1452
4fc2a4a29f3d 8174109: Better queuing priorities
smarks
parents: 47304
diff changeset
  1453
            // Check Map.Entry[].class since it's the nearest public type to
4fc2a4a29f3d 8174109: Better queuing priorities
smarks
parents: 47304
diff changeset
  1454
            // what we're actually creating.
4fc2a4a29f3d 8174109: Better queuing priorities
smarks
parents: 47304
diff changeset
  1455
            SharedSecrets.getJavaObjectInputStreamAccess().checkArray(s, Map.Entry[].class, cap);
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1456
            @SuppressWarnings({"rawtypes","unchecked"})
47304
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents: 47216
diff changeset
  1457
            Node<K,V>[] tab = (Node<K,V>[])new Node[cap];
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1458
            table = tab;
16855
106eaf391fbc 8011200: (coll) Optimize empty HashMap and ArrayList
mduigou
parents: 16725
diff changeset
  1459
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1460
            // 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
  1461
            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
  1462
                @SuppressWarnings("unchecked")
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1463
                    K key = (K) s.readObject();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1464
                @SuppressWarnings("unchecked")
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1465
                    V value = (V) s.readObject();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1466
                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
  1467
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1471
    /* ------------------------------------------------------------ */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1472
    // iterators
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1473
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1474
    abstract class HashIterator {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1475
        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
  1476
        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
  1477
        int expectedModCount;  // for fast-fail
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1478
        int index;             // current slot
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1479
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1480
        HashIterator() {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1481
            expectedModCount = modCount;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1482
            Node<K,V>[] t = table;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1483
            current = next = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1484
            index = 0;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1485
            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
  1486
                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
  1487
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1488
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1489
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1490
        public final boolean hasNext() {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1491
            return next != null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1492
        }
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1493
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1494
        final Node<K,V> nextNode() {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1495
            Node<K,V>[] t;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1496
            Node<K,V> e = next;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1497
            if (modCount != expectedModCount)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1498
                throw new ConcurrentModificationException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1499
            if (e == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1500
                throw new NoSuchElementException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1501
            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
  1502
                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
  1503
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1504
            return e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1505
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1506
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1507
        public final void remove() {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1508
            Node<K,V> p = current;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1509
            if (p == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1510
                throw new IllegalStateException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1511
            if (modCount != expectedModCount)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1512
                throw new ConcurrentModificationException();
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1513
            current = null;
42361
b64a8f0c668e 8170733: HashMap.HashIterator.remove method does not use cached value for the hash code.
psandoz
parents: 34712
diff changeset
  1514
            removeNode(p.hash, p.key, null, false, false);
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1515
            expectedModCount = modCount;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1516
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1517
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1518
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1519
    final class KeyIterator extends HashIterator
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1520
        implements Iterator<K> {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1521
        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
  1522
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1523
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1524
    final class ValueIterator extends HashIterator
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1525
        implements Iterator<V> {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1526
        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
  1527
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1528
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1529
    final class EntryIterator extends HashIterator
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1530
        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
  1531
        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
  1532
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1533
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1534
    /* ------------------------------------------------------------ */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1535
    // spliterators
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1536
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1537
    static class HashMapSpliterator<K,V> {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1538
        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
  1539
        Node<K,V> current;          // current node
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1540
        int index;                  // current index, modified on advance/split
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1541
        int fence;                  // one past last index
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1542
        int est;                    // size estimate
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1543
        int expectedModCount;       // for comodification checks
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1544
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1545
        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
  1546
                           int fence, int est,
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1547
                           int expectedModCount) {
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1548
            this.map = m;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1549
            this.index = origin;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1550
            this.fence = fence;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1551
            this.est = est;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1552
            this.expectedModCount = expectedModCount;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1553
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1554
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1555
        final int getFence() { // initialize fence and size on first use
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1556
            int hi;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1557
            if ((hi = fence) < 0) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1558
                HashMap<K,V> m = map;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1559
                est = m.size;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1560
                expectedModCount = m.modCount;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1561
                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
  1562
                hi = fence = (tab == null) ? 0 : tab.length;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1563
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1564
            return hi;
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
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1567
        public final long estimateSize() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1568
            getFence(); // force init
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1569
            return (long) est;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1570
        }
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
    static final class KeySpliterator<K,V>
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1574
        extends HashMapSpliterator<K,V>
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1575
        implements Spliterator<K> {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1576
        KeySpliterator(HashMap<K,V> m, int origin, int fence, int est,
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1577
                       int expectedModCount) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1578
            super(m, origin, fence, est, expectedModCount);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1579
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1580
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1581
        public KeySpliterator<K,V> trySplit() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1582
            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
  1583
            return (lo >= mid || current != null) ? null :
21352
0372edc9a995 8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents: 20199
diff changeset
  1584
                new KeySpliterator<>(map, lo, index = mid, est >>>= 1,
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1585
                                        expectedModCount);
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1586
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1587
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1588
        public void forEachRemaining(Consumer<? super K> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1589
            int i, hi, mc;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1590
            if (action == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1591
                throw new NullPointerException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1592
            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
  1593
            Node<K,V>[] tab = m.table;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1594
            if ((hi = fence) < 0) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1595
                mc = expectedModCount = m.modCount;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1596
                hi = fence = (tab == null) ? 0 : tab.length;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1597
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1598
            else
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1599
                mc = expectedModCount;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1600
            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
  1601
                (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
  1602
                Node<K,V> p = current;
17949
6c33d8f2601e 8013649: HashMap spliterator tryAdvance() encounters remaining elements after forEachRemaining()
psandoz
parents: 17939
diff changeset
  1603
                current = null;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1604
                do {
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1605
                    if (p == null)
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1606
                        p = tab[i++];
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1607
                    else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1608
                        action.accept(p.key);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1609
                        p = p.next;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1610
                    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1611
                } while (p != null || i < hi);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1612
                if (m.modCount != mc)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1613
                    throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1614
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1615
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1616
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1617
        public boolean tryAdvance(Consumer<? super K> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1618
            int hi;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1619
            if (action == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1620
                throw new NullPointerException();
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1621
            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
  1622
            if (tab != null && tab.length >= (hi = getFence()) && index >= 0) {
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1623
                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
  1624
                    if (current == null)
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1625
                        current = tab[index++];
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1626
                    else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1627
                        K k = current.key;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1628
                        current = current.next;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1629
                        action.accept(k);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1630
                        if (map.modCount != expectedModCount)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1631
                            throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1632
                        return true;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1633
                    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1634
                }
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
            return false;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1637
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1638
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1639
        public int characteristics() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1640
            return (fence < 0 || est == map.size ? Spliterator.SIZED : 0) |
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1641
                Spliterator.DISTINCT;
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
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1645
    static final class ValueSpliterator<K,V>
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1646
        extends HashMapSpliterator<K,V>
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1647
        implements Spliterator<V> {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1648
        ValueSpliterator(HashMap<K,V> m, int origin, int fence, int est,
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1649
                         int expectedModCount) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1650
            super(m, origin, fence, est, expectedModCount);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1651
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1652
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1653
        public ValueSpliterator<K,V> trySplit() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1654
            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
  1655
            return (lo >= mid || current != null) ? null :
21352
0372edc9a995 8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents: 20199
diff changeset
  1656
                new ValueSpliterator<>(map, lo, index = mid, est >>>= 1,
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1657
                                          expectedModCount);
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1658
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1659
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1660
        public void forEachRemaining(Consumer<? super V> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1661
            int i, hi, mc;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1662
            if (action == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1663
                throw new NullPointerException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1664
            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
  1665
            Node<K,V>[] tab = m.table;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1666
            if ((hi = fence) < 0) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1667
                mc = expectedModCount = m.modCount;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1668
                hi = fence = (tab == null) ? 0 : tab.length;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1669
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1670
            else
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1671
                mc = expectedModCount;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1672
            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
  1673
                (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
  1674
                Node<K,V> p = current;
17949
6c33d8f2601e 8013649: HashMap spliterator tryAdvance() encounters remaining elements after forEachRemaining()
psandoz
parents: 17939
diff changeset
  1675
                current = null;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1676
                do {
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1677
                    if (p == null)
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1678
                        p = tab[i++];
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1679
                    else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1680
                        action.accept(p.value);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1681
                        p = p.next;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1682
                    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1683
                } while (p != null || i < hi);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1684
                if (m.modCount != mc)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1685
                    throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1686
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1687
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1688
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1689
        public boolean tryAdvance(Consumer<? super V> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1690
            int hi;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1691
            if (action == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1692
                throw new NullPointerException();
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1693
            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
  1694
            if (tab != null && tab.length >= (hi = getFence()) && index >= 0) {
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1695
                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
  1696
                    if (current == null)
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1697
                        current = tab[index++];
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1698
                    else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1699
                        V v = current.value;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1700
                        current = current.next;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1701
                        action.accept(v);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1702
                        if (map.modCount != expectedModCount)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1703
                            throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1704
                        return true;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1705
                    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1706
                }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1707
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1708
            return false;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1709
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1710
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1711
        public int characteristics() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1712
            return (fence < 0 || est == map.size ? Spliterator.SIZED : 0);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1713
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1714
    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1715
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1716
    static final class EntrySpliterator<K,V>
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1717
        extends HashMapSpliterator<K,V>
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1718
        implements Spliterator<Map.Entry<K,V>> {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1719
        EntrySpliterator(HashMap<K,V> m, int origin, int fence, int est,
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1720
                         int expectedModCount) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1721
            super(m, origin, fence, est, expectedModCount);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1722
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1723
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1724
        public EntrySpliterator<K,V> trySplit() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1725
            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
  1726
            return (lo >= mid || current != null) ? null :
21352
0372edc9a995 8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents: 20199
diff changeset
  1727
                new EntrySpliterator<>(map, lo, index = mid, est >>>= 1,
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1728
                                          expectedModCount);
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1729
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1730
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1731
        public void forEachRemaining(Consumer<? super Map.Entry<K,V>> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1732
            int i, hi, mc;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1733
            if (action == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1734
                throw new NullPointerException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1735
            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
  1736
            Node<K,V>[] tab = m.table;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1737
            if ((hi = fence) < 0) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1738
                mc = expectedModCount = m.modCount;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1739
                hi = fence = (tab == null) ? 0 : tab.length;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1740
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1741
            else
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1742
                mc = expectedModCount;
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1743
            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
  1744
                (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
  1745
                Node<K,V> p = current;
17949
6c33d8f2601e 8013649: HashMap spliterator tryAdvance() encounters remaining elements after forEachRemaining()
psandoz
parents: 17939
diff changeset
  1746
                current = null;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1747
                do {
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1748
                    if (p == null)
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1749
                        p = tab[i++];
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1750
                    else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1751
                        action.accept(p);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1752
                        p = p.next;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1753
                    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1754
                } while (p != null || i < hi);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1755
                if (m.modCount != mc)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1756
                    throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1757
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1758
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1759
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1760
        public boolean tryAdvance(Consumer<? super Map.Entry<K,V>> action) {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1761
            int hi;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1762
            if (action == null)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1763
                throw new NullPointerException();
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1764
            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
  1765
            if (tab != null && tab.length >= (hi = getFence()) && index >= 0) {
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1766
                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
  1767
                    if (current == null)
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1768
                        current = tab[index++];
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1769
                    else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1770
                        Node<K,V> e = current;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1771
                        current = current.next;
17168
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1772
                        action.accept(e);
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1773
                        if (map.modCount != expectedModCount)
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1774
                            throw new ConcurrentModificationException();
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1775
                        return true;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1776
                    }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1777
                }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1778
            }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1779
            return false;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1780
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1781
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1782
        public int characteristics() {
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1783
            return (fence < 0 || est == map.size ? Spliterator.SIZED : 0) |
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1784
                Spliterator.DISTINCT;
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1785
        }
b7d3500f2516 8011426: java.util collection Spliterator implementations
psandoz
parents: 16867
diff changeset
  1786
    }
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1787
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1788
    /* ------------------------------------------------------------ */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1789
    // LinkedHashMap support
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
     * 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
  1794
     * 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
  1795
     * 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
  1796
     * 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
  1797
     * classes, and HashSet.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1798
     */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1799
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1800
    // 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
  1801
    Node<K,V> newNode(int hash, K key, V value, Node<K,V> next) {
21352
0372edc9a995 8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents: 20199
diff changeset
  1802
        return new Node<>(hash, key, value, next);
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1803
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1804
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1805
    // 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
  1806
    Node<K,V> replacementNode(Node<K,V> p, Node<K,V> next) {
21352
0372edc9a995 8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents: 20199
diff changeset
  1807
        return new Node<>(p.hash, p.key, p.value, next);
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1808
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1809
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1810
    // Create a tree bin node
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1811
    TreeNode<K,V> newTreeNode(int hash, K key, V value, Node<K,V> next) {
21352
0372edc9a995 8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents: 20199
diff changeset
  1812
        return new TreeNode<>(hash, key, value, next);
19806
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
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1815
    // For treeifyBin
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1816
    TreeNode<K,V> replacementTreeNode(Node<K,V> p, Node<K,V> next) {
21352
0372edc9a995 8024688: further split Map and ConcurrentMap defaults eliminating looping from Map defaults, Map.merge fixes and doc fixes.
mduigou
parents: 20199
diff changeset
  1817
        return new TreeNode<>(p.hash, p.key, p.value, next);
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1818
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1819
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1820
    /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1821
     * 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
  1822
     */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1823
    void reinitialize() {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1824
        table = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1825
        entrySet = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1826
        keySet = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1827
        values = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1828
        modCount = 0;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1829
        threshold = 0;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1830
        size = 0;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1831
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1832
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1833
    // 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
  1834
    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
  1835
    void afterNodeInsertion(boolean evict) { }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1836
    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
  1837
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1838
    // 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
  1839
    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
  1840
        Node<K,V>[] tab;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1841
        if (size > 0 && (tab = table) != null) {
47304
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents: 47216
diff changeset
  1842
            for (Node<K,V> e : tab) {
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 22055
diff changeset
  1843
                for (; e != null; e = e.next) {
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1844
                    s.writeObject(e.key);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1845
                    s.writeObject(e.value);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1846
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1847
            }
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
    /* ------------------------------------------------------------ */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1852
    // Tree bins
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1853
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1854
    /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1855
     * 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
  1856
     * 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
  1857
     * linked node.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1858
     */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1859
    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
  1860
        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
  1861
        TreeNode<K,V> left;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1862
        TreeNode<K,V> right;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1863
        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
  1864
        boolean red;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1865
        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
  1866
            super(hash, key, val, next);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1867
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1868
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1869
        /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1870
         * 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
  1871
         */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1872
        final TreeNode<K,V> root() {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1873
            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
  1874
                if ((p = r.parent) == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1875
                    return r;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1876
                r = p;
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
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1879
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1880
        /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1881
         * 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
  1882
         */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1883
        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
  1884
            int n;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1885
            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
  1886
                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
  1887
                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
  1888
                if (root != first) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1889
                    Node<K,V> rn;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1890
                    tab[index] = root;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1891
                    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
  1892
                    if ((rn = root.next) != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1893
                        ((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
  1894
                    if (rp != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1895
                        rp.next = rn;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1896
                    if (first != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1897
                        first.prev = root;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1898
                    root.next = first;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1899
                    root.prev = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1900
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1901
                assert checkInvariants(root);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1902
            }
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
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1905
        /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1906
         * 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
  1907
         * 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
  1908
         * comparing keys.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1909
         */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1910
        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
  1911
            TreeNode<K,V> p = this;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1912
            do {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1913
                int ph, dir; K pk;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1914
                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
  1915
                if ((ph = p.hash) > h)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1916
                    p = pl;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1917
                else if (ph < h)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1918
                    p = pr;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1919
                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
  1920
                    return p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1921
                else if (pl == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1922
                    p = pr;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1923
                else if (pr == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1924
                    p = pl;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1925
                else if ((kc != null ||
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1926
                          (kc = comparableClassFor(k)) != null) &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1927
                         (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
  1928
                    p = (dir < 0) ? pl : pr;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1929
                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
  1930
                    return q;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1931
                else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1932
                    p = pl;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1933
            } while (p != null);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1934
            return null;
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
         * Calls find for root node.
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> getTreeNode(int h, Object k) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1941
            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
  1942
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1943
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1944
        /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1945
         * 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
  1946
         * 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
  1947
         * 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
  1948
         * 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
  1949
         * necessary simplifies testing a bit.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1950
         */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1951
        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
  1952
            int d;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1953
            if (a == null || b == null ||
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1954
                (d = a.getClass().getName().
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1955
                 compareTo(b.getClass().getName())) == 0)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1956
                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
  1957
                     -1 : 1);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1958
            return d;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1959
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1960
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1961
        /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1962
         * 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
  1963
         */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1964
        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
  1965
            TreeNode<K,V> root = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1966
            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
  1967
                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
  1968
                x.left = x.right = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1969
                if (root == null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1970
                    x.parent = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1971
                    x.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1972
                    root = x;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1973
                }
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
                    K k = x.key;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1976
                    int h = x.hash;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1977
                    Class<?> kc = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1978
                    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
  1979
                        int dir, ph;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1980
                        K pk = p.key;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1981
                        if ((ph = p.hash) > h)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1982
                            dir = -1;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1983
                        else if (ph < h)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1984
                            dir = 1;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1985
                        else if ((kc == null &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1986
                                  (kc = comparableClassFor(k)) == null) ||
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1987
                                 (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
  1988
                            dir = tieBreakOrder(k, pk);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1989
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1990
                        TreeNode<K,V> xp = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1991
                        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
  1992
                            x.parent = xp;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1993
                            if (dir <= 0)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1994
                                xp.left = x;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1995
                            else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1996
                                xp.right = x;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1997
                            root = balanceInsertion(root, x);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1998
                            break;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  1999
                        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2000
                    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2001
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2002
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2003
            moveRootToFront(tab, root);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2004
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2005
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2006
        /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2007
         * 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
  2008
         * this node.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2009
         */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2010
        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
  2011
            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
  2012
            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
  2013
                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
  2014
                if (tl == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2015
                    hd = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2016
                else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2017
                    tl.next = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2018
                tl = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2019
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2020
            return hd;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2021
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2022
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2023
        /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2024
         * Tree version of putVal.
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2025
         */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2026
        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
  2027
                                       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
  2028
            Class<?> kc = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2029
            boolean searched = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2030
            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
  2031
            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
  2032
                int dir, ph; K pk;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2033
                if ((ph = p.hash) > h)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2034
                    dir = -1;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2035
                else if (ph < h)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2036
                    dir = 1;
24859
c54a1ef9eaae 8046085: inserting null key into HashMap treebin fails.
mduigou
parents: 22078
diff changeset
  2037
                else if ((pk = p.key) == k || (k != null && k.equals(pk)))
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2038
                    return p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2039
                else if ((kc == null &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2040
                          (kc = comparableClassFor(k)) == null) ||
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2041
                         (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
  2042
                    if (!searched) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2043
                        TreeNode<K,V> q, ch;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2044
                        searched = true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2045
                        if (((ch = p.left) != null &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2046
                             (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
  2047
                            ((ch = p.right) != null &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2048
                             (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
  2049
                            return q;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2050
                    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2051
                    dir = tieBreakOrder(k, pk);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2052
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2053
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2054
                TreeNode<K,V> xp = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2055
                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
  2056
                    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
  2057
                    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
  2058
                    if (dir <= 0)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2059
                        xp.left = x;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2060
                    else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2061
                        xp.right = x;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2062
                    xp.next = x;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2063
                    x.parent = x.prev = xp;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2064
                    if (xpn != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2065
                        ((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
  2066
                    moveRootToFront(tab, balanceInsertion(root, x));
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2067
                    return null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2068
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2069
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2070
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2071
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2072
        /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2073
         * 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
  2074
         * 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
  2075
         * 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
  2076
         * 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
  2077
         * 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
  2078
         * 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
  2079
         * 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
  2080
         * 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
  2081
         */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2082
        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
  2083
                                  boolean movable) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2084
            int n;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2085
            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
  2086
                return;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2087
            int index = (n - 1) & hash;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2088
            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
  2089
            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
  2090
            if (pred == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2091
                tab[index] = first = succ;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2092
            else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2093
                pred.next = succ;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2094
            if (succ != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2095
                succ.prev = pred;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2096
            if (first == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2097
                return;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2098
            if (root.parent != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2099
                root = root.root();
47304
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents: 47216
diff changeset
  2100
            if (root == null
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents: 47216
diff changeset
  2101
                || (movable
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents: 47216
diff changeset
  2102
                    && (root.right == null
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents: 47216
diff changeset
  2103
                        || (rl = root.left) == null
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents: 47216
diff changeset
  2104
                        || rl.left == null))) {
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2105
                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
  2106
                return;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2107
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2108
            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
  2109
            if (pl != null && pr != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2110
                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
  2111
                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
  2112
                    s = sl;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2113
                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
  2114
                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
  2115
                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
  2116
                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
  2117
                    p.parent = s;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2118
                    s.right = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2119
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2120
                else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2121
                    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
  2122
                    if ((p.parent = sp) != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2123
                        if (s == sp.left)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2124
                            sp.left = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2125
                        else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2126
                            sp.right = p;
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 ((s.right = pr) != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2129
                        pr.parent = s;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2130
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2131
                p.left = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2132
                if ((p.right = sr) != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2133
                    sr.parent = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2134
                if ((s.left = pl) != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2135
                    pl.parent = s;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2136
                if ((s.parent = pp) == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2137
                    root = s;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2138
                else if (p == pp.left)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2139
                    pp.left = s;
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
                    pp.right = s;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2142
                if (sr != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2143
                    replacement = sr;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2144
                else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2145
                    replacement = p;
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
            else if (pl != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2148
                replacement = pl;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2149
            else if (pr != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2150
                replacement = pr;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2151
            else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2152
                replacement = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2153
            if (replacement != p) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2154
                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
  2155
                if (pp == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2156
                    root = replacement;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2157
                else if (p == pp.left)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2158
                    pp.left = replacement;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2159
                else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2160
                    pp.right = replacement;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2161
                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
  2162
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2163
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2164
            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
  2165
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2166
            if (replacement == p) {  // detach
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2167
                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
  2168
                p.parent = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2169
                if (pp != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2170
                    if (p == pp.left)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2171
                        pp.left = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2172
                    else if (p == pp.right)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2173
                        pp.right = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2174
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2175
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2176
            if (movable)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2177
                moveRootToFront(tab, r);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2178
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2179
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2180
        /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2181
         * 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
  2182
         * 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
  2183
         * 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
  2184
         *
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2185
         * @param map the map
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2186
         * @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
  2187
         * @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
  2188
         * @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
  2189
         */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2190
        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
  2191
            TreeNode<K,V> b = this;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2192
            // 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
  2193
            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
  2194
            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
  2195
            int lc = 0, hc = 0;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2196
            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
  2197
                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
  2198
                e.next = null;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2199
                if ((e.hash & bit) == 0) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2200
                    if ((e.prev = loTail) == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2201
                        loHead = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2202
                    else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2203
                        loTail.next = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2204
                    loTail = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2205
                    ++lc;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2206
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2207
                else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2208
                    if ((e.prev = hiTail) == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2209
                        hiHead = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2210
                    else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2211
                        hiTail.next = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2212
                    hiTail = e;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2213
                    ++hc;
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
            if (loHead != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2218
                if (lc <= UNTREEIFY_THRESHOLD)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2219
                    tab[index] = loHead.untreeify(map);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2220
                else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2221
                    tab[index] = loHead;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2222
                    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
  2223
                        loHead.treeify(tab);
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
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2226
            if (hiHead != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2227
                if (hc <= UNTREEIFY_THRESHOLD)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2228
                    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
  2229
                else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2230
                    tab[index + bit] = hiHead;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2231
                    if (loHead != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2232
                        hiHead.treeify(tab);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2233
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2234
            }
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
        // 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
  2239
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2240
        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
  2241
                                              TreeNode<K,V> p) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2242
            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
  2243
            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
  2244
                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
  2245
                    rl.parent = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2246
                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
  2247
                    (root = r).red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2248
                else if (pp.left == p)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2249
                    pp.left = r;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2250
                else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2251
                    pp.right = r;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2252
                r.left = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2253
                p.parent = r;
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
            return root;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2256
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2257
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2258
        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
  2259
                                               TreeNode<K,V> p) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2260
            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
  2261
            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
  2262
                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
  2263
                    lr.parent = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2264
                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
  2265
                    (root = l).red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2266
                else if (pp.right == p)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2267
                    pp.right = l;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2268
                else
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2269
                    pp.left = l;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2270
                l.right = p;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2271
                p.parent = l;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2272
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2273
            return root;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2274
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2275
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2276
        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
  2277
                                                    TreeNode<K,V> x) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2278
            x.red = true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2279
            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
  2280
                if ((xp = x.parent) == null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2281
                    x.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2282
                    return x;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2283
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2284
                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
  2285
                    return root;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2286
                if (xp == (xppl = xpp.left)) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2287
                    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
  2288
                        xppr.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2289
                        xp.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2290
                        xpp.red = true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2291
                        x = xpp;
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 {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2294
                        if (x == xp.right) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2295
                            root = rotateLeft(root, x = xp);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2296
                            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
  2297
                        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2298
                        if (xp != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2299
                            xp.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2300
                            if (xpp != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2301
                                xpp.red = true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2302
                                root = rotateRight(root, xpp);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2303
                            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2304
                        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2305
                    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2306
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2307
                else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2308
                    if (xppl != null && xppl.red) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2309
                        xppl.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2310
                        xp.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2311
                        xpp.red = true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2312
                        x = xpp;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2313
                    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2314
                    else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2315
                        if (x == xp.left) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2316
                            root = rotateRight(root, x = xp);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2317
                            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
  2318
                        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2319
                        if (xp != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2320
                            xp.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2321
                            if (xpp != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2322
                                xpp.red = true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2323
                                root = rotateLeft(root, xpp);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2324
                            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2325
                        }
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
                }
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
        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
  2332
                                                   TreeNode<K,V> x) {
47304
3f5f9bc0bdc2 8186171: HashMap: Entry.setValue may not work after Iterator.remove() called for previous entries
dl
parents: 47216
diff changeset
  2333
            for (TreeNode<K,V> xp, xpl, xpr;;) {
19806
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2334
                if (x == null || x == root)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2335
                    return root;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2336
                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
  2337
                    x.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2338
                    return x;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2339
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2340
                else if (x.red) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2341
                    x.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2342
                    return root;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2343
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2344
                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
  2345
                    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
  2346
                        xpr.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2347
                        xp.red = true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2348
                        root = rotateLeft(root, xp);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2349
                        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
  2350
                    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2351
                    if (xpr == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2352
                        x = xp;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2353
                    else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2354
                        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
  2355
                        if ((sr == null || !sr.red) &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2356
                            (sl == null || !sl.red)) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2357
                            xpr.red = true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2358
                            x = xp;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2359
                        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2360
                        else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2361
                            if (sr == null || !sr.red) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2362
                                if (sl != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2363
                                    sl.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2364
                                xpr.red = true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2365
                                root = rotateRight(root, xpr);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2366
                                xpr = (xp = x.parent) == null ?
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2367
                                    null : xp.right;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2368
                            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2369
                            if (xpr != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2370
                                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
  2371
                                if ((sr = xpr.right) != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2372
                                    sr.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2373
                            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2374
                            if (xp != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2375
                                xp.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2376
                                root = rotateLeft(root, xp);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2377
                            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2378
                            x = root;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2379
                        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2380
                    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2381
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2382
                else { // symmetric
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2383
                    if (xpl != null && xpl.red) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2384
                        xpl.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2385
                        xp.red = true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2386
                        root = rotateRight(root, xp);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2387
                        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
  2388
                    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2389
                    if (xpl == null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2390
                        x = xp;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2391
                    else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2392
                        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
  2393
                        if ((sl == null || !sl.red) &&
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2394
                            (sr == null || !sr.red)) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2395
                            xpl.red = true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2396
                            x = xp;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2397
                        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2398
                        else {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2399
                            if (sl == null || !sl.red) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2400
                                if (sr != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2401
                                    sr.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2402
                                xpl.red = true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2403
                                root = rotateLeft(root, xpl);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2404
                                xpl = (xp = x.parent) == null ?
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2405
                                    null : xp.left;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2406
                            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2407
                            if (xpl != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2408
                                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
  2409
                                if ((sl = xpl.left) != null)
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2410
                                    sl.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2411
                            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2412
                            if (xp != null) {
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2413
                                xp.red = false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2414
                                root = rotateRight(root, xp);
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2415
                            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2416
                            x = root;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2417
                        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2418
                    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2419
                }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2420
            }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2421
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2422
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2423
        /**
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2424
         * Recursive invariant check
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2425
         */
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2426
        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
  2427
            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
  2428
                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
  2429
            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
  2430
                return false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2431
            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
  2432
                return false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2433
            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
  2434
                return false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2435
            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
  2436
                return false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2437
            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
  2438
                return false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2439
            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
  2440
                return false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2441
            if (tl != null && !checkInvariants(tl))
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2442
                return false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2443
            if (tr != null && !checkInvariants(tr))
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2444
                return false;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2445
            return true;
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2446
        }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2447
    }
dda89341ee2d 8023463: Improvements to HashMap/LinkedHashMap use of bins/buckets and trees (red/black)
psandoz
parents: 19184
diff changeset
  2448
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
}