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