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