src/java.base/share/classes/java/util/concurrent/ConcurrentSkipListMap.java
author darcy
Wed, 16 Oct 2019 16:55:52 -0700
changeset 58657 6252605fb005
parent 55032 11d9cd8d99f3
permissions -rw-r--r--
8232230: Suppress warnings on non-serializable non-transient instance fields in java.util.concurrent Reviewed-by: martin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * 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
     6
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
     8
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4110
diff changeset
    22
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * This file is available under and governed by the GNU General Public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * License version 2 only, as published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * However, the following notice accompanied the original version of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * file:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * Written by Doug Lea with assistance from members of JCP JSR-166
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Expert Group and released to the public domain, as explained at
9242
ef138d47df58 7034657: Update Creative Commons license URL in legal notices
dl
parents: 8764
diff changeset
    33
 * http://creativecommons.org/publicdomain/zero/1.0/
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
package java.util.concurrent;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
    37
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 39039
diff changeset
    38
import java.lang.invoke.MethodHandles;
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 39039
diff changeset
    39
import java.lang.invoke.VarHandle;
19378
0b98a290dd86 8022326: Spliterator for values of j.u.c.ConcurrentSkipListMap does not report ORDERED
psandoz
parents: 18767
diff changeset
    40
import java.io.Serializable;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    41
import java.util.AbstractCollection;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    42
import java.util.AbstractMap;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    43
import java.util.AbstractSet;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    44
import java.util.ArrayList;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    45
import java.util.Collection;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    46
import java.util.Collections;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    47
import java.util.Comparator;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    48
import java.util.Iterator;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    49
import java.util.List;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    50
import java.util.Map;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    51
import java.util.NavigableSet;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    52
import java.util.NoSuchElementException;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    53
import java.util.Set;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    54
import java.util.SortedMap;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    55
import java.util.Spliterator;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
    56
import java.util.function.BiConsumer;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    57
import java.util.function.BiFunction;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    58
import java.util.function.Consumer;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    59
import java.util.function.Function;
30441
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
    60
import java.util.function.Predicate;
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
    61
import java.util.concurrent.atomic.LongAdder;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * A scalable concurrent {@link ConcurrentNavigableMap} implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * The map is sorted according to the {@linkplain Comparable natural
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * ordering} of its keys, or by a {@link Comparator} provided at map
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * creation time, depending on which constructor is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * <p>This class implements a concurrent variant of <a
8764
f34ee71fc38a 6495521: (doc) ConcurrentSkipListMap links to web page NOT describing SkipList
dl
parents: 8544
diff changeset
    70
 * href="http://en.wikipedia.org/wiki/Skip_list" target="_top">SkipLists</a>
f34ee71fc38a 6495521: (doc) ConcurrentSkipListMap links to web page NOT describing SkipList
dl
parents: 8544
diff changeset
    71
 * providing expected average <i>log(n)</i> time cost for the
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    72
 * {@code containsKey}, {@code get}, {@code put} and
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    73
 * {@code remove} operations and their variants.  Insertion, removal,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * update, and access operations safely execute concurrently by
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19378
diff changeset
    75
 * multiple threads.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19378
diff changeset
    76
 *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19378
diff changeset
    77
 * <p>Iterators and spliterators are
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19378
diff changeset
    78
 * <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19378
diff changeset
    79
 *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19378
diff changeset
    80
 * <p>Ascending key ordered views and their iterators are faster than
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19378
diff changeset
    81
 * descending ones.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    83
 * <p>All {@code Map.Entry} pairs returned by methods in this class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * and its views represent snapshots of mappings at the time they were
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    85
 * produced. They do <em>not</em> support the {@code Entry.setValue}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * method. (Note however that it is possible to change mappings in the
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    87
 * associated map using {@code put}, {@code putIfAbsent}, or
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    88
 * {@code replace}, depending on exactly which effect you need.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
    90
 * <p>Beware that bulk operations {@code putAll}, {@code equals},
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    91
 * {@code toArray}, {@code containsValue}, and {@code clear} are
9515
04056ec0f477 7038885: Improved bulk operation disclaimers for concurrent collections
dl
parents: 9242
diff changeset
    92
 * <em>not</em> guaranteed to be performed atomically. For example, an
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    93
 * iterator operating concurrently with a {@code putAll} operation
9515
04056ec0f477 7038885: Improved bulk operation disclaimers for concurrent collections
dl
parents: 9242
diff changeset
    94
 * might view only some of the added elements.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * <p>This class and its views and iterators implement all of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * <em>optional</em> methods of the {@link Map} and {@link Iterator}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * interfaces. Like most other concurrent collections, this class does
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
    99
 * <em>not</em> permit the use of {@code null} keys or values because some
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * null return values cannot be reliably distinguished from the absence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * <p>This class is a member of the
49433
b6671a111395 8199465: {@docRoot} references need to be updated to reflect new module/package structure
jjg
parents: 48541
diff changeset
   104
 * <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * Java Collections Framework</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * @param <K> the type of keys maintained by this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * @param <V> the type of mapped values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
public class ConcurrentSkipListMap<K,V> extends AbstractMap<K,V>
19378
0b98a290dd86 8022326: Spliterator for values of j.u.c.ConcurrentSkipListMap does not report ORDERED
psandoz
parents: 18767
diff changeset
   113
    implements ConcurrentNavigableMap<K,V>, Cloneable, Serializable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * This class implements a tree-like two-dimensionally linked skip
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * list in which the index levels are represented in separate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * nodes from the base nodes holding data.  There are two reasons
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * for taking this approach instead of the usual array-based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * structure: 1) Array based implementations seem to encounter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * more complexity and overhead 2) We can use cheaper algorithms
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * for the heavily-traversed index lists than can be used for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * base lists.  Here's a picture of some of the basics for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * possible list with 2 levels of index:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Head nodes          Index nodes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * +-+    right        +-+                      +-+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * |2|---------------->| |--------------------->| |->null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * +-+                 +-+                      +-+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *  | down              |                        |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *  v                   v                        v
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * +-+            +-+  +-+       +-+            +-+       +-+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * |1|----------->| |->| |------>| |----------->| |------>| |->null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * +-+            +-+  +-+       +-+            +-+       +-+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *  v              |    |         |              |         |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * Nodes  next     v    v         v              v         v
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * +-+  +-+  +-+  +-+  +-+  +-+  +-+  +-+  +-+  +-+  +-+  +-+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * | |->|A|->|B|->|C|->|D|->|E|->|F|->|G|->|H|->|I|->|J|->|K|->null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * +-+  +-+  +-+  +-+  +-+  +-+  +-+  +-+  +-+  +-+  +-+  +-+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * The base lists use a variant of the HM linked ordered set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * algorithm. See Tim Harris, "A pragmatic implementation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * non-blocking linked lists"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * http://www.cl.cam.ac.uk/~tlh20/publications.html and Maged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Michael "High Performance Dynamic Lock-Free Hash Tables and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * List-Based Sets"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * http://www.research.ibm.com/people/m/michael/pubs.htm.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * basic idea in these lists is to mark the "next" pointers of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * deleted nodes when deleting to avoid conflicts with concurrent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * insertions, and when traversing to keep track of triples
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * (predecessor, node, successor) in order to detect when and how
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * to unlink these deleted nodes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Rather than using mark-bits to mark list deletions (which can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * be slow and space-intensive using AtomicMarkedReference), nodes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * use direct CAS'able next pointers.  On deletion, instead of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * marking a pointer, they splice in another node that can be
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   157
     * thought of as standing for a marked pointer (see method
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   158
     * unlinkNode).  Using plain nodes acts roughly like "boxed"
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   159
     * implementations of marked pointers, but uses new nodes only
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   160
     * when nodes are deleted, not for every link.  This requires less
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   161
     * space and supports faster traversal. Even if marked references
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   162
     * were better supported by JVMs, traversal using this technique
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   163
     * might still be faster because any search need only read ahead
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   164
     * one more node than otherwise required (to check for trailing
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   165
     * marker) rather than unmasking mark bits or whatever on each
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   166
     * read.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * This approach maintains the essential property needed in the HM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * algorithm of changing the next-pointer of a deleted node so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * that any other CAS of it will fail, but implements the idea by
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   171
     * changing the pointer to point to a different node (with
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   172
     * otherwise illegal null fields), not by marking it.  While it
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   173
     * would be possible to further squeeze space by defining marker
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   174
     * nodes not to have key/value fields, it isn't worth the extra
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   175
     * type-testing overhead.  The deletion markers are rarely
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   176
     * encountered during traversal, are easily detected via null
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   177
     * checks that are needed anyway, and are normally quickly garbage
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   178
     * collected. (Note that this technique would not work well in
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   179
     * systems without garbage collection.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * In addition to using deletion markers, the lists also use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * nullness of value fields to indicate deletion, in a style
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * similar to typical lazy-deletion schemes.  If a node's value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * null, then it is considered logically deleted and ignored even
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   185
     * though it is still reachable.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Here's the sequence of events for a deletion of node n with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * predecessor b and successor f, initially:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *        +------+       +------+      +------+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *   ...  |   b  |------>|   n  |----->|   f  | ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *        +------+       +------+      +------+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * 1. CAS n's value field from non-null to null.
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   195
     *    Traversals encountering a node with null value ignore it.
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   196
     *    However, ongoing insertions and deletions might still modify
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *    n's next pointer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * 2. CAS n's next pointer to point to a new marker node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *    From this point on, no other nodes can be appended to n.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *    which avoids deletion errors in CAS-based linked lists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *        +------+       +------+      +------+       +------+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *   ...  |   b  |------>|   n  |----->|marker|------>|   f  | ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *        +------+       +------+      +------+       +------+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * 3. CAS b's next pointer over both n and its marker.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *    From this point on, no new traversals will encounter n,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *    and it can eventually be GCed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *        +------+                                    +------+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *   ...  |   b  |----------------------------------->|   f  | ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *        +------+                                    +------+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * A failure at step 1 leads to simple retry due to a lost race
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * with another operation. Steps 2-3 can fail because some other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * thread noticed during a traversal a node with null value and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * helped out by marking and/or unlinking.  This helping-out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * ensures that no thread can become stuck waiting for progress of
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   219
     * the deleting thread.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Skip lists add indexing to this scheme, so that the base-level
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * traversals start close to the locations being found, inserted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * or deleted -- usually base level traversals only traverse a few
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * nodes. This doesn't change the basic algorithm except for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * need to make sure base traversals start at predecessors (here,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * b) that are not (structurally) deleted, otherwise retrying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * after processing the deletion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   229
     * Index levels are maintained using CAS to link and unlink
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   230
     * successors ("right" fields).  Races are allowed in index-list
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   231
     * operations that can (rarely) fail to link in a new index node.
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   232
     * (We can't do this of course for data nodes.)  However, even
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   233
     * when this happens, the index lists correctly guide search.
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   234
     * This can impact performance, but since skip lists are
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   235
     * probabilistic anyway, the net result is that under contention,
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   236
     * the effective "p" value may be lower than its nominal value.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   238
     * Index insertion and deletion sometimes require a separate
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   239
     * traversal pass occurring after the base-level action, to add or
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   240
     * remove index nodes.  This adds to single-threaded overhead, but
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   241
     * improves contended multithreaded performance by narrowing
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   242
     * interference windows, and allows deletion to ensure that all
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   243
     * index nodes will be made unreachable upon return from a public
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   244
     * remove operation, thus avoiding unwanted garbage retention.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * Indexing uses skip list parameters that maintain good search
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * performance while using sparser-than-usual indices: The
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   248
     * hardwired parameters k=1, p=0.5 (see method doPut) mean that
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   249
     * about one-quarter of the nodes have indices. Of those that do,
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   250
     * half have one level, a quarter have two, and so on (see Pugh's
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   251
     * Skip List Cookbook, sec 3.4), up to a maximum of 62 levels
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   252
     * (appropriate for up to 2^63 elements).  The expected total
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   253
     * space requirement for a map is slightly less than for the
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   254
     * current implementation of java.util.TreeMap.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * Changing the level of the index (i.e, the height of the
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   257
     * tree-like structure) also uses CAS.  Creation of an index with
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   258
     * height greater than the current level adds a level to the head
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   259
     * index by CAS'ing on a new top-most head. To maintain good
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   260
     * performance after a lot of removals, deletion methods
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   261
     * heuristically try to reduce the height if the topmost levels
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   262
     * appear to be empty.  This may encounter races in which it is
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   263
     * possible (but rare) to reduce and "lose" a level just as it is
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   264
     * about to contain an index (that will then never be
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   265
     * encountered). This does no structural harm, and in practice
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   266
     * appears to be a better option than allowing unrestrained growth
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   267
     * of levels.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   269
     * This class provides concurrent-reader-style memory consistency,
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   270
     * ensuring that read-only methods report status and/or values no
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   271
     * staler than those holding at method entry. This is done by
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   272
     * performing all publication and structural updates using
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   273
     * (volatile) CAS, placing an acquireFence in a few access
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   274
     * methods, and ensuring that linked objects are transitively
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   275
     * acquired via dependent reads (normally once) unless performing
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   276
     * a volatile-mode CAS operation (that also acts as an acquire and
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   277
     * release).  This form of fence-hoisting is similar to RCU and
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   278
     * related techniques (see McKenney's online book
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   279
     * https://www.kernel.org/pub/linux/kernel/people/paulmck/perfbook/perfbook.html)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   280
     * It minimizes overhead that may otherwise occur when using so
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   281
     * many volatile-mode reads. Using explicit acquireFences is
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   282
     * logistically easier than targeting particular fields to be read
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   283
     * in acquire mode: fences are just hoisted up as far as possible,
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   284
     * to the entry points or loop headers of a few methods. A
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   285
     * potential disadvantage is that these few remaining fences are
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   286
     * not easily optimized away by compilers under exclusively
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   287
     * single-thread use.  It requires some care to avoid volatile
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   288
     * mode reads of other fields. (Note that the memory semantics of
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   289
     * a reference dependently read in plain mode exactly once are
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   290
     * equivalent to those for atomic opaque mode.)  Iterators and
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   291
     * other traversals encounter each node and value exactly once.
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   292
     * Other operations locate an element (or position to insert an
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   293
     * element) via a sequence of dereferences. This search is broken
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   294
     * into two parts. Method findPredecessor (and its specialized
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   295
     * embeddings) searches index nodes only, returning a base-level
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   296
     * predecessor of the key. Callers carry out the base-level
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   297
     * search, restarting if encountering a marker preventing link
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   298
     * modification.  In some cases, it is possible to encounter a
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   299
     * node multiple times while descending levels. For mutative
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   300
     * operations, the reported value is validated using CAS (else
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   301
     * retrying), preserving linearizability with respect to each
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   302
     * other. Others may return any (non-null) value holding in the
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   303
     * course of the method call.  (Search-based methods also include
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   304
     * some useless-looking explicit null checks designed to allow
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   305
     * more fields to be nulled out upon removal, to reduce floating
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   306
     * garbage, but which is not currently done, pending discovery of
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   307
     * a way to do this with less impact on other operations.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   309
     * To produce random values without interference across threads,
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   310
     * we use within-JDK thread local random support (via the
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   311
     * "secondary seed", to avoid interference with user-level
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   312
     * ThreadLocalRandom.)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   313
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * For explanation of algorithms sharing at least a couple of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * features with this one, see Mikhail Fomitchev's thesis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * (http://www.cs.yorku.ca/~mikhail/), Keir Fraser's thesis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * (http://www.cl.cam.ac.uk/users/kaf24/), and Hakan Sundell's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * thesis (http://www.cs.chalmers.se/~phs/).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * Notation guide for local variables
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   321
     * Node:         b, n, f, p for  predecessor, node, successor, aux
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * Index:        q, r, d    for index node, right, down.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * Head:         h
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * Keys:         k, key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * Values:       v, value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * Comparisons:  c
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    private static final long serialVersionUID = -8627078645895051609L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    /**
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   332
     * The comparator used to maintain order in this map, or null if
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   333
     * using natural ordering.  (Non-private to simplify access in
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   334
     * nested classes.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     */
58657
6252605fb005 8232230: Suppress warnings on non-serializable non-transient instance fields in java.util.concurrent
darcy
parents: 55032
diff changeset
   337
    @SuppressWarnings("serial") // Conditionally serializable
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   338
    final Comparator<? super K> comparator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   340
    /** Lazily initialized topmost index of the skiplist. */
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   341
    private transient Index<K,V> head;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   342
    /** Lazily initialized element count */
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   343
    private transient LongAdder adder;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    /** Lazily initialized key set */
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
   345
    private transient KeySet<K,V> keySet;
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 33674
diff changeset
   346
    /** Lazily initialized values collection */
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 33674
diff changeset
   347
    private transient Values<K,V> values;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    /** Lazily initialized entry set */
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9515
diff changeset
   349
    private transient EntrySet<K,V> entrySet;
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   350
    /** Lazily initialized descending map */
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 33674
diff changeset
   351
    private transient SubMap<K,V> descendingMap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * Nodes hold keys and values, and are singly linked in sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * order, possibly with some intervening marker nodes. The list is
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   356
     * headed by a header node accessible as head.node. Headers and
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   357
     * marker nodes have null keys. The val field (but currently not
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   358
     * the key field) is nulled out upon deletion.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    static final class Node<K,V> {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   361
        final K key; // currently, never detached
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   362
        V val;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   363
        Node<K,V> next;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   364
        Node(K key, V value, Node<K,V> next) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            this.key = key;
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   366
            this.val = value;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            this.next = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    /**
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   372
     * Index nodes represent the levels of the skip list.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     */
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   374
    static final class Index<K,V> {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   375
        final Node<K,V> node;  // currently, never detached
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        final Index<K,V> down;
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   377
        Index<K,V> right;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        Index(Node<K,V> node, Index<K,V> down, Index<K,V> right) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            this.node = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            this.down = down;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            this.right = right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   385
    /* ----------------  Utilities -------------- */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    /**
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   388
     * Compares using comparator or natural ordering if null.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   389
     * Called only by methods that have performed required type checks.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     */
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   391
    @SuppressWarnings({"unchecked", "rawtypes"})
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   392
    static int cpr(Comparator c, Object x, Object y) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   393
        return (c != null) ? c.compare(x, y) : ((Comparable)x).compareTo(y);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   396
    /**
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   397
     * Returns the header for base node list, or null if uninitialized
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   398
     */
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   399
    final Node<K,V> baseHead() {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   400
        Index<K,V> h;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   401
        VarHandle.acquireFence();
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   402
        return ((h = head) == null) ? null : h.node;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   403
    }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   404
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   405
    /**
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   406
     * Tries to unlink deleted node n from predecessor b (if both
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   407
     * exist), by first splicing in a marker if not already present.
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   408
     * Upon return, node n is sure to be unlinked from b, possibly
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   409
     * via the actions of some other thread.
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   410
     *
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   411
     * @param b if nonnull, predecessor
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   412
     * @param n if nonnull, node known to be deleted
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   413
     */
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   414
    static <K,V> void unlinkNode(Node<K,V> b, Node<K,V> n) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   415
        if (b != null && n != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   416
            Node<K,V> f, p;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   417
            for (;;) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   418
                if ((f = n.next) != null && f.key == null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   419
                    p = f.next;               // already marked
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   420
                    break;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   421
                }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   422
                else if (NEXT.compareAndSet(n, f,
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   423
                                            new Node<K,V>(null, null, f))) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   424
                    p = f;                    // add marker
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   425
                    break;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   426
                }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   427
            }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   428
            NEXT.compareAndSet(b, n, p);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   429
        }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   430
    }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   431
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   432
    /**
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   433
     * Adds to element count, initializing adder if necessary
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   434
     *
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   435
     * @param c count to add
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   436
     */
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   437
    private void addCount(long c) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   438
        LongAdder a;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   439
        do {} while ((a = adder) == null &&
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   440
                     !ADDER.compareAndSet(this, null, a = new LongAdder()));
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   441
        a.add(c);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   442
    }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   443
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   444
    /**
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   445
     * Returns element count, initializing adder if necessary.
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   446
     */
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   447
    final long getAdderCount() {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   448
        LongAdder a; long c;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   449
        do {} while ((a = adder) == null &&
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   450
                     !ADDER.compareAndSet(this, null, a = new LongAdder()));
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   451
        return ((c = a.sum()) <= 0L) ? 0L : c; // ignore transient negatives
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   452
    }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   453
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    /* ---------------- Traversal -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    /**
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   457
     * Returns an index node with key strictly less than given key.
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   458
     * Also unlinks indexes to deleted nodes found along the way.
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   459
     * Callers rely on this side-effect of clearing indices to deleted
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   460
     * nodes.
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   461
     *
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   462
     * @param key if nonnull the key
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   463
     * @return a predecessor node of key, or null if uninitialized or null key
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     */
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   465
    private Node<K,V> findPredecessor(Object key, Comparator<? super K> cmp) {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   466
        Index<K,V> q;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   467
        VarHandle.acquireFence();
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   468
        if ((q = head) == null || key == null)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   469
            return null;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   470
        else {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   471
            for (Index<K,V> r, d;;) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   472
                while ((r = q.right) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   473
                    Node<K,V> p; K k;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   474
                    if ((p = r.node) == null || (k = p.key) == null ||
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   475
                        p.val == null)  // unlink index to deleted node
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   476
                        RIGHT.compareAndSet(q, r, r.right);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   477
                    else if (cpr(cmp, key, k) > 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                        q = r;
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   479
                    else
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   480
                        break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                }
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   482
                if ((d = q.down) != null)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   483
                    q = d;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   484
                else
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                    return q.node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * Returns node holding key or null if no such, clearing out any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * deleted nodes seen along the way.  Repeatedly traverses at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * base-level looking for key starting at predecessor returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * from findPredecessor, processing base-level deletions as
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   495
     * encountered. Restarts occur, at traversal step encountering
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   496
     * node n, if n's key field is null, indicating it is a marker, so
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   497
     * its predecessor is deleted before continuing, which we help do
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   498
     * by re-finding a valid predecessor.  The traversal loops in
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   499
     * doPut, doRemove, and findNear all include the same checks.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * @param key the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * @return node holding key, or null if no such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     */
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   504
    private Node<K,V> findNode(Object key) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   505
        if (key == null)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   506
            throw new NullPointerException(); // don't postpone errors
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   507
        Comparator<? super K> cmp = comparator;
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   508
        Node<K,V> b;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   509
        outer: while ((b = findPredecessor(key, cmp)) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   510
            for (;;) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   511
                Node<K,V> n; K k; V v; int c;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   512
                if ((n = b.next) == null)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   513
                    break outer;               // empty
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   514
                else if ((k = n.key) == null)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   515
                    break;                     // b is deleted
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   516
                else if ((v = n.val) == null)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   517
                    unlinkNode(b, n);          // n is deleted
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   518
                else if ((c = cpr(cmp, key, k)) > 0)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   519
                    b = n;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   520
                else if (c == 0)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   521
                    return n;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   522
                else
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   523
                    break outer;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        }
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   526
        return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    /**
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   530
     * Gets value for key. Same idea as findNode, except skips over
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   531
     * deletions and markers, and returns first encountered value to
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   532
     * avoid possibly inconsistent rereads.
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   533
     *
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   534
     * @param key the key
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * @return the value, or null if absent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     */
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   537
    private V doGet(Object key) {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   538
        Index<K,V> q;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   539
        VarHandle.acquireFence();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   540
        if (key == null)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   541
            throw new NullPointerException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   542
        Comparator<? super K> cmp = comparator;
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   543
        V result = null;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   544
        if ((q = head) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   545
            outer: for (Index<K,V> r, d;;) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   546
                while ((r = q.right) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   547
                    Node<K,V> p; K k; V v; int c;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   548
                    if ((p = r.node) == null || (k = p.key) == null ||
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   549
                        (v = p.val) == null)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   550
                        RIGHT.compareAndSet(q, r, r.right);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   551
                    else if ((c = cpr(cmp, key, k)) > 0)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   552
                        q = r;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   553
                    else if (c == 0) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   554
                        result = v;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   555
                        break outer;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   556
                    }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   557
                    else
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   558
                        break;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   559
                }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   560
                if ((d = q.down) != null)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   561
                    q = d;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   562
                else {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   563
                    Node<K,V> b, n;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   564
                    if ((b = q.node) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   565
                        while ((n = b.next) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   566
                            V v; int c;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   567
                            K k = n.key;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   568
                            if ((v = n.val) == null || k == null ||
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   569
                                (c = cpr(cmp, key, k)) > 0)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   570
                                b = n;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   571
                            else {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   572
                                if (c == 0)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   573
                                    result = v;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   574
                                break;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   575
                            }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   576
                        }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   577
                    }
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   578
                    break;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   579
                }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   580
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        }
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   582
        return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    /* ---------------- Insertion -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * Main insertion method.  Adds element if not present, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * replaces value if present and onlyIfAbsent is false.
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   590
     *
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   591
     * @param key the key
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   592
     * @param value the value that must be associated with key
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * @param onlyIfAbsent if should not insert if already present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * @return the old value, or null if newly inserted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     */
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   596
    private V doPut(K key, V value, boolean onlyIfAbsent) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   597
        if (key == null)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   598
            throw new NullPointerException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   599
        Comparator<? super K> cmp = comparator;
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   600
        for (;;) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   601
            Index<K,V> h; Node<K,V> b;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   602
            VarHandle.acquireFence();
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   603
            int levels = 0;                    // number of levels descended
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   604
            if ((h = head) == null) {          // try to initialize
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   605
                Node<K,V> base = new Node<K,V>(null, null, null);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   606
                h = new Index<K,V>(base, null, null);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   607
                b = (HEAD.compareAndSet(this, null, h)) ? base : null;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   608
            }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   609
            else {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   610
                for (Index<K,V> q = h, r, d;;) { // count while descending
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   611
                    while ((r = q.right) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   612
                        Node<K,V> p; K k;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   613
                        if ((p = r.node) == null || (k = p.key) == null ||
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   614
                            p.val == null)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   615
                            RIGHT.compareAndSet(q, r, r.right);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   616
                        else if (cpr(cmp, key, k) > 0)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   617
                            q = r;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   618
                        else
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   619
                            break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                    }
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   621
                    if ((d = q.down) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   622
                        ++levels;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   623
                        q = d;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   624
                    }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   625
                    else {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   626
                        b = q.node;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   627
                        break;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   628
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            }
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   631
            if (b != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   632
                Node<K,V> z = null;              // new node, if inserted
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   633
                for (;;) {                       // find insertion point
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   634
                    Node<K,V> n, p; K k; V v; int c;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   635
                    if ((n = b.next) == null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   636
                        if (b.key == null)       // if empty, type check key now
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   637
                            cpr(cmp, key, key);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   638
                        c = -1;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   639
                    }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   640
                    else if ((k = n.key) == null)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   641
                        break;                   // can't append; restart
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   642
                    else if ((v = n.val) == null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   643
                        unlinkNode(b, n);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   644
                        c = 1;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   645
                    }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   646
                    else if ((c = cpr(cmp, key, k)) > 0)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   647
                        b = n;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   648
                    else if (c == 0 &&
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   649
                             (onlyIfAbsent || VAL.compareAndSet(n, v, value)))
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   650
                        return v;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   651
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   652
                    if (c < 0 &&
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   653
                        NEXT.compareAndSet(b, n,
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   654
                                           p = new Node<K,V>(key, value, n))) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   655
                        z = p;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   656
                        break;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   657
                    }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   658
                }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   659
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   660
                if (z != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   661
                    int lr = ThreadLocalRandom.nextSecondarySeed();
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   662
                    if ((lr & 0x3) == 0) {       // add indices with 1/4 prob
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   663
                        int hr = ThreadLocalRandom.nextSecondarySeed();
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   664
                        long rnd = ((long)hr << 32) | ((long)lr & 0xffffffffL);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   665
                        int skips = levels;      // levels to descend before add
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   666
                        Index<K,V> x = null;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   667
                        for (;;) {               // create at most 62 indices
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   668
                            x = new Index<K,V>(z, x, null);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   669
                            if (rnd >= 0L || --skips < 0)
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   670
                                break;
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   671
                            else
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   672
                                rnd <<= 1;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   673
                        }
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   674
                        if (addIndices(h, skips, x, cmp) && skips < 0 &&
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   675
                            head == h) {         // try to add new level
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   676
                            Index<K,V> hx = new Index<K,V>(z, x, null);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   677
                            Index<K,V> nh = new Index<K,V>(h.node, h, hx);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   678
                            HEAD.compareAndSet(this, h, nh);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   679
                        }
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   680
                        if (z.val == null)       // deleted while adding indices
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   681
                            findPredecessor(key, cmp); // clean
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   682
                    }
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   683
                    addCount(1L);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   684
                    return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        }
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   688
    }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   689
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   690
    /**
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   691
     * Add indices after an insertion. Descends iteratively to the
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   692
     * highest level of insertion, then recursively, to chain index
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   693
     * nodes to lower ones. Returns null on (staleness) failure,
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   694
     * disabling higher-level insertions. Recursion depths are
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   695
     * exponentially less probable.
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   696
     *
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   697
     * @param q starting index for current level
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   698
     * @param skips levels to skip before inserting
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   699
     * @param x index for this insertion
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   700
     * @param cmp comparator
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   701
     */
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   702
    static <K,V> boolean addIndices(Index<K,V> q, int skips, Index<K,V> x,
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   703
                                    Comparator<? super K> cmp) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   704
        Node<K,V> z; K key;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   705
        if (x != null && (z = x.node) != null && (key = z.key) != null &&
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   706
            q != null) {                            // hoist checks
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   707
            boolean retrying = false;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   708
            for (;;) {                              // find splice point
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   709
                Index<K,V> r, d; int c;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   710
                if ((r = q.right) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   711
                    Node<K,V> p; K k;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   712
                    if ((p = r.node) == null || (k = p.key) == null ||
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   713
                        p.val == null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   714
                        RIGHT.compareAndSet(q, r, r.right);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   715
                        c = 0;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   716
                    }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   717
                    else if ((c = cpr(cmp, key, k)) > 0)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   718
                        q = r;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   719
                    else if (c == 0)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   720
                        break;                      // stale
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   721
                }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   722
                else
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   723
                    c = -1;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   724
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   725
                if (c < 0) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   726
                    if ((d = q.down) != null && skips > 0) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   727
                        --skips;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   728
                        q = d;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   729
                    }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   730
                    else if (d != null && !retrying &&
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   731
                             !addIndices(d, 0, x.down, cmp))
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   732
                        break;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   733
                    else {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   734
                        x.right = r;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   735
                        if (RIGHT.compareAndSet(q, r, x))
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   736
                            return true;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   737
                        else
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   738
                            retrying = true;         // re-find splice point
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   739
                    }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   740
                }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   741
            }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   742
        }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   743
        return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    /* ---------------- Deletion -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * Main deletion method. Locates node, nulls value, appends a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * deletion marker, unlinks predecessor, removes associated index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * nodes, and possibly reduces head index level.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     *
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   753
     * @param key the key
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * @param value if non-null, the value that must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * associated with key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * @return the node, or null if not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     */
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   758
    final V doRemove(Object key, Object value) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   759
        if (key == null)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   760
            throw new NullPointerException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   761
        Comparator<? super K> cmp = comparator;
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   762
        V result = null;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   763
        Node<K,V> b;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   764
        outer: while ((b = findPredecessor(key, cmp)) != null &&
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   765
                      result == null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   766
            for (;;) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   767
                Node<K,V> n; K k; V v; int c;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   768
                if ((n = b.next) == null)
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   769
                    break outer;
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   770
                else if ((k = n.key) == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                    break;
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   772
                else if ((v = n.val) == null)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   773
                    unlinkNode(b, n);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   774
                else if ((c = cpr(cmp, key, k)) > 0)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   775
                    b = n;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   776
                else if (c < 0)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   777
                    break outer;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   778
                else if (value != null && !value.equals(v))
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   779
                    break outer;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   780
                else if (VAL.compareAndSet(n, v, null)) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   781
                    result = v;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   782
                    unlinkNode(b, n);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   783
                    break; // loop to clean up
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        }
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   787
        if (result != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   788
            tryReduceLevel();
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   789
            addCount(-1L);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   790
        }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   791
        return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * Possibly reduce head level if it has no nodes.  This method can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * (rarely) make mistakes, in which case levels can disappear even
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * though they are about to contain index nodes. This impacts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * performance, not correctness.  To minimize mistakes as well as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * to reduce hysteresis, the level is reduced by one only if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * topmost three levels look empty. Also, if the removed level
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * looks non-empty after CAS, we try to change it back quick
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * before anyone notices our mistake! (This trick works pretty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * well because this method will practically never make mistakes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * unless current thread stalls immediately before first CAS, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * which case it is very unlikely to stall again immediately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * afterwards, so will recover.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * We put up with all this rather than just let levels grow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * because otherwise, even a small map that has undergone a large
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * number of insertions and removals will have a lot of levels,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * slowing down access more than would an occasional unwanted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * reduction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    private void tryReduceLevel() {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   815
        Index<K,V> h, d, e;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   816
        if ((h = head) != null && h.right == null &&
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   817
            (d = h.down) != null && d.right == null &&
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   818
            (e = d.down) != null && e.right == null &&
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   819
            HEAD.compareAndSet(this, h, d) &&
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   820
            h.right != null)   // recheck
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   821
            HEAD.compareAndSet(this, d, h);  // try to backout
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    /* ---------------- Finding and removing first element -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    /**
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   827
     * Gets first valid node, unlinking deleted nodes if encountered.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     * @return first node or null if empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     */
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   830
    final Node<K,V> findFirst() {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   831
        Node<K,V> b, n;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   832
        if ((b = baseHead()) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   833
            while ((n = b.next) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   834
                if (n.val == null)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   835
                    unlinkNode(b, n);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   836
                else
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   837
                    return n;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   838
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        }
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   840
        return null;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   841
    }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   842
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   843
    /**
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   844
     * Entry snapshot version of findFirst
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   845
     */
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   846
    final AbstractMap.SimpleImmutableEntry<K,V> findFirstEntry() {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   847
        Node<K,V> b, n; V v;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   848
        if ((b = baseHead()) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   849
            while ((n = b.next) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   850
                if ((v = n.val) == null)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   851
                    unlinkNode(b, n);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   852
                else
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   853
                    return new AbstractMap.SimpleImmutableEntry<K,V>(n.key, v);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   854
            }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   855
        }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   856
        return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * Removes first entry; returns its snapshot.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * @return null if empty, else snapshot of first entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     */
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   863
    private AbstractMap.SimpleImmutableEntry<K,V> doRemoveFirstEntry() {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   864
        Node<K,V> b, n; V v;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   865
        if ((b = baseHead()) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   866
            while ((n = b.next) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   867
                if ((v = n.val) == null || VAL.compareAndSet(n, v, null)) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   868
                    K k = n.key;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   869
                    unlinkNode(b, n);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   870
                    if (v != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   871
                        tryReduceLevel();
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   872
                        findPredecessor(k, comparator); // clean index
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   873
                        addCount(-1L);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   874
                        return new AbstractMap.SimpleImmutableEntry<K,V>(k, v);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   875
                    }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   876
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        }
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   879
        return null;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   880
    }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   881
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   882
    /* ---------------- Finding and removing last element -------------- */
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   883
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   884
    /**
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   885
     * Specialized version of find to get last valid node.
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   886
     * @return last node or null if empty
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   887
     */
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   888
    final Node<K,V> findLast() {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   889
        outer: for (;;) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   890
            Index<K,V> q; Node<K,V> b;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   891
            VarHandle.acquireFence();
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   892
            if ((q = head) == null)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   893
                break;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   894
            for (Index<K,V> r, d;;) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   895
                while ((r = q.right) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   896
                    Node<K,V> p;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   897
                    if ((p = r.node) == null || p.val == null)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   898
                        RIGHT.compareAndSet(q, r, r.right);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   899
                    else
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   900
                        q = r;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   901
                }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   902
                if ((d = q.down) != null)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   903
                    q = d;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   904
                else {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   905
                    b = q.node;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   906
                    break;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   907
                }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   908
            }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   909
            if (b != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   910
                for (;;) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   911
                    Node<K,V> n;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   912
                    if ((n = b.next) == null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   913
                        if (b.key == null) // empty
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   914
                            break outer;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   915
                        else
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   916
                            return b;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   917
                    }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   918
                    else if (n.key == null)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   919
                        break;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   920
                    else if (n.val == null)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   921
                        unlinkNode(b, n);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   922
                    else
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   923
                        b = n;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   924
                }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   925
            }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   926
        }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   927
        return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
    /**
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   931
     * Entry version of findLast
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   932
     * @return Entry for last node or null if empty
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     */
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   934
    final AbstractMap.SimpleImmutableEntry<K,V> findLastEntry() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        for (;;) {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   936
            Node<K,V> n; V v;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   937
            if ((n = findLast()) == null)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   938
                return null;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   939
            if ((v = n.val) != null)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   940
                return new AbstractMap.SimpleImmutableEntry<K,V>(n.key, v);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   944
    /**
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   945
     * Removes last entry; returns its snapshot.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   946
     * Specialized variant of doRemove.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   947
     * @return null if empty, else snapshot of last entry
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   948
     */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   949
    private Map.Entry<K,V> doRemoveLastEntry() {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   950
        outer: for (;;) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   951
            Index<K,V> q; Node<K,V> b;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   952
            VarHandle.acquireFence();
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   953
            if ((q = head) == null)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   954
                break;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
   955
            for (;;) {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   956
                Index<K,V> d, r; Node<K,V> p;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   957
                while ((r = q.right) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   958
                    if ((p = r.node) == null || p.val == null)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   959
                        RIGHT.compareAndSet(q, r, r.right);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   960
                    else if (p.next != null)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   961
                        q = r;  // continue only if a successor
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   962
                    else
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                if ((d = q.down) != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                    q = d;
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   967
                else {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   968
                    b = q.node;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   969
                    break;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   970
                }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   971
            }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   972
            if (b != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   973
                for (;;) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   974
                    Node<K,V> n; K k; V v;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   975
                    if ((n = b.next) == null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   976
                        if (b.key == null) // empty
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   977
                            break outer;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   978
                        else
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   979
                            break; // retry
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   980
                    }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   981
                    else if ((k = n.key) == null)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   982
                        break;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   983
                    else if ((v = n.val) == null)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   984
                        unlinkNode(b, n);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   985
                    else if (n.next != null)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   986
                        b = n;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   987
                    else if (VAL.compareAndSet(n, v, null)) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   988
                        unlinkNode(b, n);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   989
                        tryReduceLevel();
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   990
                        findPredecessor(k, comparator); // clean index
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   991
                        addCount(-1L);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   992
                        return new AbstractMap.SimpleImmutableEntry<K,V>(k, v);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   993
                    }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   994
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        }
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
   997
        return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
    /* ---------------- Relational operations -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
    // Control values OR'ed as arguments to findNear
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
    private static final int EQ = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
    private static final int LT = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
    private static final int GT = 0; // Actually checked as !LT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     * Utility for ceiling, floor, lower, higher methods.
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1010
     * @param key the key
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * @param rel the relation -- OR'ed combination of EQ, LT, GT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     * @return nearest node fitting relation, or null if no such
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     */
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1014
    final Node<K,V> findNear(K key, int rel, Comparator<? super K> cmp) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1015
        if (key == null)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1016
            throw new NullPointerException();
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1017
        Node<K,V> result;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1018
        outer: for (Node<K,V> b;;) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1019
            if ((b = findPredecessor(key, cmp)) == null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1020
                result = null;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1021
                break;                   // empty
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1022
            }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1023
            for (;;) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1024
                Node<K,V> n; K k; int c;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1025
                if ((n = b.next) == null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1026
                    result = ((rel & LT) != 0 && b.key != null) ? b : null;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1027
                    break outer;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1028
                }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1029
                else if ((k = n.key) == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                    break;
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1031
                else if (n.val == null)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1032
                    unlinkNode(b, n);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1033
                else if (((c = cpr(cmp, key, k)) == 0 && (rel & EQ) != 0) ||
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1034
                         (c < 0 && (rel & LT) == 0)) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1035
                    result = n;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1036
                    break outer;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                }
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1038
                else if (c <= 0 && (rel & LT) != 0) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1039
                    result = (b.key != null) ? b : null;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1040
                    break outer;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1041
                }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1042
                else
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1043
                    b = n;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        }
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1046
        return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
    /**
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1050
     * Variant of findNear returning SimpleImmutableEntry
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     * @param key the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * @param rel the relation -- OR'ed combination of EQ, LT, GT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * @return Entry fitting relation, or null if no such
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     */
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1055
    final AbstractMap.SimpleImmutableEntry<K,V> findNearEntry(K key, int rel,
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1056
                                                              Comparator<? super K> cmp) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        for (;;) {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1058
            Node<K,V> n; V v;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1059
            if ((n = findNear(key, rel, cmp)) == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
                return null;
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1061
            if ((v = n.val) != null)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1062
                return new AbstractMap.SimpleImmutableEntry<K,V>(n.key, v);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
    /* ---------------- Constructors -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     * Constructs a new, empty map, sorted according to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     * {@linkplain Comparable natural ordering} of the keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
    public ConcurrentSkipListMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        this.comparator = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     * Constructs a new, empty map, sorted according to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * comparator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     * @param comparator the comparator that will be used to order this map.
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1081
     *        If {@code null}, the {@linkplain Comparable natural
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     *        ordering} of the keys will be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
    public ConcurrentSkipListMap(Comparator<? super K> comparator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        this.comparator = comparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     * Constructs a new map containing the same mappings as the given map,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     * sorted according to the {@linkplain Comparable natural ordering} of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     * the keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     * @param  m the map whose mappings are to be placed in this map
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1094
     * @throws ClassCastException if the keys in {@code m} are not
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     *         {@link Comparable}, or are not mutually comparable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * @throws NullPointerException if the specified map or any of its keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     *         or values are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
    public ConcurrentSkipListMap(Map<? extends K, ? extends V> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        this.comparator = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        putAll(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     * Constructs a new map containing the same mappings and using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     * same ordering as the specified sorted map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     * @param m the sorted map whose mappings are to be placed in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     *        map, and whose comparator is to be used to sort this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     * @throws NullPointerException if the specified sorted map or any of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     *         its keys or values are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
    public ConcurrentSkipListMap(SortedMap<K, ? extends V> m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        this.comparator = m.comparator();
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1115
        buildFromSorted(m); // initializes transients
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
    /**
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1119
     * Returns a shallow copy of this {@code ConcurrentSkipListMap}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     * instance. (The keys and values themselves are not cloned.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     * @return a shallow copy of this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
    public ConcurrentSkipListMap<K,V> clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        try {
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9515
diff changeset
  1126
            @SuppressWarnings("unchecked")
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9515
diff changeset
  1127
            ConcurrentSkipListMap<K,V> clone =
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9515
diff changeset
  1128
                (ConcurrentSkipListMap<K,V>) super.clone();
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1129
            clone.keySet = null;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1130
            clone.entrySet = null;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1131
            clone.values = null;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1132
            clone.descendingMap = null;
54682
79d13fd7db9c 8222930: ConcurrentSkipListMap.clone() shares size variable between original and clone
dl
parents: 50764
diff changeset
  1133
            clone.adder = null;
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9515
diff changeset
  1134
            clone.buildFromSorted(this);
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9515
diff changeset
  1135
            return clone;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     * Streamlined bulk insertion to initialize from elements of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * given sorted map.  Call only from constructor or clone
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
    private void buildFromSorted(SortedMap<K, ? extends V> map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        if (map == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        Iterator<? extends Map.Entry<? extends K, ? extends V>> it =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
            map.entrySet().iterator();
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1151
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1152
        /*
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1153
         * Add equally spaced indices at log intervals, using the bits
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1154
         * of count during insertion. The maximum possible resulting
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1155
         * level is less than the number of bits in a long (64). The
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1156
         * preds array tracks the current rightmost node at each
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1157
         * level.
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1158
         */
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1159
        @SuppressWarnings("unchecked")
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1160
        Index<K,V>[] preds = (Index<K,V>[])new Index<?,?>[64];
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1161
        Node<K,V> bp = new Node<K,V>(null, null, null);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1162
        Index<K,V> h = preds[0] = new Index<K,V>(bp, null, null);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1163
        long count = 0;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1164
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        while (it.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
            Map.Entry<? extends K, ? extends V> e = it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
            K k = e.getKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
            V v = e.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
            if (k == null || v == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
            Node<K,V> z = new Node<K,V>(k, v, null);
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1172
            bp = bp.next = z;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1173
            if ((++count & 3L) == 0L) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1174
                long m = count >>> 2;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1175
                int i = 0;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1176
                Index<K,V> idx = null, q;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1177
                do {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
                    idx = new Index<K,V>(z, idx, null);
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1179
                    if ((q = preds[i]) == null)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1180
                        preds[i] = h = new Index<K,V>(h.node, h, idx);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1181
                    else
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1182
                        preds[i] = q.right = idx;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1183
                } while (++i < preds.length && ((m >>>= 1) & 1L) != 0L);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        }
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1186
        if (count != 0L) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1187
            VarHandle.releaseFence(); // emulate volatile stores
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1188
            addCount(count);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1189
            head = h;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1190
            VarHandle.fullFence();
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1191
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
    /* ---------------- Serialization -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
    /**
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
  1197
     * Saves this map to a stream (that is, serializes it).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     *
19378
0b98a290dd86 8022326: Spliterator for values of j.u.c.ConcurrentSkipListMap does not report ORDERED
psandoz
parents: 18767
diff changeset
  1199
     * @param s the stream
0b98a290dd86 8022326: Spliterator for values of j.u.c.ConcurrentSkipListMap does not report ORDERED
psandoz
parents: 18767
diff changeset
  1200
     * @throws java.io.IOException if an I/O error occurs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     * @serialData The key (Object) and value (Object) for each
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     * key-value mapping represented by the map, followed by
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1203
     * {@code null}. The key-value mappings are emitted in key-order
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     * (as determined by the Comparator, or by the keys' natural
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     * ordering if no Comparator).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
    private void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
        throws java.io.IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        // Write out the Comparator and any hidden stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
        // Write out keys and values (alternating)
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1213
        Node<K,V> b, n; V v;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1214
        if ((b = baseHead()) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1215
            while ((n = b.next) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1216
                if ((v = n.val) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1217
                    s.writeObject(n.key);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1218
                    s.writeObject(v);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1219
                }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1220
                b = n;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        s.writeObject(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
    /**
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 11279
diff changeset
  1227
     * Reconstitutes this map from a stream (that is, deserializes it).
19378
0b98a290dd86 8022326: Spliterator for values of j.u.c.ConcurrentSkipListMap does not report ORDERED
psandoz
parents: 18767
diff changeset
  1228
     * @param s the stream
0b98a290dd86 8022326: Spliterator for values of j.u.c.ConcurrentSkipListMap does not report ORDERED
psandoz
parents: 18767
diff changeset
  1229
     * @throws ClassNotFoundException if the class of a serialized object
0b98a290dd86 8022326: Spliterator for values of j.u.c.ConcurrentSkipListMap does not report ORDERED
psandoz
parents: 18767
diff changeset
  1230
     *         could not be found
0b98a290dd86 8022326: Spliterator for values of j.u.c.ConcurrentSkipListMap does not report ORDERED
psandoz
parents: 18767
diff changeset
  1231
     * @throws java.io.IOException if an I/O error occurs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     */
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1233
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
    private void readObject(final java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        throws java.io.IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        // Read in the Comparator and any hidden stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1239
        // Same idea as buildFromSorted
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1240
        @SuppressWarnings("unchecked")
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1241
        Index<K,V>[] preds = (Index<K,V>[])new Index<?,?>[64];
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1242
        Node<K,V> bp = new Node<K,V>(null, null, null);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1243
        Index<K,V> h = preds[0] = new Index<K,V>(bp, null, null);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1244
        Comparator<? super K> cmp = comparator;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1245
        K prevKey = null;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1246
        long count = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
        for (;;) {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1249
            K k = (K)s.readObject();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
            if (k == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
                break;
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1252
            V v = (V)s.readObject();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
            if (v == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
                throw new NullPointerException();
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1255
            if (prevKey != null && cpr(cmp, prevKey, k) > 0)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1256
                throw new IllegalStateException("out of order");
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1257
            prevKey = k;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1258
            Node<K,V> z = new Node<K,V>(k, v, null);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1259
            bp = bp.next = z;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1260
            if ((++count & 3L) == 0L) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1261
                long m = count >>> 2;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1262
                int i = 0;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1263
                Index<K,V> idx = null, q;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1264
                do {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
                    idx = new Index<K,V>(z, idx, null);
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1266
                    if ((q = preds[i]) == null)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1267
                        preds[i] = h = new Index<K,V>(h.node, h, idx);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1268
                    else
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1269
                        preds[i] = q.right = idx;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1270
                } while (++i < preds.length && ((m >>>= 1) & 1L) != 0L);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
        }
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1273
        if (count != 0L) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1274
            VarHandle.releaseFence();
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1275
            addCount(count);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1276
            head = h;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1277
            VarHandle.fullFence();
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1278
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
    /* ------ Map API methods ------ */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
    /**
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1284
     * Returns {@code true} if this map contains a mapping for the specified
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     * key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
     * @param key key whose presence in this map is to be tested
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1288
     * @return {@code true} if this map contains a mapping for the specified key
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
     * @throws ClassCastException if the specified key cannot be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
     *         with the keys currently in the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
     * @throws NullPointerException if the specified key is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
    public boolean containsKey(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
        return doGet(key) != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
     * Returns the value to which the specified key is mapped,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
     * or {@code null} if this map contains no mapping for the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
     * <p>More formally, if this map contains a mapping from a key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
     * {@code k} to a value {@code v} such that {@code key} compares
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
     * equal to {@code k} according to the map's ordering, then this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
     * method returns {@code v}; otherwise it returns {@code null}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
     * (There can be at most one such mapping.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
     * @throws ClassCastException if the specified key cannot be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
     *         with the keys currently in the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
     * @throws NullPointerException if the specified key is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
    public V get(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
        return doGet(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
    /**
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1316
     * Returns the value to which the specified key is mapped,
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1317
     * or the given defaultValue if this map contains no mapping for the key.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1318
     *
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1319
     * @param key the key
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1320
     * @param defaultValue the value to return if this map contains
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1321
     * no mapping for the given key
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1322
     * @return the mapping for the key, if present; else the defaultValue
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1323
     * @throws NullPointerException if the specified key is null
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1324
     * @since 1.8
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1325
     */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1326
    public V getOrDefault(Object key, V defaultValue) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1327
        V v;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1328
        return (v = doGet(key)) == null ? defaultValue : v;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1329
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1330
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1331
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
     * Associates the specified value with the specified key in this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
     * If the map previously contained a mapping for the key, the old
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
     * value is replaced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
     * @param key key with which the specified value is to be associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     * @param value value to be associated with the specified key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
     * @return the previous value associated with the specified key, or
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1339
     *         {@code null} if there was no mapping for the key
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
     * @throws ClassCastException if the specified key cannot be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
     *         with the keys currently in the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
     * @throws NullPointerException if the specified key or value is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
    public V put(K key, V value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
        if (value == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
        return doPut(key, value, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
     * Removes the mapping for the specified key from this map if present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
     * @param  key key for which mapping should be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
     * @return the previous value associated with the specified key, or
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1355
     *         {@code null} if there was no mapping for the key
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
     * @throws ClassCastException if the specified key cannot be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
     *         with the keys currently in the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
     * @throws NullPointerException if the specified key is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
    public V remove(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        return doRemove(key, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
    /**
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1365
     * Returns {@code true} if this map maps one or more keys to the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
     * specified value.  This operation requires time linear in the
9515
04056ec0f477 7038885: Improved bulk operation disclaimers for concurrent collections
dl
parents: 9242
diff changeset
  1367
     * map size. Additionally, it is possible for the map to change
04056ec0f477 7038885: Improved bulk operation disclaimers for concurrent collections
dl
parents: 9242
diff changeset
  1368
     * during execution of this method, in which case the returned
04056ec0f477 7038885: Improved bulk operation disclaimers for concurrent collections
dl
parents: 9242
diff changeset
  1369
     * result may be inaccurate.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
     * @param value value whose presence in this map is to be tested
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1372
     * @return {@code true} if a mapping to {@code value} exists;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1373
     *         {@code false} otherwise
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
     * @throws NullPointerException if the specified value is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
    public boolean containsValue(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
        if (value == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
            throw new NullPointerException();
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1379
        Node<K,V> b, n; V v;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1380
        if ((b = baseHead()) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1381
            while ((n = b.next) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1382
                if ((v = n.val) != null && value.equals(v))
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1383
                    return true;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1384
                else
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1385
                    b = n;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1386
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
    /**
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1392
     * {@inheritDoc}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
    public int size() {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1395
        long c;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1396
        return ((baseHead() == null) ? 0 :
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1397
                ((c = getAdderCount()) >= Integer.MAX_VALUE) ?
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1398
                Integer.MAX_VALUE : (int) c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
    /**
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1402
     * {@inheritDoc}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
        return findFirst() == null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
     * Removes all of the mappings from this map.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
    public void clear() {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1412
        Index<K,V> h, r, d; Node<K,V> b;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1413
        VarHandle.acquireFence();
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1414
        while ((h = head) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1415
            if ((r = h.right) != null)        // remove indices
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1416
                RIGHT.compareAndSet(h, r, null);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1417
            else if ((d = h.down) != null)    // remove levels
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1418
                HEAD.compareAndSet(this, h, d);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1419
            else {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1420
                long count = 0L;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1421
                if ((b = h.node) != null) {    // remove nodes
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1422
                    Node<K,V> n; V v;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1423
                    while ((n = b.next) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1424
                        if ((v = n.val) != null &&
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1425
                            VAL.compareAndSet(n, v, null)) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1426
                            --count;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1427
                            v = null;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1428
                        }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1429
                        if (v == null)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1430
                            unlinkNode(b, n);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1431
                    }
42320
bfc781c5078f 8166507: ConcurrentSkipListSet.clear() can leave the Set in an invalid state
dl
parents: 39725
diff changeset
  1432
                }
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1433
                if (count != 0L)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1434
                    addCount(count);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1435
                else
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1436
                    break;
42320
bfc781c5078f 8166507: ConcurrentSkipListSet.clear() can leave the Set in an invalid state
dl
parents: 39725
diff changeset
  1437
            }
bfc781c5078f 8166507: ConcurrentSkipListSet.clear() can leave the Set in an invalid state
dl
parents: 39725
diff changeset
  1438
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1441
    /**
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1442
     * If the specified key is not already associated with a value,
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1443
     * attempts to compute its value using the given mapping function
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1444
     * and enters it into this map unless {@code null}.  The function
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1445
     * is <em>NOT</em> guaranteed to be applied once atomically only
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1446
     * if the value is not present.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1447
     *
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1448
     * @param key key with which the specified value is to be associated
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1449
     * @param mappingFunction the function to compute a value
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1450
     * @return the current (existing or computed) value associated with
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1451
     *         the specified key, or null if the computed value is null
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1452
     * @throws NullPointerException if the specified key is null
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1453
     *         or the mappingFunction is null
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1454
     * @since 1.8
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1455
     */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1456
    public V computeIfAbsent(K key,
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1457
                             Function<? super K, ? extends V> mappingFunction) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1458
        if (key == null || mappingFunction == null)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1459
            throw new NullPointerException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1460
        V v, p, r;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1461
        if ((v = doGet(key)) == null &&
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1462
            (r = mappingFunction.apply(key)) != null)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1463
            v = (p = doPut(key, r, true)) == null ? r : p;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1464
        return v;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1465
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1466
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1467
    /**
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1468
     * If the value for the specified key is present, attempts to
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1469
     * compute a new mapping given the key and its current mapped
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1470
     * value. The function is <em>NOT</em> guaranteed to be applied
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1471
     * once atomically.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1472
     *
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1473
     * @param key key with which a value may be associated
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1474
     * @param remappingFunction the function to compute a value
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1475
     * @return the new value associated with the specified key, or null if none
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1476
     * @throws NullPointerException if the specified key is null
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1477
     *         or the remappingFunction is null
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1478
     * @since 1.8
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1479
     */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1480
    public V computeIfPresent(K key,
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1481
                              BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1482
        if (key == null || remappingFunction == null)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1483
            throw new NullPointerException();
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1484
        Node<K,V> n; V v;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1485
        while ((n = findNode(key)) != null) {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1486
            if ((v = n.val) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1487
                V r = remappingFunction.apply(key, v);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1488
                if (r != null) {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1489
                    if (VAL.compareAndSet(n, v, r))
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1490
                        return r;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1491
                }
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1492
                else if (doRemove(key, v) != null)
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1493
                    break;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1494
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1495
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1496
        return null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1497
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1498
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1499
    /**
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1500
     * Attempts to compute a mapping for the specified key and its
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1501
     * current mapped value (or {@code null} if there is no current
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1502
     * mapping). The function is <em>NOT</em> guaranteed to be applied
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1503
     * once atomically.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1504
     *
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1505
     * @param key key with which the specified value is to be associated
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1506
     * @param remappingFunction the function to compute a value
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1507
     * @return the new value associated with the specified key, or null if none
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1508
     * @throws NullPointerException if the specified key is null
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1509
     *         or the remappingFunction is null
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1510
     * @since 1.8
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1511
     */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1512
    public V compute(K key,
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1513
                     BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1514
        if (key == null || remappingFunction == null)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1515
            throw new NullPointerException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1516
        for (;;) {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1517
            Node<K,V> n; V v; V r;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1518
            if ((n = findNode(key)) == null) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1519
                if ((r = remappingFunction.apply(key, null)) == null)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1520
                    break;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1521
                if (doPut(key, r, true) == null)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1522
                    return r;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1523
            }
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1524
            else if ((v = n.val) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1525
                if ((r = remappingFunction.apply(key, v)) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1526
                    if (VAL.compareAndSet(n, v, r))
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1527
                        return r;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1528
                }
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1529
                else if (doRemove(key, v) != null)
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1530
                    break;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1531
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1532
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1533
        return null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1534
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1535
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1536
    /**
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1537
     * If the specified key is not already associated with a value,
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1538
     * associates it with the given value.  Otherwise, replaces the
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1539
     * value with the results of the given remapping function, or
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1540
     * removes if {@code null}. The function is <em>NOT</em>
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1541
     * guaranteed to be applied once atomically.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1542
     *
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1543
     * @param key key with which the specified value is to be associated
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1544
     * @param value the value to use if absent
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1545
     * @param remappingFunction the function to recompute a value if present
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1546
     * @return the new value associated with the specified key, or null if none
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1547
     * @throws NullPointerException if the specified key or value is null
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1548
     *         or the remappingFunction is null
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1549
     * @since 1.8
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1550
     */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1551
    public V merge(K key, V value,
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1552
                   BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1553
        if (key == null || value == null || remappingFunction == null)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1554
            throw new NullPointerException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1555
        for (;;) {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1556
            Node<K,V> n; V v; V r;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1557
            if ((n = findNode(key)) == null) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1558
                if (doPut(key, value, true) == null)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1559
                    return value;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1560
            }
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1561
            else if ((v = n.val) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1562
                if ((r = remappingFunction.apply(v, value)) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1563
                    if (VAL.compareAndSet(n, v, r))
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1564
                        return r;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1565
                }
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1566
                else if (doRemove(key, v) != null)
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1567
                    return null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1568
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1569
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1570
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1571
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
    /* ---------------- View methods -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
     * Note: Lazy initialization works for views because view classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
     * are stateless/immutable so it doesn't matter wrt correctness if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
     * more than one is created (which will only rarely happen).  Even
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
     * so, the following idiom conservatively ensures that the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
     * returns the one it created if it does so, not one created by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
     * another racing thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
     * Returns a {@link NavigableSet} view of the keys contained in this map.
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19378
diff changeset
  1585
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19378
diff changeset
  1586
     * <p>The set's iterator returns the keys in ascending order.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19378
diff changeset
  1587
     * The set's spliterator additionally reports {@link Spliterator#CONCURRENT},
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19378
diff changeset
  1588
     * {@link Spliterator#NONNULL}, {@link Spliterator#SORTED} and
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19378
diff changeset
  1589
     * {@link Spliterator#ORDERED}, with an encounter order that is ascending
45937
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 44926
diff changeset
  1590
     * key order.
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 44926
diff changeset
  1591
     *
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 44926
diff changeset
  1592
     * <p>The {@linkplain Spliterator#getComparator() spliterator's comparator}
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 44926
diff changeset
  1593
     * is {@code null} if the {@linkplain #comparator() map's comparator}
646816090183 8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents: 44926
diff changeset
  1594
     * is {@code null}.
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19378
diff changeset
  1595
     * Otherwise, the spliterator's comparator is the same as or imposes the
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19378
diff changeset
  1596
     * same total ordering as the map's comparator.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19378
diff changeset
  1597
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19378
diff changeset
  1598
     * <p>The set is backed by the map, so changes to the map are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
     * reflected in the set, and vice-versa.  The set supports element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
     * removal, which removes the corresponding mapping from the map,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
     * via the {@code Iterator.remove}, {@code Set.remove},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
     * {@code removeAll}, {@code retainAll}, and {@code clear}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
     * operations.  It does not support the {@code add} or {@code addAll}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
     * operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
     *
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19378
diff changeset
  1606
     * <p>The view's iterators and spliterators are
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19378
diff changeset
  1607
     * <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
     * <p>This method is equivalent to method {@code navigableKeySet}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
     * @return a navigable set view of the keys in this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
     */
9515
04056ec0f477 7038885: Improved bulk operation disclaimers for concurrent collections
dl
parents: 9242
diff changeset
  1613
    public NavigableSet<K> keySet() {
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 33674
diff changeset
  1614
        KeySet<K,V> ks;
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 33674
diff changeset
  1615
        if ((ks = keySet) != null) return ks;
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 33674
diff changeset
  1616
        return keySet = new KeySet<>(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
    public NavigableSet<K> navigableKeySet() {
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 33674
diff changeset
  1620
        KeySet<K,V> ks;
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 33674
diff changeset
  1621
        if ((ks = keySet) != null) return ks;
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 33674
diff changeset
  1622
        return keySet = new KeySet<>(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
     * Returns a {@link Collection} view of the values contained in this map.
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19378
diff changeset
  1627
     * <p>The collection's iterator returns the values in ascending order
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19378
diff changeset
  1628
     * of the corresponding keys. The collections's spliterator additionally
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19378
diff changeset
  1629
     * reports {@link Spliterator#CONCURRENT}, {@link Spliterator#NONNULL} and
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19378
diff changeset
  1630
     * {@link Spliterator#ORDERED}, with an encounter order that is ascending
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19378
diff changeset
  1631
     * order of the corresponding keys.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19378
diff changeset
  1632
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19378
diff changeset
  1633
     * <p>The collection is backed by the map, so changes to the map are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
     * reflected in the collection, and vice-versa.  The collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
     * supports element removal, which removes the corresponding
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1636
     * mapping from the map, via the {@code Iterator.remove},
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1637
     * {@code Collection.remove}, {@code removeAll},
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1638
     * {@code retainAll} and {@code clear} operations.  It does not
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1639
     * support the {@code add} or {@code addAll} operations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
     *
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19378
diff changeset
  1641
     * <p>The view's iterators and spliterators are
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19378
diff changeset
  1642
     * <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
    public Collection<V> values() {
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 33674
diff changeset
  1645
        Values<K,V> vs;
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 33674
diff changeset
  1646
        if ((vs = values) != null) return vs;
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 33674
diff changeset
  1647
        return values = new Values<>(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
     * Returns a {@link Set} view of the mappings contained in this map.
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19378
diff changeset
  1652
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19378
diff changeset
  1653
     * <p>The set's iterator returns the entries in ascending key order.  The
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19378
diff changeset
  1654
     * set's spliterator additionally reports {@link Spliterator#CONCURRENT},
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19378
diff changeset
  1655
     * {@link Spliterator#NONNULL}, {@link Spliterator#SORTED} and
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19378
diff changeset
  1656
     * {@link Spliterator#ORDERED}, with an encounter order that is ascending
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19378
diff changeset
  1657
     * key order.
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19378
diff changeset
  1658
     *
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19378
diff changeset
  1659
     * <p>The set is backed by the map, so changes to the map are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
     * reflected in the set, and vice-versa.  The set supports element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
     * removal, which removes the corresponding mapping from the map,
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1662
     * via the {@code Iterator.remove}, {@code Set.remove},
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1663
     * {@code removeAll}, {@code retainAll} and {@code clear}
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1664
     * operations.  It does not support the {@code add} or
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1665
     * {@code addAll} operations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
     *
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19378
diff changeset
  1667
     * <p>The view's iterators and spliterators are
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19378
diff changeset
  1668
     * <a href="package-summary.html#Weakly"><i>weakly consistent</i></a>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
     *
19428
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19378
diff changeset
  1670
     * <p>The {@code Map.Entry} elements traversed by the {@code iterator}
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19378
diff changeset
  1671
     * or {@code spliterator} do <em>not</em> support the {@code setValue}
83f87aff7b07 8022318: Document Spliterator characteristics and binding policy of java util concurrent collection impls
psandoz
parents: 19378
diff changeset
  1672
     * operation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
     * @return a set view of the mappings contained in this map,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
     *         sorted in ascending key order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
    public Set<Map.Entry<K,V>> entrySet() {
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 33674
diff changeset
  1678
        EntrySet<K,V> es;
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 33674
diff changeset
  1679
        if ((es = entrySet) != null) return es;
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 33674
diff changeset
  1680
        return entrySet = new EntrySet<K,V>(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
    public ConcurrentNavigableMap<K,V> descendingMap() {
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 33674
diff changeset
  1684
        ConcurrentNavigableMap<K,V> dm;
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 33674
diff changeset
  1685
        if ((dm = descendingMap) != null) return dm;
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 33674
diff changeset
  1686
        return descendingMap =
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 33674
diff changeset
  1687
            new SubMap<K,V>(this, null, false, null, false, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
    public NavigableSet<K> descendingKeySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
        return descendingMap().navigableKeySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
    /* ---------------- AbstractMap Overrides -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
     * Compares the specified object with this map for equality.
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1698
     * Returns {@code true} if the given object is also a map and the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
     * two maps represent the same mappings.  More formally, two maps
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1700
     * {@code m1} and {@code m2} represent the same mappings if
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1701
     * {@code m1.entrySet().equals(m2.entrySet())}.  This
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
     * operation may return misleading results if either map is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
     * concurrently modified during execution of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
     * @param o object to be compared for equality with this map
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1706
     * @return {@code true} if the specified object is equal to this map
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
    public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
        if (o == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
        if (!(o instanceof Map))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
        Map<?,?> m = (Map<?,?>) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
        try {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1715
            Comparator<? super K> cmp = comparator;
55032
11d9cd8d99f3 8224698: ConcurrentSkipListMap.java does not compile with the Eclipse Java Compiler
dl
parents: 54682
diff changeset
  1716
            // See JDK-8223553 for Iterator type wildcard rationale
11d9cd8d99f3 8224698: ConcurrentSkipListMap.java does not compile with the Eclipse Java Compiler
dl
parents: 54682
diff changeset
  1717
            Iterator<? extends Map.Entry<?,?>> it = m.entrySet().iterator();
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1718
            if (m instanceof SortedMap &&
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1719
                ((SortedMap<?,?>)m).comparator() == cmp) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1720
                Node<K,V> b, n;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1721
                if ((b = baseHead()) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1722
                    while ((n = b.next) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1723
                        K k; V v;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1724
                        if ((v = n.val) != null && (k = n.key) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1725
                            if (!it.hasNext())
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1726
                                return false;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1727
                            Map.Entry<?,?> e = it.next();
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1728
                            Object mk = e.getKey();
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1729
                            Object mv = e.getValue();
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1730
                            if (mk == null || mv == null)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1731
                                return false;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1732
                            try {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1733
                                if (cpr(cmp, k, mk) != 0)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1734
                                    return false;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1735
                            } catch (ClassCastException cce) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1736
                                return false;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1737
                            }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1738
                            if (!mv.equals(v))
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1739
                                return false;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1740
                        }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1741
                        b = n;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1742
                    }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1743
                }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1744
                return !it.hasNext();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
            }
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1746
            else {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1747
                while (it.hasNext()) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1748
                    V v;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1749
                    Map.Entry<?,?> e = it.next();
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1750
                    Object mk = e.getKey();
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1751
                    Object mv = e.getValue();
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1752
                    if (mk == null || mv == null ||
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1753
                        (v = get(mk)) == null || !v.equals(mv))
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1754
                        return false;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1755
                }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1756
                Node<K,V> b, n;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1757
                if ((b = baseHead()) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1758
                    K k; V v; Object mv;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1759
                    while ((n = b.next) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1760
                        if ((v = n.val) != null && (k = n.key) != null &&
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1761
                            ((mv = m.get(k)) == null || !mv.equals(v)))
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1762
                            return false;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1763
                        b = n;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1764
                    }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1765
                }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1766
                return true;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1767
            }
50764
5637aca18f1d 8203681: Miscellaneous changes imported from jsr166 CVS 2018-06
dl
parents: 49565
diff changeset
  1768
        } catch (ClassCastException | NullPointerException unused) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
    /* ------ ConcurrentMap API methods ------ */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
     * @return the previous value associated with the specified key,
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1779
     *         or {@code null} if there was no mapping for the key
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
     * @throws ClassCastException if the specified key cannot be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
     *         with the keys currently in the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
     * @throws NullPointerException if the specified key or value is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
    public V putIfAbsent(K key, V value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
        if (value == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
        return doPut(key, value, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
     * @throws ClassCastException if the specified key cannot be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
     *         with the keys currently in the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
     * @throws NullPointerException if the specified key is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
    public boolean remove(Object key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
        if (key == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
            throw new NullPointerException();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1800
        return value != null && doRemove(key, value) != null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
     * @throws ClassCastException if the specified key cannot be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
     *         with the keys currently in the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
     * @throws NullPointerException if any of the arguments are null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
    public boolean replace(K key, V oldValue, V newValue) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1811
        if (key == null || oldValue == null || newValue == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
        for (;;) {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1814
            Node<K,V> n; V v;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1815
            if ((n = findNode(key)) == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
                return false;
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1817
            if ((v = n.val) != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
                if (!oldValue.equals(v))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
                    return false;
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1820
                if (VAL.compareAndSet(n, v, newValue))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
     * @return the previous value associated with the specified key,
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1830
     *         or {@code null} if there was no mapping for the key
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
     * @throws ClassCastException if the specified key cannot be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
     *         with the keys currently in the map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
     * @throws NullPointerException if the specified key or value is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
    public V replace(K key, V value) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1836
        if (key == null || value == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
        for (;;) {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1839
            Node<K,V> n; V v;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1840
            if ((n = findNode(key)) == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
                return null;
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1842
            if ((v = n.val) != null && VAL.compareAndSet(n, v, value))
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1843
                return v;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
    /* ------ SortedMap API methods ------ */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
    public Comparator<? super K> comparator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
        return comparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
     * @throws NoSuchElementException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
    public K firstKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
        Node<K,V> n = findFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
        if (n == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
            throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
        return n.key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
     * @throws NoSuchElementException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
    public K lastKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
        Node<K,V> n = findLast();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
        if (n == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
            throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
        return n.key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
     * @throws NullPointerException if {@code fromKey} or {@code toKey} is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
    public ConcurrentNavigableMap<K,V> subMap(K fromKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
                                              boolean fromInclusive,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
                                              K toKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
                                              boolean toInclusive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
        if (fromKey == null || toKey == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
        return new SubMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
            (this, fromKey, fromInclusive, toKey, toInclusive, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
     * @throws NullPointerException if {@code toKey} is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
    public ConcurrentNavigableMap<K,V> headMap(K toKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
                                               boolean inclusive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
        if (toKey == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
        return new SubMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
            (this, null, false, toKey, inclusive, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
     * @throws NullPointerException if {@code fromKey} is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
    public ConcurrentNavigableMap<K,V> tailMap(K fromKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
                                               boolean inclusive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
        if (fromKey == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
        return new SubMap<K,V>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
            (this, fromKey, inclusive, null, false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
     * @throws NullPointerException if {@code fromKey} or {@code toKey} is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
    public ConcurrentNavigableMap<K,V> subMap(K fromKey, K toKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
        return subMap(fromKey, true, toKey, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
     * @throws NullPointerException if {@code toKey} is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
    public ConcurrentNavigableMap<K,V> headMap(K toKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
        return headMap(toKey, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
     * @throws NullPointerException if {@code fromKey} is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
    public ConcurrentNavigableMap<K,V> tailMap(K fromKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
        return tailMap(fromKey, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
    /* ---------------- Relational operations -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
     * Returns a key-value mapping associated with the greatest key
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1945
     * strictly less than the given key, or {@code null} if there is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
     * no such key. The returned entry does <em>not</em> support the
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1947
     * {@code Entry.setValue} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
     * @throws NullPointerException if the specified key is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
    public Map.Entry<K,V> lowerEntry(K key) {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1953
        return findNearEntry(key, LT, comparator);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
     * @throws NullPointerException if the specified key is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
    public K lowerKey(K key) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1961
        Node<K,V> n = findNear(key, LT, comparator);
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
  1962
        return (n == null) ? null : n.key;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
     * Returns a key-value mapping associated with the greatest key
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1967
     * less than or equal to the given key, or {@code null} if there
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
     * is no such key. The returned entry does <em>not</em> support
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1969
     * the {@code Entry.setValue} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
     * @param key the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
     * @throws NullPointerException if the specified key is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
    public Map.Entry<K,V> floorEntry(K key) {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1976
        return findNearEntry(key, LT|EQ, comparator);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
     * @param key the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
     * @throws NullPointerException if the specified key is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
    public K floorKey(K key) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1985
        Node<K,V> n = findNear(key, LT|EQ, comparator);
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
  1986
        return (n == null) ? null : n.key;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
     * Returns a key-value mapping associated with the least key
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1991
     * greater than or equal to the given key, or {@code null} if
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
     * there is no such entry. The returned entry does <em>not</em>
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  1993
     * support the {@code Entry.setValue} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
     * @throws NullPointerException if the specified key is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
    public Map.Entry<K,V> ceilingEntry(K key) {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  1999
        return findNearEntry(key, GT|EQ, comparator);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
     * @throws NullPointerException if the specified key is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
    public K ceilingKey(K key) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2007
        Node<K,V> n = findNear(key, GT|EQ, comparator);
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
  2008
        return (n == null) ? null : n.key;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
     * Returns a key-value mapping associated with the least key
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2013
     * strictly greater than the given key, or {@code null} if there
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
     * is no such key. The returned entry does <em>not</em> support
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2015
     * the {@code Entry.setValue} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
     * @param key the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
     * @throws NullPointerException if the specified key is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
    public Map.Entry<K,V> higherEntry(K key) {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2022
        return findNearEntry(key, GT, comparator);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
     * @param key the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
     * @throws ClassCastException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
     * @throws NullPointerException if the specified key is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
    public K higherKey(K key) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2031
        Node<K,V> n = findNear(key, GT, comparator);
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
  2032
        return (n == null) ? null : n.key;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
     * Returns a key-value mapping associated with the least
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2037
     * key in this map, or {@code null} if the map is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
     * The returned entry does <em>not</em> support
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2039
     * the {@code Entry.setValue} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
    public Map.Entry<K,V> firstEntry() {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2042
        return findFirstEntry();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
     * Returns a key-value mapping associated with the greatest
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2047
     * key in this map, or {@code null} if the map is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
     * The returned entry does <em>not</em> support
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2049
     * the {@code Entry.setValue} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
    public Map.Entry<K,V> lastEntry() {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2052
        return findLastEntry();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
     * Removes and returns a key-value mapping associated with
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2057
     * the least key in this map, or {@code null} if the map is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
     * The returned entry does <em>not</em> support
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2059
     * the {@code Entry.setValue} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
    public Map.Entry<K,V> pollFirstEntry() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
        return doRemoveFirstEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
     * Removes and returns a key-value mapping associated with
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2067
     * the greatest key in this map, or {@code null} if the map is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
     * The returned entry does <em>not</em> support
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2069
     * the {@code Entry.setValue} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
    public Map.Entry<K,V> pollLastEntry() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
        return doRemoveLastEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
    /* ---------------- Iterators -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
    /**
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2078
     * Base of iterator classes
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
    abstract class Iter<T> implements Iterator<T> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
        /** the last node returned by next() */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
        Node<K,V> lastReturned;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
        /** the next node to return from next(); */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
        Node<K,V> next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
        /** Cache of next value field to maintain weak consistency */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
        V nextValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
        /** Initializes ascending iterator for entire range. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
        Iter() {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2090
            advance(baseHead());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
        public final boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
            return next != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
        /** Advances next to higher entry. */
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2098
        final void advance(Node<K,V> b) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2099
            Node<K,V> n = null;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2100
            V v = null;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2101
            if ((lastReturned = b) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2102
                while ((n = b.next) != null && (v = n.val) == null)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2103
                    b = n;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
            }
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2105
            nextValue = v;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2106
            next = n;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2109
        public final void remove() {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2110
            Node<K,V> n; K k;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2111
            if ((n = lastReturned) == null || (k = n.key) == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
                throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
            // It would not be worth all of the overhead to directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
            // unlink from here. Using remove is fast enough.
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2115
            ConcurrentSkipListMap.this.remove(k);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
            lastReturned = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
    final class ValueIterator extends Iter<V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
        public V next() {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2122
            V v;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2123
            if ((v = nextValue) == null)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2124
                throw new NoSuchElementException();
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2125
            advance(next);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
            return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
    final class KeyIterator extends Iter<K> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
        public K next() {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2132
            Node<K,V> n;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2133
            if ((n = next) == null)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2134
                throw new NoSuchElementException();
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2135
            K k = n.key;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2136
            advance(n);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2137
            return k;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
    final class EntryIterator extends Iter<Map.Entry<K,V>> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
        public Map.Entry<K,V> next() {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2143
            Node<K,V> n;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2144
            if ((n = next) == null)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2145
                throw new NoSuchElementException();
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2146
            K k = n.key;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
            V v = nextValue;
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2148
            advance(n);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2149
            return new AbstractMap.SimpleImmutableEntry<K,V>(k, v);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
    /* ---------------- View Classes -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
     * View classes are static, delegating to a ConcurrentNavigableMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
     * to allow use by SubMaps, which outweighs the ugliness of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
     * needing type-tests for Iterator methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
    static final <E> List<E> toList(Collection<E> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
        // Using size() here would be a pessimization.
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2163
        ArrayList<E> list = new ArrayList<E>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
        for (E e : c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
            list.add(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
        return list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2169
    static final class KeySet<K,V>
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2170
            extends AbstractSet<K> implements NavigableSet<K> {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2171
        final ConcurrentNavigableMap<K,V> m;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2172
        KeySet(ConcurrentNavigableMap<K,V> map) { m = map; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
        public int size() { return m.size(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
        public boolean isEmpty() { return m.isEmpty(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
        public boolean contains(Object o) { return m.containsKey(o); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
        public boolean remove(Object o) { return m.remove(o) != null; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
        public void clear() { m.clear(); }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2178
        public K lower(K e) { return m.lowerKey(e); }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2179
        public K floor(K e) { return m.floorKey(e); }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2180
        public K ceiling(K e) { return m.ceilingKey(e); }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2181
        public K higher(K e) { return m.higherKey(e); }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2182
        public Comparator<? super K> comparator() { return m.comparator(); }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2183
        public K first() { return m.firstKey(); }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2184
        public K last() { return m.lastKey(); }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2185
        public K pollFirst() {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2186
            Map.Entry<K,V> e = m.pollFirstEntry();
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
  2187
            return (e == null) ? null : e.getKey();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
        }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2189
        public K pollLast() {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2190
            Map.Entry<K,V> e = m.pollLastEntry();
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
  2191
            return (e == null) ? null : e.getKey();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
        }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2193
        public Iterator<K> iterator() {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2194
            return (m instanceof ConcurrentSkipListMap)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2195
                ? ((ConcurrentSkipListMap<K,V>)m).new KeyIterator()
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2196
                : ((SubMap<K,V>)m).new SubMapKeyIterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
            if (o == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
            if (!(o instanceof Set))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
            Collection<?> c = (Collection<?>) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
                return containsAll(c) && c.containsAll(this);
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47305
diff changeset
  2206
            } catch (ClassCastException | NullPointerException unused) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
        public Object[] toArray()     { return toList(this).toArray();  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
        public <T> T[] toArray(T[] a) { return toList(this).toArray(a); }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2212
        public Iterator<K> descendingIterator() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
            return descendingSet().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
        }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2215
        public NavigableSet<K> subSet(K fromElement,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
                                      boolean fromInclusive,
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2217
                                      K toElement,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
                                      boolean toInclusive) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2219
            return new KeySet<>(m.subMap(fromElement, fromInclusive,
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2220
                                         toElement,   toInclusive));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
        }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2222
        public NavigableSet<K> headSet(K toElement, boolean inclusive) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2223
            return new KeySet<>(m.headMap(toElement, inclusive));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
        }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2225
        public NavigableSet<K> tailSet(K fromElement, boolean inclusive) {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2226
            return new KeySet<>(m.tailMap(fromElement, inclusive));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
        }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2228
        public NavigableSet<K> subSet(K fromElement, K toElement) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
            return subSet(fromElement, true, toElement, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
        }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2231
        public NavigableSet<K> headSet(K toElement) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
            return headSet(toElement, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
        }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2234
        public NavigableSet<K> tailSet(K fromElement) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
            return tailSet(fromElement, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
        }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2237
        public NavigableSet<K> descendingSet() {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2238
            return new KeySet<>(m.descendingMap());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
        }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2240
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2241
        public Spliterator<K> spliterator() {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2242
            return (m instanceof ConcurrentSkipListMap)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2243
                ? ((ConcurrentSkipListMap<K,V>)m).keySpliterator()
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2244
                : ((SubMap<K,V>)m).new SubMapKeyIterator();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2245
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2248
    static final class Values<K,V> extends AbstractCollection<V> {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2249
        final ConcurrentNavigableMap<K,V> m;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2250
        Values(ConcurrentNavigableMap<K,V> map) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
            m = map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
        }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2253
        public Iterator<V> iterator() {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2254
            return (m instanceof ConcurrentSkipListMap)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2255
                ? ((ConcurrentSkipListMap<K,V>)m).new ValueIterator()
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2256
                : ((SubMap<K,V>)m).new SubMapValueIterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
        }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2258
        public int size() { return m.size(); }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2259
        public boolean isEmpty() { return m.isEmpty(); }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2260
        public boolean contains(Object o) { return m.containsValue(o); }
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2261
        public void clear() { m.clear(); }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
        public Object[] toArray()     { return toList(this).toArray();  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
        public <T> T[] toArray(T[] a) { return toList(this).toArray(a); }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2264
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2265
        public Spliterator<V> spliterator() {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2266
            return (m instanceof ConcurrentSkipListMap)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2267
                ? ((ConcurrentSkipListMap<K,V>)m).valueSpliterator()
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2268
                : ((SubMap<K,V>)m).new SubMapValueIterator();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2269
        }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2270
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2271
        public boolean removeIf(Predicate<? super V> filter) {
30441
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  2272
            if (filter == null) throw new NullPointerException();
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  2273
            if (m instanceof ConcurrentSkipListMap)
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2274
                return ((ConcurrentSkipListMap<K,V>)m).removeValueIf(filter);
30441
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  2275
            // else use iterator
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2276
            Iterator<Map.Entry<K,V>> it =
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2277
                ((SubMap<K,V>)m).new SubMapEntryIterator();
30441
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  2278
            boolean removed = false;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  2279
            while (it.hasNext()) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2280
                Map.Entry<K,V> e = it.next();
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2281
                V v = e.getValue();
30441
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  2282
                if (filter.test(v) && m.remove(e.getKey(), v))
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  2283
                    removed = true;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  2284
            }
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  2285
            return removed;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  2286
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2289
    static final class EntrySet<K,V> extends AbstractSet<Map.Entry<K,V>> {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2290
        final ConcurrentNavigableMap<K,V> m;
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2291
        EntrySet(ConcurrentNavigableMap<K,V> map) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
            m = map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
        }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2294
        public Iterator<Map.Entry<K,V>> iterator() {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2295
            return (m instanceof ConcurrentSkipListMap)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2296
                ? ((ConcurrentSkipListMap<K,V>)m).new EntryIterator()
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2297
                : ((SubMap<K,V>)m).new SubMapEntryIterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
        public boolean contains(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
            if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
                return false;
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9515
diff changeset
  2303
            Map.Entry<?,?> e = (Map.Entry<?,?>)o;
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2304
            V v = m.get(e.getKey());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
            return v != null && v.equals(e.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
        public boolean remove(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
            if (!(o instanceof Map.Entry))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
                return false;
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9515
diff changeset
  2310
            Map.Entry<?,?> e = (Map.Entry<?,?>)o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
            return m.remove(e.getKey(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
                            e.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
        public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
            return m.isEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
        public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
            return m.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
            m.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
        public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
            if (o == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
            if (!(o instanceof Set))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
            Collection<?> c = (Collection<?>) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
                return containsAll(c) && c.containsAll(this);
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47305
diff changeset
  2331
            } catch (ClassCastException | NullPointerException unused) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
        public Object[] toArray()     { return toList(this).toArray();  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
        public <T> T[] toArray(T[] a) { return toList(this).toArray(a); }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2337
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2338
        public Spliterator<Map.Entry<K,V>> spliterator() {
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2339
            return (m instanceof ConcurrentSkipListMap)
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2340
                ? ((ConcurrentSkipListMap<K,V>)m).entrySpliterator()
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2341
                : ((SubMap<K,V>)m).new SubMapEntryIterator();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2342
        }
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2343
        public boolean removeIf(Predicate<? super Entry<K,V>> filter) {
30441
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  2344
            if (filter == null) throw new NullPointerException();
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  2345
            if (m instanceof ConcurrentSkipListMap)
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2346
                return ((ConcurrentSkipListMap<K,V>)m).removeEntryIf(filter);
30441
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  2347
            // else use iterator
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2348
            Iterator<Map.Entry<K,V>> it =
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2349
                ((SubMap<K,V>)m).new SubMapEntryIterator();
30441
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  2350
            boolean removed = false;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  2351
            while (it.hasNext()) {
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2352
                Map.Entry<K,V> e = it.next();
30441
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  2353
                if (filter.test(e) && m.remove(e.getKey(), e.getValue()))
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  2354
                    removed = true;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  2355
            }
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  2356
            return removed;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  2357
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
     * Submaps returned by {@link ConcurrentSkipListMap} submap operations
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2362
     * represent a subrange of mappings of their underlying maps.
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2363
     * Instances of this class support all methods of their underlying
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2364
     * maps, differing in that mappings outside their range are ignored,
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2365
     * and attempts to add mappings outside their ranges result in {@link
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2366
     * IllegalArgumentException}.  Instances of this class are constructed
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2367
     * only using the {@code subMap}, {@code headMap}, and {@code tailMap}
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2368
     * methods of their underlying maps.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
     * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
    static final class SubMap<K,V> extends AbstractMap<K,V>
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 33674
diff changeset
  2373
        implements ConcurrentNavigableMap<K,V>, Serializable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
        private static final long serialVersionUID = -7647078645895051609L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
        /** Underlying map */
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2377
        final ConcurrentSkipListMap<K,V> m;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
        /** lower bound key, or null if from start */
58657
6252605fb005 8232230: Suppress warnings on non-serializable non-transient instance fields in java.util.concurrent
darcy
parents: 55032
diff changeset
  2379
        @SuppressWarnings("serial") // Conditionally serializable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
        private final K lo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
        /** upper bound key, or null if to end */
58657
6252605fb005 8232230: Suppress warnings on non-serializable non-transient instance fields in java.util.concurrent
darcy
parents: 55032
diff changeset
  2382
        @SuppressWarnings("serial") // Conditionally serializable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
        private final K hi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
        /** inclusion flag for lo */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
        private final boolean loInclusive;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
        /** inclusion flag for hi */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
        private final boolean hiInclusive;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
        /** direction */
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2389
        final boolean isDescending;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
        // Lazily initialized view holders
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2392
        private transient KeySet<K,V> keySetView;
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 33674
diff changeset
  2393
        private transient Values<K,V> valuesView;
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 33674
diff changeset
  2394
        private transient EntrySet<K,V> entrySetView;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
        /**
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2397
         * Creates a new submap, initializing all fields.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
        SubMap(ConcurrentSkipListMap<K,V> map,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
               K fromKey, boolean fromInclusive,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
               K toKey, boolean toInclusive,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
               boolean isDescending) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2403
            Comparator<? super K> cmp = map.comparator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
            if (fromKey != null && toKey != null &&
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2405
                cpr(cmp, fromKey, toKey) > 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
                throw new IllegalArgumentException("inconsistent range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
            this.m = map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
            this.lo = fromKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
            this.hi = toKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
            this.loInclusive = fromInclusive;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
            this.hiInclusive = toInclusive;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
            this.isDescending = isDescending;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
        /* ----------------  Utilities -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2417
        boolean tooLow(Object key, Comparator<? super K> cmp) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2418
            int c;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2419
            return (lo != null && ((c = cpr(cmp, key, lo)) < 0 ||
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2420
                                   (c == 0 && !loInclusive)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2423
        boolean tooHigh(Object key, Comparator<? super K> cmp) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2424
            int c;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2425
            return (hi != null && ((c = cpr(cmp, key, hi)) > 0 ||
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2426
                                   (c == 0 && !hiInclusive)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2429
        boolean inBounds(Object key, Comparator<? super K> cmp) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2430
            return !tooLow(key, cmp) && !tooHigh(key, cmp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2433
        void checkKeyBounds(K key, Comparator<? super K> cmp) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
            if (key == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
                throw new NullPointerException();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2436
            if (!inBounds(key, cmp))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
                throw new IllegalArgumentException("key out of range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
        /**
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2441
         * Returns true if node key is less than upper bound of range.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
         */
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2443
        boolean isBeforeEnd(ConcurrentSkipListMap.Node<K,V> n,
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2444
                            Comparator<? super K> cmp) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
            if (n == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
            if (hi == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
            K k = n.key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
            if (k == null) // pass by markers and headers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
                return true;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2452
            int c = cpr(cmp, k, hi);
48541
946e34c2dec9 8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents: 47305
diff changeset
  2453
            return c < 0 || (c == 0 && hiInclusive);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
         * Returns lowest node. This node might not be in range, so
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2458
         * most usages need to check bounds.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
         */
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2460
        ConcurrentSkipListMap.Node<K,V> loNode(Comparator<? super K> cmp) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
            if (lo == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
                return m.findFirst();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
            else if (loInclusive)
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2464
                return m.findNear(lo, GT|EQ, cmp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
            else
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2466
                return m.findNear(lo, GT, cmp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
         * Returns highest node. This node might not be in range, so
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2471
         * most usages need to check bounds.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
         */
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2473
        ConcurrentSkipListMap.Node<K,V> hiNode(Comparator<? super K> cmp) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
            if (hi == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
                return m.findLast();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
            else if (hiInclusive)
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2477
                return m.findNear(hi, LT|EQ, cmp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
            else
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2479
                return m.findNear(hi, LT, cmp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
        /**
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 30441
diff changeset
  2483
         * Returns lowest absolute key (ignoring directionality).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
         */
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2485
        K lowestKey() {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2486
            Comparator<? super K> cmp = m.comparator;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2487
            ConcurrentSkipListMap.Node<K,V> n = loNode(cmp);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2488
            if (isBeforeEnd(n, cmp))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
                return n.key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
        /**
30655
d83f50188ca9 8080422: some docs cleanup for core libs
avstepan
parents: 30441
diff changeset
  2495
         * Returns highest absolute key (ignoring directionality).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
         */
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2497
        K highestKey() {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2498
            Comparator<? super K> cmp = m.comparator;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2499
            ConcurrentSkipListMap.Node<K,V> n = hiNode(cmp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
            if (n != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
                K last = n.key;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2502
                if (inBounds(last, cmp))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
                    return last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
            throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2508
        Map.Entry<K,V> lowestEntry() {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2509
            Comparator<? super K> cmp = m.comparator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
            for (;;) {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2511
                ConcurrentSkipListMap.Node<K,V> n; V v;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2512
                if ((n = loNode(cmp)) == null || !isBeforeEnd(n, cmp))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
                    return null;
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2514
                else if ((v = n.val) != null)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2515
                    return new AbstractMap.SimpleImmutableEntry<K,V>(n.key, v);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2519
        Map.Entry<K,V> highestEntry() {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2520
            Comparator<? super K> cmp = m.comparator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
            for (;;) {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2522
                ConcurrentSkipListMap.Node<K,V> n; V v;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2523
                if ((n = hiNode(cmp)) == null || !inBounds(n.key, cmp))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
                    return null;
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2525
                else if ((v = n.val) != null)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2526
                    return new AbstractMap.SimpleImmutableEntry<K,V>(n.key, v);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2530
        Map.Entry<K,V> removeLowest() {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2531
            Comparator<? super K> cmp = m.comparator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
            for (;;) {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2533
                ConcurrentSkipListMap.Node<K,V> n; K k; V v;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2534
                if ((n = loNode(cmp)) == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
                    return null;
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2536
                else if (!inBounds((k = n.key), cmp))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
                    return null;
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2538
                else if ((v = m.doRemove(k, null)) != null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
                    return new AbstractMap.SimpleImmutableEntry<K,V>(k, v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2543
        Map.Entry<K,V> removeHighest() {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2544
            Comparator<? super K> cmp = m.comparator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
            for (;;) {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2546
                ConcurrentSkipListMap.Node<K,V> n; K k; V v;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2547
                if ((n = hiNode(cmp)) == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
                    return null;
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2549
                else if (!inBounds((k = n.key), cmp))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
                    return null;
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2551
                else if ((v = m.doRemove(k, null)) != null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
                    return new AbstractMap.SimpleImmutableEntry<K,V>(k, v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
        /**
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2557
         * Submap version of ConcurrentSkipListMap.findNearEntry.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
         */
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2559
        Map.Entry<K,V> getNearEntry(K key, int rel) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2560
            Comparator<? super K> cmp = m.comparator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
            if (isDescending) { // adjust relation for direction
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9515
diff changeset
  2562
                if ((rel & LT) == 0)
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9515
diff changeset
  2563
                    rel |= LT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
                else
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9515
diff changeset
  2565
                    rel &= ~LT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
            }
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2567
            if (tooLow(key, cmp))
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9515
diff changeset
  2568
                return ((rel & LT) != 0) ? null : lowestEntry();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2569
            if (tooHigh(key, cmp))
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9515
diff changeset
  2570
                return ((rel & LT) != 0) ? highestEntry() : null;
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2571
            AbstractMap.SimpleImmutableEntry<K,V> e =
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2572
                m.findNearEntry(key, rel, cmp);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2573
            if (e == null || !inBounds(e.getKey(), cmp))
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2574
                return null;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2575
            else
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2576
                return e;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
        // Almost the same as getNearEntry, except for keys
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2580
        K getNearKey(K key, int rel) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2581
            Comparator<? super K> cmp = m.comparator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
            if (isDescending) { // adjust relation for direction
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9515
diff changeset
  2583
                if ((rel & LT) == 0)
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9515
diff changeset
  2584
                    rel |= LT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
                else
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9515
diff changeset
  2586
                    rel &= ~LT;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
            }
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2588
            if (tooLow(key, cmp)) {
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9515
diff changeset
  2589
                if ((rel & LT) == 0) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2590
                    ConcurrentSkipListMap.Node<K,V> n = loNode(cmp);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2591
                    if (isBeforeEnd(n, cmp))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
                        return n.key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
            }
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2596
            if (tooHigh(key, cmp)) {
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9515
diff changeset
  2597
                if ((rel & LT) != 0) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2598
                    ConcurrentSkipListMap.Node<K,V> n = hiNode(cmp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
                    if (n != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
                        K last = n.key;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2601
                        if (inBounds(last, cmp))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
                            return last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
            for (;;) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2608
                Node<K,V> n = m.findNear(key, rel, cmp);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2609
                if (n == null || !inBounds(n.key, cmp))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
                    return null;
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2611
                if (n.val != null)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2612
                    return n.key;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
        /* ----------------  Map API methods -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
        public boolean containsKey(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
            if (key == null) throw new NullPointerException();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2620
            return inBounds(key, m.comparator) && m.containsKey(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
        public V get(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
            if (key == null) throw new NullPointerException();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2625
            return (!inBounds(key, m.comparator)) ? null : m.get(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
        public V put(K key, V value) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2629
            checkKeyBounds(key, m.comparator);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
            return m.put(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
        public V remove(Object key) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2634
            return (!inBounds(key, m.comparator)) ? null : m.remove(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
        public int size() {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2638
            Comparator<? super K> cmp = m.comparator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
            long count = 0;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2640
            for (ConcurrentSkipListMap.Node<K,V> n = loNode(cmp);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2641
                 isBeforeEnd(n, cmp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
                 n = n.next) {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2643
                if (n.val != null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
                    ++count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
            }
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
  2646
            return count >= Integer.MAX_VALUE ? Integer.MAX_VALUE : (int)count;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
        public boolean isEmpty() {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2650
            Comparator<? super K> cmp = m.comparator;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2651
            return !isBeforeEnd(loNode(cmp), cmp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
        public boolean containsValue(Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
            if (value == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
                throw new NullPointerException();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2657
            Comparator<? super K> cmp = m.comparator;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2658
            for (ConcurrentSkipListMap.Node<K,V> n = loNode(cmp);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2659
                 isBeforeEnd(n, cmp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
                 n = n.next) {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2661
                V v = n.val;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
                if (v != null && value.equals(v))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
        public void clear() {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2669
            Comparator<? super K> cmp = m.comparator;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2670
            for (ConcurrentSkipListMap.Node<K,V> n = loNode(cmp);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2671
                 isBeforeEnd(n, cmp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2672
                 n = n.next) {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2673
                if (n.val != null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
                    m.remove(n.key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2676
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2677
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
        /* ----------------  ConcurrentMap API methods -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2680
        public V putIfAbsent(K key, V value) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2681
            checkKeyBounds(key, m.comparator);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2682
            return m.putIfAbsent(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2684
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2685
        public boolean remove(Object key, Object value) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2686
            return inBounds(key, m.comparator) && m.remove(key, value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2687
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2688
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2689
        public boolean replace(K key, V oldValue, V newValue) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2690
            checkKeyBounds(key, m.comparator);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2691
            return m.replace(key, oldValue, newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2692
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2693
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2694
        public V replace(K key, V value) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2695
            checkKeyBounds(key, m.comparator);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2696
            return m.replace(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2697
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2698
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2699
        /* ----------------  SortedMap API methods -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2700
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2701
        public Comparator<? super K> comparator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
            Comparator<? super K> cmp = m.comparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2703
            if (isDescending)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2704
                return Collections.reverseOrder(cmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
                return cmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2710
         * Utility to create submaps, where given bounds override
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2711
         * unbounded(null) ones and/or are checked against bounded ones.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2712
         */
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2713
        SubMap<K,V> newSubMap(K fromKey, boolean fromInclusive,
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2714
                              K toKey, boolean toInclusive) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2715
            Comparator<? super K> cmp = m.comparator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2716
            if (isDescending) { // flip senses
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
                K tk = fromKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
                fromKey = toKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
                toKey = tk;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
                boolean ti = fromInclusive;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
                fromInclusive = toInclusive;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
                toInclusive = ti;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
            if (lo != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
                if (fromKey == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
                    fromKey = lo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
                    fromInclusive = loInclusive;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
                else {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2730
                    int c = cpr(cmp, fromKey, lo);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
                    if (c < 0 || (c == 0 && !loInclusive && fromInclusive))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
                        throw new IllegalArgumentException("key out of range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
            if (hi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2736
                if (toKey == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2737
                    toKey = hi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
                    toInclusive = hiInclusive;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2740
                else {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2741
                    int c = cpr(cmp, toKey, hi);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2742
                    if (c > 0 || (c == 0 && !hiInclusive && toInclusive))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2743
                        throw new IllegalArgumentException("key out of range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2744
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2745
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2746
            return new SubMap<K,V>(m, fromKey, fromInclusive,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2747
                                   toKey, toInclusive, isDescending);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2748
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2749
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2750
        public SubMap<K,V> subMap(K fromKey, boolean fromInclusive,
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2751
                                  K toKey, boolean toInclusive) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
            if (fromKey == null || toKey == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2753
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2754
            return newSubMap(fromKey, fromInclusive, toKey, toInclusive);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2756
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2757
        public SubMap<K,V> headMap(K toKey, boolean inclusive) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2758
            if (toKey == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2759
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2760
            return newSubMap(null, false, toKey, inclusive);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2761
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2762
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2763
        public SubMap<K,V> tailMap(K fromKey, boolean inclusive) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2764
            if (fromKey == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2765
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2766
            return newSubMap(fromKey, inclusive, null, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2767
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2768
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2769
        public SubMap<K,V> subMap(K fromKey, K toKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2770
            return subMap(fromKey, true, toKey, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2771
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2772
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2773
        public SubMap<K,V> headMap(K toKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2774
            return headMap(toKey, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2775
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2776
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2777
        public SubMap<K,V> tailMap(K fromKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2778
            return tailMap(fromKey, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
        public SubMap<K,V> descendingMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
            return new SubMap<K,V>(m, lo, loInclusive,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
                                   hi, hiInclusive, !isDescending);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
        /* ----------------  Relational methods -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
        public Map.Entry<K,V> ceilingEntry(K key) {
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9515
diff changeset
  2789
            return getNearEntry(key, GT|EQ);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2792
        public K ceilingKey(K key) {
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9515
diff changeset
  2793
            return getNearKey(key, GT|EQ);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
        public Map.Entry<K,V> lowerEntry(K key) {
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9515
diff changeset
  2797
            return getNearEntry(key, LT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2799
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
        public K lowerKey(K key) {
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9515
diff changeset
  2801
            return getNearKey(key, LT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2802
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
        public Map.Entry<K,V> floorEntry(K key) {
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9515
diff changeset
  2805
            return getNearEntry(key, LT|EQ);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2806
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2807
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2808
        public K floorKey(K key) {
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9515
diff changeset
  2809
            return getNearKey(key, LT|EQ);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2810
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2811
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2812
        public Map.Entry<K,V> higherEntry(K key) {
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9515
diff changeset
  2813
            return getNearEntry(key, GT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2814
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2815
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2816
        public K higherKey(K key) {
11279
d9dab5ec5044 7118066: Warnings in java.util.concurrent package
dl
parents: 9515
diff changeset
  2817
            return getNearKey(key, GT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2818
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2819
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2820
        public K firstKey() {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
  2821
            return isDescending ? highestKey() : lowestKey();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2822
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2823
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2824
        public K lastKey() {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
  2825
            return isDescending ? lowestKey() : highestKey();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2826
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2827
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2828
        public Map.Entry<K,V> firstEntry() {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
  2829
            return isDescending ? highestEntry() : lowestEntry();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2830
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2831
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2832
        public Map.Entry<K,V> lastEntry() {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
  2833
            return isDescending ? lowestEntry() : highestEntry();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2834
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2835
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2836
        public Map.Entry<K,V> pollFirstEntry() {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
  2837
            return isDescending ? removeHighest() : removeLowest();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2838
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2839
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2840
        public Map.Entry<K,V> pollLastEntry() {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
  2841
            return isDescending ? removeLowest() : removeHighest();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2842
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2843
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2844
        /* ---------------- Submap Views -------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2845
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2846
        public NavigableSet<K> keySet() {
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 33674
diff changeset
  2847
            KeySet<K,V> ks;
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 33674
diff changeset
  2848
            if ((ks = keySetView) != null) return ks;
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 33674
diff changeset
  2849
            return keySetView = new KeySet<>(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2850
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2851
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2852
        public NavigableSet<K> navigableKeySet() {
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 33674
diff changeset
  2853
            KeySet<K,V> ks;
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 33674
diff changeset
  2854
            if ((ks = keySetView) != null) return ks;
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 33674
diff changeset
  2855
            return keySetView = new KeySet<>(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2856
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
        public Collection<V> values() {
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 33674
diff changeset
  2859
            Values<K,V> vs;
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 33674
diff changeset
  2860
            if ((vs = valuesView) != null) return vs;
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 33674
diff changeset
  2861
            return valuesView = new Values<>(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
        public Set<Map.Entry<K,V>> entrySet() {
38551
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 33674
diff changeset
  2865
            EntrySet<K,V> es;
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 33674
diff changeset
  2866
            if ((es = entrySetView) != null) return es;
82c48058acc2 8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents: 33674
diff changeset
  2867
            return entrySetView = new EntrySet<K,V>(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2868
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2869
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
        public NavigableSet<K> descendingKeySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
            return descendingMap().navigableKeySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
         * Variant of main Iter class to traverse through submaps.
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  2876
         * Also serves as back-up Spliterator for views.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
         */
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2878
        abstract class SubMapIter<T> implements Iterator<T>, Spliterator<T> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2879
            /** the last node returned by next() */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2880
            Node<K,V> lastReturned;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2881
            /** the next node to return from next(); */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2882
            Node<K,V> next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2883
            /** Cache of next value field to maintain weak consistency */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2884
            V nextValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2885
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2886
            SubMapIter() {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2887
                VarHandle.acquireFence();
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2888
                Comparator<? super K> cmp = m.comparator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2889
                for (;;) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2890
                    next = isDescending ? hiNode(cmp) : loNode(cmp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2891
                    if (next == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2892
                        break;
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2893
                    V x = next.val;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2894
                    if (x != null) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2895
                        if (! inBounds(next.key, cmp))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2896
                            next = null;
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2897
                        else
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2898
                            nextValue = x;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2899
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2900
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2901
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2902
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2903
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2904
            public final boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2905
                return next != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2906
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2907
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2908
            final void advance() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2909
                if (next == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2910
                    throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2911
                lastReturned = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2912
                if (isDescending)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2913
                    descend();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2914
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2915
                    ascend();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2916
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2917
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2918
            private void ascend() {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2919
                Comparator<? super K> cmp = m.comparator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2920
                for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2921
                    next = next.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2922
                    if (next == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2923
                        break;
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2924
                    V x = next.val;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2925
                    if (x != null) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2926
                        if (tooHigh(next.key, cmp))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2927
                            next = null;
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2928
                        else
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2929
                            nextValue = x;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2930
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2931
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2932
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2933
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2934
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2935
            private void descend() {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2936
                Comparator<? super K> cmp = m.comparator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2937
                for (;;) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2938
                    next = m.findNear(lastReturned.key, LT, cmp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2939
                    if (next == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2940
                        break;
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2941
                    V x = next.val;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2942
                    if (x != null) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2943
                        if (tooLow(next.key, cmp))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2944
                            next = null;
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2945
                        else
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  2946
                            nextValue = x;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2947
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2948
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2949
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2950
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2951
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2952
            public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2953
                Node<K,V> l = lastReturned;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2954
                if (l == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2955
                    throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2956
                m.remove(l.key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2957
                lastReturned = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2958
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2959
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2960
            public Spliterator<T> trySplit() {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2961
                return null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2962
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2963
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2964
            public boolean tryAdvance(Consumer<? super T> action) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2965
                if (hasNext()) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2966
                    action.accept(next());
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2967
                    return true;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2968
                }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2969
                return false;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2970
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2971
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2972
            public void forEachRemaining(Consumer<? super T> action) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2973
                while (hasNext())
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2974
                    action.accept(next());
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2975
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2976
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2977
            public long estimateSize() {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2978
                return Long.MAX_VALUE;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2979
            }
19378
0b98a290dd86 8022326: Spliterator for values of j.u.c.ConcurrentSkipListMap does not report ORDERED
psandoz
parents: 18767
diff changeset
  2980
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2981
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2982
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2983
        final class SubMapValueIterator extends SubMapIter<V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2984
            public V next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2985
                V v = nextValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2986
                advance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2987
                return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2988
            }
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2989
            public int characteristics() {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2990
                return 0;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  2991
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2992
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2993
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2994
        final class SubMapKeyIterator extends SubMapIter<K> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2995
            public K next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2996
                Node<K,V> n = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2997
                advance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2998
                return n.key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2999
            }
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3000
            public int characteristics() {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3001
                return Spliterator.DISTINCT | Spliterator.ORDERED |
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3002
                    Spliterator.SORTED;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3003
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3004
            public final Comparator<? super K> getComparator() {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3005
                return SubMap.this.comparator();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3006
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3007
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3008
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3009
        final class SubMapEntryIterator extends SubMapIter<Map.Entry<K,V>> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3010
            public Map.Entry<K,V> next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3011
                Node<K,V> n = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3012
                V v = nextValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3013
                advance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3014
                return new AbstractMap.SimpleImmutableEntry<K,V>(n.key, v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3015
            }
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3016
            public int characteristics() {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3017
                return Spliterator.DISTINCT;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3018
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3019
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3020
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3021
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3022
    // default Map method overrides
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3023
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3024
    public void forEach(BiConsumer<? super K, ? super V> action) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3025
        if (action == null) throw new NullPointerException();
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3026
        Node<K,V> b, n; V v;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3027
        if ((b = baseHead()) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3028
            while ((n = b.next) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3029
                if ((v = n.val) != null)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3030
                    action.accept(n.key, v);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3031
                b = n;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3032
            }
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3033
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3034
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3035
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3036
    public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3037
        if (function == null) throw new NullPointerException();
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3038
        Node<K,V> b, n; V v;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3039
        if ((b = baseHead()) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3040
            while ((n = b.next) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3041
                while ((v = n.val) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3042
                    V r = function.apply(n.key, v);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3043
                    if (r == null) throw new NullPointerException();
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3044
                    if (VAL.compareAndSet(n, v, r))
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3045
                        break;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3046
                }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3047
                b = n;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3048
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3049
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3050
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3051
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3052
    /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  3053
     * Helper method for EntrySet.removeIf.
30441
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  3054
     */
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  3055
    boolean removeEntryIf(Predicate<? super Entry<K,V>> function) {
30441
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  3056
        if (function == null) throw new NullPointerException();
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  3057
        boolean removed = false;
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3058
        Node<K,V> b, n; V v;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3059
        if ((b = baseHead()) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3060
            while ((n = b.next) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3061
                if ((v = n.val) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3062
                    K k = n.key;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3063
                    Map.Entry<K,V> e = new AbstractMap.SimpleImmutableEntry<>(k, v);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3064
                    if (function.test(e) && remove(k, v))
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3065
                        removed = true;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3066
                }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3067
                b = n;
30441
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  3068
            }
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  3069
        }
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  3070
        return removed;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  3071
    }
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  3072
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  3073
    /**
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  3074
     * Helper method for Values.removeIf.
30441
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  3075
     */
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  3076
    boolean removeValueIf(Predicate<? super V> function) {
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  3077
        if (function == null) throw new NullPointerException();
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  3078
        boolean removed = false;
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3079
        Node<K,V> b, n; V v;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3080
        if ((b = baseHead()) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3081
            while ((n = b.next) != null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3082
                if ((v = n.val) != null && function.test(v) && remove(n.key, v))
30441
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  3083
                    removed = true;
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3084
                b = n;
30441
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  3085
            }
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  3086
        }
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  3087
        return removed;
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  3088
    }
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  3089
415a5242e046 8078645: removeIf(filter) in ConcurrentHashMap removes entries for which filter is false
psandoz
parents: 25859
diff changeset
  3090
    /**
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3091
     * Base class providing common structure for Spliterators.
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3092
     * (Although not all that much common functionality; as usual for
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3093
     * view classes, details annoyingly vary in key, value, and entry
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3094
     * subclasses in ways that are not worth abstracting out for
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3095
     * internal classes.)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3096
     *
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3097
     * The basic split strategy is to recursively descend from top
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3098
     * level, row by row, descending to next row when either split
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3099
     * off, or the end of row is encountered. Control of the number of
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3100
     * splits relies on some statistical estimation: The expected
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3101
     * remaining number of elements of a skip list when advancing
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3102
     * either across or down decreases by about 25%.
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3103
     */
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3104
    abstract static class CSLMSpliterator<K,V> {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3105
        final Comparator<? super K> comparator;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3106
        final K fence;     // exclusive upper bound for keys, or null if to end
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3107
        Index<K,V> row;    // the level to split out
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3108
        Node<K,V> current; // current traversal node; initialize at origin
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3109
        long est;          // size estimate
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3110
        CSLMSpliterator(Comparator<? super K> comparator, Index<K,V> row,
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3111
                        Node<K,V> origin, K fence, long est) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3112
            this.comparator = comparator; this.row = row;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3113
            this.current = origin; this.fence = fence; this.est = est;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3114
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3115
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3116
        public final long estimateSize() { return est; }
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3117
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3118
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3119
    static final class KeySpliterator<K,V> extends CSLMSpliterator<K,V>
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3120
        implements Spliterator<K> {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3121
        KeySpliterator(Comparator<? super K> comparator, Index<K,V> row,
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3122
                       Node<K,V> origin, K fence, long est) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3123
            super(comparator, row, origin, fence, est);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3124
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3125
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  3126
        public KeySpliterator<K,V> trySplit() {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3127
            Node<K,V> e; K ek;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3128
            Comparator<? super K> cmp = comparator;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3129
            K f = fence;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3130
            if ((e = current) != null && (ek = e.key) != null) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3131
                for (Index<K,V> q = row; q != null; q = row = q.down) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3132
                    Index<K,V> s; Node<K,V> b, n; K sk;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3133
                    if ((s = q.right) != null && (b = s.node) != null &&
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3134
                        (n = b.next) != null && n.val != null &&
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3135
                        (sk = n.key) != null && cpr(cmp, sk, ek) > 0 &&
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3136
                        (f == null || cpr(cmp, sk, f) < 0)) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3137
                        current = n;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3138
                        Index<K,V> r = q.down;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3139
                        row = (s.right != null) ? s : s.down;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3140
                        est -= est >>> 2;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3141
                        return new KeySpliterator<K,V>(cmp, r, e, sk, est);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3142
                    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3143
                }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3144
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3145
            return null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3146
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3147
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3148
        public void forEachRemaining(Consumer<? super K> action) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3149
            if (action == null) throw new NullPointerException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3150
            Comparator<? super K> cmp = comparator;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3151
            K f = fence;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3152
            Node<K,V> e = current;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3153
            current = null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3154
            for (; e != null; e = e.next) {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3155
                K k;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3156
                if ((k = e.key) != null && f != null && cpr(cmp, f, k) <= 0)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3157
                    break;
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3158
                if (e.val != null)
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3159
                    action.accept(k);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3160
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3161
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3162
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3163
        public boolean tryAdvance(Consumer<? super K> action) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3164
            if (action == null) throw new NullPointerException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3165
            Comparator<? super K> cmp = comparator;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3166
            K f = fence;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3167
            Node<K,V> e = current;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3168
            for (; e != null; e = e.next) {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3169
                K k;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3170
                if ((k = e.key) != null && f != null && cpr(cmp, f, k) <= 0) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3171
                    e = null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3172
                    break;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3173
                }
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3174
                if (e.val != null) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3175
                    current = e.next;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3176
                    action.accept(k);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3177
                    return true;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3178
                }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3179
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3180
            current = e;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3181
            return false;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3182
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3183
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3184
        public int characteristics() {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3185
            return Spliterator.DISTINCT | Spliterator.SORTED |
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3186
                Spliterator.ORDERED | Spliterator.CONCURRENT |
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3187
                Spliterator.NONNULL;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3188
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3189
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3190
        public final Comparator<? super K> getComparator() {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3191
            return comparator;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3192
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3193
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3194
    // factory method for KeySpliterator
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3195
    final KeySpliterator<K,V> keySpliterator() {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3196
        Index<K,V> h; Node<K,V> n; long est;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3197
        VarHandle.acquireFence();
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3198
        if ((h = head) == null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3199
            n = null;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3200
            est = 0L;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3201
        }
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3202
        else {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3203
            n = h.node;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3204
            est = getAdderCount();
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3205
        }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3206
        return new KeySpliterator<K,V>(comparator, h, n, null, est);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3207
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3208
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3209
    static final class ValueSpliterator<K,V> extends CSLMSpliterator<K,V>
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3210
        implements Spliterator<V> {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3211
        ValueSpliterator(Comparator<? super K> comparator, Index<K,V> row,
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3212
                       Node<K,V> origin, K fence, long est) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3213
            super(comparator, row, origin, fence, est);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3214
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3215
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  3216
        public ValueSpliterator<K,V> trySplit() {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3217
            Node<K,V> e; K ek;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3218
            Comparator<? super K> cmp = comparator;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3219
            K f = fence;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3220
            if ((e = current) != null && (ek = e.key) != null) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3221
                for (Index<K,V> q = row; q != null; q = row = q.down) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3222
                    Index<K,V> s; Node<K,V> b, n; K sk;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3223
                    if ((s = q.right) != null && (b = s.node) != null &&
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3224
                        (n = b.next) != null && n.val != null &&
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3225
                        (sk = n.key) != null && cpr(cmp, sk, ek) > 0 &&
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3226
                        (f == null || cpr(cmp, sk, f) < 0)) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3227
                        current = n;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3228
                        Index<K,V> r = q.down;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3229
                        row = (s.right != null) ? s : s.down;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3230
                        est -= est >>> 2;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3231
                        return new ValueSpliterator<K,V>(cmp, r, e, sk, est);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3232
                    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3233
                }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3234
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3235
            return null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3236
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3237
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3238
        public void forEachRemaining(Consumer<? super V> action) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3239
            if (action == null) throw new NullPointerException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3240
            Comparator<? super K> cmp = comparator;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3241
            K f = fence;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3242
            Node<K,V> e = current;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3243
            current = null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3244
            for (; e != null; e = e.next) {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3245
                K k; V v;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3246
                if ((k = e.key) != null && f != null && cpr(cmp, f, k) <= 0)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3247
                    break;
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3248
                if ((v = e.val) != null)
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3249
                    action.accept(v);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3250
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3251
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3252
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3253
        public boolean tryAdvance(Consumer<? super V> action) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3254
            if (action == null) throw new NullPointerException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3255
            Comparator<? super K> cmp = comparator;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3256
            K f = fence;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3257
            Node<K,V> e = current;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3258
            for (; e != null; e = e.next) {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3259
                K k; V v;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3260
                if ((k = e.key) != null && f != null && cpr(cmp, f, k) <= 0) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3261
                    e = null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3262
                    break;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3263
                }
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3264
                if ((v = e.val) != null) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3265
                    current = e.next;
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3266
                    action.accept(v);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3267
                    return true;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3268
                }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3269
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3270
            current = e;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3271
            return false;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3272
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3273
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3274
        public int characteristics() {
19378
0b98a290dd86 8022326: Spliterator for values of j.u.c.ConcurrentSkipListMap does not report ORDERED
psandoz
parents: 18767
diff changeset
  3275
            return Spliterator.CONCURRENT | Spliterator.ORDERED |
0b98a290dd86 8022326: Spliterator for values of j.u.c.ConcurrentSkipListMap does not report ORDERED
psandoz
parents: 18767
diff changeset
  3276
                Spliterator.NONNULL;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3277
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3278
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3279
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3280
    // Almost the same as keySpliterator()
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3281
    final ValueSpliterator<K,V> valueSpliterator() {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3282
        Index<K,V> h; Node<K,V> n; long est;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3283
        VarHandle.acquireFence();
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3284
        if ((h = head) == null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3285
            n = null;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3286
            est = 0L;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3287
        }
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3288
        else {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3289
            n = h.node;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3290
            est = getAdderCount();
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3291
        }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3292
        return new ValueSpliterator<K,V>(comparator, h, n, null, est);
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3293
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3294
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3295
    static final class EntrySpliterator<K,V> extends CSLMSpliterator<K,V>
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3296
        implements Spliterator<Map.Entry<K,V>> {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3297
        EntrySpliterator(Comparator<? super K> comparator, Index<K,V> row,
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3298
                         Node<K,V> origin, K fence, long est) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3299
            super(comparator, row, origin, fence, est);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3300
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3301
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  3302
        public EntrySpliterator<K,V> trySplit() {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3303
            Node<K,V> e; K ek;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3304
            Comparator<? super K> cmp = comparator;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3305
            K f = fence;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3306
            if ((e = current) != null && (ek = e.key) != null) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3307
                for (Index<K,V> q = row; q != null; q = row = q.down) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3308
                    Index<K,V> s; Node<K,V> b, n; K sk;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3309
                    if ((s = q.right) != null && (b = s.node) != null &&
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3310
                        (n = b.next) != null && n.val != null &&
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3311
                        (sk = n.key) != null && cpr(cmp, sk, ek) > 0 &&
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3312
                        (f == null || cpr(cmp, sk, f) < 0)) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3313
                        current = n;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3314
                        Index<K,V> r = q.down;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3315
                        row = (s.right != null) ? s : s.down;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3316
                        est -= est >>> 2;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3317
                        return new EntrySpliterator<K,V>(cmp, r, e, sk, est);
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3318
                    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3319
                }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3320
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3321
            return null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3322
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3323
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3324
        public void forEachRemaining(Consumer<? super Map.Entry<K,V>> action) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3325
            if (action == null) throw new NullPointerException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3326
            Comparator<? super K> cmp = comparator;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3327
            K f = fence;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3328
            Node<K,V> e = current;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3329
            current = null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3330
            for (; e != null; e = e.next) {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3331
                K k; V v;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3332
                if ((k = e.key) != null && f != null && cpr(cmp, f, k) <= 0)
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3333
                    break;
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3334
                if ((v = e.val) != null) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3335
                    action.accept
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3336
                        (new AbstractMap.SimpleImmutableEntry<K,V>(k, v));
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3337
                }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3338
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3339
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3340
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3341
        public boolean tryAdvance(Consumer<? super Map.Entry<K,V>> action) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3342
            if (action == null) throw new NullPointerException();
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3343
            Comparator<? super K> cmp = comparator;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3344
            K f = fence;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3345
            Node<K,V> e = current;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3346
            for (; e != null; e = e.next) {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3347
                K k; V v;
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3348
                if ((k = e.key) != null && f != null && cpr(cmp, f, k) <= 0) {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3349
                    e = null;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3350
                    break;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3351
                }
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3352
                if ((v = e.val) != null) {
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3353
                    current = e.next;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3354
                    action.accept
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3355
                        (new AbstractMap.SimpleImmutableEntry<K,V>(k, v));
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3356
                    return true;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3357
                }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3358
            }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3359
            current = e;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3360
            return false;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3361
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3362
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3363
        public int characteristics() {
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3364
            return Spliterator.DISTINCT | Spliterator.SORTED |
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3365
                Spliterator.ORDERED | Spliterator.CONCURRENT |
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3366
                Spliterator.NONNULL;
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3367
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3368
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3369
        public final Comparator<Map.Entry<K,V>> getComparator() {
19378
0b98a290dd86 8022326: Spliterator for values of j.u.c.ConcurrentSkipListMap does not report ORDERED
psandoz
parents: 18767
diff changeset
  3370
            // Adapt or create a key-based comparator
0b98a290dd86 8022326: Spliterator for values of j.u.c.ConcurrentSkipListMap does not report ORDERED
psandoz
parents: 18767
diff changeset
  3371
            if (comparator != null) {
0b98a290dd86 8022326: Spliterator for values of j.u.c.ConcurrentSkipListMap does not report ORDERED
psandoz
parents: 18767
diff changeset
  3372
                return Map.Entry.comparingByKey(comparator);
0b98a290dd86 8022326: Spliterator for values of j.u.c.ConcurrentSkipListMap does not report ORDERED
psandoz
parents: 18767
diff changeset
  3373
            }
0b98a290dd86 8022326: Spliterator for values of j.u.c.ConcurrentSkipListMap does not report ORDERED
psandoz
parents: 18767
diff changeset
  3374
            else {
0b98a290dd86 8022326: Spliterator for values of j.u.c.ConcurrentSkipListMap does not report ORDERED
psandoz
parents: 18767
diff changeset
  3375
                return (Comparator<Map.Entry<K,V>> & Serializable) (e1, e2) -> {
0b98a290dd86 8022326: Spliterator for values of j.u.c.ConcurrentSkipListMap does not report ORDERED
psandoz
parents: 18767
diff changeset
  3376
                    @SuppressWarnings("unchecked")
0b98a290dd86 8022326: Spliterator for values of j.u.c.ConcurrentSkipListMap does not report ORDERED
psandoz
parents: 18767
diff changeset
  3377
                    Comparable<? super K> k1 = (Comparable<? super K>) e1.getKey();
0b98a290dd86 8022326: Spliterator for values of j.u.c.ConcurrentSkipListMap does not report ORDERED
psandoz
parents: 18767
diff changeset
  3378
                    return k1.compareTo(e2.getKey());
0b98a290dd86 8022326: Spliterator for values of j.u.c.ConcurrentSkipListMap does not report ORDERED
psandoz
parents: 18767
diff changeset
  3379
                };
0b98a290dd86 8022326: Spliterator for values of j.u.c.ConcurrentSkipListMap does not report ORDERED
psandoz
parents: 18767
diff changeset
  3380
            }
18767
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3381
        }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3382
    }
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3383
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3384
    // Almost the same as keySpliterator()
6214297bf27d 8011427: java.util.concurrent collection Spliterator implementations
psandoz
parents: 14325
diff changeset
  3385
    final EntrySpliterator<K,V> entrySpliterator() {
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3386
        Index<K,V> h; Node<K,V> n; long est;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3387
        VarHandle.acquireFence();
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3388
        if ((h = head) == null) {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3389
            n = null;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3390
            est = 0L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3391
        }
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3392
        else {
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3393
            n = h.node;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3394
            est = getAdderCount();
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3395
        }
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3396
        return new EntrySpliterator<K,V>(comparator, h, n, null, est);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3397
    }
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
  3398
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 39039
diff changeset
  3399
    // VarHandle mechanics
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 39039
diff changeset
  3400
    private static final VarHandle HEAD;
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3401
    private static final VarHandle ADDER;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3402
    private static final VarHandle NEXT;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3403
    private static final VarHandle VAL;
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3404
    private static final VarHandle RIGHT;
8544
225896f7b33c 7017493: ConcurrentLinkedDeque: Unexpected initialization order can lead to crash due to use of Unsafe
dl
parents: 7976
diff changeset
  3405
    static {
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
  3406
        try {
39725
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 39039
diff changeset
  3407
            MethodHandles.Lookup l = MethodHandles.lookup();
9548f8d846e9 8080603: Replace Unsafe with VarHandle in java.util.concurrent classes
dl
parents: 39039
diff changeset
  3408
            HEAD = l.findVarHandle(ConcurrentSkipListMap.class, "head",
47305
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3409
                                   Index.class);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3410
            ADDER = l.findVarHandle(ConcurrentSkipListMap.class, "adder",
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3411
                                    LongAdder.class);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3412
            NEXT = l.findVarHandle(Node.class, "next", Node.class);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3413
            VAL = l.findVarHandle(Node.class, "val", Object.class);
62cd7fef87b6 8186226: Improve ConcurrentSkipListMap performance on weak memory model machines
dl
parents: 47216
diff changeset
  3414
            RIGHT = l.findVarHandle(Index.class, "right", Index.class);
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 30655
diff changeset
  3415
        } catch (ReflectiveOperationException e) {
49565
b5705ade8c8d 8197531: Miscellaneous changes imported from jsr166 CVS 2018-04
dl
parents: 49433
diff changeset
  3416
            throw new ExceptionInInitializerError(e);
7976
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
  3417
        }
f273c0d04215 7005424: Resync java.util.concurrent classes with Dougs CVS - Jan 2011
dl
parents: 5506
diff changeset
  3418
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3419
}