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