ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166e/ConcurrentHashMapV8.java
Revision: 1.121
Committed: Sat Dec 21 21:32:34 2013 UTC (10 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.120: +2 -2 lines
Log Message:
whitespace

File Contents

# User Rev Content
1 dl 1.1 /*
2     * Written by Doug Lea with assistance from members of JCP JSR-166
3     * Expert Group and released to the public domain, as explained at
4     * http://creativecommons.org/publicdomain/zero/1.0/
5     */
6    
7     package jsr166e;
8 dl 1.71
9 dl 1.102 import jsr166e.ForkJoinPool;
10    
11     import java.io.ObjectStreamField;
12     import java.io.Serializable;
13     import java.lang.reflect.ParameterizedType;
14     import java.lang.reflect.Type;
15 dl 1.112 import java.util.AbstractMap;
16 dl 1.24 import java.util.Arrays;
17 dl 1.1 import java.util.Collection;
18 dl 1.102 import java.util.Comparator;
19     import java.util.ConcurrentModificationException;
20     import java.util.Enumeration;
21     import java.util.HashMap;
22 dl 1.1 import java.util.Hashtable;
23     import java.util.Iterator;
24 dl 1.102 import java.util.Map;
25 dl 1.1 import java.util.NoSuchElementException;
26 dl 1.102 import java.util.Set;
27 dl 1.1 import java.util.concurrent.ConcurrentMap;
28 dl 1.102 import java.util.concurrent.atomic.AtomicReference;
29 dl 1.82 import java.util.concurrent.atomic.AtomicInteger;
30 dl 1.102 import java.util.concurrent.locks.LockSupport;
31     import java.util.concurrent.locks.ReentrantLock;
32 dl 1.79
33 dl 1.1 /**
34     * A hash table supporting full concurrency of retrievals and
35     * high expected concurrency for updates. This class obeys the
36     * same functional specification as {@link java.util.Hashtable}, and
37     * includes versions of methods corresponding to each method of
38     * {@code Hashtable}. However, even though all operations are
39     * thread-safe, retrieval operations do <em>not</em> entail locking,
40     * and there is <em>not</em> any support for locking the entire table
41     * in a way that prevents all access. This class is fully
42     * interoperable with {@code Hashtable} in programs that rely on its
43     * thread safety but not on its synchronization details.
44     *
45 jsr166 1.78 * <p>Retrieval operations (including {@code get}) generally do not
46 dl 1.1 * block, so may overlap with update operations (including {@code put}
47     * and {@code remove}). Retrievals reflect the results of the most
48     * recently <em>completed</em> update operations holding upon their
49 dl 1.59 * onset. (More formally, an update operation for a given key bears a
50     * <em>happens-before</em> relation with any (non-null) retrieval for
51     * that key reporting the updated value.) For aggregate operations
52     * such as {@code putAll} and {@code clear}, concurrent retrievals may
53     * reflect insertion or removal of only some entries. Similarly,
54     * Iterators and Enumerations return elements reflecting the state of
55     * the hash table at some point at or since the creation of the
56     * iterator/enumeration. They do <em>not</em> throw {@link
57     * ConcurrentModificationException}. However, iterators are designed
58     * to be used by only one thread at a time. Bear in mind that the
59     * results of aggregate status methods including {@code size}, {@code
60     * isEmpty}, and {@code containsValue} are typically useful only when
61     * a map is not undergoing concurrent updates in other threads.
62     * Otherwise the results of these methods reflect transient states
63     * that may be adequate for monitoring or estimation purposes, but not
64     * for program control.
65 dl 1.1 *
66 jsr166 1.78 * <p>The table is dynamically expanded when there are too many
67 dl 1.16 * collisions (i.e., keys that have distinct hash codes but fall into
68     * the same slot modulo the table size), with the expected average
69 dl 1.24 * effect of maintaining roughly two bins per mapping (corresponding
70     * to a 0.75 load factor threshold for resizing). There may be much
71     * variance around this average as mappings are added and removed, but
72     * overall, this maintains a commonly accepted time/space tradeoff for
73     * hash tables. However, resizing this or any other kind of hash
74     * table may be a relatively slow operation. When possible, it is a
75     * good idea to provide a size estimate as an optional {@code
76 dl 1.16 * initialCapacity} constructor argument. An additional optional
77     * {@code loadFactor} constructor argument provides a further means of
78     * customizing initial table capacity by specifying the table density
79     * to be used in calculating the amount of space to allocate for the
80     * given number of elements. Also, for compatibility with previous
81     * versions of this class, constructors may optionally specify an
82     * expected {@code concurrencyLevel} as an additional hint for
83     * internal sizing. Note that using many keys with exactly the same
84 jsr166 1.31 * {@code hashCode()} is a sure way to slow down performance of any
85 dl 1.102 * hash table. To ameliorate impact, when keys are {@link Comparable},
86     * this class may use comparison order among keys to help break ties.
87 dl 1.1 *
88 jsr166 1.78 * <p>A {@link Set} projection of a ConcurrentHashMapV8 may be created
89 dl 1.70 * (using {@link #newKeySet()} or {@link #newKeySet(int)}), or viewed
90     * (using {@link #keySet(Object)} when only keys are of interest, and the
91     * mapped values are (perhaps transiently) not used or all take the
92     * same mapping value.
93     *
94 dl 1.1 * <p>This class and its views and iterators implement all of the
95     * <em>optional</em> methods of the {@link Map} and {@link Iterator}
96     * interfaces.
97     *
98 jsr166 1.78 * <p>Like {@link Hashtable} but unlike {@link HashMap}, this class
99 dl 1.1 * does <em>not</em> allow {@code null} to be used as a key or value.
100     *
101 dl 1.102 * <p>ConcurrentHashMapV8s support a set of sequential and parallel bulk
102     * operations that are designed
103     * to be safely, and often sensibly, applied even with maps that are
104     * being concurrently updated by other threads; for example, when
105     * computing a snapshot summary of the values in a shared registry.
106     * There are three kinds of operation, each with four forms, accepting
107     * functions with Keys, Values, Entries, and (Key, Value) arguments
108     * and/or return values. Because the elements of a ConcurrentHashMapV8
109     * are not ordered in any particular way, and may be processed in
110     * different orders in different parallel executions, the correctness
111     * of supplied functions should not depend on any ordering, or on any
112     * other objects or values that may transiently change while
113     * computation is in progress; and except for forEach actions, should
114     * ideally be side-effect-free. Bulk operations on {@link java.util.Map.Entry}
115     * objects do not support method {@code setValue}.
116 dl 1.70 *
117     * <ul>
118     * <li> forEach: Perform a given action on each element.
119     * A variant form applies a given transformation on each element
120     * before performing the action.</li>
121     *
122     * <li> search: Return the first available non-null result of
123     * applying a given function on each element; skipping further
124     * search when a result is found.</li>
125     *
126     * <li> reduce: Accumulate each element. The supplied reduction
127     * function cannot rely on ordering (more formally, it should be
128     * both associative and commutative). There are five variants:
129     *
130     * <ul>
131     *
132     * <li> Plain reductions. (There is not a form of this method for
133     * (key, value) function arguments since there is no corresponding
134     * return type.)</li>
135     *
136     * <li> Mapped reductions that accumulate the results of a given
137     * function applied to each element.</li>
138     *
139     * <li> Reductions to scalar doubles, longs, and ints, using a
140     * given basis value.</li>
141     *
142 dl 1.102 * </ul>
143 dl 1.70 * </li>
144     * </ul>
145 dl 1.102 *
146     * <p>These bulk operations accept a {@code parallelismThreshold}
147     * argument. Methods proceed sequentially if the current map size is
148     * estimated to be less than the given threshold. Using a value of
149     * {@code Long.MAX_VALUE} suppresses all parallelism. Using a value
150     * of {@code 1} results in maximal parallelism by partitioning into
151     * enough subtasks to fully utilize the {@link
152     * ForkJoinPool#commonPool()} that is used for all parallel
153     * computations. Normally, you would initially choose one of these
154     * extreme values, and then measure performance of using in-between
155     * values that trade off overhead versus throughput.
156 dl 1.70 *
157     * <p>The concurrency properties of bulk operations follow
158     * from those of ConcurrentHashMapV8: Any non-null result returned
159     * from {@code get(key)} and related access methods bears a
160     * happens-before relation with the associated insertion or
161     * update. The result of any bulk operation reflects the
162     * composition of these per-element relations (but is not
163     * necessarily atomic with respect to the map as a whole unless it
164     * is somehow known to be quiescent). Conversely, because keys
165     * and values in the map are never null, null serves as a reliable
166     * atomic indicator of the current lack of any result. To
167     * maintain this property, null serves as an implicit basis for
168     * all non-scalar reduction operations. For the double, long, and
169     * int versions, the basis should be one that, when combined with
170     * any other value, returns that other value (more formally, it
171     * should be the identity element for the reduction). Most common
172     * reductions have these properties; for example, computing a sum
173     * with basis 0 or a minimum with basis MAX_VALUE.
174     *
175     * <p>Search and transformation functions provided as arguments
176     * should similarly return null to indicate the lack of any result
177     * (in which case it is not used). In the case of mapped
178     * reductions, this also enables transformations to serve as
179     * filters, returning null (or, in the case of primitive
180     * specializations, the identity basis) if the element should not
181     * be combined. You can create compound transformations and
182     * filterings by composing them yourself under this "null means
183     * there is nothing there now" rule before using them in search or
184     * reduce operations.
185     *
186     * <p>Methods accepting and/or returning Entry arguments maintain
187     * key-value associations. They may be useful for example when
188     * finding the key for the greatest value. Note that "plain" Entry
189     * arguments can be supplied using {@code new
190     * AbstractMap.SimpleEntry(k,v)}.
191     *
192 jsr166 1.78 * <p>Bulk operations may complete abruptly, throwing an
193 dl 1.70 * exception encountered in the application of a supplied
194     * function. Bear in mind when handling such exceptions that other
195     * concurrently executing functions could also have thrown
196     * exceptions, or would have done so if the first exception had
197     * not occurred.
198     *
199 dl 1.84 * <p>Speedups for parallel compared to sequential forms are common
200     * but not guaranteed. Parallel operations involving brief functions
201     * on small maps may execute more slowly than sequential forms if the
202     * underlying work to parallelize the computation is more expensive
203     * than the computation itself. Similarly, parallelization may not
204     * lead to much actual parallelism if all processors are busy
205     * performing unrelated tasks.
206 dl 1.70 *
207 jsr166 1.78 * <p>All arguments to all task methods must be non-null.
208 dl 1.70 *
209     * <p><em>jsr166e note: During transition, this class
210     * uses nested functional interfaces with different names but the
211 jsr166 1.77 * same forms as those expected for JDK8.</em>
212 dl 1.70 *
213 dl 1.1 * <p>This class is a member of the
214     * <a href="{@docRoot}/../technotes/guides/collections/index.html">
215     * Java Collections Framework</a>.
216     *
217 jsr166 1.22 * @since 1.5
218 dl 1.1 * @author Doug Lea
219     * @param <K> the type of keys maintained by this map
220     * @param <V> the type of mapped values
221     */
222 dl 1.112 public class ConcurrentHashMapV8<K,V> extends AbstractMap<K,V>
223 jsr166 1.99 implements ConcurrentMap<K,V>, Serializable {
224 dl 1.1 private static final long serialVersionUID = 7249069246763182397L;
225    
226     /**
227 dl 1.102 * An object for traversing and partitioning elements of a source.
228     * This interface provides a subset of the functionality of JDK8
229     * java.util.Spliterator.
230     */
231     public static interface ConcurrentHashMapSpliterator<T> {
232 jsr166 1.103 /**
233 dl 1.102 * If possible, returns a new spliterator covering
234     * approximately one half of the elements, which will not be
235     * covered by this spliterator. Returns null if cannot be
236     * split.
237     */
238     ConcurrentHashMapSpliterator<T> trySplit();
239     /**
240     * Returns an estimate of the number of elements covered by
241     * this Spliterator.
242     */
243     long estimateSize();
244    
245     /** Applies the action to each untraversed element */
246     void forEachRemaining(Action<? super T> action);
247     /** If an element remains, applies the action and returns true. */
248     boolean tryAdvance(Action<? super T> action);
249 dl 1.41 }
250    
251 dl 1.102 // Sams
252     /** Interface describing a void action of one argument */
253     public interface Action<A> { void apply(A a); }
254     /** Interface describing a void action of two arguments */
255     public interface BiAction<A,B> { void apply(A a, B b); }
256     /** Interface describing a function of one argument */
257     public interface Fun<A,T> { T apply(A a); }
258     /** Interface describing a function of two arguments */
259     public interface BiFun<A,B,T> { T apply(A a, B b); }
260     /** Interface describing a function mapping its argument to a double */
261     public interface ObjectToDouble<A> { double apply(A a); }
262     /** Interface describing a function mapping its argument to a long */
263     public interface ObjectToLong<A> { long apply(A a); }
264     /** Interface describing a function mapping its argument to an int */
265     public interface ObjectToInt<A> {int apply(A a); }
266     /** Interface describing a function mapping two arguments to a double */
267     public interface ObjectByObjectToDouble<A,B> { double apply(A a, B b); }
268     /** Interface describing a function mapping two arguments to a long */
269     public interface ObjectByObjectToLong<A,B> { long apply(A a, B b); }
270     /** Interface describing a function mapping two arguments to an int */
271     public interface ObjectByObjectToInt<A,B> {int apply(A a, B b); }
272     /** Interface describing a function mapping two doubles to a double */
273     public interface DoubleByDoubleToDouble { double apply(double a, double b); }
274     /** Interface describing a function mapping two longs to a long */
275     public interface LongByLongToLong { long apply(long a, long b); }
276     /** Interface describing a function mapping two ints to an int */
277     public interface IntByIntToInt { int apply(int a, int b); }
278    
279 dl 1.116
280 dl 1.1 /*
281     * Overview:
282     *
283     * The primary design goal of this hash table is to maintain
284     * concurrent readability (typically method get(), but also
285     * iterators and related methods) while minimizing update
286 dl 1.24 * contention. Secondary goals are to keep space consumption about
287     * the same or better than java.util.HashMap, and to support high
288     * initial insertion rates on an empty table by many threads.
289 dl 1.1 *
290 dl 1.102 * This map usually acts as a binned (bucketed) hash table. Each
291     * key-value mapping is held in a Node. Most nodes are instances
292     * of the basic Node class with hash, key, value, and next
293     * fields. However, various subclasses exist: TreeNodes are
294     * arranged in balanced trees, not lists. TreeBins hold the roots
295     * of sets of TreeNodes. ForwardingNodes are placed at the heads
296     * of bins during resizing. ReservationNodes are used as
297     * placeholders while establishing values in computeIfAbsent and
298     * related methods. The types TreeBin, ForwardingNode, and
299     * ReservationNode do not hold normal user keys, values, or
300     * hashes, and are readily distinguishable during search etc
301     * because they have negative hash fields and null key and value
302     * fields. (These special nodes are either uncommon or transient,
303     * so the impact of carrying around some unused fields is
304 jsr166 1.108 * insignificant.)
305 dl 1.1 *
306     * The table is lazily initialized to a power-of-two size upon the
307 dl 1.38 * first insertion. Each bin in the table normally contains a
308     * list of Nodes (most often, the list has only zero or one Node).
309     * Table accesses require volatile/atomic reads, writes, and
310     * CASes. Because there is no other way to arrange this without
311     * adding further indirections, we use intrinsics
312 dl 1.102 * (sun.misc.Unsafe) operations.
313 dl 1.24 *
314 dl 1.82 * We use the top (sign) bit of Node hash fields for control
315     * purposes -- it is available anyway because of addressing
316 dl 1.102 * constraints. Nodes with negative hash fields are specially
317     * handled or ignored in map methods.
318 dl 1.14 *
319 dl 1.27 * Insertion (via put or its variants) of the first node in an
320 dl 1.14 * empty bin is performed by just CASing it to the bin. This is
321 dl 1.38 * by far the most common case for put operations under most
322     * key/hash distributions. Other update operations (insert,
323     * delete, and replace) require locks. We do not want to waste
324     * the space required to associate a distinct lock object with
325     * each bin, so instead use the first node of a bin list itself as
326 dl 1.82 * a lock. Locking support for these locks relies on builtin
327     * "synchronized" monitors.
328 dl 1.24 *
329     * Using the first node of a list as a lock does not by itself
330     * suffice though: When a node is locked, any update must first
331     * validate that it is still the first node after locking it, and
332     * retry if not. Because new nodes are always appended to lists,
333     * once a node is first in a bin, it remains first until deleted
334 dl 1.102 * or the bin becomes invalidated (upon resizing).
335 dl 1.14 *
336 dl 1.24 * The main disadvantage of per-bin locks is that other update
337 dl 1.14 * operations on other nodes in a bin list protected by the same
338     * lock can stall, for example when user equals() or mapping
339 dl 1.38 * functions take a long time. However, statistically, under
340     * random hash codes, this is not a common problem. Ideally, the
341     * frequency of nodes in bins follows a Poisson distribution
342 dl 1.14 * (http://en.wikipedia.org/wiki/Poisson_distribution) with a
343 dl 1.16 * parameter of about 0.5 on average, given the resizing threshold
344     * of 0.75, although with a large variance because of resizing
345     * granularity. Ignoring variance, the expected occurrences of
346     * list size k are (exp(-0.5) * pow(0.5, k) / factorial(k)). The
347 dl 1.38 * first values are:
348 dl 1.16 *
349 dl 1.38 * 0: 0.60653066
350     * 1: 0.30326533
351     * 2: 0.07581633
352     * 3: 0.01263606
353     * 4: 0.00157952
354     * 5: 0.00015795
355     * 6: 0.00001316
356     * 7: 0.00000094
357     * 8: 0.00000006
358     * more: less than 1 in ten million
359 dl 1.16 *
360     * Lock contention probability for two threads accessing distinct
361 dl 1.38 * elements is roughly 1 / (8 * #elements) under random hashes.
362     *
363     * Actual hash code distributions encountered in practice
364     * sometimes deviate significantly from uniform randomness. This
365     * includes the case when N > (1<<30), so some keys MUST collide.
366     * Similarly for dumb or hostile usages in which multiple keys are
367 dl 1.102 * designed to have identical hash codes or ones that differs only
368     * in masked-out high bits. So we use a secondary strategy that
369     * applies when the number of nodes in a bin exceeds a
370     * threshold. These TreeBins use a balanced tree to hold nodes (a
371     * specialized form of red-black trees), bounding search time to
372     * O(log N). Each search step in a TreeBin is at least twice as
373 dl 1.38 * slow as in a regular list, but given that N cannot exceed
374     * (1<<64) (before running out of addresses) this bounds search
375     * steps, lock hold times, etc, to reasonable constants (roughly
376     * 100 nodes inspected per operation worst case) so long as keys
377     * are Comparable (which is very common -- String, Long, etc).
378     * TreeBin nodes (TreeNodes) also maintain the same "next"
379     * traversal pointers as regular nodes, so can be traversed in
380     * iterators in the same way.
381 dl 1.1 *
382 dl 1.38 * The table is resized when occupancy exceeds a percentage
383 dl 1.82 * threshold (nominally, 0.75, but see below). Any thread
384     * noticing an overfull bin may assist in resizing after the
385 dl 1.116 * initiating thread allocates and sets up the replacement array.
386     * However, rather than stalling, these other threads may proceed
387     * with insertions etc. The use of TreeBins shields us from the
388     * worst case effects of overfilling while resizes are in
389 dl 1.82 * progress. Resizing proceeds by transferring bins, one by one,
390 dl 1.116 * from the table to the next table. However, threads claim small
391     * blocks of indices to transfer (via field transferIndex) before
392 dl 1.117 * doing so, reducing contention. A generation stamp in field
393     * sizeCtl ensures that resizings do not overlap. Because we are
394     * using power-of-two expansion, the elements from each bin must
395     * either stay at same index, or move with a power of two
396     * offset. We eliminate unnecessary node creation by catching
397     * cases where old nodes can be reused because their next fields
398     * won't change. On average, only about one-sixth of them need
399     * cloning when a table doubles. The nodes they replace will be
400     * garbage collectable as soon as they are no longer referenced by
401     * any reader thread that may be in the midst of concurrently
402     * traversing table. Upon transfer, the old table bin contains
403     * only a special forwarding node (with hash field "MOVED") that
404     * contains the next table as its key. On encountering a
405     * forwarding node, access and update operations restart, using
406     * the new table.
407 dl 1.82 *
408     * Each bin transfer requires its bin lock, which can stall
409     * waiting for locks while resizing. However, because other
410     * threads can join in and help resize rather than contend for
411     * locks, average aggregate waits become shorter as resizing
412     * progresses. The transfer operation must also ensure that all
413     * accessible bins in both the old and new table are usable by any
414 dl 1.116 * traversal. This is arranged in part by proceeding from the
415     * last bin (table.length - 1) up towards the first. Upon seeing
416     * a forwarding node, traversals (see class Traverser) arrange to
417     * move to the new table without revisiting nodes. To ensure that
418     * no intervening nodes are skipped even when moved out of order,
419     * a stack (see class TableStack) is created on first encounter of
420     * a forwarding node during a traversal, to maintain its place if
421     * later processing the current table. The need for these
422     * save/restore mechanics is relatively rare, but when one
423     * forwarding node is encountered, typically many more will be.
424     * So Traversers use a simple caching scheme to avoid creating so
425     * many new TableStack nodes. (Thanks to Peter Levart for
426     * suggesting use of a stack here.)
427 dl 1.14 *
428 dl 1.24 * The traversal scheme also applies to partial traversals of
429 dl 1.52 * ranges of bins (via an alternate Traverser constructor)
430 dl 1.41 * to support partitioned aggregate operations. Also, read-only
431     * operations give up if ever forwarded to a null table, which
432     * provides support for shutdown-style clearing, which is also not
433     * currently implemented.
434 dl 1.14 *
435     * Lazy table initialization minimizes footprint until first use,
436     * and also avoids resizings when the first operation is from a
437     * putAll, constructor with map argument, or deserialization.
438 dl 1.24 * These cases attempt to override the initial capacity settings,
439     * but harmlessly fail to take effect in cases of races.
440 dl 1.1 *
441 dl 1.82 * The element count is maintained using a specialization of
442     * LongAdder. We need to incorporate a specialization rather than
443     * just use a LongAdder in order to access implicit
444     * contention-sensing that leads to creation of multiple
445     * CounterCells. The counter mechanics avoid contention on
446     * updates but can encounter cache thrashing if read too
447     * frequently during concurrent access. To avoid reading so often,
448     * resizing under contention is attempted only upon adding to a
449     * bin already holding two or more nodes. Under uniform hash
450     * distributions, the probability of this occurring at threshold
451     * is around 13%, meaning that only about 1 in 8 puts check
452 dl 1.102 * threshold (and after resizing, many fewer do so).
453     *
454     * TreeBins use a special form of comparison for search and
455     * related operations (which is the main reason we cannot use
456     * existing collections such as TreeMaps). TreeBins contain
457     * Comparable elements, but may contain others, as well as
458 dl 1.112 * elements that are Comparable but not necessarily Comparable for
459     * the same T, so we cannot invoke compareTo among them. To handle
460     * this, the tree is ordered primarily by hash value, then by
461     * Comparable.compareTo order if applicable. On lookup at a node,
462     * if elements are not comparable or compare as 0 then both left
463     * and right children may need to be searched in the case of tied
464     * hash values. (This corresponds to the full list search that
465     * would be necessary if all elements were non-Comparable and had
466     * tied hashes.) On insertion, to keep a total ordering (or as
467     * close as is required here) across rebalancings, we compare
468     * classes and identityHashCodes as tie-breakers. The red-black
469     * balancing code is updated from pre-jdk-collections
470 dl 1.102 * (http://gee.cs.oswego.edu/dl/classes/collections/RBCell.java)
471     * based in turn on Cormen, Leiserson, and Rivest "Introduction to
472     * Algorithms" (CLR).
473     *
474     * TreeBins also require an additional locking mechanism. While
475     * list traversal is always possible by readers even during
476 jsr166 1.108 * updates, tree traversal is not, mainly because of tree-rotations
477 dl 1.102 * that may change the root node and/or its linkages. TreeBins
478     * include a simple read-write lock mechanism parasitic on the
479     * main bin-synchronization strategy: Structural adjustments
480     * associated with an insertion or removal are already bin-locked
481     * (and so cannot conflict with other writers) but must wait for
482     * ongoing readers to finish. Since there can be only one such
483     * waiter, we use a simple scheme using a single "waiter" field to
484     * block writers. However, readers need never block. If the root
485     * lock is held, they proceed along the slow traversal path (via
486     * next-pointers) until the lock becomes available or the list is
487     * exhausted, whichever comes first. These cases are not fast, but
488     * maximize aggregate expected throughput.
489 dl 1.14 *
490     * Maintaining API and serialization compatibility with previous
491     * versions of this class introduces several oddities. Mainly: We
492     * leave untouched but unused constructor arguments refering to
493 dl 1.24 * concurrencyLevel. We accept a loadFactor constructor argument,
494     * but apply it only to initial table capacity (which is the only
495     * time that we can guarantee to honor it.) We also declare an
496     * unused "Segment" class that is instantiated in minimal form
497     * only when serializing.
498 dl 1.102 *
499 dl 1.112 * Also, solely for compatibility with previous versions of this
500     * class, it extends AbstractMap, even though all of its methods
501     * are overridden, so it is just useless baggage.
502     *
503 dl 1.102 * This file is organized to make things a little easier to follow
504     * while reading than they might otherwise: First the main static
505     * declarations and utilities, then fields, then main public
506     * methods (with a few factorings of multiple public methods into
507     * internal ones), then sizing methods, trees, traversers, and
508     * bulk operations.
509 dl 1.1 */
510    
511     /* ---------------- Constants -------------- */
512    
513     /**
514 dl 1.16 * The largest possible table capacity. This value must be
515     * exactly 1<<30 to stay within Java array allocation and indexing
516 dl 1.24 * bounds for power of two table sizes, and is further required
517     * because the top two bits of 32bit hash fields are used for
518     * control purposes.
519 dl 1.1 */
520 dl 1.14 private static final int MAXIMUM_CAPACITY = 1 << 30;
521 dl 1.1
522     /**
523 dl 1.14 * The default initial table capacity. Must be a power of 2
524     * (i.e., at least 1) and at most MAXIMUM_CAPACITY.
525 dl 1.1 */
526 dl 1.14 private static final int DEFAULT_CAPACITY = 16;
527 dl 1.1
528     /**
529 dl 1.24 * The largest possible (non-power of two) array size.
530     * Needed by toArray and related methods.
531     */
532     static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
533    
534     /**
535     * The default concurrency level for this table. Unused but
536     * defined for compatibility with previous versions of this class.
537     */
538     private static final int DEFAULT_CONCURRENCY_LEVEL = 16;
539    
540     /**
541 dl 1.16 * The load factor for this table. Overrides of this value in
542     * constructors affect only the initial table capacity. The
543 dl 1.24 * actual floating point value isn't normally used -- it is
544     * simpler to use expressions such as {@code n - (n >>> 2)} for
545     * the associated resizing threshold.
546 dl 1.1 */
547 dl 1.16 private static final float LOAD_FACTOR = 0.75f;
548 dl 1.1
549     /**
550 dl 1.38 * The bin count threshold for using a tree rather than list for a
551 dl 1.102 * bin. Bins are converted to trees when adding an element to a
552     * bin with at least this many nodes. The value must be greater
553     * than 2, and should be at least 8 to mesh with assumptions in
554     * tree removal about conversion back to plain bins upon
555     * shrinkage.
556     */
557     static final int TREEIFY_THRESHOLD = 8;
558    
559     /**
560     * The bin count threshold for untreeifying a (split) bin during a
561     * resize operation. Should be less than TREEIFY_THRESHOLD, and at
562     * most 6 to mesh with shrinkage detection under removal.
563     */
564     static final int UNTREEIFY_THRESHOLD = 6;
565    
566     /**
567     * The smallest table capacity for which bins may be treeified.
568     * (Otherwise the table is resized if too many nodes in a bin.)
569     * The value should be at least 4 * TREEIFY_THRESHOLD to avoid
570     * conflicts between resizing and treeification thresholds.
571 dl 1.38 */
572 dl 1.102 static final int MIN_TREEIFY_CAPACITY = 64;
573 dl 1.38
574 dl 1.82 /**
575     * Minimum number of rebinnings per transfer step. Ranges are
576     * subdivided to allow multiple resizer threads. This value
577     * serves as a lower bound to avoid resizers encountering
578     * excessive memory contention. The value should be at least
579     * DEFAULT_CAPACITY.
580     */
581     private static final int MIN_TRANSFER_STRIDE = 16;
582    
583 dl 1.117 /**
584     * The number of bits used for generation stamp in sizeCtl.
585     * Must be at least 6 for 32bit arrays.
586     */
587     private static int RESIZE_STAMP_BITS = 16;
588    
589     /**
590     * The maximum number of threads that can help resize.
591     * Must fit in 32 - RESIZE_STAMP_BITS bits.
592     */
593     private static final int MAX_RESIZERS = (1 << (32 - RESIZE_STAMP_BITS)) - 1;
594    
595     /**
596     * The bit shift for recording size stamp in sizeCtl.
597     */
598     private static final int RESIZE_STAMP_SHIFT = 32 - RESIZE_STAMP_BITS;
599    
600 dl 1.24 /*
601 dl 1.82 * Encodings for Node hash fields. See above for explanation.
602 dl 1.1 */
603 dl 1.109 static final int MOVED = -1; // hash for forwarding nodes
604     static final int TREEBIN = -2; // hash for roots of trees
605     static final int RESERVED = -3; // hash for transient reservations
606 dl 1.82 static final int HASH_BITS = 0x7fffffff; // usable bits of normal node hash
607    
608     /** Number of CPUS, to place bounds on some sizings */
609     static final int NCPU = Runtime.getRuntime().availableProcessors();
610    
611 dl 1.102 /** For serialization compatibility. */
612     private static final ObjectStreamField[] serialPersistentFields = {
613     new ObjectStreamField("segments", Segment[].class),
614     new ObjectStreamField("segmentMask", Integer.TYPE),
615     new ObjectStreamField("segmentShift", Integer.TYPE)
616     };
617    
618     /* ---------------- Nodes -------------- */
619    
620     /**
621     * Key-value entry. This class is never exported out as a
622     * user-mutable Map.Entry (i.e., one supporting setValue; see
623     * MapEntry below), but can be used for read-only traversals used
624 jsr166 1.108 * in bulk tasks. Subclasses of Node with a negative hash field
625 dl 1.102 * are special, and contain null keys and values (but are never
626     * exported). Otherwise, keys and vals are never null.
627     */
628     static class Node<K,V> implements Map.Entry<K,V> {
629     final int hash;
630     final K key;
631     volatile V val;
632 dl 1.109 volatile Node<K,V> next;
633 dl 1.102
634     Node(int hash, K key, V val, Node<K,V> next) {
635     this.hash = hash;
636     this.key = key;
637     this.val = val;
638     this.next = next;
639     }
640    
641     public final K getKey() { return key; }
642     public final V getValue() { return val; }
643     public final int hashCode() { return key.hashCode() ^ val.hashCode(); }
644     public final String toString(){ return key + "=" + val; }
645     public final V setValue(V value) {
646     throw new UnsupportedOperationException();
647     }
648 dl 1.82
649 dl 1.102 public final boolean equals(Object o) {
650     Object k, v, u; Map.Entry<?,?> e;
651     return ((o instanceof Map.Entry) &&
652     (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
653     (v = e.getValue()) != null &&
654     (k == key || k.equals(key)) &&
655     (v == (u = val) || v.equals(u)));
656     }
657 dl 1.82
658 dl 1.102 /**
659     * Virtualized support for map.get(); overridden in subclasses.
660     */
661     Node<K,V> find(int h, Object k) {
662     Node<K,V> e = this;
663     if (k != null) {
664     do {
665     K ek;
666     if (e.hash == h &&
667     ((ek = e.key) == k || (ek != null && k.equals(ek))))
668     return e;
669     } while ((e = e.next) != null);
670     }
671     return null;
672     }
673 dl 1.82 }
674    
675 dl 1.102 /* ---------------- Static utilities -------------- */
676    
677 dl 1.82 /**
678 dl 1.102 * Spreads (XORs) higher bits of hash to lower and also forces top
679     * bit to 0. Because the table uses power-of-two masking, sets of
680     * hashes that vary only in bits above the current mask will
681     * always collide. (Among known examples are sets of Float keys
682     * holding consecutive whole numbers in small tables.) So we
683     * apply a transform that spreads the impact of higher bits
684     * downward. There is a tradeoff between speed, utility, and
685     * quality of bit-spreading. Because many common sets of hashes
686     * are already reasonably distributed (so don't benefit from
687     * spreading), and because we use trees to handle large sets of
688     * collisions in bins, we just XOR some shifted bits in the
689     * cheapest possible way to reduce systematic lossage, as well as
690     * to incorporate impact of the highest bits that would otherwise
691     * never be used in index calculations because of table bounds.
692 dl 1.82 */
693 dl 1.102 static final int spread(int h) {
694     return (h ^ (h >>> 16)) & HASH_BITS;
695 dl 1.82 }
696    
697     /**
698 dl 1.102 * Returns a power of two table size for the given desired capacity.
699     * See Hackers Delight, sec 3.2
700 dl 1.82 */
701 dl 1.102 private static final int tableSizeFor(int c) {
702     int n = c - 1;
703     n |= n >>> 1;
704     n |= n >>> 2;
705     n |= n >>> 4;
706     n |= n >>> 8;
707     n |= n >>> 16;
708     return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
709     }
710 dl 1.82
711     /**
712 dl 1.102 * Returns x's Class if it is of the form "class C implements
713     * Comparable<C>", else null.
714 dl 1.82 */
715 dl 1.102 static Class<?> comparableClassFor(Object x) {
716     if (x instanceof Comparable) {
717     Class<?> c; Type[] ts, as; Type t; ParameterizedType p;
718     if ((c = x.getClass()) == String.class) // bypass checks
719     return c;
720     if ((ts = c.getGenericInterfaces()) != null) {
721     for (int i = 0; i < ts.length; ++i) {
722     if (((t = ts[i]) instanceof ParameterizedType) &&
723     ((p = (ParameterizedType)t).getRawType() ==
724     Comparable.class) &&
725     (as = p.getActualTypeArguments()) != null &&
726     as.length == 1 && as[0] == c) // type arg is c
727     return c;
728     }
729     }
730     }
731     return null;
732     }
733 dl 1.82
734     /**
735 dl 1.102 * Returns k.compareTo(x) if x matches kc (k's screened comparable
736     * class), else 0.
737 dl 1.82 */
738 dl 1.102 @SuppressWarnings({"rawtypes","unchecked"}) // for cast to Comparable
739     static int compareComparables(Class<?> kc, Object k, Object x) {
740     return (x == null || x.getClass() != kc ? 0 :
741     ((Comparable)k).compareTo(x));
742     }
743    
744     /* ---------------- Table element access -------------- */
745    
746     /*
747     * Volatile access methods are used for table elements as well as
748     * elements of in-progress next table while resizing. All uses of
749     * the tab arguments must be null checked by callers. All callers
750     * also paranoically precheck that tab's length is not zero (or an
751     * equivalent check), thus ensuring that any index argument taking
752     * the form of a hash value anded with (length - 1) is a valid
753     * index. Note that, to be correct wrt arbitrary concurrency
754     * errors by users, these checks must operate on local variables,
755     * which accounts for some odd-looking inline assignments below.
756     * Note that calls to setTabAt always occur within locked regions,
757 dl 1.117 * and so in principle require only release ordering, not
758 dl 1.109 * full volatile semantics, but are currently coded as volatile
759     * writes to be conservative.
760 dl 1.102 */
761    
762     @SuppressWarnings("unchecked")
763     static final <K,V> Node<K,V> tabAt(Node<K,V>[] tab, int i) {
764     return (Node<K,V>)U.getObjectVolatile(tab, ((long)i << ASHIFT) + ABASE);
765     }
766 dl 1.24
767 dl 1.102 static final <K,V> boolean casTabAt(Node<K,V>[] tab, int i,
768     Node<K,V> c, Node<K,V> v) {
769     return U.compareAndSwapObject(tab, ((long)i << ASHIFT) + ABASE, c, v);
770     }
771 jsr166 1.103
772 dl 1.102 static final <K,V> void setTabAt(Node<K,V>[] tab, int i, Node<K,V> v) {
773 dl 1.109 U.putObjectVolatile(tab, ((long)i << ASHIFT) + ABASE, v);
774 dl 1.102 }
775 jsr166 1.103
776 dl 1.24 /* ---------------- Fields -------------- */
777    
778     /**
779     * The array of bins. Lazily initialized upon first insertion.
780     * Size is always a power of two. Accessed directly by iterators.
781     */
782 dl 1.102 transient volatile Node<K,V>[] table;
783 dl 1.14
784 dl 1.16 /**
785 dl 1.82 * The next table to use; non-null only while resizing.
786     */
787 dl 1.102 private transient volatile Node<K,V>[] nextTable;
788 dl 1.82
789     /**
790     * Base counter value, used mainly when there is no contention,
791     * but also as a fallback during table initialization
792     * races. Updated via CAS.
793 dl 1.16 */
794 dl 1.82 private transient volatile long baseCount;
795 dl 1.24
796     /**
797     * Table initialization and resizing control. When negative, the
798 dl 1.82 * table is being initialized or resized: -1 for initialization,
799     * else -(1 + the number of active resizing threads). Otherwise,
800     * when table is null, holds the initial table size to use upon
801     * creation, or 0 for default. After initialization, holds the
802     * next element count value upon which to resize the table.
803 dl 1.24 */
804     private transient volatile int sizeCtl;
805    
806 dl 1.82 /**
807     * The next table index (plus one) to split while resizing.
808     */
809     private transient volatile int transferIndex;
810    
811     /**
812 dl 1.102 * Spinlock (locked via CAS) used when resizing and/or creating CounterCells.
813 dl 1.82 */
814 dl 1.102 private transient volatile int cellsBusy;
815 dl 1.82
816     /**
817     * Table of counter cells. When non-null, size is a power of 2.
818     */
819     private transient volatile CounterCell[] counterCells;
820    
821 dl 1.24 // views
822 dl 1.70 private transient KeySetView<K,V> keySet;
823 dl 1.75 private transient ValuesView<K,V> values;
824     private transient EntrySetView<K,V> entrySet;
825 dl 1.24
826 dl 1.16
827 dl 1.102 /* ---------------- Public operations -------------- */
828 dl 1.38
829 dl 1.102 /**
830     * Creates a new, empty map with the default initial table size (16).
831     */
832     public ConcurrentHashMapV8() {
833 dl 1.38 }
834    
835 dl 1.102 /**
836     * Creates a new, empty map with an initial table size
837     * accommodating the specified number of elements without the need
838     * to dynamically resize.
839     *
840     * @param initialCapacity The implementation performs internal
841     * sizing to accommodate this many elements.
842     * @throws IllegalArgumentException if the initial capacity of
843     * elements is negative
844     */
845     public ConcurrentHashMapV8(int initialCapacity) {
846     if (initialCapacity < 0)
847     throw new IllegalArgumentException();
848     int cap = ((initialCapacity >= (MAXIMUM_CAPACITY >>> 1)) ?
849     MAXIMUM_CAPACITY :
850     tableSizeFor(initialCapacity + (initialCapacity >>> 1) + 1));
851     this.sizeCtl = cap;
852 dl 1.38 }
853    
854 dl 1.102 /**
855     * Creates a new map with the same mappings as the given map.
856     *
857     * @param m the map
858     */
859     public ConcurrentHashMapV8(Map<? extends K, ? extends V> m) {
860     this.sizeCtl = DEFAULT_CAPACITY;
861     putAll(m);
862 dl 1.38 }
863    
864 dl 1.102 /**
865     * Creates a new, empty map with an initial table size based on
866     * the given number of elements ({@code initialCapacity}) and
867     * initial table density ({@code loadFactor}).
868     *
869     * @param initialCapacity the initial capacity. The implementation
870     * performs internal sizing to accommodate this many elements,
871     * given the specified load factor.
872     * @param loadFactor the load factor (table density) for
873     * establishing the initial table size
874     * @throws IllegalArgumentException if the initial capacity of
875     * elements is negative or the load factor is nonpositive
876     *
877     * @since 1.6
878     */
879     public ConcurrentHashMapV8(int initialCapacity, float loadFactor) {
880     this(initialCapacity, loadFactor, 1);
881     }
882 dl 1.1
883     /**
884 dl 1.102 * Creates a new, empty map with an initial table size based on
885     * the given number of elements ({@code initialCapacity}), table
886     * density ({@code loadFactor}), and number of concurrently
887     * updating threads ({@code concurrencyLevel}).
888     *
889     * @param initialCapacity the initial capacity. The implementation
890     * performs internal sizing to accommodate this many elements,
891     * given the specified load factor.
892     * @param loadFactor the load factor (table density) for
893     * establishing the initial table size
894     * @param concurrencyLevel the estimated number of concurrently
895     * updating threads. The implementation may use this value as
896     * a sizing hint.
897     * @throws IllegalArgumentException if the initial capacity is
898     * negative or the load factor or concurrencyLevel are
899     * nonpositive
900 dl 1.1 */
901 dl 1.102 public ConcurrentHashMapV8(int initialCapacity,
902     float loadFactor, int concurrencyLevel) {
903     if (!(loadFactor > 0.0f) || initialCapacity < 0 || concurrencyLevel <= 0)
904     throw new IllegalArgumentException();
905     if (initialCapacity < concurrencyLevel) // Use at least as many bins
906     initialCapacity = concurrencyLevel; // as estimated threads
907     long size = (long)(1.0 + (long)initialCapacity / loadFactor);
908     int cap = (size >= (long)MAXIMUM_CAPACITY) ?
909     MAXIMUM_CAPACITY : tableSizeFor((int)size);
910     this.sizeCtl = cap;
911 dl 1.24 }
912 dl 1.1
913 dl 1.102 // Original (since JDK1.2) Map methods
914 dl 1.38
915     /**
916 dl 1.102 * {@inheritDoc}
917 dl 1.38 */
918 dl 1.102 public int size() {
919     long n = sumCount();
920     return ((n < 0L) ? 0 :
921     (n > (long)Integer.MAX_VALUE) ? Integer.MAX_VALUE :
922     (int)n);
923 dl 1.38 }
924 dl 1.1
925 dl 1.38 /**
926 dl 1.102 * {@inheritDoc}
927     */
928     public boolean isEmpty() {
929     return sumCount() <= 0L; // ignore transient negative values
930     }
931    
932     /**
933     * Returns the value to which the specified key is mapped,
934     * or {@code null} if this map contains no mapping for the key.
935 dl 1.38 *
936 dl 1.102 * <p>More formally, if this map contains a mapping from a key
937     * {@code k} to a value {@code v} such that {@code key.equals(k)},
938     * then this method returns {@code v}; otherwise it returns
939     * {@code null}. (There can be at most one such mapping.)
940 dl 1.38 *
941 dl 1.102 * @throws NullPointerException if the specified key is null
942 dl 1.1 */
943 dl 1.102 public V get(Object key) {
944     Node<K,V>[] tab; Node<K,V> e, p; int n, eh; K ek;
945     int h = spread(key.hashCode());
946     if ((tab = table) != null && (n = tab.length) > 0 &&
947     (e = tabAt(tab, (n - 1) & h)) != null) {
948     if ((eh = e.hash) == h) {
949     if ((ek = e.key) == key || (ek != null && key.equals(ek)))
950     return e.val;
951     }
952     else if (eh < 0)
953     return (p = e.find(h, key)) != null ? p.val : null;
954     while ((e = e.next) != null) {
955     if (e.hash == h &&
956     ((ek = e.key) == key || (ek != null && key.equals(ek))))
957     return e.val;
958 dl 1.41 }
959     }
960 dl 1.102 return null;
961     }
962 dl 1.41
963 dl 1.102 /**
964     * Tests if the specified object is a key in this table.
965     *
966     * @param key possible key
967     * @return {@code true} if and only if the specified object
968     * is a key in this table, as determined by the
969     * {@code equals} method; {@code false} otherwise
970     * @throws NullPointerException if the specified key is null
971     */
972     public boolean containsKey(Object key) {
973     return get(key) != null;
974     }
975 dl 1.41
976 dl 1.102 /**
977     * Returns {@code true} if this map maps one or more keys to the
978     * specified value. Note: This method may require a full traversal
979     * of the map, and is much slower than method {@code containsKey}.
980     *
981     * @param value value whose presence in this map is to be tested
982     * @return {@code true} if this map maps one or more keys to the
983     * specified value
984     * @throws NullPointerException if the specified value is null
985     */
986     public boolean containsValue(Object value) {
987     if (value == null)
988     throw new NullPointerException();
989     Node<K,V>[] t;
990     if ((t = table) != null) {
991     Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
992     for (Node<K,V> p; (p = it.advance()) != null; ) {
993     V v;
994     if ((v = p.val) == value || (v != null && value.equals(v)))
995     return true;
996 dl 1.38 }
997     }
998 dl 1.102 return false;
999     }
1000 dl 1.38
1001 dl 1.102 /**
1002     * Maps the specified key to the specified value in this table.
1003     * Neither the key nor the value can be null.
1004     *
1005     * <p>The value can be retrieved by calling the {@code get} method
1006     * with a key that is equal to the original key.
1007     *
1008     * @param key key with which the specified value is to be associated
1009     * @param value value to be associated with the specified key
1010     * @return the previous value associated with {@code key}, or
1011     * {@code null} if there was no mapping for {@code key}
1012     * @throws NullPointerException if the specified key or value is null
1013     */
1014     public V put(K key, V value) {
1015     return putVal(key, value, false);
1016     }
1017 dl 1.38
1018 dl 1.102 /** Implementation for put and putIfAbsent */
1019     final V putVal(K key, V value, boolean onlyIfAbsent) {
1020     if (key == null || value == null) throw new NullPointerException();
1021     int hash = spread(key.hashCode());
1022     int binCount = 0;
1023     for (Node<K,V>[] tab = table;;) {
1024     Node<K,V> f; int n, i, fh;
1025     if (tab == null || (n = tab.length) == 0)
1026     tab = initTable();
1027     else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {
1028     if (casTabAt(tab, i, null,
1029     new Node<K,V>(hash, key, value, null)))
1030     break; // no lock when adding to empty bin
1031 dl 1.38 }
1032 dl 1.102 else if ((fh = f.hash) == MOVED)
1033     tab = helpTransfer(tab, f);
1034 dl 1.38 else {
1035 dl 1.102 V oldVal = null;
1036     synchronized (f) {
1037     if (tabAt(tab, i) == f) {
1038     if (fh >= 0) {
1039     binCount = 1;
1040     for (Node<K,V> e = f;; ++binCount) {
1041     K ek;
1042     if (e.hash == hash &&
1043     ((ek = e.key) == key ||
1044     (ek != null && key.equals(ek)))) {
1045     oldVal = e.val;
1046     if (!onlyIfAbsent)
1047     e.val = value;
1048     break;
1049 dl 1.41 }
1050 dl 1.102 Node<K,V> pred = e;
1051     if ((e = e.next) == null) {
1052     pred.next = new Node<K,V>(hash, key,
1053     value, null);
1054     break;
1055 dl 1.41 }
1056 dl 1.38 }
1057     }
1058 dl 1.102 else if (f instanceof TreeBin) {
1059     Node<K,V> p;
1060     binCount = 2;
1061     if ((p = ((TreeBin<K,V>)f).putTreeVal(hash, key,
1062     value)) != null) {
1063     oldVal = p.val;
1064     if (!onlyIfAbsent)
1065     p.val = value;
1066 dl 1.38 }
1067     }
1068     }
1069     }
1070 dl 1.102 if (binCount != 0) {
1071     if (binCount >= TREEIFY_THRESHOLD)
1072     treeifyBin(tab, i);
1073     if (oldVal != null)
1074     return oldVal;
1075     break;
1076     }
1077 dl 1.41 }
1078 dl 1.38 }
1079 dl 1.102 addCount(1L, binCount);
1080     return null;
1081 dl 1.1 }
1082    
1083 dl 1.14 /**
1084 dl 1.102 * Copies all of the mappings from the specified map to this one.
1085     * These mappings replace any mappings that this map had for any of the
1086     * keys currently in the specified map.
1087     *
1088     * @param m mappings to be stored in this map
1089 dl 1.14 */
1090 dl 1.102 public void putAll(Map<? extends K, ? extends V> m) {
1091     tryPresize(m.size());
1092     for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
1093     putVal(e.getKey(), e.getValue(), false);
1094 dl 1.38 }
1095    
1096     /**
1097 dl 1.102 * Removes the key (and its corresponding value) from this map.
1098     * This method does nothing if the key is not in the map.
1099     *
1100     * @param key the key that needs to be removed
1101     * @return the previous value associated with {@code key}, or
1102     * {@code null} if there was no mapping for {@code key}
1103     * @throws NullPointerException if the specified key is null
1104 dl 1.38 */
1105 dl 1.102 public V remove(Object key) {
1106     return replaceNode(key, null, null);
1107 dl 1.1 }
1108    
1109 dl 1.27 /**
1110     * Implementation for the four public remove/replace methods:
1111     * Replaces node value with v, conditional upon match of cv if
1112     * non-null. If resulting value is null, delete.
1113     */
1114 dl 1.102 final V replaceNode(Object key, V value, Object cv) {
1115     int hash = spread(key.hashCode());
1116     for (Node<K,V>[] tab = table;;) {
1117     Node<K,V> f; int n, i, fh;
1118     if (tab == null || (n = tab.length) == 0 ||
1119     (f = tabAt(tab, i = (n - 1) & hash)) == null)
1120 dl 1.27 break;
1121 dl 1.102 else if ((fh = f.hash) == MOVED)
1122     tab = helpTransfer(tab, f);
1123     else {
1124     V oldVal = null;
1125     boolean validated = false;
1126     synchronized (f) {
1127     if (tabAt(tab, i) == f) {
1128     if (fh >= 0) {
1129     validated = true;
1130     for (Node<K,V> e = f, pred = null;;) {
1131     K ek;
1132     if (e.hash == hash &&
1133     ((ek = e.key) == key ||
1134     (ek != null && key.equals(ek)))) {
1135     V ev = e.val;
1136     if (cv == null || cv == ev ||
1137     (ev != null && cv.equals(ev))) {
1138     oldVal = ev;
1139     if (value != null)
1140     e.val = value;
1141     else if (pred != null)
1142     pred.next = e.next;
1143     else
1144     setTabAt(tab, i, e.next);
1145     }
1146     break;
1147     }
1148     pred = e;
1149     if ((e = e.next) == null)
1150     break;
1151     }
1152     }
1153     else if (f instanceof TreeBin) {
1154 dl 1.38 validated = true;
1155 dl 1.102 TreeBin<K,V> t = (TreeBin<K,V>)f;
1156     TreeNode<K,V> r, p;
1157     if ((r = t.root) != null &&
1158     (p = r.findTreeNode(hash, key, null)) != null) {
1159 dl 1.84 V pv = p.val;
1160 dl 1.102 if (cv == null || cv == pv ||
1161     (pv != null && cv.equals(pv))) {
1162 dl 1.38 oldVal = pv;
1163 dl 1.102 if (value != null)
1164     p.val = value;
1165     else if (t.removeTreeNode(p))
1166     setTabAt(tab, i, untreeify(t.first));
1167 dl 1.38 }
1168     }
1169     }
1170     }
1171 dl 1.102 }
1172     if (validated) {
1173     if (oldVal != null) {
1174     if (value == null)
1175 dl 1.82 addCount(-1L, -1);
1176 dl 1.102 return oldVal;
1177 dl 1.38 }
1178 dl 1.102 break;
1179 dl 1.38 }
1180     }
1181 dl 1.102 }
1182     return null;
1183     }
1184    
1185     /**
1186     * Removes all of the mappings from this map.
1187     */
1188     public void clear() {
1189     long delta = 0L; // negative number of deletions
1190     int i = 0;
1191     Node<K,V>[] tab = table;
1192     while (tab != null && i < tab.length) {
1193     int fh;
1194     Node<K,V> f = tabAt(tab, i);
1195     if (f == null)
1196     ++i;
1197     else if ((fh = f.hash) == MOVED) {
1198     tab = helpTransfer(tab, f);
1199     i = 0; // restart
1200     }
1201 dl 1.82 else {
1202 jsr166 1.83 synchronized (f) {
1203 dl 1.27 if (tabAt(tab, i) == f) {
1204 dl 1.102 Node<K,V> p = (fh >= 0 ? f :
1205     (f instanceof TreeBin) ?
1206     ((TreeBin<K,V>)f).first : null);
1207     while (p != null) {
1208     --delta;
1209     p = p.next;
1210 dl 1.27 }
1211 dl 1.102 setTabAt(tab, i++, null);
1212 dl 1.27 }
1213     }
1214     }
1215     }
1216 dl 1.102 if (delta != 0L)
1217     addCount(delta, -1);
1218     }
1219    
1220     /**
1221     * Returns a {@link Set} view of the keys contained in this map.
1222     * The set is backed by the map, so changes to the map are
1223     * reflected in the set, and vice-versa. The set supports element
1224     * removal, which removes the corresponding mapping from this map,
1225     * via the {@code Iterator.remove}, {@code Set.remove},
1226     * {@code removeAll}, {@code retainAll}, and {@code clear}
1227     * operations. It does not support the {@code add} or
1228     * {@code addAll} operations.
1229     *
1230     * <p>The view's {@code iterator} is a "weakly consistent" iterator
1231     * that will never throw {@link ConcurrentModificationException},
1232     * and guarantees to traverse elements as they existed upon
1233     * construction of the iterator, and may (but is not guaranteed to)
1234     * reflect any modifications subsequent to construction.
1235     *
1236     * @return the set view
1237     */
1238     public KeySetView<K,V> keySet() {
1239     KeySetView<K,V> ks;
1240     return (ks = keySet) != null ? ks : (keySet = new KeySetView<K,V>(this, null));
1241     }
1242    
1243     /**
1244     * Returns a {@link Collection} view of the values contained in this map.
1245     * The collection is backed by the map, so changes to the map are
1246     * reflected in the collection, and vice-versa. The collection
1247     * supports element removal, which removes the corresponding
1248     * mapping from this map, via the {@code Iterator.remove},
1249     * {@code Collection.remove}, {@code removeAll},
1250     * {@code retainAll}, and {@code clear} operations. It does not
1251     * support the {@code add} or {@code addAll} operations.
1252     *
1253     * <p>The view's {@code iterator} is a "weakly consistent" iterator
1254     * that will never throw {@link ConcurrentModificationException},
1255     * and guarantees to traverse elements as they existed upon
1256     * construction of the iterator, and may (but is not guaranteed to)
1257     * reflect any modifications subsequent to construction.
1258     *
1259     * @return the collection view
1260     */
1261     public Collection<V> values() {
1262     ValuesView<K,V> vs;
1263     return (vs = values) != null ? vs : (values = new ValuesView<K,V>(this));
1264     }
1265    
1266     /**
1267     * Returns a {@link Set} view of the mappings contained in this map.
1268     * The set is backed by the map, so changes to the map are
1269     * reflected in the set, and vice-versa. The set supports element
1270     * removal, which removes the corresponding mapping from the map,
1271     * via the {@code Iterator.remove}, {@code Set.remove},
1272     * {@code removeAll}, {@code retainAll}, and {@code clear}
1273     * operations.
1274     *
1275     * <p>The view's {@code iterator} is a "weakly consistent" iterator
1276     * that will never throw {@link ConcurrentModificationException},
1277     * and guarantees to traverse elements as they existed upon
1278     * construction of the iterator, and may (but is not guaranteed to)
1279     * reflect any modifications subsequent to construction.
1280     *
1281     * @return the set view
1282     */
1283     public Set<Map.Entry<K,V>> entrySet() {
1284     EntrySetView<K,V> es;
1285     return (es = entrySet) != null ? es : (entrySet = new EntrySetView<K,V>(this));
1286 dl 1.27 }
1287    
1288 dl 1.102 /**
1289     * Returns the hash code value for this {@link Map}, i.e.,
1290     * the sum of, for each key-value pair in the map,
1291     * {@code key.hashCode() ^ value.hashCode()}.
1292     *
1293     * @return the hash code value for this map
1294 dl 1.27 */
1295 dl 1.102 public int hashCode() {
1296     int h = 0;
1297     Node<K,V>[] t;
1298     if ((t = table) != null) {
1299     Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
1300     for (Node<K,V> p; (p = it.advance()) != null; )
1301     h += p.key.hashCode() ^ p.val.hashCode();
1302     }
1303     return h;
1304     }
1305 dl 1.27
1306 dl 1.102 /**
1307     * Returns a string representation of this map. The string
1308     * representation consists of a list of key-value mappings (in no
1309     * particular order) enclosed in braces ("{@code {}}"). Adjacent
1310     * mappings are separated by the characters {@code ", "} (comma
1311     * and space). Each key-value mapping is rendered as the key
1312     * followed by an equals sign ("{@code =}") followed by the
1313     * associated value.
1314     *
1315     * @return a string representation of this map
1316     */
1317     public String toString() {
1318     Node<K,V>[] t;
1319     int f = (t = table) == null ? 0 : t.length;
1320     Traverser<K,V> it = new Traverser<K,V>(t, f, 0, f);
1321     StringBuilder sb = new StringBuilder();
1322     sb.append('{');
1323     Node<K,V> p;
1324     if ((p = it.advance()) != null) {
1325     for (;;) {
1326     K k = p.key;
1327     V v = p.val;
1328     sb.append(k == this ? "(this Map)" : k);
1329     sb.append('=');
1330     sb.append(v == this ? "(this Map)" : v);
1331     if ((p = it.advance()) == null)
1332     break;
1333     sb.append(',').append(' ');
1334 dl 1.14 }
1335 dl 1.102 }
1336     return sb.append('}').toString();
1337     }
1338    
1339     /**
1340     * Compares the specified object with this map for equality.
1341     * Returns {@code true} if the given object is a map with the same
1342     * mappings as this map. This operation may return misleading
1343     * results if either map is concurrently modified during execution
1344     * of this method.
1345     *
1346     * @param o object to be compared for equality with this map
1347     * @return {@code true} if the specified object is equal to this map
1348     */
1349     public boolean equals(Object o) {
1350     if (o != this) {
1351     if (!(o instanceof Map))
1352     return false;
1353     Map<?,?> m = (Map<?,?>) o;
1354     Node<K,V>[] t;
1355     int f = (t = table) == null ? 0 : t.length;
1356     Traverser<K,V> it = new Traverser<K,V>(t, f, 0, f);
1357     for (Node<K,V> p; (p = it.advance()) != null; ) {
1358     V val = p.val;
1359     Object v = m.get(p.key);
1360     if (v == null || (v != val && !v.equals(val)))
1361     return false;
1362 dl 1.38 }
1363 dl 1.102 for (Map.Entry<?,?> e : m.entrySet()) {
1364     Object mk, mv, v;
1365     if ((mk = e.getKey()) == null ||
1366     (mv = e.getValue()) == null ||
1367     (v = get(mk)) == null ||
1368     (mv != v && !mv.equals(v)))
1369     return false;
1370 dl 1.1 }
1371     }
1372 dl 1.102 return true;
1373     }
1374    
1375     /**
1376     * Stripped-down version of helper class used in previous version,
1377     * declared for the sake of serialization compatibility
1378     */
1379     static class Segment<K,V> extends ReentrantLock implements Serializable {
1380     private static final long serialVersionUID = 2249069246763182397L;
1381     final float loadFactor;
1382     Segment(float lf) { this.loadFactor = lf; }
1383 dl 1.27 }
1384    
1385 dl 1.102 /**
1386     * Saves the state of the {@code ConcurrentHashMapV8} instance to a
1387     * stream (i.e., serializes it).
1388     * @param s the stream
1389 jsr166 1.113 * @throws java.io.IOException if an I/O error occurs
1390 dl 1.102 * @serialData
1391     * the key (Object) and value (Object)
1392     * for each key-value mapping, followed by a null pair.
1393     * The key-value mappings are emitted in no particular order.
1394     */
1395     private void writeObject(java.io.ObjectOutputStream s)
1396     throws java.io.IOException {
1397     // For serialization compatibility
1398     // Emulate segment calculation from previous version of this class
1399     int sshift = 0;
1400     int ssize = 1;
1401     while (ssize < DEFAULT_CONCURRENCY_LEVEL) {
1402     ++sshift;
1403     ssize <<= 1;
1404     }
1405     int segmentShift = 32 - sshift;
1406     int segmentMask = ssize - 1;
1407     @SuppressWarnings("unchecked") Segment<K,V>[] segments = (Segment<K,V>[])
1408     new Segment<?,?>[DEFAULT_CONCURRENCY_LEVEL];
1409     for (int i = 0; i < segments.length; ++i)
1410     segments[i] = new Segment<K,V>(LOAD_FACTOR);
1411     s.putFields().put("segments", segments);
1412     s.putFields().put("segmentShift", segmentShift);
1413     s.putFields().put("segmentMask", segmentMask);
1414     s.writeFields();
1415    
1416     Node<K,V>[] t;
1417     if ((t = table) != null) {
1418     Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
1419     for (Node<K,V> p; (p = it.advance()) != null; ) {
1420     s.writeObject(p.key);
1421     s.writeObject(p.val);
1422 dl 1.27 }
1423 dl 1.102 }
1424     s.writeObject(null);
1425     s.writeObject(null);
1426     segments = null; // throw away
1427     }
1428    
1429     /**
1430     * Reconstitutes the instance from a stream (that is, deserializes it).
1431     * @param s the stream
1432 jsr166 1.113 * @throws ClassNotFoundException if the class of a serialized object
1433     * could not be found
1434     * @throws java.io.IOException if an I/O error occurs
1435 dl 1.102 */
1436     private void readObject(java.io.ObjectInputStream s)
1437     throws java.io.IOException, ClassNotFoundException {
1438     /*
1439     * To improve performance in typical cases, we create nodes
1440     * while reading, then place in table once size is known.
1441     * However, we must also validate uniqueness and deal with
1442     * overpopulated bins while doing so, which requires
1443     * specialized versions of putVal mechanics.
1444     */
1445     sizeCtl = -1; // force exclusion for table construction
1446     s.defaultReadObject();
1447     long size = 0L;
1448     Node<K,V> p = null;
1449     for (;;) {
1450     @SuppressWarnings("unchecked") K k = (K) s.readObject();
1451     @SuppressWarnings("unchecked") V v = (V) s.readObject();
1452     if (k != null && v != null) {
1453     p = new Node<K,V>(spread(k.hashCode()), k, v, p);
1454     ++size;
1455 dl 1.38 }
1456 dl 1.102 else
1457     break;
1458     }
1459     if (size == 0L)
1460     sizeCtl = 0;
1461     else {
1462     int n;
1463     if (size >= (long)(MAXIMUM_CAPACITY >>> 1))
1464     n = MAXIMUM_CAPACITY;
1465 dl 1.27 else {
1466 dl 1.102 int sz = (int)size;
1467     n = tableSizeFor(sz + (sz >>> 1) + 1);
1468     }
1469 jsr166 1.115 @SuppressWarnings("unchecked")
1470     Node<K,V>[] tab = (Node<K,V>[])new Node<?,?>[n];
1471 dl 1.102 int mask = n - 1;
1472     long added = 0L;
1473     while (p != null) {
1474     boolean insertAtFront;
1475     Node<K,V> next = p.next, first;
1476     int h = p.hash, j = h & mask;
1477     if ((first = tabAt(tab, j)) == null)
1478     insertAtFront = true;
1479     else {
1480     K k = p.key;
1481     if (first.hash < 0) {
1482     TreeBin<K,V> t = (TreeBin<K,V>)first;
1483     if (t.putTreeVal(h, k, p.val) == null)
1484     ++added;
1485     insertAtFront = false;
1486     }
1487     else {
1488     int binCount = 0;
1489     insertAtFront = true;
1490     Node<K,V> q; K qk;
1491     for (q = first; q != null; q = q.next) {
1492     if (q.hash == h &&
1493     ((qk = q.key) == k ||
1494     (qk != null && k.equals(qk)))) {
1495     insertAtFront = false;
1496 dl 1.82 break;
1497     }
1498 dl 1.102 ++binCount;
1499     }
1500     if (insertAtFront && binCount >= TREEIFY_THRESHOLD) {
1501     insertAtFront = false;
1502     ++added;
1503     p.next = first;
1504     TreeNode<K,V> hd = null, tl = null;
1505     for (q = p; q != null; q = q.next) {
1506     TreeNode<K,V> t = new TreeNode<K,V>
1507     (q.hash, q.key, q.val, null, null);
1508     if ((t.prev = tl) == null)
1509     hd = t;
1510     else
1511     tl.next = t;
1512     tl = t;
1513 dl 1.27 }
1514 dl 1.102 setTabAt(tab, j, new TreeBin<K,V>(hd));
1515 dl 1.27 }
1516 dl 1.38 }
1517 dl 1.82 }
1518 dl 1.102 if (insertAtFront) {
1519     ++added;
1520     p.next = first;
1521     setTabAt(tab, j, p);
1522     }
1523     p = next;
1524     }
1525     table = tab;
1526     sizeCtl = n - (n >>> 2);
1527     baseCount = added;
1528     }
1529     }
1530    
1531     // ConcurrentMap methods
1532    
1533     /**
1534     * {@inheritDoc}
1535     *
1536     * @return the previous value associated with the specified key,
1537     * or {@code null} if there was no mapping for the key
1538     * @throws NullPointerException if the specified key or value is null
1539     */
1540     public V putIfAbsent(K key, V value) {
1541     return putVal(key, value, true);
1542     }
1543    
1544     /**
1545     * {@inheritDoc}
1546     *
1547     * @throws NullPointerException if the specified key is null
1548     */
1549     public boolean remove(Object key, Object value) {
1550     if (key == null)
1551     throw new NullPointerException();
1552     return value != null && replaceNode(key, null, value) != null;
1553     }
1554    
1555     /**
1556     * {@inheritDoc}
1557     *
1558     * @throws NullPointerException if any of the arguments are null
1559     */
1560     public boolean replace(K key, V oldValue, V newValue) {
1561     if (key == null || oldValue == null || newValue == null)
1562     throw new NullPointerException();
1563     return replaceNode(key, newValue, oldValue) != null;
1564     }
1565    
1566     /**
1567     * {@inheritDoc}
1568     *
1569     * @return the previous value associated with the specified key,
1570     * or {@code null} if there was no mapping for the key
1571     * @throws NullPointerException if the specified key or value is null
1572     */
1573     public V replace(K key, V value) {
1574     if (key == null || value == null)
1575     throw new NullPointerException();
1576     return replaceNode(key, value, null);
1577     }
1578    
1579     // Overrides of JDK8+ Map extension method defaults
1580    
1581     /**
1582     * Returns the value to which the specified key is mapped, or the
1583     * given default value if this map contains no mapping for the
1584     * key.
1585     *
1586     * @param key the key whose associated value is to be returned
1587     * @param defaultValue the value to return if this map contains
1588     * no mapping for the given key
1589     * @return the mapping for the key, if present; else the default value
1590     * @throws NullPointerException if the specified key is null
1591     */
1592     public V getOrDefault(Object key, V defaultValue) {
1593     V v;
1594     return (v = get(key)) == null ? defaultValue : v;
1595     }
1596    
1597     public void forEach(BiAction<? super K, ? super V> action) {
1598     if (action == null) throw new NullPointerException();
1599     Node<K,V>[] t;
1600     if ((t = table) != null) {
1601     Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
1602     for (Node<K,V> p; (p = it.advance()) != null; ) {
1603     action.apply(p.key, p.val);
1604     }
1605     }
1606     }
1607    
1608     public void replaceAll(BiFun<? super K, ? super V, ? extends V> function) {
1609     if (function == null) throw new NullPointerException();
1610     Node<K,V>[] t;
1611     if ((t = table) != null) {
1612     Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
1613     for (Node<K,V> p; (p = it.advance()) != null; ) {
1614     V oldValue = p.val;
1615     for (K key = p.key;;) {
1616     V newValue = function.apply(key, oldValue);
1617     if (newValue == null)
1618     throw new NullPointerException();
1619     if (replaceNode(key, newValue, oldValue) != null ||
1620     (oldValue = get(key)) == null)
1621     break;
1622 dl 1.1 }
1623     }
1624     }
1625     }
1626    
1627 dl 1.102 /**
1628     * If the specified key is not already associated with a value,
1629     * attempts to compute its value using the given mapping function
1630     * and enters it into this map unless {@code null}. The entire
1631     * method invocation is performed atomically, so the function is
1632     * applied at most once per key. Some attempted update operations
1633     * on this map by other threads may be blocked while computation
1634     * is in progress, so the computation should be short and simple,
1635     * and must not attempt to update any other mappings of this map.
1636     *
1637     * @param key key with which the specified value is to be associated
1638     * @param mappingFunction the function to compute a value
1639     * @return the current (existing or computed) value associated with
1640     * the specified key, or null if the computed value is null
1641     * @throws NullPointerException if the specified key or mappingFunction
1642     * is null
1643     * @throws IllegalStateException if the computation detectably
1644     * attempts a recursive update to this map that would
1645     * otherwise never complete
1646     * @throws RuntimeException or Error if the mappingFunction does so,
1647     * in which case the mapping is left unestablished
1648     */
1649     public V computeIfAbsent(K key, Fun<? super K, ? extends V> mappingFunction) {
1650     if (key == null || mappingFunction == null)
1651 dl 1.82 throw new NullPointerException();
1652 dl 1.102 int h = spread(key.hashCode());
1653 dl 1.84 V val = null;
1654 dl 1.102 int binCount = 0;
1655     for (Node<K,V>[] tab = table;;) {
1656     Node<K,V> f; int n, i, fh;
1657     if (tab == null || (n = tab.length) == 0)
1658 dl 1.24 tab = initTable();
1659 dl 1.102 else if ((f = tabAt(tab, i = (n - 1) & h)) == null) {
1660     Node<K,V> r = new ReservationNode<K,V>();
1661     synchronized (r) {
1662     if (casTabAt(tab, i, null, r)) {
1663     binCount = 1;
1664     Node<K,V> node = null;
1665 dl 1.82 try {
1666 dl 1.102 if ((val = mappingFunction.apply(key)) != null)
1667     node = new Node<K,V>(h, key, val, null);
1668 dl 1.82 } finally {
1669 dl 1.102 setTabAt(tab, i, node);
1670 dl 1.1 }
1671     }
1672     }
1673 dl 1.102 if (binCount != 0)
1674 dl 1.10 break;
1675 dl 1.1 }
1676 dl 1.102 else if ((fh = f.hash) == MOVED)
1677     tab = helpTransfer(tab, f);
1678     else {
1679     boolean added = false;
1680     synchronized (f) {
1681     if (tabAt(tab, i) == f) {
1682     if (fh >= 0) {
1683     binCount = 1;
1684     for (Node<K,V> e = f;; ++binCount) {
1685     K ek; V ev;
1686     if (e.hash == h &&
1687     ((ek = e.key) == key ||
1688     (ek != null && key.equals(ek)))) {
1689     val = e.val;
1690     break;
1691     }
1692     Node<K,V> pred = e;
1693     if ((e = e.next) == null) {
1694     if ((val = mappingFunction.apply(key)) != null) {
1695     added = true;
1696     pred.next = new Node<K,V>(h, key, val, null);
1697     }
1698     break;
1699 dl 1.38 }
1700     }
1701 dl 1.102 }
1702     else if (f instanceof TreeBin) {
1703     binCount = 2;
1704     TreeBin<K,V> t = (TreeBin<K,V>)f;
1705     TreeNode<K,V> r, p;
1706     if ((r = t.root) != null &&
1707     (p = r.findTreeNode(h, key, null)) != null)
1708     val = p.val;
1709     else if ((val = mappingFunction.apply(key)) != null) {
1710     added = true;
1711     t.putTreeVal(h, key, val);
1712 dl 1.41 }
1713 dl 1.38 }
1714     }
1715     }
1716 dl 1.102 if (binCount != 0) {
1717     if (binCount >= TREEIFY_THRESHOLD)
1718     treeifyBin(tab, i);
1719     if (!added)
1720     return val;
1721     break;
1722     }
1723 dl 1.38 }
1724 dl 1.102 }
1725     if (val != null)
1726     addCount(1L, binCount);
1727     return val;
1728     }
1729    
1730     /**
1731     * If the value for the specified key is present, attempts to
1732     * compute a new mapping given the key and its current mapped
1733     * value. The entire method invocation is performed atomically.
1734     * Some attempted update operations on this map by other threads
1735     * may be blocked while computation is in progress, so the
1736     * computation should be short and simple, and must not attempt to
1737     * update any other mappings of this map.
1738     *
1739     * @param key key with which a value may be associated
1740     * @param remappingFunction the function to compute a value
1741     * @return the new value associated with the specified key, or null if none
1742     * @throws NullPointerException if the specified key or remappingFunction
1743     * is null
1744     * @throws IllegalStateException if the computation detectably
1745     * attempts a recursive update to this map that would
1746     * otherwise never complete
1747     * @throws RuntimeException or Error if the remappingFunction does so,
1748     * in which case the mapping is unchanged
1749     */
1750     public V computeIfPresent(K key, BiFun<? super K, ? super V, ? extends V> remappingFunction) {
1751     if (key == null || remappingFunction == null)
1752     throw new NullPointerException();
1753     int h = spread(key.hashCode());
1754     V val = null;
1755     int delta = 0;
1756     int binCount = 0;
1757     for (Node<K,V>[] tab = table;;) {
1758     Node<K,V> f; int n, i, fh;
1759     if (tab == null || (n = tab.length) == 0)
1760     tab = initTable();
1761     else if ((f = tabAt(tab, i = (n - 1) & h)) == null)
1762     break;
1763     else if ((fh = f.hash) == MOVED)
1764     tab = helpTransfer(tab, f);
1765 dl 1.82 else {
1766 jsr166 1.83 synchronized (f) {
1767 dl 1.24 if (tabAt(tab, i) == f) {
1768 dl 1.102 if (fh >= 0) {
1769     binCount = 1;
1770     for (Node<K,V> e = f, pred = null;; ++binCount) {
1771     K ek;
1772     if (e.hash == h &&
1773     ((ek = e.key) == key ||
1774     (ek != null && key.equals(ek)))) {
1775     val = remappingFunction.apply(key, e.val);
1776     if (val != null)
1777     e.val = val;
1778     else {
1779     delta = -1;
1780     Node<K,V> en = e.next;
1781     if (pred != null)
1782     pred.next = en;
1783     else
1784     setTabAt(tab, i, en);
1785     }
1786     break;
1787     }
1788     pred = e;
1789     if ((e = e.next) == null)
1790     break;
1791     }
1792     }
1793     else if (f instanceof TreeBin) {
1794     binCount = 2;
1795     TreeBin<K,V> t = (TreeBin<K,V>)f;
1796     TreeNode<K,V> r, p;
1797     if ((r = t.root) != null &&
1798     (p = r.findTreeNode(h, key, null)) != null) {
1799     val = remappingFunction.apply(key, p.val);
1800 dl 1.27 if (val != null)
1801 dl 1.102 p.val = val;
1802 dl 1.41 else {
1803     delta = -1;
1804 dl 1.102 if (t.removeTreeNode(p))
1805     setTabAt(tab, i, untreeify(t.first));
1806 dl 1.1 }
1807     }
1808     }
1809     }
1810     }
1811 dl 1.102 if (binCount != 0)
1812 dl 1.10 break;
1813 dl 1.1 }
1814 dl 1.10 }
1815 dl 1.82 if (delta != 0)
1816 dl 1.102 addCount((long)delta, binCount);
1817 dl 1.84 return val;
1818 dl 1.1 }
1819    
1820 dl 1.102 /**
1821     * Attempts to compute a mapping for the specified key and its
1822     * current mapped value (or {@code null} if there is no current
1823     * mapping). The entire method invocation is performed atomically.
1824     * Some attempted update operations on this map by other threads
1825     * may be blocked while computation is in progress, so the
1826     * computation should be short and simple, and must not attempt to
1827     * update any other mappings of this Map.
1828     *
1829     * @param key key with which the specified value is to be associated
1830     * @param remappingFunction the function to compute a value
1831     * @return the new value associated with the specified key, or null if none
1832     * @throws NullPointerException if the specified key or remappingFunction
1833     * is null
1834     * @throws IllegalStateException if the computation detectably
1835     * attempts a recursive update to this map that would
1836     * otherwise never complete
1837     * @throws RuntimeException or Error if the remappingFunction does so,
1838     * in which case the mapping is unchanged
1839     */
1840     public V compute(K key,
1841     BiFun<? super K, ? super V, ? extends V> remappingFunction) {
1842     if (key == null || remappingFunction == null)
1843 dl 1.82 throw new NullPointerException();
1844 dl 1.102 int h = spread(key.hashCode());
1845 dl 1.84 V val = null;
1846 dl 1.52 int delta = 0;
1847 dl 1.102 int binCount = 0;
1848     for (Node<K,V>[] tab = table;;) {
1849     Node<K,V> f; int n, i, fh;
1850     if (tab == null || (n = tab.length) == 0)
1851 dl 1.52 tab = initTable();
1852 dl 1.102 else if ((f = tabAt(tab, i = (n - 1) & h)) == null) {
1853     Node<K,V> r = new ReservationNode<K,V>();
1854     synchronized (r) {
1855     if (casTabAt(tab, i, null, r)) {
1856     binCount = 1;
1857     Node<K,V> node = null;
1858     try {
1859     if ((val = remappingFunction.apply(key, null)) != null) {
1860     delta = 1;
1861     node = new Node<K,V>(h, key, val, null);
1862     }
1863     } finally {
1864     setTabAt(tab, i, node);
1865     }
1866     }
1867     }
1868     if (binCount != 0)
1869 dl 1.52 break;
1870     }
1871 dl 1.102 else if ((fh = f.hash) == MOVED)
1872     tab = helpTransfer(tab, f);
1873     else {
1874     synchronized (f) {
1875     if (tabAt(tab, i) == f) {
1876     if (fh >= 0) {
1877     binCount = 1;
1878     for (Node<K,V> e = f, pred = null;; ++binCount) {
1879     K ek;
1880     if (e.hash == h &&
1881     ((ek = e.key) == key ||
1882     (ek != null && key.equals(ek)))) {
1883     val = remappingFunction.apply(key, e.val);
1884     if (val != null)
1885     e.val = val;
1886     else {
1887     delta = -1;
1888     Node<K,V> en = e.next;
1889     if (pred != null)
1890     pred.next = en;
1891     else
1892     setTabAt(tab, i, en);
1893     }
1894     break;
1895     }
1896     pred = e;
1897     if ((e = e.next) == null) {
1898     val = remappingFunction.apply(key, null);
1899     if (val != null) {
1900     delta = 1;
1901     pred.next =
1902     new Node<K,V>(h, key, val, null);
1903     }
1904     break;
1905     }
1906     }
1907     }
1908     else if (f instanceof TreeBin) {
1909     binCount = 1;
1910     TreeBin<K,V> t = (TreeBin<K,V>)f;
1911     TreeNode<K,V> r, p;
1912     if ((r = t.root) != null)
1913     p = r.findTreeNode(h, key, null);
1914     else
1915     p = null;
1916     V pv = (p == null) ? null : p.val;
1917     val = remappingFunction.apply(key, pv);
1918 dl 1.52 if (val != null) {
1919     if (p != null)
1920     p.val = val;
1921     else {
1922     delta = 1;
1923 dl 1.102 t.putTreeVal(h, key, val);
1924 dl 1.52 }
1925     }
1926     else if (p != null) {
1927     delta = -1;
1928 dl 1.102 if (t.removeTreeNode(p))
1929     setTabAt(tab, i, untreeify(t.first));
1930 dl 1.52 }
1931     }
1932     }
1933     }
1934 dl 1.102 if (binCount != 0) {
1935     if (binCount >= TREEIFY_THRESHOLD)
1936     treeifyBin(tab, i);
1937     break;
1938 dl 1.52 }
1939     }
1940     }
1941 dl 1.82 if (delta != 0)
1942 dl 1.102 addCount((long)delta, binCount);
1943 dl 1.84 return val;
1944 dl 1.52 }
1945    
1946 dl 1.102 /**
1947     * If the specified key is not already associated with a
1948     * (non-null) value, associates it with the given value.
1949     * Otherwise, replaces the value with the results of the given
1950     * remapping function, or removes if {@code null}. The entire
1951     * method invocation is performed atomically. Some attempted
1952     * update operations on this map by other threads may be blocked
1953     * while computation is in progress, so the computation should be
1954     * short and simple, and must not attempt to update any other
1955     * mappings of this Map.
1956     *
1957     * @param key key with which the specified value is to be associated
1958     * @param value the value to use if absent
1959     * @param remappingFunction the function to recompute a value if present
1960     * @return the new value associated with the specified key, or null if none
1961     * @throws NullPointerException if the specified key or the
1962     * remappingFunction is null
1963     * @throws RuntimeException or Error if the remappingFunction does so,
1964     * in which case the mapping is unchanged
1965     */
1966     public V merge(K key, V value, BiFun<? super V, ? super V, ? extends V> remappingFunction) {
1967     if (key == null || value == null || remappingFunction == null)
1968     throw new NullPointerException();
1969     int h = spread(key.hashCode());
1970     V val = null;
1971     int delta = 0;
1972     int binCount = 0;
1973     for (Node<K,V>[] tab = table;;) {
1974     Node<K,V> f; int n, i, fh;
1975     if (tab == null || (n = tab.length) == 0)
1976     tab = initTable();
1977     else if ((f = tabAt(tab, i = (n - 1) & h)) == null) {
1978     if (casTabAt(tab, i, null, new Node<K,V>(h, key, value, null))) {
1979     delta = 1;
1980     val = value;
1981 dl 1.27 break;
1982     }
1983 dl 1.102 }
1984     else if ((fh = f.hash) == MOVED)
1985     tab = helpTransfer(tab, f);
1986     else {
1987     synchronized (f) {
1988     if (tabAt(tab, i) == f) {
1989     if (fh >= 0) {
1990     binCount = 1;
1991     for (Node<K,V> e = f, pred = null;; ++binCount) {
1992     K ek;
1993     if (e.hash == h &&
1994     ((ek = e.key) == key ||
1995     (ek != null && key.equals(ek)))) {
1996     val = remappingFunction.apply(e.val, value);
1997     if (val != null)
1998     e.val = val;
1999 dl 1.38 else {
2000 dl 1.102 delta = -1;
2001     Node<K,V> en = e.next;
2002     if (pred != null)
2003     pred.next = en;
2004     else
2005     setTabAt(tab, i, en);
2006 dl 1.38 }
2007 dl 1.102 break;
2008     }
2009     pred = e;
2010     if ((e = e.next) == null) {
2011     delta = 1;
2012     val = value;
2013     pred.next =
2014     new Node<K,V>(h, key, val, null);
2015     break;
2016 dl 1.38 }
2017     }
2018     }
2019 dl 1.102 else if (f instanceof TreeBin) {
2020     binCount = 2;
2021     TreeBin<K,V> t = (TreeBin<K,V>)f;
2022     TreeNode<K,V> r = t.root;
2023     TreeNode<K,V> p = (r == null) ? null :
2024     r.findTreeNode(h, key, null);
2025     val = (p == null) ? value :
2026     remappingFunction.apply(p.val, value);
2027     if (val != null) {
2028     if (p != null)
2029     p.val = val;
2030     else {
2031     delta = 1;
2032     t.putTreeVal(h, key, val);
2033 dl 1.27 }
2034     }
2035 dl 1.102 else if (p != null) {
2036     delta = -1;
2037     if (t.removeTreeNode(p))
2038     setTabAt(tab, i, untreeify(t.first));
2039 dl 1.90 }
2040 dl 1.24 }
2041     }
2042 dl 1.1 }
2043 dl 1.102 if (binCount != 0) {
2044     if (binCount >= TREEIFY_THRESHOLD)
2045     treeifyBin(tab, i);
2046     break;
2047     }
2048 dl 1.1 }
2049     }
2050 dl 1.102 if (delta != 0)
2051     addCount((long)delta, binCount);
2052     return val;
2053 dl 1.1 }
2054    
2055 dl 1.102 // Hashtable legacy methods
2056    
2057 dl 1.82 /**
2058 dl 1.102 * Legacy method testing if some key maps into the specified value
2059     * in this table. This method is identical in functionality to
2060     * {@link #containsValue(Object)}, and exists solely to ensure
2061     * full compatibility with class {@link java.util.Hashtable},
2062     * which supported this method prior to introduction of the
2063     * Java Collections framework.
2064     *
2065     * @param value a value to search for
2066     * @return {@code true} if and only if some key maps to the
2067     * {@code value} argument in this table as
2068     * determined by the {@code equals} method;
2069     * {@code false} otherwise
2070     * @throws NullPointerException if the specified value is null
2071     */
2072     @Deprecated public boolean contains(Object value) {
2073     return containsValue(value);
2074     }
2075    
2076     /**
2077     * Returns an enumeration of the keys in this table.
2078     *
2079     * @return an enumeration of the keys in this table
2080     * @see #keySet()
2081     */
2082     public Enumeration<K> keys() {
2083     Node<K,V>[] t;
2084     int f = (t = table) == null ? 0 : t.length;
2085     return new KeyIterator<K,V>(t, f, 0, f, this);
2086     }
2087    
2088     /**
2089     * Returns an enumeration of the values in this table.
2090     *
2091     * @return an enumeration of the values in this table
2092     * @see #values()
2093     */
2094     public Enumeration<V> elements() {
2095     Node<K,V>[] t;
2096     int f = (t = table) == null ? 0 : t.length;
2097     return new ValueIterator<K,V>(t, f, 0, f, this);
2098     }
2099    
2100     // ConcurrentHashMapV8-only methods
2101    
2102     /**
2103     * Returns the number of mappings. This method should be used
2104     * instead of {@link #size} because a ConcurrentHashMapV8 may
2105     * contain more mappings than can be represented as an int. The
2106     * value returned is an estimate; the actual count may differ if
2107     * there are concurrent insertions or removals.
2108     *
2109     * @return the number of mappings
2110     * @since 1.8
2111     */
2112     public long mappingCount() {
2113     long n = sumCount();
2114     return (n < 0L) ? 0L : n; // ignore transient negative values
2115     }
2116    
2117     /**
2118     * Creates a new {@link Set} backed by a ConcurrentHashMapV8
2119     * from the given type to {@code Boolean.TRUE}.
2120     *
2121     * @return the new set
2122     * @since 1.8
2123     */
2124     public static <K> KeySetView<K,Boolean> newKeySet() {
2125     return new KeySetView<K,Boolean>
2126     (new ConcurrentHashMapV8<K,Boolean>(), Boolean.TRUE);
2127     }
2128    
2129     /**
2130     * Creates a new {@link Set} backed by a ConcurrentHashMapV8
2131     * from the given type to {@code Boolean.TRUE}.
2132     *
2133     * @param initialCapacity The implementation performs internal
2134     * sizing to accommodate this many elements.
2135 jsr166 1.111 * @return the new set
2136 dl 1.102 * @throws IllegalArgumentException if the initial capacity of
2137     * elements is negative
2138     * @since 1.8
2139     */
2140     public static <K> KeySetView<K,Boolean> newKeySet(int initialCapacity) {
2141     return new KeySetView<K,Boolean>
2142     (new ConcurrentHashMapV8<K,Boolean>(initialCapacity), Boolean.TRUE);
2143     }
2144    
2145     /**
2146     * Returns a {@link Set} view of the keys in this map, using the
2147     * given common mapped value for any additions (i.e., {@link
2148     * Collection#add} and {@link Collection#addAll(Collection)}).
2149     * This is of course only appropriate if it is acceptable to use
2150     * the same value for all additions from this view.
2151     *
2152     * @param mappedValue the mapped value to use for any additions
2153     * @return the set view
2154     * @throws NullPointerException if the mappedValue is null
2155 dl 1.82 */
2156 dl 1.102 public KeySetView<K,V> keySet(V mappedValue) {
2157     if (mappedValue == null)
2158     throw new NullPointerException();
2159     return new KeySetView<K,V>(this, mappedValue);
2160     }
2161    
2162     /* ---------------- Special Nodes -------------- */
2163    
2164     /**
2165     * A node inserted at head of bins during transfer operations.
2166     */
2167     static final class ForwardingNode<K,V> extends Node<K,V> {
2168     final Node<K,V>[] nextTable;
2169     ForwardingNode(Node<K,V>[] tab) {
2170     super(MOVED, null, null, null);
2171     this.nextTable = tab;
2172     }
2173    
2174     Node<K,V> find(int h, Object k) {
2175 dl 1.110 // loop to avoid arbitrarily deep recursion on forwarding nodes
2176     outer: for (Node<K,V>[] tab = nextTable;;) {
2177     Node<K,V> e; int n;
2178     if (k == null || tab == null || (n = tab.length) == 0 ||
2179     (e = tabAt(tab, (n - 1) & h)) == null)
2180     return null;
2181     for (;;) {
2182 dl 1.102 int eh; K ek;
2183     if ((eh = e.hash) == h &&
2184     ((ek = e.key) == k || (ek != null && k.equals(ek))))
2185     return e;
2186 dl 1.110 if (eh < 0) {
2187     if (e instanceof ForwardingNode) {
2188     tab = ((ForwardingNode<K,V>)e).nextTable;
2189     continue outer;
2190     }
2191     else
2192     return e.find(h, k);
2193     }
2194     if ((e = e.next) == null)
2195     return null;
2196     }
2197 dl 1.82 }
2198     }
2199     }
2200    
2201 dl 1.24 /**
2202 dl 1.102 * A place-holder node used in computeIfAbsent and compute
2203 dl 1.24 */
2204 dl 1.102 static final class ReservationNode<K,V> extends Node<K,V> {
2205     ReservationNode() {
2206     super(RESERVED, null, null, null);
2207     }
2208    
2209     Node<K,V> find(int h, Object k) {
2210     return null;
2211     }
2212 dl 1.24 }
2213    
2214 dl 1.102 /* ---------------- Table Initialization and Resizing -------------- */
2215    
2216 dl 1.24 /**
2217 dl 1.117 * Returns the stamp bits for resizing a table of size n.
2218     * Must be negative when shifted left by RESIZE_STAMP_SHIFT.
2219     */
2220     static final int resizeStamp(int n) {
2221 jsr166 1.119 return Integer.numberOfLeadingZeros(n) | (1 << (RESIZE_STAMP_BITS - 1));
2222 dl 1.117 }
2223    
2224     /**
2225 dl 1.24 * Initializes table, using the size recorded in sizeCtl.
2226     */
2227 dl 1.102 private final Node<K,V>[] initTable() {
2228     Node<K,V>[] tab; int sc;
2229     while ((tab = table) == null || tab.length == 0) {
2230 dl 1.24 if ((sc = sizeCtl) < 0)
2231     Thread.yield(); // lost initialization race; just spin
2232 dl 1.82 else if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {
2233 dl 1.24 try {
2234 dl 1.102 if ((tab = table) == null || tab.length == 0) {
2235 dl 1.24 int n = (sc > 0) ? sc : DEFAULT_CAPACITY;
2236 jsr166 1.115 @SuppressWarnings("unchecked")
2237 dl 1.117 Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n];
2238 dl 1.102 table = tab = nt;
2239 dl 1.27 sc = n - (n >>> 2);
2240 dl 1.24 }
2241     } finally {
2242     sizeCtl = sc;
2243     }
2244     break;
2245     }
2246     }
2247     return tab;
2248     }
2249    
2250     /**
2251 dl 1.82 * Adds to count, and if table is too small and not already
2252     * resizing, initiates transfer. If already resizing, helps
2253     * perform transfer if work is available. Rechecks occupancy
2254     * after a transfer to see if another resize is already needed
2255     * because resizings are lagging additions.
2256     *
2257     * @param x the count to add
2258     * @param check if <0, don't check resize, if <= 1 only check if uncontended
2259     */
2260     private final void addCount(long x, int check) {
2261     CounterCell[] as; long b, s;
2262     if ((as = counterCells) != null ||
2263     !U.compareAndSwapLong(this, BASECOUNT, b = baseCount, s = b + x)) {
2264     CounterHashCode hc; CounterCell a; long v; int m;
2265     boolean uncontended = true;
2266     if ((hc = threadCounterHashCode.get()) == null ||
2267     as == null || (m = as.length - 1) < 0 ||
2268     (a = as[m & hc.code]) == null ||
2269     !(uncontended =
2270     U.compareAndSwapLong(a, CELLVALUE, v = a.value, v + x))) {
2271     fullAddCount(x, hc, uncontended);
2272     return;
2273     }
2274     if (check <= 1)
2275     return;
2276     s = sumCount();
2277     }
2278     if (check >= 0) {
2279 dl 1.117 Node<K,V>[] tab, nt; int n, sc;
2280 dl 1.82 while (s >= (long)(sc = sizeCtl) && (tab = table) != null &&
2281 dl 1.117 (n = tab.length) < MAXIMUM_CAPACITY) {
2282     int rs = resizeStamp(n);
2283 dl 1.82 if (sc < 0) {
2284 dl 1.117 if ((sc >>> RESIZE_STAMP_SHIFT) != rs || sc == rs + 1 ||
2285 jsr166 1.119 sc == rs + MAX_RESIZERS || (nt = nextTable) == null ||
2286     transferIndex <= 0)
2287 dl 1.82 break;
2288 dl 1.117 if (U.compareAndSwapInt(this, SIZECTL, sc, sc + 1))
2289 dl 1.82 transfer(tab, nt);
2290 dl 1.24 }
2291 dl 1.117 else if (U.compareAndSwapInt(this, SIZECTL, sc,
2292 jsr166 1.119 (rs << RESIZE_STAMP_SHIFT) + 2))
2293 dl 1.82 transfer(tab, null);
2294     s = sumCount();
2295 dl 1.24 }
2296     }
2297     }
2298    
2299 dl 1.27 /**
2300 dl 1.102 * Helps transfer if a resize is in progress.
2301     */
2302     final Node<K,V>[] helpTransfer(Node<K,V>[] tab, Node<K,V> f) {
2303     Node<K,V>[] nextTab; int sc;
2304 dl 1.117 if (tab != null && (f instanceof ForwardingNode) &&
2305 dl 1.102 (nextTab = ((ForwardingNode<K,V>)f).nextTable) != null) {
2306 jsr166 1.119 int rs = resizeStamp(tab.length);
2307 dl 1.117 while (nextTab == nextTable && table == tab &&
2308 jsr166 1.119 (sc = sizeCtl) < 0) {
2309     if ((sc >>> RESIZE_STAMP_SHIFT) != rs || sc == rs + 1 ||
2310 dl 1.117 sc == rs + MAX_RESIZERS || transferIndex <= 0)
2311 jsr166 1.119 break;
2312     if (U.compareAndSwapInt(this, SIZECTL, sc, sc + 1)) {
2313 dl 1.116 transfer(tab, nextTab);
2314     break;
2315     }
2316     }
2317 dl 1.102 return nextTab;
2318     }
2319     return table;
2320     }
2321    
2322     /**
2323 dl 1.27 * Tries to presize table to accommodate the given number of elements.
2324     *
2325     * @param size number of elements (doesn't need to be perfectly accurate)
2326     */
2327 dl 1.102 private final void tryPresize(int size) {
2328 dl 1.27 int c = (size >= (MAXIMUM_CAPACITY >>> 1)) ? MAXIMUM_CAPACITY :
2329     tableSizeFor(size + (size >>> 1) + 1);
2330     int sc;
2331     while ((sc = sizeCtl) >= 0) {
2332 dl 1.102 Node<K,V>[] tab = table; int n;
2333 dl 1.27 if (tab == null || (n = tab.length) == 0) {
2334 jsr166 1.30 n = (sc > c) ? sc : c;
2335 dl 1.82 if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) {
2336 dl 1.27 try {
2337     if (table == tab) {
2338 jsr166 1.115 @SuppressWarnings("unchecked")
2339 dl 1.117 Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n];
2340 dl 1.102 table = nt;
2341 dl 1.27 sc = n - (n >>> 2);
2342     }
2343     } finally {
2344     sizeCtl = sc;
2345     }
2346     }
2347     }
2348     else if (c <= sc || n >= MAXIMUM_CAPACITY)
2349     break;
2350 dl 1.117 else if (tab == table) {
2351     int rs = resizeStamp(n);
2352     if (sc < 0) {
2353 jsr166 1.119 Node<K,V>[] nt;
2354 dl 1.117 if ((sc >>> RESIZE_STAMP_SHIFT) != rs || sc == rs + 1 ||
2355     sc == rs + MAX_RESIZERS || (nt = nextTable) == null ||
2356     transferIndex <= 0)
2357     break;
2358     if (U.compareAndSwapInt(this, SIZECTL, sc, sc + 1))
2359     transfer(tab, nt);
2360     }
2361     else if (U.compareAndSwapInt(this, SIZECTL, sc,
2362 jsr166 1.119 (rs << RESIZE_STAMP_SHIFT) + 2))
2363 dl 1.117 transfer(tab, null);
2364     }
2365 dl 1.27 }
2366     }
2367    
2368 jsr166 1.92 /**
2369 dl 1.24 * Moves and/or copies the nodes in each bin to new table. See
2370     * above for explanation.
2371     */
2372 dl 1.102 private final void transfer(Node<K,V>[] tab, Node<K,V>[] nextTab) {
2373 dl 1.82 int n = tab.length, stride;
2374     if ((stride = (NCPU > 1) ? (n >>> 3) / NCPU : n) < MIN_TRANSFER_STRIDE)
2375     stride = MIN_TRANSFER_STRIDE; // subdivide range
2376     if (nextTab == null) { // initiating
2377     try {
2378 jsr166 1.115 @SuppressWarnings("unchecked")
2379 dl 1.116 Node<K,V>[] nt = (Node<K,V>[])new Node<?,?>[n << 1];
2380 dl 1.102 nextTab = nt;
2381 jsr166 1.83 } catch (Throwable ex) { // try to cope with OOME
2382 dl 1.82 sizeCtl = Integer.MAX_VALUE;
2383     return;
2384     }
2385     nextTable = nextTab;
2386     transferIndex = n;
2387     }
2388     int nextn = nextTab.length;
2389 dl 1.102 ForwardingNode<K,V> fwd = new ForwardingNode<K,V>(nextTab);
2390 dl 1.82 boolean advance = true;
2391 dl 1.110 boolean finishing = false; // to ensure sweep before committing nextTab
2392 dl 1.82 for (int i = 0, bound = 0;;) {
2393 dl 1.116 Node<K,V> f; int fh;
2394 dl 1.82 while (advance) {
2395 dl 1.116 int nextIndex, nextBound;
2396 dl 1.110 if (--i >= bound || finishing)
2397 dl 1.82 advance = false;
2398 dl 1.116 else if ((nextIndex = transferIndex) <= 0) {
2399 dl 1.82 i = -1;
2400     advance = false;
2401     }
2402     else if (U.compareAndSwapInt
2403     (this, TRANSFERINDEX, nextIndex,
2404 jsr166 1.83 nextBound = (nextIndex > stride ?
2405 dl 1.82 nextIndex - stride : 0))) {
2406     bound = nextBound;
2407     i = nextIndex - 1;
2408     advance = false;
2409     }
2410     }
2411     if (i < 0 || i >= n || i + n >= nextn) {
2412 dl 1.116 int sc;
2413 dl 1.110 if (finishing) {
2414     nextTable = null;
2415     table = nextTab;
2416     sizeCtl = (n << 1) - (n >>> 1);
2417     return;
2418     }
2419 dl 1.117 if (U.compareAndSwapInt(this, SIZECTL, sc = sizeCtl, sc - 1)) {
2420 dl 1.120 if ((sc - 2) != resizeStamp(n) << RESIZE_STAMP_SHIFT)
2421 dl 1.116 return;
2422     finishing = advance = true;
2423     i = n; // recheck before commit
2424 dl 1.24 }
2425     }
2426 dl 1.116 else if ((f = tabAt(tab, i)) == null)
2427     advance = casTabAt(tab, i, null, fwd);
2428 dl 1.102 else if ((fh = f.hash) == MOVED)
2429     advance = true; // already processed
2430     else {
2431 jsr166 1.83 synchronized (f) {
2432 dl 1.82 if (tabAt(tab, i) == f) {
2433 dl 1.102 Node<K,V> ln, hn;
2434     if (fh >= 0) {
2435     int runBit = fh & n;
2436     Node<K,V> lastRun = f;
2437     for (Node<K,V> p = f.next; p != null; p = p.next) {
2438     int b = p.hash & n;
2439     if (b != runBit) {
2440     runBit = b;
2441     lastRun = p;
2442     }
2443 dl 1.82 }
2444 dl 1.102 if (runBit == 0) {
2445     ln = lastRun;
2446     hn = null;
2447 dl 1.82 }
2448     else {
2449 dl 1.102 hn = lastRun;
2450     ln = null;
2451     }
2452     for (Node<K,V> p = f; p != lastRun; p = p.next) {
2453     int ph = p.hash; K pk = p.key; V pv = p.val;
2454     if ((ph & n) == 0)
2455     ln = new Node<K,V>(ph, pk, pv, ln);
2456     else
2457     hn = new Node<K,V>(ph, pk, pv, hn);
2458 dl 1.82 }
2459 dl 1.109 setTabAt(nextTab, i, ln);
2460     setTabAt(nextTab, i + n, hn);
2461     setTabAt(tab, i, fwd);
2462     advance = true;
2463 dl 1.82 }
2464 dl 1.102 else if (f instanceof TreeBin) {
2465     TreeBin<K,V> t = (TreeBin<K,V>)f;
2466     TreeNode<K,V> lo = null, loTail = null;
2467     TreeNode<K,V> hi = null, hiTail = null;
2468     int lc = 0, hc = 0;
2469     for (Node<K,V> e = t.first; e != null; e = e.next) {
2470     int h = e.hash;
2471     TreeNode<K,V> p = new TreeNode<K,V>
2472     (h, e.key, e.val, null, null);
2473     if ((h & n) == 0) {
2474     if ((p.prev = loTail) == null)
2475     lo = p;
2476     else
2477     loTail.next = p;
2478     loTail = p;
2479     ++lc;
2480     }
2481     else {
2482     if ((p.prev = hiTail) == null)
2483     hi = p;
2484     else
2485     hiTail.next = p;
2486     hiTail = p;
2487     ++hc;
2488     }
2489     }
2490 jsr166 1.104 ln = (lc <= UNTREEIFY_THRESHOLD) ? untreeify(lo) :
2491     (hc != 0) ? new TreeBin<K,V>(lo) : t;
2492     hn = (hc <= UNTREEIFY_THRESHOLD) ? untreeify(hi) :
2493     (lc != 0) ? new TreeBin<K,V>(hi) : t;
2494 dl 1.109 setTabAt(nextTab, i, ln);
2495     setTabAt(nextTab, i + n, hn);
2496     setTabAt(tab, i, fwd);
2497     advance = true;
2498 dl 1.82 }
2499 dl 1.24 }
2500     }
2501     }
2502     }
2503     }
2504    
2505 dl 1.102 /* ---------------- Conversion from/to TreeBins -------------- */
2506 dl 1.82
2507 dl 1.102 /**
2508     * Replaces all linked nodes in bin at given index unless table is
2509     * too small, in which case resizes instead.
2510     */
2511     private final void treeifyBin(Node<K,V>[] tab, int index) {
2512     Node<K,V> b; int n, sc;
2513     if (tab != null) {
2514 dl 1.117 if ((n = tab.length) < MIN_TREEIFY_CAPACITY)
2515     tryPresize(n << 1);
2516 dl 1.109 else if ((b = tabAt(tab, index)) != null && b.hash >= 0) {
2517 dl 1.102 synchronized (b) {
2518     if (tabAt(tab, index) == b) {
2519     TreeNode<K,V> hd = null, tl = null;
2520     for (Node<K,V> e = b; e != null; e = e.next) {
2521     TreeNode<K,V> p =
2522     new TreeNode<K,V>(e.hash, e.key, e.val,
2523     null, null);
2524     if ((p.prev = tl) == null)
2525     hd = p;
2526     else
2527     tl.next = p;
2528     tl = p;
2529 dl 1.38 }
2530 dl 1.102 setTabAt(tab, index, new TreeBin<K,V>(hd));
2531 dl 1.38 }
2532     }
2533 dl 1.82 }
2534 dl 1.27 }
2535     }
2536 jsr166 1.119
2537 dl 1.102 /**
2538 jsr166 1.105 * Returns a list on non-TreeNodes replacing those in given list.
2539 dl 1.102 */
2540     static <K,V> Node<K,V> untreeify(Node<K,V> b) {
2541     Node<K,V> hd = null, tl = null;
2542     for (Node<K,V> q = b; q != null; q = q.next) {
2543     Node<K,V> p = new Node<K,V>(q.hash, q.key, q.val, null);
2544     if (tl == null)
2545     hd = p;
2546     else
2547     tl.next = p;
2548     tl = p;
2549     }
2550     return hd;
2551     }
2552    
2553     /* ---------------- TreeNodes -------------- */
2554 dl 1.14
2555 dl 1.1 /**
2556 dl 1.102 * Nodes for use in TreeBins
2557 dl 1.14 */
2558 dl 1.102 static final class TreeNode<K,V> extends Node<K,V> {
2559     TreeNode<K,V> parent; // red-black tree links
2560     TreeNode<K,V> left;
2561     TreeNode<K,V> right;
2562     TreeNode<K,V> prev; // needed to unlink next upon deletion
2563     boolean red;
2564    
2565     TreeNode(int hash, K key, V val, Node<K,V> next,
2566     TreeNode<K,V> parent) {
2567     super(hash, key, val, next);
2568     this.parent = parent;
2569     }
2570    
2571     Node<K,V> find(int h, Object k) {
2572     return findTreeNode(h, k, null);
2573     }
2574    
2575     /**
2576     * Returns the TreeNode (or null if not found) for the given key
2577     * starting at given root.
2578     */
2579     final TreeNode<K,V> findTreeNode(int h, Object k, Class<?> kc) {
2580     if (k != null) {
2581     TreeNode<K,V> p = this;
2582 jsr166 1.121 do {
2583 dl 1.102 int ph, dir; K pk; TreeNode<K,V> q;
2584     TreeNode<K,V> pl = p.left, pr = p.right;
2585     if ((ph = p.hash) > h)
2586     p = pl;
2587     else if (ph < h)
2588     p = pr;
2589     else if ((pk = p.key) == k || (pk != null && k.equals(pk)))
2590     return p;
2591 dl 1.112 else if (pl == null)
2592     p = pr;
2593     else if (pr == null)
2594     p = pl;
2595 jsr166 1.103 else if ((kc != null ||
2596 dl 1.102 (kc = comparableClassFor(k)) != null) &&
2597     (dir = compareComparables(kc, k, pk)) != 0)
2598     p = (dir < 0) ? pl : pr;
2599 dl 1.112 else if ((q = pr.findTreeNode(h, k, kc)) != null)
2600     return q;
2601     else
2602 dl 1.102 p = pl;
2603     } while (p != null);
2604     }
2605     return null;
2606     }
2607     }
2608    
2609     /* ---------------- TreeBins -------------- */
2610    
2611     /**
2612     * TreeNodes used at the heads of bins. TreeBins do not hold user
2613     * keys or values, but instead point to list of TreeNodes and
2614     * their root. They also maintain a parasitic read-write lock
2615     * forcing writers (who hold bin lock) to wait for readers (who do
2616     * not) to complete before tree restructuring operations.
2617     */
2618     static final class TreeBin<K,V> extends Node<K,V> {
2619     TreeNode<K,V> root;
2620     volatile TreeNode<K,V> first;
2621     volatile Thread waiter;
2622     volatile int lockState;
2623     // values for lockState
2624     static final int WRITER = 1; // set while holding write lock
2625     static final int WAITER = 2; // set when waiting for write lock
2626     static final int READER = 4; // increment value for setting read lock
2627    
2628     /**
2629 dl 1.112 * Tie-breaking utility for ordering insertions when equal
2630     * hashCodes and non-comparable. We don't require a total
2631     * order, just a consistent insertion rule to maintain
2632     * equivalence across rebalancings. Tie-breaking further than
2633     * necessary simplifies testing a bit.
2634     */
2635     static int tieBreakOrder(Object a, Object b) {
2636     int d;
2637     if (a == null || b == null ||
2638     (d = a.getClass().getName().
2639     compareTo(b.getClass().getName())) == 0)
2640     d = (System.identityHashCode(a) <= System.identityHashCode(b) ?
2641     -1 : 1);
2642     return d;
2643     }
2644    
2645     /**
2646 dl 1.102 * Creates bin with initial set of nodes headed by b.
2647     */
2648     TreeBin(TreeNode<K,V> b) {
2649     super(TREEBIN, null, null, null);
2650     this.first = b;
2651     TreeNode<K,V> r = null;
2652     for (TreeNode<K,V> x = b, next; x != null; x = next) {
2653     next = (TreeNode<K,V>)x.next;
2654     x.left = x.right = null;
2655     if (r == null) {
2656     x.parent = null;
2657     x.red = false;
2658     r = x;
2659     }
2660     else {
2661 dl 1.112 K k = x.key;
2662     int h = x.hash;
2663 dl 1.102 Class<?> kc = null;
2664     for (TreeNode<K,V> p = r;;) {
2665     int dir, ph;
2666 dl 1.112 K pk = p.key;
2667     if ((ph = p.hash) > h)
2668 dl 1.102 dir = -1;
2669 dl 1.112 else if (ph < h)
2670 dl 1.102 dir = 1;
2671 dl 1.112 else if ((kc == null &&
2672     (kc = comparableClassFor(k)) == null) ||
2673     (dir = compareComparables(kc, k, pk)) == 0)
2674     dir = tieBreakOrder(k, pk);
2675     TreeNode<K,V> xp = p;
2676 dl 1.102 if ((p = (dir <= 0) ? p.left : p.right) == null) {
2677     x.parent = xp;
2678     if (dir <= 0)
2679     xp.left = x;
2680     else
2681     xp.right = x;
2682     r = balanceInsertion(r, x);
2683     break;
2684     }
2685     }
2686     }
2687     }
2688     this.root = r;
2689 dl 1.112 assert checkInvariants(root);
2690 dl 1.102 }
2691    
2692     /**
2693 jsr166 1.105 * Acquires write lock for tree restructuring.
2694 dl 1.102 */
2695     private final void lockRoot() {
2696     if (!U.compareAndSwapInt(this, LOCKSTATE, 0, WRITER))
2697     contendedLock(); // offload to separate method
2698     }
2699    
2700     /**
2701 jsr166 1.105 * Releases write lock for tree restructuring.
2702 dl 1.102 */
2703     private final void unlockRoot() {
2704     lockState = 0;
2705     }
2706 dl 1.14
2707 dl 1.102 /**
2708 jsr166 1.105 * Possibly blocks awaiting root lock.
2709 dl 1.102 */
2710     private final void contendedLock() {
2711     boolean waiting = false;
2712     for (int s;;) {
2713 dl 1.117 if (((s = lockState) & ~WAITER) == 0) {
2714 dl 1.102 if (U.compareAndSwapInt(this, LOCKSTATE, s, WRITER)) {
2715     if (waiting)
2716     waiter = null;
2717     return;
2718     }
2719     }
2720 dl 1.114 else if ((s & WAITER) == 0) {
2721 dl 1.102 if (U.compareAndSwapInt(this, LOCKSTATE, s, s | WAITER)) {
2722     waiting = true;
2723     waiter = Thread.currentThread();
2724     }
2725     }
2726     else if (waiting)
2727     LockSupport.park(this);
2728     }
2729 dl 1.14 }
2730    
2731 dl 1.102 /**
2732     * Returns matching node or null if none. Tries to search
2733 jsr166 1.108 * using tree comparisons from root, but continues linear
2734 dl 1.102 * search when lock not available.
2735     */
2736 dl 1.118 final Node<K,V> find(int h, Object k) {
2737 dl 1.102 if (k != null) {
2738 dl 1.118 for (Node<K,V> e = first; e != null; ) {
2739 dl 1.102 int s; K ek;
2740     if (((s = lockState) & (WAITER|WRITER)) != 0) {
2741     if (e.hash == h &&
2742     ((ek = e.key) == k || (ek != null && k.equals(ek))))
2743     return e;
2744 dl 1.118 e = e.next;
2745 dl 1.102 }
2746     else if (U.compareAndSwapInt(this, LOCKSTATE, s,
2747     s + READER)) {
2748     TreeNode<K,V> r, p;
2749     try {
2750     p = ((r = root) == null ? null :
2751     r.findTreeNode(h, k, null));
2752     } finally {
2753     Thread w;
2754     int ls;
2755     do {} while (!U.compareAndSwapInt
2756     (this, LOCKSTATE,
2757     ls = lockState, ls - READER));
2758     if (ls == (READER|WAITER) && (w = waiter) != null)
2759     LockSupport.unpark(w);
2760     }
2761     return p;
2762     }
2763     }
2764 dl 1.79 }
2765 dl 1.102 return null;
2766 dl 1.41 }
2767    
2768     /**
2769 dl 1.102 * Finds or adds a node.
2770     * @return null if added
2771 dl 1.41 */
2772 dl 1.102 final TreeNode<K,V> putTreeVal(int h, K k, V v) {
2773     Class<?> kc = null;
2774 dl 1.112 boolean searched = false;
2775 dl 1.102 for (TreeNode<K,V> p = root;;) {
2776 dl 1.112 int dir, ph; K pk;
2777 dl 1.102 if (p == null) {
2778     first = root = new TreeNode<K,V>(h, k, v, null, null);
2779     break;
2780     }
2781     else if ((ph = p.hash) > h)
2782     dir = -1;
2783     else if (ph < h)
2784     dir = 1;
2785     else if ((pk = p.key) == k || (pk != null && k.equals(pk)))
2786     return p;
2787     else if ((kc == null &&
2788     (kc = comparableClassFor(k)) == null) ||
2789     (dir = compareComparables(kc, k, pk)) == 0) {
2790 dl 1.112 if (!searched) {
2791     TreeNode<K,V> q, ch;
2792     searched = true;
2793     if (((ch = p.left) != null &&
2794     (q = ch.findTreeNode(h, k, kc)) != null) ||
2795     ((ch = p.right) != null &&
2796     (q = ch.findTreeNode(h, k, kc)) != null))
2797     return q;
2798     }
2799     dir = tieBreakOrder(k, pk);
2800 dl 1.102 }
2801 dl 1.112
2802 dl 1.102 TreeNode<K,V> xp = p;
2803 dl 1.112 if ((p = (dir <= 0) ? p.left : p.right) == null) {
2804 dl 1.102 TreeNode<K,V> x, f = first;
2805     first = x = new TreeNode<K,V>(h, k, v, f, xp);
2806     if (f != null)
2807     f.prev = x;
2808 dl 1.112 if (dir <= 0)
2809 dl 1.102 xp.left = x;
2810 dl 1.63 else
2811 dl 1.102 xp.right = x;
2812     if (!xp.red)
2813     x.red = true;
2814     else {
2815     lockRoot();
2816     try {
2817     root = balanceInsertion(root, x);
2818     } finally {
2819     unlockRoot();
2820 dl 1.38 }
2821 dl 1.102 }
2822     break;
2823 dl 1.1 }
2824 dl 1.102 }
2825     assert checkInvariants(root);
2826     return null;
2827 dl 1.1 }
2828 dl 1.41
2829 dl 1.102 /**
2830     * Removes the given node, that must be present before this
2831     * call. This is messier than typical red-black deletion code
2832     * because we cannot swap the contents of an interior node
2833     * with a leaf successor that is pinned by "next" pointers
2834     * that are accessible independently of lock. So instead we
2835     * swap the tree linkages.
2836     *
2837 jsr166 1.106 * @return true if now too small, so should be untreeified
2838 dl 1.102 */
2839     final boolean removeTreeNode(TreeNode<K,V> p) {
2840     TreeNode<K,V> next = (TreeNode<K,V>)p.next;
2841     TreeNode<K,V> pred = p.prev; // unlink traversal pointers
2842     TreeNode<K,V> r, rl;
2843     if (pred == null)
2844     first = next;
2845     else
2846     pred.next = next;
2847     if (next != null)
2848     next.prev = pred;
2849     if (first == null) {
2850     root = null;
2851     return true;
2852     }
2853     if ((r = root) == null || r.right == null || // too small
2854     (rl = r.left) == null || rl.left == null)
2855     return true;
2856     lockRoot();
2857     try {
2858     TreeNode<K,V> replacement;
2859     TreeNode<K,V> pl = p.left;
2860     TreeNode<K,V> pr = p.right;
2861     if (pl != null && pr != null) {
2862     TreeNode<K,V> s = pr, sl;
2863     while ((sl = s.left) != null) // find successor
2864     s = sl;
2865     boolean c = s.red; s.red = p.red; p.red = c; // swap colors
2866     TreeNode<K,V> sr = s.right;
2867     TreeNode<K,V> pp = p.parent;
2868     if (s == pr) { // p was s's direct parent
2869     p.parent = s;
2870     s.right = p;
2871     }
2872     else {
2873     TreeNode<K,V> sp = s.parent;
2874     if ((p.parent = sp) != null) {
2875     if (s == sp.left)
2876     sp.left = p;
2877     else
2878     sp.right = p;
2879     }
2880     if ((s.right = pr) != null)
2881     pr.parent = s;
2882     }
2883     p.left = null;
2884     if ((p.right = sr) != null)
2885     sr.parent = p;
2886     if ((s.left = pl) != null)
2887     pl.parent = s;
2888     if ((s.parent = pp) == null)
2889     r = s;
2890     else if (p == pp.left)
2891     pp.left = s;
2892     else
2893     pp.right = s;
2894     if (sr != null)
2895     replacement = sr;
2896     else
2897     replacement = p;
2898     }
2899     else if (pl != null)
2900     replacement = pl;
2901     else if (pr != null)
2902     replacement = pr;
2903     else
2904     replacement = p;
2905     if (replacement != p) {
2906     TreeNode<K,V> pp = replacement.parent = p.parent;
2907     if (pp == null)
2908     r = replacement;
2909     else if (p == pp.left)
2910     pp.left = replacement;
2911     else
2912     pp.right = replacement;
2913     p.left = p.right = p.parent = null;
2914     }
2915    
2916     root = (p.red) ? r : balanceDeletion(r, replacement);
2917    
2918     if (p == replacement) { // detach pointers
2919     TreeNode<K,V> pp;
2920     if ((pp = p.parent) != null) {
2921     if (p == pp.left)
2922     pp.left = null;
2923     else if (p == pp.right)
2924     pp.right = null;
2925     p.parent = null;
2926     }
2927     }
2928     } finally {
2929     unlockRoot();
2930     }
2931     assert checkInvariants(root);
2932     return false;
2933 dl 1.41 }
2934    
2935 dl 1.102 /* ------------------------------------------------------------ */
2936     // Red-black tree methods, all adapted from CLR
2937    
2938     static <K,V> TreeNode<K,V> rotateLeft(TreeNode<K,V> root,
2939     TreeNode<K,V> p) {
2940     TreeNode<K,V> r, pp, rl;
2941     if (p != null && (r = p.right) != null) {
2942     if ((rl = p.right = r.left) != null)
2943     rl.parent = p;
2944     if ((pp = r.parent = p.parent) == null)
2945     (root = r).red = false;
2946     else if (pp.left == p)
2947     pp.left = r;
2948     else
2949     pp.right = r;
2950     r.left = p;
2951     p.parent = r;
2952     }
2953     return root;
2954 dl 1.41 }
2955    
2956 dl 1.102 static <K,V> TreeNode<K,V> rotateRight(TreeNode<K,V> root,
2957     TreeNode<K,V> p) {
2958     TreeNode<K,V> l, pp, lr;
2959     if (p != null && (l = p.left) != null) {
2960     if ((lr = p.left = l.right) != null)
2961     lr.parent = p;
2962     if ((pp = l.parent = p.parent) == null)
2963     (root = l).red = false;
2964     else if (pp.right == p)
2965     pp.right = l;
2966     else
2967     pp.left = l;
2968     l.right = p;
2969     p.parent = l;
2970     }
2971     return root;
2972     }
2973 dl 1.79
2974 dl 1.102 static <K,V> TreeNode<K,V> balanceInsertion(TreeNode<K,V> root,
2975     TreeNode<K,V> x) {
2976     x.red = true;
2977     for (TreeNode<K,V> xp, xpp, xppl, xppr;;) {
2978     if ((xp = x.parent) == null) {
2979     x.red = false;
2980     return x;
2981     }
2982     else if (!xp.red || (xpp = xp.parent) == null)
2983     return root;
2984     if (xp == (xppl = xpp.left)) {
2985     if ((xppr = xpp.right) != null && xppr.red) {
2986     xppr.red = false;
2987     xp.red = false;
2988     xpp.red = true;
2989     x = xpp;
2990     }
2991     else {
2992     if (x == xp.right) {
2993     root = rotateLeft(root, x = xp);
2994     xpp = (xp = x.parent) == null ? null : xp.parent;
2995     }
2996     if (xp != null) {
2997     xp.red = false;
2998     if (xpp != null) {
2999     xpp.red = true;
3000     root = rotateRight(root, xpp);
3001     }
3002     }
3003     }
3004     }
3005     else {
3006     if (xppl != null && xppl.red) {
3007     xppl.red = false;
3008     xp.red = false;
3009     xpp.red = true;
3010     x = xpp;
3011     }
3012     else {
3013     if (x == xp.left) {
3014     root = rotateRight(root, x = xp);
3015     xpp = (xp = x.parent) == null ? null : xp.parent;
3016     }
3017     if (xp != null) {
3018     xp.red = false;
3019     if (xpp != null) {
3020     xpp.red = true;
3021     root = rotateLeft(root, xpp);
3022     }
3023     }
3024     }
3025     }
3026     }
3027     }
3028 dl 1.79
3029 dl 1.102 static <K,V> TreeNode<K,V> balanceDeletion(TreeNode<K,V> root,
3030     TreeNode<K,V> x) {
3031 jsr166 1.121 for (TreeNode<K,V> xp, xpl, xpr;;) {
3032 dl 1.102 if (x == null || x == root)
3033     return root;
3034     else if ((xp = x.parent) == null) {
3035     x.red = false;
3036     return x;
3037     }
3038     else if (x.red) {
3039     x.red = false;
3040     return root;
3041     }
3042     else if ((xpl = xp.left) == x) {
3043     if ((xpr = xp.right) != null && xpr.red) {
3044     xpr.red = false;
3045     xp.red = true;
3046     root = rotateLeft(root, xp);
3047     xpr = (xp = x.parent) == null ? null : xp.right;
3048     }
3049     if (xpr == null)
3050     x = xp;
3051     else {
3052     TreeNode<K,V> sl = xpr.left, sr = xpr.right;
3053     if ((sr == null || !sr.red) &&
3054     (sl == null || !sl.red)) {
3055     xpr.red = true;
3056     x = xp;
3057     }
3058     else {
3059     if (sr == null || !sr.red) {
3060     if (sl != null)
3061     sl.red = false;
3062     xpr.red = true;
3063     root = rotateRight(root, xpr);
3064     xpr = (xp = x.parent) == null ?
3065     null : xp.right;
3066     }
3067     if (xpr != null) {
3068     xpr.red = (xp == null) ? false : xp.red;
3069     if ((sr = xpr.right) != null)
3070     sr.red = false;
3071     }
3072     if (xp != null) {
3073     xp.red = false;
3074     root = rotateLeft(root, xp);
3075     }
3076     x = root;
3077     }
3078     }
3079     }
3080     else { // symmetric
3081     if (xpl != null && xpl.red) {
3082     xpl.red = false;
3083     xp.red = true;
3084     root = rotateRight(root, xp);
3085     xpl = (xp = x.parent) == null ? null : xp.left;
3086     }
3087     if (xpl == null)
3088     x = xp;
3089     else {
3090     TreeNode<K,V> sl = xpl.left, sr = xpl.right;
3091     if ((sl == null || !sl.red) &&
3092     (sr == null || !sr.red)) {
3093     xpl.red = true;
3094     x = xp;
3095     }
3096     else {
3097     if (sl == null || !sl.red) {
3098     if (sr != null)
3099     sr.red = false;
3100     xpl.red = true;
3101     root = rotateLeft(root, xpl);
3102     xpl = (xp = x.parent) == null ?
3103     null : xp.left;
3104     }
3105     if (xpl != null) {
3106     xpl.red = (xp == null) ? false : xp.red;
3107     if ((sl = xpl.left) != null)
3108     sl.red = false;
3109     }
3110     if (xp != null) {
3111     xp.red = false;
3112     root = rotateRight(root, xp);
3113     }
3114     x = root;
3115     }
3116     }
3117     }
3118     }
3119     }
3120 jsr166 1.103
3121 dl 1.79 /**
3122 dl 1.102 * Recursive invariant check
3123 dl 1.79 */
3124 dl 1.102 static <K,V> boolean checkInvariants(TreeNode<K,V> t) {
3125     TreeNode<K,V> tp = t.parent, tl = t.left, tr = t.right,
3126     tb = t.prev, tn = (TreeNode<K,V>)t.next;
3127     if (tb != null && tb.next != t)
3128     return false;
3129     if (tn != null && tn.prev != t)
3130     return false;
3131     if (tp != null && t != tp.left && t != tp.right)
3132     return false;
3133     if (tl != null && (tl.parent != t || tl.hash > t.hash))
3134     return false;
3135     if (tr != null && (tr.parent != t || tr.hash < t.hash))
3136     return false;
3137     if (t.red && tl != null && tl.red && tr != null && tr.red)
3138     return false;
3139     if (tl != null && !checkInvariants(tl))
3140     return false;
3141     if (tr != null && !checkInvariants(tr))
3142     return false;
3143     return true;
3144 dl 1.79 }
3145    
3146 dl 1.102 private static final sun.misc.Unsafe U;
3147     private static final long LOCKSTATE;
3148     static {
3149     try {
3150     U = getUnsafe();
3151     Class<?> k = TreeBin.class;
3152     LOCKSTATE = U.objectFieldOffset
3153     (k.getDeclaredField("lockState"));
3154     } catch (Exception e) {
3155     throw new Error(e);
3156     }
3157     }
3158 dl 1.1 }
3159    
3160 dl 1.102 /* ----------------Table Traversal -------------- */
3161 dl 1.1
3162     /**
3163 dl 1.116 * Records the table, its length, and current traversal index for a
3164     * traverser that must process a region of a forwarded table before
3165     * proceeding with current table.
3166     */
3167     static final class TableStack<K,V> {
3168     int length;
3169     int index;
3170     Node<K,V>[] tab;
3171     TableStack<K,V> next;
3172     }
3173    
3174     /**
3175 dl 1.102 * Encapsulates traversal for methods such as containsValue; also
3176     * serves as a base class for other iterators and spliterators.
3177     *
3178     * Method advance visits once each still-valid node that was
3179     * reachable upon iterator construction. It might miss some that
3180     * were added to a bin after the bin was visited, which is OK wrt
3181     * consistency guarantees. Maintaining this property in the face
3182     * of possible ongoing resizes requires a fair amount of
3183     * bookkeeping state that is difficult to optimize away amidst
3184     * volatile accesses. Even so, traversal maintains reasonable
3185     * throughput.
3186 dl 1.1 *
3187 dl 1.102 * Normally, iteration proceeds bin-by-bin traversing lists.
3188     * However, if the table has been resized, then all future steps
3189     * must traverse both the bin at the current index as well as at
3190     * (index + baseSize); and so on for further resizings. To
3191     * paranoically cope with potential sharing by users of iterators
3192     * across threads, iteration terminates if a bounds checks fails
3193     * for a table read.
3194 dl 1.1 */
3195 dl 1.102 static class Traverser<K,V> {
3196     Node<K,V>[] tab; // current table; updated if resized
3197     Node<K,V> next; // the next entry to use
3198 dl 1.116 TableStack<K,V> stack, spare; // to save/restore on ForwardingNodes
3199 dl 1.102 int index; // index of bin to use next
3200     int baseIndex; // current index of initial table
3201     int baseLimit; // index bound for initial table
3202     final int baseSize; // initial table size
3203    
3204     Traverser(Node<K,V>[] tab, int size, int index, int limit) {
3205     this.tab = tab;
3206     this.baseSize = size;
3207     this.baseIndex = this.index = index;
3208     this.baseLimit = limit;
3209     this.next = null;
3210     }
3211 dl 1.1
3212 dl 1.102 /**
3213     * Advances if possible, returning next valid node, or null if none.
3214     */
3215     final Node<K,V> advance() {
3216     Node<K,V> e;
3217     if ((e = next) != null)
3218     e = e.next;
3219     for (;;) {
3220 dl 1.116 Node<K,V>[] t; int i, n; // must use locals in checks
3221 dl 1.102 if (e != null)
3222     return next = e;
3223     if (baseIndex >= baseLimit || (t = tab) == null ||
3224     (n = t.length) <= (i = index) || i < 0)
3225     return next = null;
3226 dl 1.116 if ((e = tabAt(t, i)) != null && e.hash < 0) {
3227 dl 1.102 if (e instanceof ForwardingNode) {
3228     tab = ((ForwardingNode<K,V>)e).nextTable;
3229     e = null;
3230 dl 1.116 pushState(t, i, n);
3231 dl 1.102 continue;
3232 dl 1.75 }
3233 dl 1.102 else if (e instanceof TreeBin)
3234     e = ((TreeBin<K,V>)e).first;
3235     else
3236     e = null;
3237 dl 1.75 }
3238 dl 1.116 if (stack != null)
3239     recoverState(n);
3240     else if ((index = i + baseSize) >= n)
3241     index = ++baseIndex; // visit upper slots if present
3242     }
3243     }
3244    
3245     /**
3246     * Saves traversal state upon encountering a forwarding node.
3247     */
3248     private void pushState(Node<K,V>[] t, int i, int n) {
3249     TableStack<K,V> s = spare; // reuse if possible
3250     if (s != null)
3251     spare = s.next;
3252     else
3253     s = new TableStack<K,V>();
3254     s.tab = t;
3255     s.length = n;
3256     s.index = i;
3257     s.next = stack;
3258     stack = s;
3259     }
3260    
3261     /**
3262     * Possibly pops traversal state.
3263     *
3264     * @param n length of current table
3265     */
3266     private void recoverState(int n) {
3267     TableStack<K,V> s; int len;
3268     while ((s = stack) != null && (index += (len = s.length)) >= n) {
3269     n = len;
3270     index = s.index;
3271     tab = s.tab;
3272     s.tab = null;
3273     TableStack<K,V> next = s.next;
3274     s.next = spare; // save for reuse
3275     stack = next;
3276     spare = s;
3277 dl 1.24 }
3278 dl 1.116 if (s == null && (index += baseSize) >= n)
3279     index = ++baseIndex;
3280 dl 1.24 }
3281 dl 1.75 }
3282    
3283     /**
3284 dl 1.102 * Base of key, value, and entry Iterators. Adds fields to
3285 jsr166 1.105 * Traverser to support iterator.remove.
3286 dl 1.75 */
3287 dl 1.102 static class BaseIterator<K,V> extends Traverser<K,V> {
3288     final ConcurrentHashMapV8<K,V> map;
3289     Node<K,V> lastReturned;
3290     BaseIterator(Node<K,V>[] tab, int size, int index, int limit,
3291     ConcurrentHashMapV8<K,V> map) {
3292     super(tab, size, index, limit);
3293     this.map = map;
3294     advance();
3295     }
3296    
3297     public final boolean hasNext() { return next != null; }
3298     public final boolean hasMoreElements() { return next != null; }
3299    
3300     public final void remove() {
3301     Node<K,V> p;
3302     if ((p = lastReturned) == null)
3303     throw new IllegalStateException();
3304     lastReturned = null;
3305     map.replaceNode(p.key, null, null);
3306     }
3307 dl 1.75 }
3308    
3309 dl 1.102 static final class KeyIterator<K,V> extends BaseIterator<K,V>
3310     implements Iterator<K>, Enumeration<K> {
3311     KeyIterator(Node<K,V>[] tab, int index, int size, int limit,
3312     ConcurrentHashMapV8<K,V> map) {
3313     super(tab, index, size, limit, map);
3314     }
3315    
3316     public final K next() {
3317     Node<K,V> p;
3318     if ((p = next) == null)
3319     throw new NoSuchElementException();
3320     K k = p.key;
3321     lastReturned = p;
3322     advance();
3323     return k;
3324     }
3325    
3326     public final K nextElement() { return next(); }
3327 dl 1.75 }
3328    
3329 dl 1.102 static final class ValueIterator<K,V> extends BaseIterator<K,V>
3330     implements Iterator<V>, Enumeration<V> {
3331     ValueIterator(Node<K,V>[] tab, int index, int size, int limit,
3332     ConcurrentHashMapV8<K,V> map) {
3333     super(tab, index, size, limit, map);
3334     }
3335    
3336     public final V next() {
3337     Node<K,V> p;
3338     if ((p = next) == null)
3339     throw new NoSuchElementException();
3340     V v = p.val;
3341     lastReturned = p;
3342     advance();
3343     return v;
3344 dl 1.84 }
3345 dl 1.102
3346     public final V nextElement() { return next(); }
3347 dl 1.75 }
3348    
3349 dl 1.102 static final class EntryIterator<K,V> extends BaseIterator<K,V>
3350     implements Iterator<Map.Entry<K,V>> {
3351     EntryIterator(Node<K,V>[] tab, int index, int size, int limit,
3352     ConcurrentHashMapV8<K,V> map) {
3353     super(tab, index, size, limit, map);
3354     }
3355    
3356     public final Map.Entry<K,V> next() {
3357     Node<K,V> p;
3358     if ((p = next) == null)
3359     throw new NoSuchElementException();
3360     K k = p.key;
3361     V v = p.val;
3362     lastReturned = p;
3363     advance();
3364     return new MapEntry<K,V>(k, v, map);
3365 dl 1.84 }
3366 dl 1.75 }
3367    
3368     /**
3369 dl 1.102 * Exported Entry for EntryIterator
3370 dl 1.75 */
3371 dl 1.102 static final class MapEntry<K,V> implements Map.Entry<K,V> {
3372     final K key; // non-null
3373     V val; // non-null
3374     final ConcurrentHashMapV8<K,V> map;
3375     MapEntry(K key, V val, ConcurrentHashMapV8<K,V> map) {
3376     this.key = key;
3377     this.val = val;
3378     this.map = map;
3379     }
3380     public K getKey() { return key; }
3381     public V getValue() { return val; }
3382     public int hashCode() { return key.hashCode() ^ val.hashCode(); }
3383     public String toString() { return key + "=" + val; }
3384    
3385     public boolean equals(Object o) {
3386     Object k, v; Map.Entry<?,?> e;
3387     return ((o instanceof Map.Entry) &&
3388     (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
3389     (v = e.getValue()) != null &&
3390     (k == key || k.equals(key)) &&
3391     (v == val || v.equals(val)));
3392     }
3393    
3394     /**
3395     * Sets our entry's value and writes through to the map. The
3396     * value to return is somewhat arbitrary here. Since we do not
3397     * necessarily track asynchronous changes, the most recent
3398     * "previous" value could be different from what we return (or
3399     * could even have been removed, in which case the put will
3400     * re-establish). We do not and cannot guarantee more.
3401     */
3402     public V setValue(V value) {
3403     if (value == null) throw new NullPointerException();
3404     V v = val;
3405     val = value;
3406     map.put(key, value);
3407     return v;
3408 dl 1.84 }
3409 dl 1.75 }
3410    
3411 dl 1.102 static final class KeySpliterator<K,V> extends Traverser<K,V>
3412     implements ConcurrentHashMapSpliterator<K> {
3413     long est; // size estimate
3414     KeySpliterator(Node<K,V>[] tab, int size, int index, int limit,
3415     long est) {
3416     super(tab, size, index, limit);
3417     this.est = est;
3418     }
3419    
3420     public ConcurrentHashMapSpliterator<K> trySplit() {
3421     int i, f, h;
3422     return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null :
3423     new KeySpliterator<K,V>(tab, baseSize, baseLimit = h,
3424     f, est >>>= 1);
3425     }
3426    
3427     public void forEachRemaining(Action<? super K> action) {
3428     if (action == null) throw new NullPointerException();
3429     for (Node<K,V> p; (p = advance()) != null;)
3430     action.apply(p.key);
3431     }
3432    
3433     public boolean tryAdvance(Action<? super K> action) {
3434     if (action == null) throw new NullPointerException();
3435     Node<K,V> p;
3436     if ((p = advance()) == null)
3437     return false;
3438     action.apply(p.key);
3439     return true;
3440 dl 1.84 }
3441 dl 1.102
3442     public long estimateSize() { return est; }
3443    
3444 dl 1.75 }
3445    
3446 dl 1.102 static final class ValueSpliterator<K,V> extends Traverser<K,V>
3447     implements ConcurrentHashMapSpliterator<V> {
3448     long est; // size estimate
3449     ValueSpliterator(Node<K,V>[] tab, int size, int index, int limit,
3450     long est) {
3451     super(tab, size, index, limit);
3452     this.est = est;
3453     }
3454    
3455     public ConcurrentHashMapSpliterator<V> trySplit() {
3456     int i, f, h;
3457     return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null :
3458     new ValueSpliterator<K,V>(tab, baseSize, baseLimit = h,
3459     f, est >>>= 1);
3460     }
3461    
3462     public void forEachRemaining(Action<? super V> action) {
3463     if (action == null) throw new NullPointerException();
3464     for (Node<K,V> p; (p = advance()) != null;)
3465     action.apply(p.val);
3466     }
3467    
3468     public boolean tryAdvance(Action<? super V> action) {
3469     if (action == null) throw new NullPointerException();
3470     Node<K,V> p;
3471     if ((p = advance()) == null)
3472     return false;
3473     action.apply(p.val);
3474     return true;
3475     }
3476    
3477     public long estimateSize() { return est; }
3478    
3479 dl 1.75 }
3480    
3481 dl 1.102 static final class EntrySpliterator<K,V> extends Traverser<K,V>
3482     implements ConcurrentHashMapSpliterator<Map.Entry<K,V>> {
3483     final ConcurrentHashMapV8<K,V> map; // To export MapEntry
3484     long est; // size estimate
3485     EntrySpliterator(Node<K,V>[] tab, int size, int index, int limit,
3486     long est, ConcurrentHashMapV8<K,V> map) {
3487     super(tab, size, index, limit);
3488     this.map = map;
3489     this.est = est;
3490     }
3491    
3492     public ConcurrentHashMapSpliterator<Map.Entry<K,V>> trySplit() {
3493     int i, f, h;
3494     return (h = ((i = baseIndex) + (f = baseLimit)) >>> 1) <= i ? null :
3495     new EntrySpliterator<K,V>(tab, baseSize, baseLimit = h,
3496     f, est >>>= 1, map);
3497     }
3498    
3499     public void forEachRemaining(Action<? super Map.Entry<K,V>> action) {
3500     if (action == null) throw new NullPointerException();
3501     for (Node<K,V> p; (p = advance()) != null; )
3502     action.apply(new MapEntry<K,V>(p.key, p.val, map));
3503     }
3504    
3505     public boolean tryAdvance(Action<? super Map.Entry<K,V>> action) {
3506     if (action == null) throw new NullPointerException();
3507     Node<K,V> p;
3508     if ((p = advance()) == null)
3509     return false;
3510     action.apply(new MapEntry<K,V>(p.key, p.val, map));
3511     return true;
3512     }
3513    
3514     public long estimateSize() { return est; }
3515    
3516 dl 1.75 }
3517    
3518 dl 1.102 // Parallel bulk operations
3519    
3520 dl 1.75 /**
3521 dl 1.102 * Computes initial batch value for bulk tasks. The returned value
3522     * is approximately exp2 of the number of times (minus one) to
3523     * split task by two before executing leaf action. This value is
3524     * faster to compute and more convenient to use as a guide to
3525     * splitting than is the depth, since it is used while dividing by
3526     * two anyway.
3527     */
3528     final int batchFor(long b) {
3529     long n;
3530     if (b == Long.MAX_VALUE || (n = sumCount()) <= 1L || n < b)
3531     return 0;
3532     int sp = ForkJoinPool.getCommonPoolParallelism() << 2; // slack of 4
3533     return (b <= 0L || (n /= b) >= sp) ? sp : (int)n;
3534 dl 1.75 }
3535    
3536     /**
3537 dl 1.84 * Performs the given action for each (key, value).
3538     *
3539 dl 1.102 * @param parallelismThreshold the (estimated) number of elements
3540     * needed for this operation to be executed in parallel
3541 dl 1.84 * @param action the action
3542 dl 1.102 * @since 1.8
3543 dl 1.75 */
3544 dl 1.102 public void forEach(long parallelismThreshold,
3545     BiAction<? super K,? super V> action) {
3546     if (action == null) throw new NullPointerException();
3547     new ForEachMappingTask<K,V>
3548     (null, batchFor(parallelismThreshold), 0, 0, table,
3549     action).invoke();
3550 dl 1.84 }
3551 dl 1.75
3552 dl 1.84 /**
3553     * Performs the given action for each non-null transformation
3554     * of each (key, value).
3555     *
3556 dl 1.102 * @param parallelismThreshold the (estimated) number of elements
3557     * needed for this operation to be executed in parallel
3558 dl 1.84 * @param transformer a function returning the transformation
3559 jsr166 1.91 * for an element, or null if there is no transformation (in
3560 jsr166 1.93 * which case the action is not applied)
3561 dl 1.84 * @param action the action
3562 dl 1.102 * @since 1.8
3563 dl 1.84 */
3564 dl 1.102 public <U> void forEach(long parallelismThreshold,
3565     BiFun<? super K, ? super V, ? extends U> transformer,
3566     Action<? super U> action) {
3567     if (transformer == null || action == null)
3568     throw new NullPointerException();
3569     new ForEachTransformedMappingTask<K,V,U>
3570     (null, batchFor(parallelismThreshold), 0, 0, table,
3571     transformer, action).invoke();
3572 dl 1.84 }
3573 dl 1.75
3574 dl 1.84 /**
3575     * Returns a non-null result from applying the given search
3576     * function on each (key, value), or null if none. Upon
3577     * success, further element processing is suppressed and the
3578     * results of any other parallel invocations of the search
3579     * function are ignored.
3580     *
3581 dl 1.102 * @param parallelismThreshold the (estimated) number of elements
3582     * needed for this operation to be executed in parallel
3583 dl 1.84 * @param searchFunction a function returning a non-null
3584     * result on success, else null
3585     * @return a non-null result from applying the given search
3586     * function on each (key, value), or null if none
3587 dl 1.102 * @since 1.8
3588 dl 1.84 */
3589 dl 1.102 public <U> U search(long parallelismThreshold,
3590     BiFun<? super K, ? super V, ? extends U> searchFunction) {
3591     if (searchFunction == null) throw new NullPointerException();
3592     return new SearchMappingsTask<K,V,U>
3593     (null, batchFor(parallelismThreshold), 0, 0, table,
3594     searchFunction, new AtomicReference<U>()).invoke();
3595 dl 1.84 }
3596 dl 1.75
3597 dl 1.84 /**
3598     * Returns the result of accumulating the given transformation
3599     * of all (key, value) pairs using the given reducer to
3600     * combine values, or null if none.
3601     *
3602 dl 1.102 * @param parallelismThreshold the (estimated) number of elements
3603     * needed for this operation to be executed in parallel
3604 dl 1.84 * @param transformer a function returning the transformation
3605 jsr166 1.91 * for an element, or null if there is no transformation (in
3606 jsr166 1.93 * which case it is not combined)
3607 dl 1.84 * @param reducer a commutative associative combining function
3608     * @return the result of accumulating the given transformation
3609     * of all (key, value) pairs
3610 dl 1.102 * @since 1.8
3611 dl 1.84 */
3612 dl 1.102 public <U> U reduce(long parallelismThreshold,
3613     BiFun<? super K, ? super V, ? extends U> transformer,
3614     BiFun<? super U, ? super U, ? extends U> reducer) {
3615     if (transformer == null || reducer == null)
3616     throw new NullPointerException();
3617     return new MapReduceMappingsTask<K,V,U>
3618     (null, batchFor(parallelismThreshold), 0, 0, table,
3619     null, transformer, reducer).invoke();
3620 dl 1.84 }
3621 dl 1.75
3622 dl 1.84 /**
3623     * Returns the result of accumulating the given transformation
3624     * of all (key, value) pairs using the given reducer to
3625     * combine values, and the given basis as an identity value.
3626     *
3627 dl 1.102 * @param parallelismThreshold the (estimated) number of elements
3628     * needed for this operation to be executed in parallel
3629 dl 1.84 * @param transformer a function returning the transformation
3630     * for an element
3631     * @param basis the identity (initial default value) for the reduction
3632     * @param reducer a commutative associative combining function
3633     * @return the result of accumulating the given transformation
3634     * of all (key, value) pairs
3635 dl 1.102 * @since 1.8
3636 dl 1.84 */
3637 dl 1.107 public double reduceToDouble(long parallelismThreshold,
3638     ObjectByObjectToDouble<? super K, ? super V> transformer,
3639     double basis,
3640     DoubleByDoubleToDouble reducer) {
3641 dl 1.102 if (transformer == null || reducer == null)
3642     throw new NullPointerException();
3643     return new MapReduceMappingsToDoubleTask<K,V>
3644     (null, batchFor(parallelismThreshold), 0, 0, table,
3645     null, transformer, basis, reducer).invoke();
3646 dl 1.84 }
3647    
3648     /**
3649     * Returns the result of accumulating the given transformation
3650     * of all (key, value) pairs using the given reducer to
3651     * combine values, and the given basis as an identity value.
3652     *
3653 dl 1.102 * @param parallelismThreshold the (estimated) number of elements
3654     * needed for this operation to be executed in parallel
3655 dl 1.84 * @param transformer a function returning the transformation
3656     * for an element
3657     * @param basis the identity (initial default value) for the reduction
3658     * @param reducer a commutative associative combining function
3659     * @return the result of accumulating the given transformation
3660     * of all (key, value) pairs
3661 dl 1.102 * @since 1.8
3662 dl 1.84 */
3663 dl 1.102 public long reduceToLong(long parallelismThreshold,
3664     ObjectByObjectToLong<? super K, ? super V> transformer,
3665     long basis,
3666     LongByLongToLong reducer) {
3667     if (transformer == null || reducer == null)
3668     throw new NullPointerException();
3669     return new MapReduceMappingsToLongTask<K,V>
3670     (null, batchFor(parallelismThreshold), 0, 0, table,
3671     null, transformer, basis, reducer).invoke();
3672 dl 1.84 }
3673    
3674     /**
3675     * Returns the result of accumulating the given transformation
3676     * of all (key, value) pairs using the given reducer to
3677     * combine values, and the given basis as an identity value.
3678     *
3679 dl 1.102 * @param parallelismThreshold the (estimated) number of elements
3680     * needed for this operation to be executed in parallel
3681 dl 1.84 * @param transformer a function returning the transformation
3682     * for an element
3683     * @param basis the identity (initial default value) for the reduction
3684     * @param reducer a commutative associative combining function
3685     * @return the result of accumulating the given transformation
3686     * of all (key, value) pairs
3687 dl 1.102 * @since 1.8
3688 dl 1.84 */
3689 dl 1.102 public int reduceToInt(long parallelismThreshold,
3690     ObjectByObjectToInt<? super K, ? super V> transformer,
3691     int basis,
3692     IntByIntToInt reducer) {
3693     if (transformer == null || reducer == null)
3694     throw new NullPointerException();
3695     return new MapReduceMappingsToIntTask<K,V>
3696     (null, batchFor(parallelismThreshold), 0, 0, table,
3697     null, transformer, basis, reducer).invoke();
3698 dl 1.84 }
3699    
3700     /**
3701     * Performs the given action for each key.
3702     *
3703 dl 1.102 * @param parallelismThreshold the (estimated) number of elements
3704     * needed for this operation to be executed in parallel
3705 dl 1.84 * @param action the action
3706 dl 1.102 * @since 1.8
3707 dl 1.84 */
3708 dl 1.102 public void forEachKey(long parallelismThreshold,
3709     Action<? super K> action) {
3710     if (action == null) throw new NullPointerException();
3711     new ForEachKeyTask<K,V>
3712     (null, batchFor(parallelismThreshold), 0, 0, table,
3713     action).invoke();
3714 dl 1.84 }
3715    
3716     /**
3717     * Performs the given action for each non-null transformation
3718     * of each key.
3719     *
3720 dl 1.102 * @param parallelismThreshold the (estimated) number of elements
3721     * needed for this operation to be executed in parallel
3722 dl 1.84 * @param transformer a function returning the transformation
3723 jsr166 1.91 * for an element, or null if there is no transformation (in
3724 jsr166 1.93 * which case the action is not applied)
3725 dl 1.84 * @param action the action
3726 dl 1.102 * @since 1.8
3727 dl 1.84 */
3728 dl 1.102 public <U> void forEachKey(long parallelismThreshold,
3729     Fun<? super K, ? extends U> transformer,
3730     Action<? super U> action) {
3731     if (transformer == null || action == null)
3732     throw new NullPointerException();
3733     new ForEachTransformedKeyTask<K,V,U>
3734     (null, batchFor(parallelismThreshold), 0, 0, table,
3735     transformer, action).invoke();
3736 dl 1.84 }
3737    
3738     /**
3739     * Returns a non-null result from applying the given search
3740     * function on each key, or null if none. Upon success,
3741     * further element processing is suppressed and the results of
3742     * any other parallel invocations of the search function are
3743     * ignored.
3744     *
3745 dl 1.102 * @param parallelismThreshold the (estimated) number of elements
3746     * needed for this operation to be executed in parallel
3747 dl 1.84 * @param searchFunction a function returning a non-null
3748     * result on success, else null
3749     * @return a non-null result from applying the given search
3750     * function on each key, or null if none
3751 dl 1.102 * @since 1.8
3752 dl 1.84 */
3753 dl 1.102 public <U> U searchKeys(long parallelismThreshold,
3754     Fun<? super K, ? extends U> searchFunction) {
3755     if (searchFunction == null) throw new NullPointerException();
3756     return new SearchKeysTask<K,V,U>
3757     (null, batchFor(parallelismThreshold), 0, 0, table,
3758     searchFunction, new AtomicReference<U>()).invoke();
3759 dl 1.84 }
3760    
3761     /**
3762     * Returns the result of accumulating all keys using the given
3763     * reducer to combine values, or null if none.
3764     *
3765 dl 1.102 * @param parallelismThreshold the (estimated) number of elements
3766     * needed for this operation to be executed in parallel
3767 dl 1.84 * @param reducer a commutative associative combining function
3768     * @return the result of accumulating all keys using the given
3769     * reducer to combine values, or null if none
3770 dl 1.102 * @since 1.8
3771 dl 1.84 */
3772 dl 1.102 public K reduceKeys(long parallelismThreshold,
3773     BiFun<? super K, ? super K, ? extends K> reducer) {
3774     if (reducer == null) throw new NullPointerException();
3775     return new ReduceKeysTask<K,V>
3776     (null, batchFor(parallelismThreshold), 0, 0, table,
3777     null, reducer).invoke();
3778 dl 1.84 }
3779    
3780     /**
3781     * Returns the result of accumulating the given transformation
3782     * of all keys using the given reducer to combine values, or
3783     * null if none.
3784     *
3785 dl 1.102 * @param parallelismThreshold the (estimated) number of elements
3786     * needed for this operation to be executed in parallel
3787 dl 1.84 * @param transformer a function returning the transformation
3788 jsr166 1.91 * for an element, or null if there is no transformation (in
3789 jsr166 1.93 * which case it is not combined)
3790 dl 1.84 * @param reducer a commutative associative combining function
3791     * @return the result of accumulating the given transformation
3792     * of all keys
3793 dl 1.102 * @since 1.8
3794 dl 1.84 */
3795 dl 1.102 public <U> U reduceKeys(long parallelismThreshold,
3796     Fun<? super K, ? extends U> transformer,
3797 dl 1.84 BiFun<? super U, ? super U, ? extends U> reducer) {
3798 dl 1.102 if (transformer == null || reducer == null)
3799     throw new NullPointerException();
3800     return new MapReduceKeysTask<K,V,U>
3801     (null, batchFor(parallelismThreshold), 0, 0, table,
3802     null, transformer, reducer).invoke();
3803 dl 1.84 }
3804    
3805     /**
3806     * Returns the result of accumulating the given transformation
3807     * of all keys using the given reducer to combine values, and
3808     * the given basis as an identity value.
3809     *
3810 dl 1.102 * @param parallelismThreshold the (estimated) number of elements
3811     * needed for this operation to be executed in parallel
3812 dl 1.84 * @param transformer a function returning the transformation
3813     * for an element
3814     * @param basis the identity (initial default value) for the reduction
3815     * @param reducer a commutative associative combining function
3816 dl 1.102 * @return the result of accumulating the given transformation
3817 dl 1.84 * of all keys
3818 dl 1.102 * @since 1.8
3819 dl 1.84 */
3820 dl 1.102 public double reduceKeysToDouble(long parallelismThreshold,
3821     ObjectToDouble<? super K> transformer,
3822     double basis,
3823     DoubleByDoubleToDouble reducer) {
3824     if (transformer == null || reducer == null)
3825     throw new NullPointerException();
3826     return new MapReduceKeysToDoubleTask<K,V>
3827     (null, batchFor(parallelismThreshold), 0, 0, table,
3828     null, transformer, basis, reducer).invoke();
3829 dl 1.84 }
3830    
3831     /**
3832     * Returns the result of accumulating the given transformation
3833     * of all keys using the given reducer to combine values, and
3834     * the given basis as an identity value.
3835     *
3836 dl 1.102 * @param parallelismThreshold the (estimated) number of elements
3837     * needed for this operation to be executed in parallel
3838 dl 1.84 * @param transformer a function returning the transformation
3839     * for an element
3840     * @param basis the identity (initial default value) for the reduction
3841     * @param reducer a commutative associative combining function
3842     * @return the result of accumulating the given transformation
3843     * of all keys
3844 dl 1.102 * @since 1.8
3845 dl 1.84 */
3846 dl 1.102 public long reduceKeysToLong(long parallelismThreshold,
3847     ObjectToLong<? super K> transformer,
3848     long basis,
3849     LongByLongToLong reducer) {
3850     if (transformer == null || reducer == null)
3851     throw new NullPointerException();
3852     return new MapReduceKeysToLongTask<K,V>
3853     (null, batchFor(parallelismThreshold), 0, 0, table,
3854     null, transformer, basis, reducer).invoke();
3855 dl 1.84 }
3856    
3857     /**
3858     * Returns the result of accumulating the given transformation
3859     * of all keys using the given reducer to combine values, and
3860     * the given basis as an identity value.
3861     *
3862 dl 1.102 * @param parallelismThreshold the (estimated) number of elements
3863     * needed for this operation to be executed in parallel
3864 dl 1.84 * @param transformer a function returning the transformation
3865     * for an element
3866     * @param basis the identity (initial default value) for the reduction
3867     * @param reducer a commutative associative combining function
3868     * @return the result of accumulating the given transformation
3869     * of all keys
3870 dl 1.102 * @since 1.8
3871 dl 1.84 */
3872 dl 1.102 public int reduceKeysToInt(long parallelismThreshold,
3873     ObjectToInt<? super K> transformer,
3874     int basis,
3875     IntByIntToInt reducer) {
3876     if (transformer == null || reducer == null)
3877     throw new NullPointerException();
3878     return new MapReduceKeysToIntTask<K,V>
3879     (null, batchFor(parallelismThreshold), 0, 0, table,
3880     null, transformer, basis, reducer).invoke();
3881 dl 1.84 }
3882    
3883     /**
3884     * Performs the given action for each value.
3885     *
3886 dl 1.102 * @param parallelismThreshold the (estimated) number of elements
3887     * needed for this operation to be executed in parallel
3888 dl 1.84 * @param action the action
3889 dl 1.102 * @since 1.8
3890 dl 1.84 */
3891 dl 1.102 public void forEachValue(long parallelismThreshold,
3892     Action<? super V> action) {
3893     if (action == null)
3894     throw new NullPointerException();
3895     new ForEachValueTask<K,V>
3896     (null, batchFor(parallelismThreshold), 0, 0, table,
3897     action).invoke();
3898 dl 1.84 }
3899    
3900     /**
3901     * Performs the given action for each non-null transformation
3902     * of each value.
3903     *
3904 dl 1.102 * @param parallelismThreshold the (estimated) number of elements
3905     * needed for this operation to be executed in parallel
3906 dl 1.84 * @param transformer a function returning the transformation
3907 jsr166 1.91 * for an element, or null if there is no transformation (in
3908 jsr166 1.93 * which case the action is not applied)
3909 dl 1.102 * @param action the action
3910     * @since 1.8
3911 dl 1.84 */
3912 dl 1.102 public <U> void forEachValue(long parallelismThreshold,
3913     Fun<? super V, ? extends U> transformer,
3914     Action<? super U> action) {
3915     if (transformer == null || action == null)
3916     throw new NullPointerException();
3917     new ForEachTransformedValueTask<K,V,U>
3918     (null, batchFor(parallelismThreshold), 0, 0, table,
3919     transformer, action).invoke();
3920 dl 1.84 }
3921    
3922     /**
3923     * Returns a non-null result from applying the given search
3924     * function on each value, or null if none. Upon success,
3925     * further element processing is suppressed and the results of
3926     * any other parallel invocations of the search function are
3927     * ignored.
3928     *
3929 dl 1.102 * @param parallelismThreshold the (estimated) number of elements
3930     * needed for this operation to be executed in parallel
3931 dl 1.84 * @param searchFunction a function returning a non-null
3932     * result on success, else null
3933     * @return a non-null result from applying the given search
3934     * function on each value, or null if none
3935 dl 1.102 * @since 1.8
3936 dl 1.84 */
3937 dl 1.102 public <U> U searchValues(long parallelismThreshold,
3938     Fun<? super V, ? extends U> searchFunction) {
3939     if (searchFunction == null) throw new NullPointerException();
3940     return new SearchValuesTask<K,V,U>
3941     (null, batchFor(parallelismThreshold), 0, 0, table,
3942     searchFunction, new AtomicReference<U>()).invoke();
3943 dl 1.84 }
3944    
3945     /**
3946     * Returns the result of accumulating all values using the
3947     * given reducer to combine values, or null if none.
3948     *
3949 dl 1.102 * @param parallelismThreshold the (estimated) number of elements
3950     * needed for this operation to be executed in parallel
3951 dl 1.84 * @param reducer a commutative associative combining function
3952 dl 1.102 * @return the result of accumulating all values
3953     * @since 1.8
3954 dl 1.84 */
3955 dl 1.102 public V reduceValues(long parallelismThreshold,
3956     BiFun<? super V, ? super V, ? extends V> reducer) {
3957     if (reducer == null) throw new NullPointerException();
3958     return new ReduceValuesTask<K,V>
3959     (null, batchFor(parallelismThreshold), 0, 0, table,
3960     null, reducer).invoke();
3961 dl 1.84 }
3962    
3963     /**
3964     * Returns the result of accumulating the given transformation
3965     * of all values using the given reducer to combine values, or
3966     * null if none.
3967     *
3968 dl 1.102 * @param parallelismThreshold the (estimated) number of elements
3969     * needed for this operation to be executed in parallel
3970 dl 1.84 * @param transformer a function returning the transformation
3971 jsr166 1.91 * for an element, or null if there is no transformation (in
3972 jsr166 1.93 * which case it is not combined)
3973 dl 1.84 * @param reducer a commutative associative combining function
3974     * @return the result of accumulating the given transformation
3975     * of all values
3976 dl 1.102 * @since 1.8
3977 dl 1.84 */
3978 dl 1.102 public <U> U reduceValues(long parallelismThreshold,
3979     Fun<? super V, ? extends U> transformer,
3980     BiFun<? super U, ? super U, ? extends U> reducer) {
3981     if (transformer == null || reducer == null)
3982     throw new NullPointerException();
3983     return new MapReduceValuesTask<K,V,U>
3984     (null, batchFor(parallelismThreshold), 0, 0, table,
3985     null, transformer, reducer).invoke();
3986 dl 1.84 }
3987    
3988     /**
3989     * Returns the result of accumulating the given transformation
3990     * of all values using the given reducer to combine values,
3991     * and the given basis as an identity value.
3992     *
3993 dl 1.102 * @param parallelismThreshold the (estimated) number of elements
3994     * needed for this operation to be executed in parallel
3995 dl 1.84 * @param transformer a function returning the transformation
3996     * for an element
3997     * @param basis the identity (initial default value) for the reduction
3998     * @param reducer a commutative associative combining function
3999     * @return the result of accumulating the given transformation
4000     * of all values
4001 dl 1.102 * @since 1.8
4002 dl 1.84 */
4003 dl 1.102 public double reduceValuesToDouble(long parallelismThreshold,
4004     ObjectToDouble<? super V> transformer,
4005     double basis,
4006     DoubleByDoubleToDouble reducer) {
4007     if (transformer == null || reducer == null)
4008     throw new NullPointerException();
4009     return new MapReduceValuesToDoubleTask<K,V>
4010     (null, batchFor(parallelismThreshold), 0, 0, table,
4011     null, transformer, basis, reducer).invoke();
4012 dl 1.84 }
4013    
4014     /**
4015     * Returns the result of accumulating the given transformation
4016     * of all values using the given reducer to combine values,
4017     * and the given basis as an identity value.
4018     *
4019 dl 1.102 * @param parallelismThreshold the (estimated) number of elements
4020     * needed for this operation to be executed in parallel
4021 dl 1.84 * @param transformer a function returning the transformation
4022     * for an element
4023     * @param basis the identity (initial default value) for the reduction
4024     * @param reducer a commutative associative combining function
4025     * @return the result of accumulating the given transformation
4026     * of all values
4027 dl 1.102 * @since 1.8
4028 dl 1.84 */
4029 dl 1.102 public long reduceValuesToLong(long parallelismThreshold,
4030     ObjectToLong<? super V> transformer,
4031     long basis,
4032     LongByLongToLong reducer) {
4033     if (transformer == null || reducer == null)
4034     throw new NullPointerException();
4035     return new MapReduceValuesToLongTask<K,V>
4036     (null, batchFor(parallelismThreshold), 0, 0, table,
4037     null, transformer, basis, reducer).invoke();
4038 dl 1.84 }
4039    
4040     /**
4041     * Returns the result of accumulating the given transformation
4042     * of all values using the given reducer to combine values,
4043     * and the given basis as an identity value.
4044     *
4045 dl 1.102 * @param parallelismThreshold the (estimated) number of elements
4046     * needed for this operation to be executed in parallel
4047 dl 1.84 * @param transformer a function returning the transformation
4048     * for an element
4049     * @param basis the identity (initial default value) for the reduction
4050     * @param reducer a commutative associative combining function
4051     * @return the result of accumulating the given transformation
4052     * of all values
4053 dl 1.102 * @since 1.8
4054 dl 1.84 */
4055 dl 1.102 public int reduceValuesToInt(long parallelismThreshold,
4056     ObjectToInt<? super V> transformer,
4057     int basis,
4058     IntByIntToInt reducer) {
4059     if (transformer == null || reducer == null)
4060     throw new NullPointerException();
4061     return new MapReduceValuesToIntTask<K,V>
4062     (null, batchFor(parallelismThreshold), 0, 0, table,
4063     null, transformer, basis, reducer).invoke();
4064 dl 1.84 }
4065    
4066     /**
4067     * Performs the given action for each entry.
4068     *
4069 dl 1.102 * @param parallelismThreshold the (estimated) number of elements
4070     * needed for this operation to be executed in parallel
4071 dl 1.84 * @param action the action
4072 dl 1.102 * @since 1.8
4073 dl 1.84 */
4074 dl 1.102 public void forEachEntry(long parallelismThreshold,
4075     Action<? super Map.Entry<K,V>> action) {
4076     if (action == null) throw new NullPointerException();
4077     new ForEachEntryTask<K,V>(null, batchFor(parallelismThreshold), 0, 0, table,
4078     action).invoke();
4079 dl 1.84 }
4080    
4081     /**
4082     * Performs the given action for each non-null transformation
4083     * of each entry.
4084     *
4085 dl 1.102 * @param parallelismThreshold the (estimated) number of elements
4086     * needed for this operation to be executed in parallel
4087 dl 1.84 * @param transformer a function returning the transformation
4088 jsr166 1.91 * for an element, or null if there is no transformation (in
4089 jsr166 1.93 * which case the action is not applied)
4090 dl 1.84 * @param action the action
4091 dl 1.102 * @since 1.8
4092 dl 1.84 */
4093 dl 1.102 public <U> void forEachEntry(long parallelismThreshold,
4094     Fun<Map.Entry<K,V>, ? extends U> transformer,
4095     Action<? super U> action) {
4096     if (transformer == null || action == null)
4097     throw new NullPointerException();
4098     new ForEachTransformedEntryTask<K,V,U>
4099     (null, batchFor(parallelismThreshold), 0, 0, table,
4100     transformer, action).invoke();
4101 dl 1.84 }
4102    
4103     /**
4104     * Returns a non-null result from applying the given search
4105     * function on each entry, or null if none. Upon success,
4106     * further element processing is suppressed and the results of
4107     * any other parallel invocations of the search function are
4108     * ignored.
4109     *
4110 dl 1.102 * @param parallelismThreshold the (estimated) number of elements
4111     * needed for this operation to be executed in parallel
4112 dl 1.84 * @param searchFunction a function returning a non-null
4113     * result on success, else null
4114     * @return a non-null result from applying the given search
4115     * function on each entry, or null if none
4116 dl 1.102 * @since 1.8
4117 dl 1.84 */
4118 dl 1.102 public <U> U searchEntries(long parallelismThreshold,
4119     Fun<Map.Entry<K,V>, ? extends U> searchFunction) {
4120     if (searchFunction == null) throw new NullPointerException();
4121     return new SearchEntriesTask<K,V,U>
4122     (null, batchFor(parallelismThreshold), 0, 0, table,
4123     searchFunction, new AtomicReference<U>()).invoke();
4124 dl 1.84 }
4125    
4126     /**
4127     * Returns the result of accumulating all entries using the
4128     * given reducer to combine values, or null if none.
4129     *
4130 dl 1.102 * @param parallelismThreshold the (estimated) number of elements
4131     * needed for this operation to be executed in parallel
4132 dl 1.84 * @param reducer a commutative associative combining function
4133     * @return the result of accumulating all entries
4134 dl 1.102 * @since 1.8
4135 dl 1.84 */
4136 dl 1.102 public Map.Entry<K,V> reduceEntries(long parallelismThreshold,
4137     BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
4138     if (reducer == null) throw new NullPointerException();
4139     return new ReduceEntriesTask<K,V>
4140     (null, batchFor(parallelismThreshold), 0, 0, table,
4141     null, reducer).invoke();
4142 dl 1.84 }
4143    
4144     /**
4145     * Returns the result of accumulating the given transformation
4146     * of all entries using the given reducer to combine values,
4147     * or null if none.
4148     *
4149 dl 1.102 * @param parallelismThreshold the (estimated) number of elements
4150     * needed for this operation to be executed in parallel
4151 dl 1.84 * @param transformer a function returning the transformation
4152 jsr166 1.91 * for an element, or null if there is no transformation (in
4153 jsr166 1.93 * which case it is not combined)
4154 dl 1.84 * @param reducer a commutative associative combining function
4155     * @return the result of accumulating the given transformation
4156     * of all entries
4157 dl 1.102 * @since 1.8
4158 dl 1.84 */
4159 dl 1.102 public <U> U reduceEntries(long parallelismThreshold,
4160     Fun<Map.Entry<K,V>, ? extends U> transformer,
4161     BiFun<? super U, ? super U, ? extends U> reducer) {
4162     if (transformer == null || reducer == null)
4163     throw new NullPointerException();
4164     return new MapReduceEntriesTask<K,V,U>
4165     (null, batchFor(parallelismThreshold), 0, 0, table,
4166     null, transformer, reducer).invoke();
4167 dl 1.84 }
4168    
4169     /**
4170     * Returns the result of accumulating the given transformation
4171     * of all entries using the given reducer to combine values,
4172     * and the given basis as an identity value.
4173     *
4174 dl 1.102 * @param parallelismThreshold the (estimated) number of elements
4175     * needed for this operation to be executed in parallel
4176 dl 1.84 * @param transformer a function returning the transformation
4177     * for an element
4178     * @param basis the identity (initial default value) for the reduction
4179     * @param reducer a commutative associative combining function
4180     * @return the result of accumulating the given transformation
4181     * of all entries
4182 dl 1.102 * @since 1.8
4183 dl 1.84 */
4184 dl 1.102 public double reduceEntriesToDouble(long parallelismThreshold,
4185     ObjectToDouble<Map.Entry<K,V>> transformer,
4186     double basis,
4187     DoubleByDoubleToDouble reducer) {
4188     if (transformer == null || reducer == null)
4189     throw new NullPointerException();
4190     return new MapReduceEntriesToDoubleTask<K,V>
4191     (null, batchFor(parallelismThreshold), 0, 0, table,
4192     null, transformer, basis, reducer).invoke();
4193 dl 1.84 }
4194    
4195     /**
4196     * Returns the result of accumulating the given transformation
4197     * of all entries using the given reducer to combine values,
4198     * and the given basis as an identity value.
4199     *
4200 dl 1.102 * @param parallelismThreshold the (estimated) number of elements
4201     * needed for this operation to be executed in parallel
4202 dl 1.84 * @param transformer a function returning the transformation
4203     * for an element
4204     * @param basis the identity (initial default value) for the reduction
4205     * @param reducer a commutative associative combining function
4206 dl 1.102 * @return the result of accumulating the given transformation
4207 dl 1.84 * of all entries
4208 dl 1.102 * @since 1.8
4209 dl 1.84 */
4210 dl 1.102 public long reduceEntriesToLong(long parallelismThreshold,
4211     ObjectToLong<Map.Entry<K,V>> transformer,
4212     long basis,
4213     LongByLongToLong reducer) {
4214     if (transformer == null || reducer == null)
4215     throw new NullPointerException();
4216     return new MapReduceEntriesToLongTask<K,V>
4217     (null, batchFor(parallelismThreshold), 0, 0, table,
4218     null, transformer, basis, reducer).invoke();
4219 dl 1.84 }
4220    
4221     /**
4222     * Returns the result of accumulating the given transformation
4223     * of all entries using the given reducer to combine values,
4224     * and the given basis as an identity value.
4225     *
4226 dl 1.102 * @param parallelismThreshold the (estimated) number of elements
4227     * needed for this operation to be executed in parallel
4228 dl 1.84 * @param transformer a function returning the transformation
4229     * for an element
4230     * @param basis the identity (initial default value) for the reduction
4231     * @param reducer a commutative associative combining function
4232     * @return the result of accumulating the given transformation
4233     * of all entries
4234 dl 1.102 * @since 1.8
4235 dl 1.84 */
4236 dl 1.102 public int reduceEntriesToInt(long parallelismThreshold,
4237     ObjectToInt<Map.Entry<K,V>> transformer,
4238     int basis,
4239     IntByIntToInt reducer) {
4240     if (transformer == null || reducer == null)
4241     throw new NullPointerException();
4242     return new MapReduceEntriesToIntTask<K,V>
4243     (null, batchFor(parallelismThreshold), 0, 0, table,
4244     null, transformer, basis, reducer).invoke();
4245 dl 1.84 }
4246    
4247    
4248     /* ----------------Views -------------- */
4249    
4250     /**
4251     * Base class for views.
4252     */
4253 dl 1.102 abstract static class CollectionView<K,V,E>
4254     implements Collection<E>, java.io.Serializable {
4255     private static final long serialVersionUID = 7249069246763182397L;
4256 jsr166 1.99 final ConcurrentHashMapV8<K,V> map;
4257 dl 1.102 CollectionView(ConcurrentHashMapV8<K,V> map) { this.map = map; }
4258 dl 1.84
4259     /**
4260     * Returns the map backing this view.
4261     *
4262     * @return the map backing this view
4263     */
4264     public ConcurrentHashMapV8<K,V> getMap() { return map; }
4265    
4266 dl 1.102 /**
4267     * Removes all of the elements from this view, by removing all
4268     * the mappings from the map backing this view.
4269     */
4270     public final void clear() { map.clear(); }
4271     public final int size() { return map.size(); }
4272     public final boolean isEmpty() { return map.isEmpty(); }
4273 dl 1.84
4274     // implementations below rely on concrete classes supplying these
4275 dl 1.102 // abstract methods
4276     /**
4277     * Returns a "weakly consistent" iterator that will never
4278     * throw {@link ConcurrentModificationException}, and
4279     * guarantees to traverse elements as they existed upon
4280     * construction of the iterator, and may (but is not
4281     * guaranteed to) reflect any modifications subsequent to
4282     * construction.
4283     */
4284     public abstract Iterator<E> iterator();
4285 jsr166 1.88 public abstract boolean contains(Object o);
4286     public abstract boolean remove(Object o);
4287 dl 1.84
4288     private static final String oomeMsg = "Required array size too large";
4289 dl 1.75
4290     public final Object[] toArray() {
4291     long sz = map.mappingCount();
4292 dl 1.102 if (sz > MAX_ARRAY_SIZE)
4293 dl 1.75 throw new OutOfMemoryError(oomeMsg);
4294     int n = (int)sz;
4295     Object[] r = new Object[n];
4296     int i = 0;
4297 dl 1.102 for (E e : this) {
4298 dl 1.75 if (i == n) {
4299     if (n >= MAX_ARRAY_SIZE)
4300     throw new OutOfMemoryError(oomeMsg);
4301     if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
4302     n = MAX_ARRAY_SIZE;
4303     else
4304     n += (n >>> 1) + 1;
4305     r = Arrays.copyOf(r, n);
4306     }
4307 dl 1.102 r[i++] = e;
4308 dl 1.75 }
4309     return (i == n) ? r : Arrays.copyOf(r, i);
4310     }
4311    
4312 dl 1.102 @SuppressWarnings("unchecked")
4313     public final <T> T[] toArray(T[] a) {
4314 dl 1.75 long sz = map.mappingCount();
4315 dl 1.102 if (sz > MAX_ARRAY_SIZE)
4316 dl 1.75 throw new OutOfMemoryError(oomeMsg);
4317     int m = (int)sz;
4318     T[] r = (a.length >= m) ? a :
4319     (T[])java.lang.reflect.Array
4320     .newInstance(a.getClass().getComponentType(), m);
4321     int n = r.length;
4322     int i = 0;
4323 dl 1.102 for (E e : this) {
4324 dl 1.75 if (i == n) {
4325     if (n >= MAX_ARRAY_SIZE)
4326     throw new OutOfMemoryError(oomeMsg);
4327     if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1)
4328     n = MAX_ARRAY_SIZE;
4329     else
4330     n += (n >>> 1) + 1;
4331     r = Arrays.copyOf(r, n);
4332     }
4333 dl 1.102 r[i++] = (T)e;
4334 dl 1.75 }
4335     if (a == r && i < n) {
4336     r[i] = null; // null-terminate
4337     return r;
4338     }
4339     return (i == n) ? r : Arrays.copyOf(r, i);
4340     }
4341    
4342 dl 1.102 /**
4343     * Returns a string representation of this collection.
4344     * The string representation consists of the string representations
4345     * of the collection's elements in the order they are returned by
4346     * its iterator, enclosed in square brackets ({@code "[]"}).
4347     * Adjacent elements are separated by the characters {@code ", "}
4348     * (comma and space). Elements are converted to strings as by
4349     * {@link String#valueOf(Object)}.
4350     *
4351     * @return a string representation of this collection
4352     */
4353 dl 1.75 public final String toString() {
4354     StringBuilder sb = new StringBuilder();
4355     sb.append('[');
4356 dl 1.102 Iterator<E> it = iterator();
4357 dl 1.75 if (it.hasNext()) {
4358     for (;;) {
4359     Object e = it.next();
4360     sb.append(e == this ? "(this Collection)" : e);
4361     if (!it.hasNext())
4362     break;
4363     sb.append(',').append(' ');
4364     }
4365     }
4366     return sb.append(']').toString();
4367     }
4368    
4369     public final boolean containsAll(Collection<?> c) {
4370     if (c != this) {
4371 dl 1.102 for (Object e : c) {
4372 dl 1.75 if (e == null || !contains(e))
4373     return false;
4374     }
4375     }
4376     return true;
4377     }
4378    
4379     public final boolean removeAll(Collection<?> c) {
4380     boolean modified = false;
4381 dl 1.102 for (Iterator<E> it = iterator(); it.hasNext();) {
4382 dl 1.75 if (c.contains(it.next())) {
4383     it.remove();
4384     modified = true;
4385     }
4386     }
4387     return modified;
4388     }
4389    
4390     public final boolean retainAll(Collection<?> c) {
4391     boolean modified = false;
4392 dl 1.102 for (Iterator<E> it = iterator(); it.hasNext();) {
4393 dl 1.75 if (!c.contains(it.next())) {
4394     it.remove();
4395     modified = true;
4396     }
4397     }
4398     return modified;
4399     }
4400    
4401     }
4402    
4403     /**
4404     * A view of a ConcurrentHashMapV8 as a {@link Set} of keys, in
4405     * which additions may optionally be enabled by mapping to a
4406 dl 1.102 * common value. This class cannot be directly instantiated.
4407     * See {@link #keySet() keySet()},
4408     * {@link #keySet(Object) keySet(V)},
4409     * {@link #newKeySet() newKeySet()},
4410     * {@link #newKeySet(int) newKeySet(int)}.
4411     *
4412     * @since 1.8
4413 dl 1.75 */
4414 dl 1.102 public static class KeySetView<K,V> extends CollectionView<K,V,K>
4415 dl 1.82 implements Set<K>, java.io.Serializable {
4416 dl 1.75 private static final long serialVersionUID = 7249069246763182397L;
4417     private final V value;
4418 jsr166 1.99 KeySetView(ConcurrentHashMapV8<K,V> map, V value) { // non-public
4419 dl 1.75 super(map);
4420     this.value = value;
4421     }
4422    
4423     /**
4424     * Returns the default mapped value for additions,
4425     * or {@code null} if additions are not supported.
4426     *
4427     * @return the default mapped value for additions, or {@code null}
4428 jsr166 1.93 * if not supported
4429 dl 1.75 */
4430     public V getMappedValue() { return value; }
4431    
4432 dl 1.102 /**
4433     * {@inheritDoc}
4434     * @throws NullPointerException if the specified key is null
4435     */
4436     public boolean contains(Object o) { return map.containsKey(o); }
4437    
4438     /**
4439     * Removes the key from this map view, by removing the key (and its
4440     * corresponding value) from the backing map. This method does
4441     * nothing if the key is not in the map.
4442     *
4443     * @param o the key to be removed from the backing map
4444     * @return {@code true} if the backing map contained the specified key
4445     * @throws NullPointerException if the specified key is null
4446     */
4447     public boolean remove(Object o) { return map.remove(o) != null; }
4448 dl 1.75
4449 dl 1.102 /**
4450     * @return an iterator over the keys of the backing map
4451     */
4452     public Iterator<K> iterator() {
4453     Node<K,V>[] t;
4454     ConcurrentHashMapV8<K,V> m = map;
4455     int f = (t = m.table) == null ? 0 : t.length;
4456     return new KeyIterator<K,V>(t, f, 0, f, m);
4457     }
4458 dl 1.75
4459     /**
4460 dl 1.102 * Adds the specified key to this set view by mapping the key to
4461     * the default mapped value in the backing map, if defined.
4462 dl 1.75 *
4463 dl 1.102 * @param e key to be added
4464     * @return {@code true} if this set changed as a result of the call
4465     * @throws NullPointerException if the specified key is null
4466     * @throws UnsupportedOperationException if no default mapped value
4467     * for additions was provided
4468 dl 1.75 */
4469     public boolean add(K e) {
4470     V v;
4471     if ((v = value) == null)
4472     throw new UnsupportedOperationException();
4473 dl 1.102 return map.putVal(e, v, true) == null;
4474 dl 1.75 }
4475 dl 1.102
4476     /**
4477     * Adds all of the elements in the specified collection to this set,
4478     * as if by calling {@link #add} on each one.
4479     *
4480     * @param c the elements to be inserted into this set
4481     * @return {@code true} if this set changed as a result of the call
4482     * @throws NullPointerException if the collection or any of its
4483     * elements are {@code null}
4484     * @throws UnsupportedOperationException if no default mapped value
4485     * for additions was provided
4486     */
4487 dl 1.75 public boolean addAll(Collection<? extends K> c) {
4488     boolean added = false;
4489     V v;
4490     if ((v = value) == null)
4491     throw new UnsupportedOperationException();
4492     for (K e : c) {
4493 dl 1.102 if (map.putVal(e, v, true) == null)
4494 dl 1.75 added = true;
4495     }
4496     return added;
4497     }
4498 dl 1.102
4499     public int hashCode() {
4500     int h = 0;
4501     for (K e : this)
4502     h += e.hashCode();
4503     return h;
4504     }
4505    
4506 dl 1.75 public boolean equals(Object o) {
4507     Set<?> c;
4508     return ((o instanceof Set) &&
4509     ((c = (Set<?>)o) == this ||
4510     (containsAll(c) && c.containsAll(this))));
4511     }
4512 dl 1.102
4513     public ConcurrentHashMapSpliterator<K> spliterator() {
4514     Node<K,V>[] t;
4515     ConcurrentHashMapV8<K,V> m = map;
4516     long n = m.sumCount();
4517     int f = (t = m.table) == null ? 0 : t.length;
4518     return new KeySpliterator<K,V>(t, f, 0, f, n < 0L ? 0L : n);
4519     }
4520    
4521     public void forEach(Action<? super K> action) {
4522     if (action == null) throw new NullPointerException();
4523     Node<K,V>[] t;
4524     if ((t = map.table) != null) {
4525     Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
4526     for (Node<K,V> p; (p = it.advance()) != null; )
4527     action.apply(p.key);
4528     }
4529     }
4530 dl 1.75 }
4531    
4532     /**
4533     * A view of a ConcurrentHashMapV8 as a {@link Collection} of
4534     * values, in which additions are disabled. This class cannot be
4535 jsr166 1.96 * directly instantiated. See {@link #values()}.
4536 dl 1.75 */
4537 dl 1.102 static final class ValuesView<K,V> extends CollectionView<K,V,V>
4538     implements Collection<V>, java.io.Serializable {
4539     private static final long serialVersionUID = 2249069246763182397L;
4540     ValuesView(ConcurrentHashMapV8<K,V> map) { super(map); }
4541     public final boolean contains(Object o) {
4542     return map.containsValue(o);
4543     }
4544    
4545 dl 1.75 public final boolean remove(Object o) {
4546     if (o != null) {
4547 dl 1.102 for (Iterator<V> it = iterator(); it.hasNext();) {
4548 dl 1.75 if (o.equals(it.next())) {
4549     it.remove();
4550     return true;
4551     }
4552     }
4553     }
4554     return false;
4555     }
4556    
4557     public final Iterator<V> iterator() {
4558 dl 1.102 ConcurrentHashMapV8<K,V> m = map;
4559     Node<K,V>[] t;
4560     int f = (t = m.table) == null ? 0 : t.length;
4561     return new ValueIterator<K,V>(t, f, 0, f, m);
4562 dl 1.75 }
4563 dl 1.102
4564 dl 1.75 public final boolean add(V e) {
4565     throw new UnsupportedOperationException();
4566     }
4567     public final boolean addAll(Collection<? extends V> c) {
4568     throw new UnsupportedOperationException();
4569     }
4570    
4571 dl 1.102 public ConcurrentHashMapSpliterator<V> spliterator() {
4572     Node<K,V>[] t;
4573     ConcurrentHashMapV8<K,V> m = map;
4574     long n = m.sumCount();
4575     int f = (t = m.table) == null ? 0 : t.length;
4576     return new ValueSpliterator<K,V>(t, f, 0, f, n < 0L ? 0L : n);
4577     }
4578    
4579     public void forEach(Action<? super V> action) {
4580     if (action == null) throw new NullPointerException();
4581     Node<K,V>[] t;
4582     if ((t = map.table) != null) {
4583     Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
4584     for (Node<K,V> p; (p = it.advance()) != null; )
4585     action.apply(p.val);
4586     }
4587     }
4588 dl 1.70 }
4589 dl 1.52
4590 dl 1.70 /**
4591 dl 1.75 * A view of a ConcurrentHashMapV8 as a {@link Set} of (key, value)
4592     * entries. This class cannot be directly instantiated. See
4593 jsr166 1.95 * {@link #entrySet()}.
4594 dl 1.70 */
4595 dl 1.102 static final class EntrySetView<K,V> extends CollectionView<K,V,Map.Entry<K,V>>
4596     implements Set<Map.Entry<K,V>>, java.io.Serializable {
4597     private static final long serialVersionUID = 2249069246763182397L;
4598 jsr166 1.99 EntrySetView(ConcurrentHashMapV8<K,V> map) { super(map); }
4599 dl 1.102
4600     public boolean contains(Object o) {
4601 dl 1.75 Object k, v, r; Map.Entry<?,?> e;
4602     return ((o instanceof Map.Entry) &&
4603     (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
4604     (r = map.get(k)) != null &&
4605     (v = e.getValue()) != null &&
4606     (v == r || v.equals(r)));
4607     }
4608 dl 1.102
4609     public boolean remove(Object o) {
4610 dl 1.75 Object k, v; Map.Entry<?,?> e;
4611     return ((o instanceof Map.Entry) &&
4612     (k = (e = (Map.Entry<?,?>)o).getKey()) != null &&
4613     (v = e.getValue()) != null &&
4614     map.remove(k, v));
4615     }
4616    
4617     /**
4618 dl 1.102 * @return an iterator over the entries of the backing map
4619 dl 1.75 */
4620 dl 1.102 public Iterator<Map.Entry<K,V>> iterator() {
4621     ConcurrentHashMapV8<K,V> m = map;
4622     Node<K,V>[] t;
4623     int f = (t = m.table) == null ? 0 : t.length;
4624     return new EntryIterator<K,V>(t, f, 0, f, m);
4625 dl 1.75 }
4626    
4627 dl 1.102 public boolean add(Entry<K,V> e) {
4628     return map.putVal(e.getKey(), e.getValue(), false) == null;
4629 dl 1.75 }
4630 dl 1.102
4631     public boolean addAll(Collection<? extends Entry<K,V>> c) {
4632 dl 1.75 boolean added = false;
4633     for (Entry<K,V> e : c) {
4634     if (add(e))
4635     added = true;
4636     }
4637     return added;
4638     }
4639 dl 1.52
4640 dl 1.102 public final int hashCode() {
4641     int h = 0;
4642     Node<K,V>[] t;
4643     if ((t = map.table) != null) {
4644     Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
4645     for (Node<K,V> p; (p = it.advance()) != null; ) {
4646     h += p.hashCode();
4647     }
4648     }
4649     return h;
4650 dl 1.52 }
4651    
4652 dl 1.102 public final boolean equals(Object o) {
4653     Set<?> c;
4654     return ((o instanceof Set) &&
4655     ((c = (Set<?>)o) == this ||
4656     (containsAll(c) && c.containsAll(this))));
4657 dl 1.52 }
4658    
4659 dl 1.102 public ConcurrentHashMapSpliterator<Map.Entry<K,V>> spliterator() {
4660     Node<K,V>[] t;
4661     ConcurrentHashMapV8<K,V> m = map;
4662     long n = m.sumCount();
4663     int f = (t = m.table) == null ? 0 : t.length;
4664     return new EntrySpliterator<K,V>(t, f, 0, f, n < 0L ? 0L : n, m);
4665 dl 1.52 }
4666    
4667 dl 1.102 public void forEach(Action<? super Map.Entry<K,V>> action) {
4668     if (action == null) throw new NullPointerException();
4669     Node<K,V>[] t;
4670     if ((t = map.table) != null) {
4671     Traverser<K,V> it = new Traverser<K,V>(t, t.length, 0, t.length);
4672     for (Node<K,V> p; (p = it.advance()) != null; )
4673     action.apply(new MapEntry<K,V>(p.key, p.val, map));
4674     }
4675 dl 1.52 }
4676    
4677 dl 1.102 }
4678 dl 1.52
4679 dl 1.102 // -------------------------------------------------------
4680 dl 1.52
4681 dl 1.102 /**
4682     * Base class for bulk tasks. Repeats some fields and code from
4683     * class Traverser, because we need to subclass CountedCompleter.
4684     */
4685     abstract static class BulkTask<K,V,R> extends CountedCompleter<R> {
4686     Node<K,V>[] tab; // same as Traverser
4687     Node<K,V> next;
4688     int index;
4689     int baseIndex;
4690     int baseLimit;
4691     final int baseSize;
4692     int batch; // split control
4693    
4694     BulkTask(BulkTask<K,V,?> par, int b, int i, int f, Node<K,V>[] t) {
4695     super(par);
4696     this.batch = b;
4697     this.index = this.baseIndex = i;
4698     if ((this.tab = t) == null)
4699     this.baseSize = this.baseLimit = 0;
4700     else if (par == null)
4701     this.baseSize = this.baseLimit = t.length;
4702     else {
4703     this.baseLimit = f;
4704     this.baseSize = par.baseSize;
4705     }
4706 dl 1.52 }
4707    
4708     /**
4709 dl 1.102 * Same as Traverser version
4710 dl 1.52 */
4711 dl 1.102 final Node<K,V> advance() {
4712     Node<K,V> e;
4713     if ((e = next) != null)
4714     e = e.next;
4715     for (;;) {
4716     Node<K,V>[] t; int i, n; K ek; // must use locals in checks
4717     if (e != null)
4718     return next = e;
4719     if (baseIndex >= baseLimit || (t = tab) == null ||
4720     (n = t.length) <= (i = index) || i < 0)
4721     return next = null;
4722     if ((e = tabAt(t, index)) != null && e.hash < 0) {
4723     if (e instanceof ForwardingNode) {
4724     tab = ((ForwardingNode<K,V>)e).nextTable;
4725     e = null;
4726     continue;
4727     }
4728     else if (e instanceof TreeBin)
4729     e = ((TreeBin<K,V>)e).first;
4730     else
4731     e = null;
4732     }
4733     if ((index += baseSize) >= n)
4734     index = ++baseIndex; // visit upper slots if present
4735     }
4736 dl 1.52 }
4737     }
4738    
4739     /*
4740     * Task classes. Coded in a regular but ugly format/style to
4741     * simplify checks that each variant differs in the right way from
4742 dl 1.82 * others. The null screenings exist because compilers cannot tell
4743     * that we've already null-checked task arguments, so we force
4744     * simplest hoisted bypass to help avoid convoluted traps.
4745 dl 1.52 */
4746 dl 1.102 @SuppressWarnings("serial")
4747     static final class ForEachKeyTask<K,V>
4748     extends BulkTask<K,V,Void> {
4749     final Action<? super K> action;
4750 dl 1.52 ForEachKeyTask
4751 dl 1.102 (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4752     Action<? super K> action) {
4753     super(p, b, i, f, t);
4754 dl 1.52 this.action = action;
4755     }
4756 dl 1.102 public final void compute() {
4757     final Action<? super K> action;
4758 dl 1.82 if ((action = this.action) != null) {
4759 dl 1.102 for (int i = baseIndex, f, h; batch > 0 &&
4760     (h = ((f = baseLimit) + i) >>> 1) > i;) {
4761     addToPendingCount(1);
4762     new ForEachKeyTask<K,V>
4763     (this, batch >>>= 1, baseLimit = h, f, tab,
4764     action).fork();
4765     }
4766     for (Node<K,V> p; (p = advance()) != null;)
4767     action.apply(p.key);
4768 dl 1.82 propagateCompletion();
4769     }
4770 dl 1.52 }
4771     }
4772    
4773 dl 1.102 @SuppressWarnings("serial")
4774     static final class ForEachValueTask<K,V>
4775     extends BulkTask<K,V,Void> {
4776     final Action<? super V> action;
4777 dl 1.52 ForEachValueTask
4778 dl 1.102 (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4779     Action<? super V> action) {
4780     super(p, b, i, f, t);
4781 dl 1.52 this.action = action;
4782     }
4783 dl 1.102 public final void compute() {
4784     final Action<? super V> action;
4785 dl 1.82 if ((action = this.action) != null) {
4786 dl 1.102 for (int i = baseIndex, f, h; batch > 0 &&
4787     (h = ((f = baseLimit) + i) >>> 1) > i;) {
4788     addToPendingCount(1);
4789     new ForEachValueTask<K,V>
4790     (this, batch >>>= 1, baseLimit = h, f, tab,
4791     action).fork();
4792     }
4793     for (Node<K,V> p; (p = advance()) != null;)
4794     action.apply(p.val);
4795 dl 1.82 propagateCompletion();
4796     }
4797 dl 1.52 }
4798     }
4799    
4800 dl 1.102 @SuppressWarnings("serial")
4801     static final class ForEachEntryTask<K,V>
4802     extends BulkTask<K,V,Void> {
4803     final Action<? super Entry<K,V>> action;
4804 dl 1.52 ForEachEntryTask
4805 dl 1.102 (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4806     Action<? super Entry<K,V>> action) {
4807     super(p, b, i, f, t);
4808 dl 1.52 this.action = action;
4809     }
4810 dl 1.102 public final void compute() {
4811     final Action<? super Entry<K,V>> action;
4812 dl 1.82 if ((action = this.action) != null) {
4813 dl 1.102 for (int i = baseIndex, f, h; batch > 0 &&
4814     (h = ((f = baseLimit) + i) >>> 1) > i;) {
4815     addToPendingCount(1);
4816     new ForEachEntryTask<K,V>
4817     (this, batch >>>= 1, baseLimit = h, f, tab,
4818     action).fork();
4819     }
4820     for (Node<K,V> p; (p = advance()) != null; )
4821     action.apply(p);
4822 dl 1.82 propagateCompletion();
4823     }
4824 dl 1.52 }
4825     }
4826    
4827 dl 1.102 @SuppressWarnings("serial")
4828     static final class ForEachMappingTask<K,V>
4829     extends BulkTask<K,V,Void> {
4830     final BiAction<? super K, ? super V> action;
4831 dl 1.52 ForEachMappingTask
4832 dl 1.102 (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4833     BiAction<? super K,? super V> action) {
4834     super(p, b, i, f, t);
4835 dl 1.52 this.action = action;
4836     }
4837 dl 1.102 public final void compute() {
4838     final BiAction<? super K, ? super V> action;
4839 dl 1.82 if ((action = this.action) != null) {
4840 dl 1.102 for (int i = baseIndex, f, h; batch > 0 &&
4841     (h = ((f = baseLimit) + i) >>> 1) > i;) {
4842     addToPendingCount(1);
4843     new ForEachMappingTask<K,V>
4844     (this, batch >>>= 1, baseLimit = h, f, tab,
4845     action).fork();
4846     }
4847     for (Node<K,V> p; (p = advance()) != null; )
4848     action.apply(p.key, p.val);
4849 dl 1.82 propagateCompletion();
4850     }
4851 dl 1.52 }
4852     }
4853    
4854 dl 1.102 @SuppressWarnings("serial")
4855     static final class ForEachTransformedKeyTask<K,V,U>
4856     extends BulkTask<K,V,Void> {
4857 dl 1.52 final Fun<? super K, ? extends U> transformer;
4858 dl 1.102 final Action<? super U> action;
4859 dl 1.52 ForEachTransformedKeyTask
4860 dl 1.102 (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4861     Fun<? super K, ? extends U> transformer, Action<? super U> action) {
4862     super(p, b, i, f, t);
4863 dl 1.79 this.transformer = transformer; this.action = action;
4864     }
4865 dl 1.102 public final void compute() {
4866 dl 1.79 final Fun<? super K, ? extends U> transformer;
4867 dl 1.102 final Action<? super U> action;
4868 dl 1.82 if ((transformer = this.transformer) != null &&
4869     (action = this.action) != null) {
4870 dl 1.102 for (int i = baseIndex, f, h; batch > 0 &&
4871     (h = ((f = baseLimit) + i) >>> 1) > i;) {
4872     addToPendingCount(1);
4873 dl 1.82 new ForEachTransformedKeyTask<K,V,U>
4874 dl 1.102 (this, batch >>>= 1, baseLimit = h, f, tab,
4875     transformer, action).fork();
4876     }
4877     for (Node<K,V> p; (p = advance()) != null; ) {
4878     U u;
4879     if ((u = transformer.apply(p.key)) != null)
4880 dl 1.82 action.apply(u);
4881     }
4882     propagateCompletion();
4883 dl 1.52 }
4884     }
4885     }
4886    
4887 dl 1.102 @SuppressWarnings("serial")
4888     static final class ForEachTransformedValueTask<K,V,U>
4889     extends BulkTask<K,V,Void> {
4890 dl 1.52 final Fun<? super V, ? extends U> transformer;
4891 dl 1.102 final Action<? super U> action;
4892 dl 1.52 ForEachTransformedValueTask
4893 dl 1.102 (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4894     Fun<? super V, ? extends U> transformer, Action<? super U> action) {
4895     super(p, b, i, f, t);
4896 dl 1.79 this.transformer = transformer; this.action = action;
4897     }
4898 dl 1.102 public final void compute() {
4899 dl 1.79 final Fun<? super V, ? extends U> transformer;
4900 dl 1.102 final Action<? super U> action;
4901 dl 1.82 if ((transformer = this.transformer) != null &&
4902     (action = this.action) != null) {
4903 dl 1.102 for (int i = baseIndex, f, h; batch > 0 &&
4904     (h = ((f = baseLimit) + i) >>> 1) > i;) {
4905     addToPendingCount(1);
4906 dl 1.82 new ForEachTransformedValueTask<K,V,U>
4907 dl 1.102 (this, batch >>>= 1, baseLimit = h, f, tab,
4908     transformer, action).fork();
4909     }
4910     for (Node<K,V> p; (p = advance()) != null; ) {
4911     U u;
4912     if ((u = transformer.apply(p.val)) != null)
4913 dl 1.82 action.apply(u);
4914     }
4915     propagateCompletion();
4916 dl 1.52 }
4917     }
4918     }
4919    
4920 dl 1.102 @SuppressWarnings("serial")
4921     static final class ForEachTransformedEntryTask<K,V,U>
4922     extends BulkTask<K,V,Void> {
4923 dl 1.52 final Fun<Map.Entry<K,V>, ? extends U> transformer;
4924 dl 1.102 final Action<? super U> action;
4925 dl 1.52 ForEachTransformedEntryTask
4926 dl 1.102 (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4927     Fun<Map.Entry<K,V>, ? extends U> transformer, Action<? super U> action) {
4928     super(p, b, i, f, t);
4929 dl 1.79 this.transformer = transformer; this.action = action;
4930     }
4931 dl 1.102 public final void compute() {
4932 dl 1.79 final Fun<Map.Entry<K,V>, ? extends U> transformer;
4933 dl 1.102 final Action<? super U> action;
4934 dl 1.82 if ((transformer = this.transformer) != null &&
4935     (action = this.action) != null) {
4936 dl 1.102 for (int i = baseIndex, f, h; batch > 0 &&
4937     (h = ((f = baseLimit) + i) >>> 1) > i;) {
4938     addToPendingCount(1);
4939 dl 1.82 new ForEachTransformedEntryTask<K,V,U>
4940 dl 1.102 (this, batch >>>= 1, baseLimit = h, f, tab,
4941     transformer, action).fork();
4942     }
4943     for (Node<K,V> p; (p = advance()) != null; ) {
4944     U u;
4945     if ((u = transformer.apply(p)) != null)
4946 dl 1.82 action.apply(u);
4947     }
4948     propagateCompletion();
4949 dl 1.52 }
4950     }
4951     }
4952    
4953 dl 1.102 @SuppressWarnings("serial")
4954     static final class ForEachTransformedMappingTask<K,V,U>
4955     extends BulkTask<K,V,Void> {
4956 dl 1.52 final BiFun<? super K, ? super V, ? extends U> transformer;
4957 dl 1.102 final Action<? super U> action;
4958 dl 1.52 ForEachTransformedMappingTask
4959 dl 1.102 (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4960 dl 1.52 BiFun<? super K, ? super V, ? extends U> transformer,
4961 dl 1.102 Action<? super U> action) {
4962     super(p, b, i, f, t);
4963 dl 1.79 this.transformer = transformer; this.action = action;
4964 dl 1.52 }
4965 dl 1.102 public final void compute() {
4966 dl 1.79 final BiFun<? super K, ? super V, ? extends U> transformer;
4967 dl 1.102 final Action<? super U> action;
4968 dl 1.82 if ((transformer = this.transformer) != null &&
4969     (action = this.action) != null) {
4970 dl 1.102 for (int i = baseIndex, f, h; batch > 0 &&
4971     (h = ((f = baseLimit) + i) >>> 1) > i;) {
4972     addToPendingCount(1);
4973 dl 1.82 new ForEachTransformedMappingTask<K,V,U>
4974 dl 1.102 (this, batch >>>= 1, baseLimit = h, f, tab,
4975     transformer, action).fork();
4976     }
4977     for (Node<K,V> p; (p = advance()) != null; ) {
4978     U u;
4979     if ((u = transformer.apply(p.key, p.val)) != null)
4980 dl 1.82 action.apply(u);
4981     }
4982     propagateCompletion();
4983 dl 1.52 }
4984     }
4985     }
4986    
4987 dl 1.102 @SuppressWarnings("serial")
4988     static final class SearchKeysTask<K,V,U>
4989     extends BulkTask<K,V,U> {
4990 dl 1.52 final Fun<? super K, ? extends U> searchFunction;
4991     final AtomicReference<U> result;
4992     SearchKeysTask
4993 dl 1.102 (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
4994 dl 1.52 Fun<? super K, ? extends U> searchFunction,
4995     AtomicReference<U> result) {
4996 dl 1.102 super(p, b, i, f, t);
4997 dl 1.52 this.searchFunction = searchFunction; this.result = result;
4998     }
4999 dl 1.79 public final U getRawResult() { return result.get(); }
5000 dl 1.102 public final void compute() {
5001 dl 1.79 final Fun<? super K, ? extends U> searchFunction;
5002     final AtomicReference<U> result;
5003 dl 1.82 if ((searchFunction = this.searchFunction) != null &&
5004     (result = this.result) != null) {
5005 dl 1.102 for (int i = baseIndex, f, h; batch > 0 &&
5006     (h = ((f = baseLimit) + i) >>> 1) > i;) {
5007 dl 1.82 if (result.get() != null)
5008     return;
5009 dl 1.102 addToPendingCount(1);
5010 dl 1.82 new SearchKeysTask<K,V,U>
5011 dl 1.102 (this, batch >>>= 1, baseLimit = h, f, tab,
5012     searchFunction, result).fork();
5013 dl 1.61 }
5014 dl 1.82 while (result.get() == null) {
5015     U u;
5016 dl 1.102 Node<K,V> p;
5017     if ((p = advance()) == null) {
5018 dl 1.82 propagateCompletion();
5019     break;
5020     }
5021 dl 1.102 if ((u = searchFunction.apply(p.key)) != null) {
5022 dl 1.82 if (result.compareAndSet(null, u))
5023     quietlyCompleteRoot();
5024     break;
5025     }
5026 dl 1.52 }
5027     }
5028     }
5029     }
5030    
5031 dl 1.102 @SuppressWarnings("serial")
5032     static final class SearchValuesTask<K,V,U>
5033     extends BulkTask<K,V,U> {
5034 dl 1.52 final Fun<? super V, ? extends U> searchFunction;
5035     final AtomicReference<U> result;
5036     SearchValuesTask
5037 dl 1.102 (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5038 dl 1.52 Fun<? super V, ? extends U> searchFunction,
5039     AtomicReference<U> result) {
5040 dl 1.102 super(p, b, i, f, t);
5041 dl 1.52 this.searchFunction = searchFunction; this.result = result;
5042     }
5043 dl 1.79 public final U getRawResult() { return result.get(); }
5044 dl 1.102 public final void compute() {
5045 dl 1.79 final Fun<? super V, ? extends U> searchFunction;
5046     final AtomicReference<U> result;
5047 dl 1.82 if ((searchFunction = this.searchFunction) != null &&
5048     (result = this.result) != null) {
5049 dl 1.102 for (int i = baseIndex, f, h; batch > 0 &&
5050     (h = ((f = baseLimit) + i) >>> 1) > i;) {
5051 dl 1.82 if (result.get() != null)
5052     return;
5053 dl 1.102 addToPendingCount(1);
5054 dl 1.82 new SearchValuesTask<K,V,U>
5055 dl 1.102 (this, batch >>>= 1, baseLimit = h, f, tab,
5056     searchFunction, result).fork();
5057 dl 1.61 }
5058 dl 1.82 while (result.get() == null) {
5059 dl 1.102 U u;
5060     Node<K,V> p;
5061     if ((p = advance()) == null) {
5062 dl 1.82 propagateCompletion();
5063     break;
5064     }
5065 dl 1.102 if ((u = searchFunction.apply(p.val)) != null) {
5066 dl 1.82 if (result.compareAndSet(null, u))
5067     quietlyCompleteRoot();
5068     break;
5069     }
5070 dl 1.52 }
5071     }
5072     }
5073     }
5074    
5075 dl 1.102 @SuppressWarnings("serial")
5076     static final class SearchEntriesTask<K,V,U>
5077     extends BulkTask<K,V,U> {
5078 dl 1.52 final Fun<Entry<K,V>, ? extends U> searchFunction;
5079     final AtomicReference<U> result;
5080     SearchEntriesTask
5081 dl 1.102 (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5082 dl 1.52 Fun<Entry<K,V>, ? extends U> searchFunction,
5083     AtomicReference<U> result) {
5084 dl 1.102 super(p, b, i, f, t);
5085 dl 1.52 this.searchFunction = searchFunction; this.result = result;
5086     }
5087 dl 1.79 public final U getRawResult() { return result.get(); }
5088 dl 1.102 public final void compute() {
5089 dl 1.79 final Fun<Entry<K,V>, ? extends U> searchFunction;
5090     final AtomicReference<U> result;
5091 dl 1.82 if ((searchFunction = this.searchFunction) != null &&
5092     (result = this.result) != null) {
5093 dl 1.102 for (int i = baseIndex, f, h; batch > 0 &&
5094     (h = ((f = baseLimit) + i) >>> 1) > i;) {
5095 dl 1.82 if (result.get() != null)
5096     return;
5097 dl 1.102 addToPendingCount(1);
5098 dl 1.82 new SearchEntriesTask<K,V,U>
5099 dl 1.102 (this, batch >>>= 1, baseLimit = h, f, tab,
5100     searchFunction, result).fork();
5101 dl 1.61 }
5102 dl 1.82 while (result.get() == null) {
5103 dl 1.102 U u;
5104     Node<K,V> p;
5105     if ((p = advance()) == null) {
5106 dl 1.82 propagateCompletion();
5107     break;
5108     }
5109 dl 1.102 if ((u = searchFunction.apply(p)) != null) {
5110 dl 1.82 if (result.compareAndSet(null, u))
5111     quietlyCompleteRoot();
5112     return;
5113     }
5114 dl 1.52 }
5115     }
5116     }
5117     }
5118    
5119 dl 1.102 @SuppressWarnings("serial")
5120     static final class SearchMappingsTask<K,V,U>
5121     extends BulkTask<K,V,U> {
5122 dl 1.52 final BiFun<? super K, ? super V, ? extends U> searchFunction;
5123     final AtomicReference<U> result;
5124     SearchMappingsTask
5125 dl 1.102 (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5126 dl 1.52 BiFun<? super K, ? super V, ? extends U> searchFunction,
5127     AtomicReference<U> result) {
5128 dl 1.102 super(p, b, i, f, t);
5129 dl 1.52 this.searchFunction = searchFunction; this.result = result;
5130     }
5131 dl 1.79 public final U getRawResult() { return result.get(); }
5132 dl 1.102 public final void compute() {
5133 dl 1.79 final BiFun<? super K, ? super V, ? extends U> searchFunction;
5134     final AtomicReference<U> result;
5135 dl 1.82 if ((searchFunction = this.searchFunction) != null &&
5136     (result = this.result) != null) {
5137 dl 1.102 for (int i = baseIndex, f, h; batch > 0 &&
5138     (h = ((f = baseLimit) + i) >>> 1) > i;) {
5139 dl 1.82 if (result.get() != null)
5140     return;
5141 dl 1.102 addToPendingCount(1);
5142 dl 1.82 new SearchMappingsTask<K,V,U>
5143 dl 1.102 (this, batch >>>= 1, baseLimit = h, f, tab,
5144     searchFunction, result).fork();
5145 dl 1.61 }
5146 dl 1.82 while (result.get() == null) {
5147 dl 1.102 U u;
5148     Node<K,V> p;
5149     if ((p = advance()) == null) {
5150 dl 1.82 propagateCompletion();
5151     break;
5152     }
5153 dl 1.102 if ((u = searchFunction.apply(p.key, p.val)) != null) {
5154 dl 1.82 if (result.compareAndSet(null, u))
5155     quietlyCompleteRoot();
5156     break;
5157     }
5158 dl 1.52 }
5159     }
5160     }
5161     }
5162    
5163 dl 1.102 @SuppressWarnings("serial")
5164     static final class ReduceKeysTask<K,V>
5165     extends BulkTask<K,V,K> {
5166 dl 1.52 final BiFun<? super K, ? super K, ? extends K> reducer;
5167     K result;
5168 dl 1.61 ReduceKeysTask<K,V> rights, nextRight;
5169 dl 1.52 ReduceKeysTask
5170 dl 1.102 (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5171 dl 1.61 ReduceKeysTask<K,V> nextRight,
5172 dl 1.52 BiFun<? super K, ? super K, ? extends K> reducer) {
5173 dl 1.102 super(p, b, i, f, t); this.nextRight = nextRight;
5174 dl 1.52 this.reducer = reducer;
5175     }
5176 dl 1.79 public final K getRawResult() { return result; }
5177 dl 1.102 public final void compute() {
5178 dl 1.82 final BiFun<? super K, ? super K, ? extends K> reducer;
5179     if ((reducer = this.reducer) != null) {
5180 dl 1.102 for (int i = baseIndex, f, h; batch > 0 &&
5181     (h = ((f = baseLimit) + i) >>> 1) > i;) {
5182     addToPendingCount(1);
5183 dl 1.82 (rights = new ReduceKeysTask<K,V>
5184 dl 1.102 (this, batch >>>= 1, baseLimit = h, f, tab,
5185     rights, reducer)).fork();
5186     }
5187 dl 1.82 K r = null;
5188 dl 1.102 for (Node<K,V> p; (p = advance()) != null; ) {
5189     K u = p.key;
5190     r = (r == null) ? u : u == null ? r : reducer.apply(r, u);
5191 dl 1.82 }
5192     result = r;
5193     CountedCompleter<?> c;
5194     for (c = firstComplete(); c != null; c = c.nextComplete()) {
5195 dl 1.102 @SuppressWarnings("unchecked") ReduceKeysTask<K,V>
5196 dl 1.82 t = (ReduceKeysTask<K,V>)c,
5197     s = t.rights;
5198     while (s != null) {
5199     K tr, sr;
5200     if ((sr = s.result) != null)
5201     t.result = (((tr = t.result) == null) ? sr :
5202     reducer.apply(tr, sr));
5203     s = t.rights = s.nextRight;
5204     }
5205 dl 1.52 }
5206 dl 1.71 }
5207 dl 1.52 }
5208     }
5209    
5210 dl 1.102 @SuppressWarnings("serial")
5211     static final class ReduceValuesTask<K,V>
5212     extends BulkTask<K,V,V> {
5213 dl 1.52 final BiFun<? super V, ? super V, ? extends V> reducer;
5214     V result;
5215 dl 1.61 ReduceValuesTask<K,V> rights, nextRight;
5216 dl 1.52 ReduceValuesTask
5217 dl 1.102 (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5218 dl 1.61 ReduceValuesTask<K,V> nextRight,
5219 dl 1.52 BiFun<? super V, ? super V, ? extends V> reducer) {
5220 dl 1.102 super(p, b, i, f, t); this.nextRight = nextRight;
5221 dl 1.52 this.reducer = reducer;
5222     }
5223 dl 1.79 public final V getRawResult() { return result; }
5224 dl 1.102 public final void compute() {
5225 dl 1.82 final BiFun<? super V, ? super V, ? extends V> reducer;
5226     if ((reducer = this.reducer) != null) {
5227 dl 1.102 for (int i = baseIndex, f, h; batch > 0 &&
5228     (h = ((f = baseLimit) + i) >>> 1) > i;) {
5229     addToPendingCount(1);
5230 dl 1.82 (rights = new ReduceValuesTask<K,V>
5231 dl 1.102 (this, batch >>>= 1, baseLimit = h, f, tab,
5232     rights, reducer)).fork();
5233     }
5234 dl 1.82 V r = null;
5235 dl 1.102 for (Node<K,V> p; (p = advance()) != null; ) {
5236     V v = p.val;
5237     r = (r == null) ? v : reducer.apply(r, v);
5238 dl 1.82 }
5239     result = r;
5240     CountedCompleter<?> c;
5241     for (c = firstComplete(); c != null; c = c.nextComplete()) {
5242 dl 1.102 @SuppressWarnings("unchecked") ReduceValuesTask<K,V>
5243 dl 1.82 t = (ReduceValuesTask<K,V>)c,
5244     s = t.rights;
5245     while (s != null) {
5246     V tr, sr;
5247     if ((sr = s.result) != null)
5248     t.result = (((tr = t.result) == null) ? sr :
5249     reducer.apply(tr, sr));
5250     s = t.rights = s.nextRight;
5251     }
5252 dl 1.52 }
5253     }
5254     }
5255     }
5256    
5257 dl 1.102 @SuppressWarnings("serial")
5258     static final class ReduceEntriesTask<K,V>
5259     extends BulkTask<K,V,Map.Entry<K,V>> {
5260 dl 1.52 final BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer;
5261     Map.Entry<K,V> result;
5262 dl 1.61 ReduceEntriesTask<K,V> rights, nextRight;
5263 dl 1.52 ReduceEntriesTask
5264 dl 1.102 (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5265 dl 1.63 ReduceEntriesTask<K,V> nextRight,
5266 dl 1.52 BiFun<Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
5267 dl 1.102 super(p, b, i, f, t); this.nextRight = nextRight;
5268 dl 1.52 this.reducer = reducer;
5269     }
5270 dl 1.79 public final Map.Entry<K,V> getRawResult() { return result; }
5271 dl 1.102 public final void compute() {
5272 dl 1.82 final BiFun<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer;
5273     if ((reducer = this.reducer) != null) {
5274 dl 1.102 for (int i = baseIndex, f, h; batch > 0 &&
5275     (h = ((f = baseLimit) + i) >>> 1) > i;) {
5276     addToPendingCount(1);
5277 dl 1.82 (rights = new ReduceEntriesTask<K,V>
5278 dl 1.102 (this, batch >>>= 1, baseLimit = h, f, tab,
5279     rights, reducer)).fork();
5280     }
5281 dl 1.82 Map.Entry<K,V> r = null;
5282 dl 1.102 for (Node<K,V> p; (p = advance()) != null; )
5283     r = (r == null) ? p : reducer.apply(r, p);
5284 dl 1.82 result = r;
5285     CountedCompleter<?> c;
5286     for (c = firstComplete(); c != null; c = c.nextComplete()) {
5287 dl 1.102 @SuppressWarnings("unchecked") ReduceEntriesTask<K,V>
5288 dl 1.82 t = (ReduceEntriesTask<K,V>)c,
5289     s = t.rights;
5290     while (s != null) {
5291     Map.Entry<K,V> tr, sr;
5292     if ((sr = s.result) != null)
5293     t.result = (((tr = t.result) == null) ? sr :
5294     reducer.apply(tr, sr));
5295     s = t.rights = s.nextRight;
5296     }
5297 dl 1.52 }
5298     }
5299     }
5300     }
5301    
5302 dl 1.102 @SuppressWarnings("serial")
5303     static final class MapReduceKeysTask<K,V,U>
5304     extends BulkTask<K,V,U> {
5305 dl 1.52 final Fun<? super K, ? extends U> transformer;
5306     final BiFun<? super U, ? super U, ? extends U> reducer;
5307     U result;
5308 dl 1.61 MapReduceKeysTask<K,V,U> rights, nextRight;
5309 dl 1.52 MapReduceKeysTask
5310 dl 1.102 (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5311 dl 1.61 MapReduceKeysTask<K,V,U> nextRight,
5312 dl 1.52 Fun<? super K, ? extends U> transformer,
5313     BiFun<? super U, ? super U, ? extends U> reducer) {
5314 dl 1.102 super(p, b, i, f, t); this.nextRight = nextRight;
5315 dl 1.52 this.transformer = transformer;
5316     this.reducer = reducer;
5317     }
5318 dl 1.79 public final U getRawResult() { return result; }
5319 dl 1.102 public final void compute() {
5320 dl 1.82 final Fun<? super K, ? extends U> transformer;
5321     final BiFun<? super U, ? super U, ? extends U> reducer;
5322     if ((transformer = this.transformer) != null &&
5323     (reducer = this.reducer) != null) {
5324 dl 1.102 for (int i = baseIndex, f, h; batch > 0 &&
5325     (h = ((f = baseLimit) + i) >>> 1) > i;) {
5326     addToPendingCount(1);
5327 dl 1.82 (rights = new MapReduceKeysTask<K,V,U>
5328 dl 1.102 (this, batch >>>= 1, baseLimit = h, f, tab,
5329     rights, transformer, reducer)).fork();
5330     }
5331     U r = null;
5332     for (Node<K,V> p; (p = advance()) != null; ) {
5333     U u;
5334     if ((u = transformer.apply(p.key)) != null)
5335 dl 1.82 r = (r == null) ? u : reducer.apply(r, u);
5336     }
5337     result = r;
5338     CountedCompleter<?> c;
5339     for (c = firstComplete(); c != null; c = c.nextComplete()) {
5340 dl 1.102 @SuppressWarnings("unchecked") MapReduceKeysTask<K,V,U>
5341 dl 1.82 t = (MapReduceKeysTask<K,V,U>)c,
5342     s = t.rights;
5343     while (s != null) {
5344     U tr, sr;
5345     if ((sr = s.result) != null)
5346     t.result = (((tr = t.result) == null) ? sr :
5347     reducer.apply(tr, sr));
5348     s = t.rights = s.nextRight;
5349     }
5350 dl 1.52 }
5351     }
5352     }
5353     }
5354    
5355 dl 1.102 @SuppressWarnings("serial")
5356     static final class MapReduceValuesTask<K,V,U>
5357     extends BulkTask<K,V,U> {
5358 dl 1.52 final Fun<? super V, ? extends U> transformer;
5359     final BiFun<? super U, ? super U, ? extends U> reducer;
5360     U result;
5361 dl 1.61 MapReduceValuesTask<K,V,U> rights, nextRight;
5362 dl 1.52 MapReduceValuesTask
5363 dl 1.102 (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5364 dl 1.61 MapReduceValuesTask<K,V,U> nextRight,
5365 dl 1.52 Fun<? super V, ? extends U> transformer,
5366     BiFun<? super U, ? super U, ? extends U> reducer) {
5367 dl 1.102 super(p, b, i, f, t); this.nextRight = nextRight;
5368 dl 1.52 this.transformer = transformer;
5369     this.reducer = reducer;
5370     }
5371 dl 1.79 public final U getRawResult() { return result; }
5372 dl 1.102 public final void compute() {
5373 dl 1.82 final Fun<? super V, ? extends U> transformer;
5374     final BiFun<? super U, ? super U, ? extends U> reducer;
5375     if ((transformer = this.transformer) != null &&
5376     (reducer = this.reducer) != null) {
5377 dl 1.102 for (int i = baseIndex, f, h; batch > 0 &&
5378     (h = ((f = baseLimit) + i) >>> 1) > i;) {
5379     addToPendingCount(1);
5380 dl 1.82 (rights = new MapReduceValuesTask<K,V,U>
5381 dl 1.102 (this, batch >>>= 1, baseLimit = h, f, tab,
5382     rights, transformer, reducer)).fork();
5383     }
5384     U r = null;
5385     for (Node<K,V> p; (p = advance()) != null; ) {
5386     U u;
5387     if ((u = transformer.apply(p.val)) != null)
5388 dl 1.82 r = (r == null) ? u : reducer.apply(r, u);
5389     }
5390     result = r;
5391     CountedCompleter<?> c;
5392     for (c = firstComplete(); c != null; c = c.nextComplete()) {
5393 dl 1.102 @SuppressWarnings("unchecked") MapReduceValuesTask<K,V,U>
5394 dl 1.82 t = (MapReduceValuesTask<K,V,U>)c,
5395     s = t.rights;
5396     while (s != null) {
5397     U tr, sr;
5398     if ((sr = s.result) != null)
5399     t.result = (((tr = t.result) == null) ? sr :
5400     reducer.apply(tr, sr));
5401     s = t.rights = s.nextRight;
5402     }
5403 dl 1.52 }
5404 dl 1.71 }
5405 dl 1.52 }
5406     }
5407    
5408 dl 1.102 @SuppressWarnings("serial")
5409     static final class MapReduceEntriesTask<K,V,U>
5410     extends BulkTask<K,V,U> {
5411 dl 1.52 final Fun<Map.Entry<K,V>, ? extends U> transformer;
5412     final BiFun<? super U, ? super U, ? extends U> reducer;
5413     U result;
5414 dl 1.61 MapReduceEntriesTask<K,V,U> rights, nextRight;
5415 dl 1.52 MapReduceEntriesTask
5416 dl 1.102 (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5417 dl 1.61 MapReduceEntriesTask<K,V,U> nextRight,
5418 dl 1.52 Fun<Map.Entry<K,V>, ? extends U> transformer,
5419     BiFun<? super U, ? super U, ? extends U> reducer) {
5420 dl 1.102 super(p, b, i, f, t); this.nextRight = nextRight;
5421 dl 1.52 this.transformer = transformer;
5422     this.reducer = reducer;
5423     }
5424 dl 1.79 public final U getRawResult() { return result; }
5425 dl 1.102 public final void compute() {
5426 dl 1.82 final Fun<Map.Entry<K,V>, ? extends U> transformer;
5427     final BiFun<? super U, ? super U, ? extends U> reducer;
5428     if ((transformer = this.transformer) != null &&
5429     (reducer = this.reducer) != null) {
5430 dl 1.102 for (int i = baseIndex, f, h; batch > 0 &&
5431     (h = ((f = baseLimit) + i) >>> 1) > i;) {
5432     addToPendingCount(1);
5433 dl 1.82 (rights = new MapReduceEntriesTask<K,V,U>
5434 dl 1.102 (this, batch >>>= 1, baseLimit = h, f, tab,
5435     rights, transformer, reducer)).fork();
5436     }
5437     U r = null;
5438     for (Node<K,V> p; (p = advance()) != null; ) {
5439     U u;
5440     if ((u = transformer.apply(p)) != null)
5441 dl 1.82 r = (r == null) ? u : reducer.apply(r, u);
5442     }
5443     result = r;
5444     CountedCompleter<?> c;
5445     for (c = firstComplete(); c != null; c = c.nextComplete()) {
5446 dl 1.102 @SuppressWarnings("unchecked") MapReduceEntriesTask<K,V,U>
5447 dl 1.82 t = (MapReduceEntriesTask<K,V,U>)c,
5448     s = t.rights;
5449     while (s != null) {
5450     U tr, sr;
5451     if ((sr = s.result) != null)
5452     t.result = (((tr = t.result) == null) ? sr :
5453     reducer.apply(tr, sr));
5454     s = t.rights = s.nextRight;
5455     }
5456 dl 1.52 }
5457     }
5458     }
5459     }
5460    
5461 dl 1.102 @SuppressWarnings("serial")
5462     static final class MapReduceMappingsTask<K,V,U>
5463     extends BulkTask<K,V,U> {
5464 dl 1.52 final BiFun<? super K, ? super V, ? extends U> transformer;
5465     final BiFun<? super U, ? super U, ? extends U> reducer;
5466     U result;
5467 dl 1.61 MapReduceMappingsTask<K,V,U> rights, nextRight;
5468 dl 1.52 MapReduceMappingsTask
5469 dl 1.102 (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5470 dl 1.61 MapReduceMappingsTask<K,V,U> nextRight,
5471 dl 1.52 BiFun<? super K, ? super V, ? extends U> transformer,
5472     BiFun<? super U, ? super U, ? extends U> reducer) {
5473 dl 1.102 super(p, b, i, f, t); this.nextRight = nextRight;
5474 dl 1.52 this.transformer = transformer;
5475     this.reducer = reducer;
5476     }
5477 dl 1.79 public final U getRawResult() { return result; }
5478 dl 1.102 public final void compute() {
5479 dl 1.82 final BiFun<? super K, ? super V, ? extends U> transformer;
5480     final BiFun<? super U, ? super U, ? extends U> reducer;
5481     if ((transformer = this.transformer) != null &&
5482     (reducer = this.reducer) != null) {
5483 dl 1.102 for (int i = baseIndex, f, h; batch > 0 &&
5484     (h = ((f = baseLimit) + i) >>> 1) > i;) {
5485     addToPendingCount(1);
5486 dl 1.82 (rights = new MapReduceMappingsTask<K,V,U>
5487 dl 1.102 (this, batch >>>= 1, baseLimit = h, f, tab,
5488     rights, transformer, reducer)).fork();
5489     }
5490     U r = null;
5491     for (Node<K,V> p; (p = advance()) != null; ) {
5492     U u;
5493     if ((u = transformer.apply(p.key, p.val)) != null)
5494 dl 1.82 r = (r == null) ? u : reducer.apply(r, u);
5495     }
5496     result = r;
5497     CountedCompleter<?> c;
5498     for (c = firstComplete(); c != null; c = c.nextComplete()) {
5499 dl 1.102 @SuppressWarnings("unchecked") MapReduceMappingsTask<K,V,U>
5500 dl 1.82 t = (MapReduceMappingsTask<K,V,U>)c,
5501     s = t.rights;
5502     while (s != null) {
5503     U tr, sr;
5504     if ((sr = s.result) != null)
5505     t.result = (((tr = t.result) == null) ? sr :
5506     reducer.apply(tr, sr));
5507     s = t.rights = s.nextRight;
5508     }
5509 dl 1.52 }
5510 dl 1.71 }
5511 dl 1.52 }
5512     }
5513    
5514 dl 1.102 @SuppressWarnings("serial")
5515     static final class MapReduceKeysToDoubleTask<K,V>
5516     extends BulkTask<K,V,Double> {
5517 dl 1.52 final ObjectToDouble<? super K> transformer;
5518     final DoubleByDoubleToDouble reducer;
5519     final double basis;
5520     double result;
5521 dl 1.61 MapReduceKeysToDoubleTask<K,V> rights, nextRight;
5522 dl 1.52 MapReduceKeysToDoubleTask
5523 dl 1.102 (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5524 dl 1.61 MapReduceKeysToDoubleTask<K,V> nextRight,
5525 dl 1.52 ObjectToDouble<? super K> transformer,
5526     double basis,
5527     DoubleByDoubleToDouble reducer) {
5528 dl 1.102 super(p, b, i, f, t); this.nextRight = nextRight;
5529 dl 1.52 this.transformer = transformer;
5530     this.basis = basis; this.reducer = reducer;
5531     }
5532 dl 1.79 public final Double getRawResult() { return result; }
5533 dl 1.102 public final void compute() {
5534 dl 1.82 final ObjectToDouble<? super K> transformer;
5535     final DoubleByDoubleToDouble reducer;
5536     if ((transformer = this.transformer) != null &&
5537     (reducer = this.reducer) != null) {
5538     double r = this.basis;
5539 dl 1.102 for (int i = baseIndex, f, h; batch > 0 &&
5540     (h = ((f = baseLimit) + i) >>> 1) > i;) {
5541     addToPendingCount(1);
5542 dl 1.82 (rights = new MapReduceKeysToDoubleTask<K,V>
5543 dl 1.102 (this, batch >>>= 1, baseLimit = h, f, tab,
5544     rights, transformer, r, reducer)).fork();
5545     }
5546     for (Node<K,V> p; (p = advance()) != null; )
5547     r = reducer.apply(r, transformer.apply(p.key));
5548 dl 1.82 result = r;
5549     CountedCompleter<?> c;
5550     for (c = firstComplete(); c != null; c = c.nextComplete()) {
5551 dl 1.102 @SuppressWarnings("unchecked") MapReduceKeysToDoubleTask<K,V>
5552 dl 1.82 t = (MapReduceKeysToDoubleTask<K,V>)c,
5553     s = t.rights;
5554     while (s != null) {
5555     t.result = reducer.apply(t.result, s.result);
5556     s = t.rights = s.nextRight;
5557     }
5558 dl 1.52 }
5559 dl 1.71 }
5560 dl 1.52 }
5561     }
5562    
5563 dl 1.102 @SuppressWarnings("serial")
5564     static final class MapReduceValuesToDoubleTask<K,V>
5565     extends BulkTask<K,V,Double> {
5566 dl 1.52 final ObjectToDouble<? super V> transformer;
5567     final DoubleByDoubleToDouble reducer;
5568     final double basis;
5569     double result;
5570 dl 1.61 MapReduceValuesToDoubleTask<K,V> rights, nextRight;
5571 dl 1.52 MapReduceValuesToDoubleTask
5572 dl 1.102 (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5573 dl 1.61 MapReduceValuesToDoubleTask<K,V> nextRight,
5574 dl 1.52 ObjectToDouble<? super V> transformer,
5575     double basis,
5576     DoubleByDoubleToDouble reducer) {
5577 dl 1.102 super(p, b, i, f, t); this.nextRight = nextRight;
5578 dl 1.52 this.transformer = transformer;
5579     this.basis = basis; this.reducer = reducer;
5580     }
5581 dl 1.79 public final Double getRawResult() { return result; }
5582 dl 1.102 public final void compute() {
5583 dl 1.82 final ObjectToDouble<? super V> transformer;
5584     final DoubleByDoubleToDouble reducer;
5585     if ((transformer = this.transformer) != null &&
5586     (reducer = this.reducer) != null) {
5587     double r = this.basis;
5588 dl 1.102 for (int i = baseIndex, f, h; batch > 0 &&
5589     (h = ((f = baseLimit) + i) >>> 1) > i;) {
5590     addToPendingCount(1);
5591 dl 1.82 (rights = new MapReduceValuesToDoubleTask<K,V>
5592 dl 1.102 (this, batch >>>= 1, baseLimit = h, f, tab,
5593     rights, transformer, r, reducer)).fork();
5594     }
5595     for (Node<K,V> p; (p = advance()) != null; )
5596     r = reducer.apply(r, transformer.apply(p.val));
5597 dl 1.82 result = r;
5598     CountedCompleter<?> c;
5599     for (c = firstComplete(); c != null; c = c.nextComplete()) {
5600 dl 1.102 @SuppressWarnings("unchecked") MapReduceValuesToDoubleTask<K,V>
5601 dl 1.82 t = (MapReduceValuesToDoubleTask<K,V>)c,
5602     s = t.rights;
5603     while (s != null) {
5604     t.result = reducer.apply(t.result, s.result);
5605     s = t.rights = s.nextRight;
5606     }
5607 dl 1.52 }
5608 dl 1.71 }
5609 dl 1.52 }
5610     }
5611    
5612 dl 1.102 @SuppressWarnings("serial")
5613     static final class MapReduceEntriesToDoubleTask<K,V>
5614     extends BulkTask<K,V,Double> {
5615 dl 1.52 final ObjectToDouble<Map.Entry<K,V>> transformer;
5616     final DoubleByDoubleToDouble reducer;
5617     final double basis;
5618     double result;
5619 dl 1.61 MapReduceEntriesToDoubleTask<K,V> rights, nextRight;
5620 dl 1.52 MapReduceEntriesToDoubleTask
5621 dl 1.102 (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5622 dl 1.61 MapReduceEntriesToDoubleTask<K,V> nextRight,
5623 dl 1.52 ObjectToDouble<Map.Entry<K,V>> transformer,
5624     double basis,
5625     DoubleByDoubleToDouble reducer) {
5626 dl 1.102 super(p, b, i, f, t); this.nextRight = nextRight;
5627 dl 1.52 this.transformer = transformer;
5628     this.basis = basis; this.reducer = reducer;
5629     }
5630 dl 1.79 public final Double getRawResult() { return result; }
5631 dl 1.102 public final void compute() {
5632 dl 1.82 final ObjectToDouble<Map.Entry<K,V>> transformer;
5633     final DoubleByDoubleToDouble reducer;
5634     if ((transformer = this.transformer) != null &&
5635     (reducer = this.reducer) != null) {
5636     double r = this.basis;
5637 dl 1.102 for (int i = baseIndex, f, h; batch > 0 &&
5638     (h = ((f = baseLimit) + i) >>> 1) > i;) {
5639     addToPendingCount(1);
5640 dl 1.82 (rights = new MapReduceEntriesToDoubleTask<K,V>
5641 dl 1.102 (this, batch >>>= 1, baseLimit = h, f, tab,
5642     rights, transformer, r, reducer)).fork();
5643     }
5644     for (Node<K,V> p; (p = advance()) != null; )
5645     r = reducer.apply(r, transformer.apply(p));
5646 dl 1.82 result = r;
5647     CountedCompleter<?> c;
5648     for (c = firstComplete(); c != null; c = c.nextComplete()) {
5649 dl 1.102 @SuppressWarnings("unchecked") MapReduceEntriesToDoubleTask<K,V>
5650 dl 1.82 t = (MapReduceEntriesToDoubleTask<K,V>)c,
5651     s = t.rights;
5652     while (s != null) {
5653     t.result = reducer.apply(t.result, s.result);
5654     s = t.rights = s.nextRight;
5655     }
5656 dl 1.52 }
5657 dl 1.71 }
5658 dl 1.52 }
5659     }
5660    
5661 dl 1.102 @SuppressWarnings("serial")
5662     static final class MapReduceMappingsToDoubleTask<K,V>
5663     extends BulkTask<K,V,Double> {
5664 dl 1.52 final ObjectByObjectToDouble<? super K, ? super V> transformer;
5665     final DoubleByDoubleToDouble reducer;
5666     final double basis;
5667     double result;
5668 dl 1.61 MapReduceMappingsToDoubleTask<K,V> rights, nextRight;
5669 dl 1.52 MapReduceMappingsToDoubleTask
5670 dl 1.102 (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5671 dl 1.61 MapReduceMappingsToDoubleTask<K,V> nextRight,
5672 dl 1.52 ObjectByObjectToDouble<? super K, ? super V> transformer,
5673     double basis,
5674     DoubleByDoubleToDouble reducer) {
5675 dl 1.102 super(p, b, i, f, t); this.nextRight = nextRight;
5676 dl 1.52 this.transformer = transformer;
5677     this.basis = basis; this.reducer = reducer;
5678     }
5679 dl 1.79 public final Double getRawResult() { return result; }
5680 dl 1.102 public final void compute() {
5681 dl 1.82 final ObjectByObjectToDouble<? super K, ? super V> transformer;
5682     final DoubleByDoubleToDouble reducer;
5683     if ((transformer = this.transformer) != null &&
5684     (reducer = this.reducer) != null) {
5685     double r = this.basis;
5686 dl 1.102 for (int i = baseIndex, f, h; batch > 0 &&
5687     (h = ((f = baseLimit) + i) >>> 1) > i;) {
5688     addToPendingCount(1);
5689 dl 1.82 (rights = new MapReduceMappingsToDoubleTask<K,V>
5690 dl 1.102 (this, batch >>>= 1, baseLimit = h, f, tab,
5691     rights, transformer, r, reducer)).fork();
5692     }
5693     for (Node<K,V> p; (p = advance()) != null; )
5694     r = reducer.apply(r, transformer.apply(p.key, p.val));
5695 dl 1.82 result = r;
5696     CountedCompleter<?> c;
5697     for (c = firstComplete(); c != null; c = c.nextComplete()) {
5698 dl 1.102 @SuppressWarnings("unchecked") MapReduceMappingsToDoubleTask<K,V>
5699 dl 1.82 t = (MapReduceMappingsToDoubleTask<K,V>)c,
5700     s = t.rights;
5701     while (s != null) {
5702     t.result = reducer.apply(t.result, s.result);
5703     s = t.rights = s.nextRight;
5704     }
5705 dl 1.52 }
5706 dl 1.71 }
5707 dl 1.52 }
5708     }
5709    
5710 dl 1.102 @SuppressWarnings("serial")
5711     static final class MapReduceKeysToLongTask<K,V>
5712     extends BulkTask<K,V,Long> {
5713 dl 1.52 final ObjectToLong<? super K> transformer;
5714     final LongByLongToLong reducer;
5715     final long basis;
5716     long result;
5717 dl 1.61 MapReduceKeysToLongTask<K,V> rights, nextRight;
5718 dl 1.52 MapReduceKeysToLongTask
5719 dl 1.102 (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5720 dl 1.61 MapReduceKeysToLongTask<K,V> nextRight,
5721 dl 1.52 ObjectToLong<? super K> transformer,
5722     long basis,
5723     LongByLongToLong reducer) {
5724 dl 1.102 super(p, b, i, f, t); this.nextRight = nextRight;
5725 dl 1.52 this.transformer = transformer;
5726     this.basis = basis; this.reducer = reducer;
5727     }
5728 dl 1.79 public final Long getRawResult() { return result; }
5729 dl 1.102 public final void compute() {
5730 dl 1.82 final ObjectToLong<? super K> transformer;
5731     final LongByLongToLong reducer;
5732     if ((transformer = this.transformer) != null &&
5733     (reducer = this.reducer) != null) {
5734     long r = this.basis;
5735 dl 1.102 for (int i = baseIndex, f, h; batch > 0 &&
5736     (h = ((f = baseLimit) + i) >>> 1) > i;) {
5737     addToPendingCount(1);
5738 dl 1.82 (rights = new MapReduceKeysToLongTask<K,V>
5739 dl 1.102 (this, batch >>>= 1, baseLimit = h, f, tab,
5740     rights, transformer, r, reducer)).fork();
5741     }
5742     for (Node<K,V> p; (p = advance()) != null; )
5743     r = reducer.apply(r, transformer.apply(p.key));
5744 dl 1.82 result = r;
5745     CountedCompleter<?> c;
5746     for (c = firstComplete(); c != null; c = c.nextComplete()) {
5747 dl 1.102 @SuppressWarnings("unchecked") MapReduceKeysToLongTask<K,V>
5748 dl 1.82 t = (MapReduceKeysToLongTask<K,V>)c,
5749     s = t.rights;
5750     while (s != null) {
5751     t.result = reducer.apply(t.result, s.result);
5752     s = t.rights = s.nextRight;
5753     }
5754 dl 1.52 }
5755 dl 1.71 }
5756 dl 1.52 }
5757     }
5758    
5759 dl 1.102 @SuppressWarnings("serial")
5760     static final class MapReduceValuesToLongTask<K,V>
5761     extends BulkTask<K,V,Long> {
5762 dl 1.52 final ObjectToLong<? super V> transformer;
5763     final LongByLongToLong reducer;
5764     final long basis;
5765     long result;
5766 dl 1.61 MapReduceValuesToLongTask<K,V> rights, nextRight;
5767 dl 1.52 MapReduceValuesToLongTask
5768 dl 1.102 (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5769 dl 1.61 MapReduceValuesToLongTask<K,V> nextRight,
5770 dl 1.52 ObjectToLong<? super V> transformer,
5771     long basis,
5772     LongByLongToLong reducer) {
5773 dl 1.102 super(p, b, i, f, t); this.nextRight = nextRight;
5774 dl 1.52 this.transformer = transformer;
5775     this.basis = basis; this.reducer = reducer;
5776     }
5777 dl 1.79 public final Long getRawResult() { return result; }
5778 dl 1.102 public final void compute() {
5779 dl 1.82 final ObjectToLong<? super V> transformer;
5780     final LongByLongToLong reducer;
5781     if ((transformer = this.transformer) != null &&
5782     (reducer = this.reducer) != null) {
5783     long r = this.basis;
5784 dl 1.102 for (int i = baseIndex, f, h; batch > 0 &&
5785     (h = ((f = baseLimit) + i) >>> 1) > i;) {
5786     addToPendingCount(1);
5787 dl 1.82 (rights = new MapReduceValuesToLongTask<K,V>
5788 dl 1.102 (this, batch >>>= 1, baseLimit = h, f, tab,
5789     rights, transformer, r, reducer)).fork();
5790     }
5791     for (Node<K,V> p; (p = advance()) != null; )
5792     r = reducer.apply(r, transformer.apply(p.val));
5793 dl 1.82 result = r;
5794     CountedCompleter<?> c;
5795     for (c = firstComplete(); c != null; c = c.nextComplete()) {
5796 dl 1.102 @SuppressWarnings("unchecked") MapReduceValuesToLongTask<K,V>
5797 dl 1.82 t = (MapReduceValuesToLongTask<K,V>)c,
5798     s = t.rights;
5799     while (s != null) {
5800     t.result = reducer.apply(t.result, s.result);
5801     s = t.rights = s.nextRight;
5802     }
5803 dl 1.52 }
5804 dl 1.71 }
5805 dl 1.52 }
5806     }
5807    
5808 dl 1.102 @SuppressWarnings("serial")
5809     static final class MapReduceEntriesToLongTask<K,V>
5810     extends BulkTask<K,V,Long> {
5811 dl 1.52 final ObjectToLong<Map.Entry<K,V>> transformer;
5812     final LongByLongToLong reducer;
5813     final long basis;
5814     long result;
5815 dl 1.61 MapReduceEntriesToLongTask<K,V> rights, nextRight;
5816 dl 1.52 MapReduceEntriesToLongTask
5817 dl 1.102 (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5818 dl 1.61 MapReduceEntriesToLongTask<K,V> nextRight,
5819 dl 1.52 ObjectToLong<Map.Entry<K,V>> transformer,
5820     long basis,
5821     LongByLongToLong reducer) {
5822 dl 1.102 super(p, b, i, f, t); this.nextRight = nextRight;
5823 dl 1.52 this.transformer = transformer;
5824     this.basis = basis; this.reducer = reducer;
5825     }
5826 dl 1.79 public final Long getRawResult() { return result; }
5827 dl 1.102 public final void compute() {
5828 dl 1.82 final ObjectToLong<Map.Entry<K,V>> transformer;
5829     final LongByLongToLong reducer;
5830     if ((transformer = this.transformer) != null &&
5831     (reducer = this.reducer) != null) {
5832     long r = this.basis;
5833 dl 1.102 for (int i = baseIndex, f, h; batch > 0 &&
5834     (h = ((f = baseLimit) + i) >>> 1) > i;) {
5835     addToPendingCount(1);
5836 dl 1.82 (rights = new MapReduceEntriesToLongTask<K,V>
5837 dl 1.102 (this, batch >>>= 1, baseLimit = h, f, tab,
5838     rights, transformer, r, reducer)).fork();
5839     }
5840     for (Node<K,V> p; (p = advance()) != null; )
5841     r = reducer.apply(r, transformer.apply(p));
5842 dl 1.82 result = r;
5843     CountedCompleter<?> c;
5844     for (c = firstComplete(); c != null; c = c.nextComplete()) {
5845 dl 1.102 @SuppressWarnings("unchecked") MapReduceEntriesToLongTask<K,V>
5846 dl 1.82 t = (MapReduceEntriesToLongTask<K,V>)c,
5847     s = t.rights;
5848     while (s != null) {
5849     t.result = reducer.apply(t.result, s.result);
5850     s = t.rights = s.nextRight;
5851     }
5852 dl 1.52 }
5853     }
5854     }
5855     }
5856    
5857 dl 1.102 @SuppressWarnings("serial")
5858     static final class MapReduceMappingsToLongTask<K,V>
5859     extends BulkTask<K,V,Long> {
5860 dl 1.52 final ObjectByObjectToLong<? super K, ? super V> transformer;
5861     final LongByLongToLong reducer;
5862     final long basis;
5863     long result;
5864 dl 1.61 MapReduceMappingsToLongTask<K,V> rights, nextRight;
5865 dl 1.52 MapReduceMappingsToLongTask
5866 dl 1.102 (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5867 dl 1.61 MapReduceMappingsToLongTask<K,V> nextRight,
5868 dl 1.52 ObjectByObjectToLong<? super K, ? super V> transformer,
5869     long basis,
5870     LongByLongToLong reducer) {
5871 dl 1.102 super(p, b, i, f, t); this.nextRight = nextRight;
5872 dl 1.52 this.transformer = transformer;
5873     this.basis = basis; this.reducer = reducer;
5874     }
5875 dl 1.79 public final Long getRawResult() { return result; }
5876 dl 1.102 public final void compute() {
5877 dl 1.82 final ObjectByObjectToLong<? super K, ? super V> transformer;
5878     final LongByLongToLong reducer;
5879     if ((transformer = this.transformer) != null &&
5880     (reducer = this.reducer) != null) {
5881     long r = this.basis;
5882 dl 1.102 for (int i = baseIndex, f, h; batch > 0 &&
5883     (h = ((f = baseLimit) + i) >>> 1) > i;) {
5884     addToPendingCount(1);
5885 dl 1.82 (rights = new MapReduceMappingsToLongTask<K,V>
5886 dl 1.102 (this, batch >>>= 1, baseLimit = h, f, tab,
5887     rights, transformer, r, reducer)).fork();
5888     }
5889     for (Node<K,V> p; (p = advance()) != null; )
5890     r = reducer.apply(r, transformer.apply(p.key, p.val));
5891 dl 1.82 result = r;
5892     CountedCompleter<?> c;
5893     for (c = firstComplete(); c != null; c = c.nextComplete()) {
5894 dl 1.102 @SuppressWarnings("unchecked") MapReduceMappingsToLongTask<K,V>
5895 dl 1.82 t = (MapReduceMappingsToLongTask<K,V>)c,
5896     s = t.rights;
5897     while (s != null) {
5898     t.result = reducer.apply(t.result, s.result);
5899     s = t.rights = s.nextRight;
5900     }
5901 dl 1.52 }
5902 dl 1.71 }
5903 dl 1.52 }
5904     }
5905    
5906 dl 1.102 @SuppressWarnings("serial")
5907     static final class MapReduceKeysToIntTask<K,V>
5908     extends BulkTask<K,V,Integer> {
5909 dl 1.52 final ObjectToInt<? super K> transformer;
5910     final IntByIntToInt reducer;
5911     final int basis;
5912     int result;
5913 dl 1.61 MapReduceKeysToIntTask<K,V> rights, nextRight;
5914 dl 1.52 MapReduceKeysToIntTask
5915 dl 1.102 (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5916 dl 1.61 MapReduceKeysToIntTask<K,V> nextRight,
5917 dl 1.52 ObjectToInt<? super K> transformer,
5918     int basis,
5919     IntByIntToInt reducer) {
5920 dl 1.102 super(p, b, i, f, t); this.nextRight = nextRight;
5921 dl 1.52 this.transformer = transformer;
5922     this.basis = basis; this.reducer = reducer;
5923     }
5924 dl 1.79 public final Integer getRawResult() { return result; }
5925 dl 1.102 public final void compute() {
5926 dl 1.82 final ObjectToInt<? super K> transformer;
5927     final IntByIntToInt reducer;
5928     if ((transformer = this.transformer) != null &&
5929     (reducer = this.reducer) != null) {
5930     int r = this.basis;
5931 dl 1.102 for (int i = baseIndex, f, h; batch > 0 &&
5932     (h = ((f = baseLimit) + i) >>> 1) > i;) {
5933     addToPendingCount(1);
5934 dl 1.82 (rights = new MapReduceKeysToIntTask<K,V>
5935 dl 1.102 (this, batch >>>= 1, baseLimit = h, f, tab,
5936     rights, transformer, r, reducer)).fork();
5937     }
5938     for (Node<K,V> p; (p = advance()) != null; )
5939     r = reducer.apply(r, transformer.apply(p.key));
5940 dl 1.82 result = r;
5941     CountedCompleter<?> c;
5942     for (c = firstComplete(); c != null; c = c.nextComplete()) {
5943 dl 1.102 @SuppressWarnings("unchecked") MapReduceKeysToIntTask<K,V>
5944 dl 1.82 t = (MapReduceKeysToIntTask<K,V>)c,
5945     s = t.rights;
5946     while (s != null) {
5947     t.result = reducer.apply(t.result, s.result);
5948     s = t.rights = s.nextRight;
5949     }
5950 dl 1.52 }
5951 dl 1.71 }
5952 dl 1.52 }
5953     }
5954    
5955 dl 1.102 @SuppressWarnings("serial")
5956     static final class MapReduceValuesToIntTask<K,V>
5957     extends BulkTask<K,V,Integer> {
5958 dl 1.52 final ObjectToInt<? super V> transformer;
5959     final IntByIntToInt reducer;
5960     final int basis;
5961     int result;
5962 dl 1.61 MapReduceValuesToIntTask<K,V> rights, nextRight;
5963 dl 1.52 MapReduceValuesToIntTask
5964 dl 1.102 (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
5965 dl 1.61 MapReduceValuesToIntTask<K,V> nextRight,
5966 dl 1.52 ObjectToInt<? super V> transformer,
5967     int basis,
5968     IntByIntToInt reducer) {
5969 dl 1.102 super(p, b, i, f, t); this.nextRight = nextRight;
5970 dl 1.52 this.transformer = transformer;
5971     this.basis = basis; this.reducer = reducer;
5972     }
5973 dl 1.79 public final Integer getRawResult() { return result; }
5974 dl 1.102 public final void compute() {
5975 dl 1.82 final ObjectToInt<? super V> transformer;
5976     final IntByIntToInt reducer;
5977     if ((transformer = this.transformer) != null &&
5978     (reducer = this.reducer) != null) {
5979     int r = this.basis;
5980 dl 1.102 for (int i = baseIndex, f, h; batch > 0 &&
5981     (h = ((f = baseLimit) + i) >>> 1) > i;) {
5982     addToPendingCount(1);
5983 dl 1.82 (rights = new MapReduceValuesToIntTask<K,V>
5984 dl 1.102 (this, batch >>>= 1, baseLimit = h, f, tab,
5985     rights, transformer, r, reducer)).fork();
5986     }
5987     for (Node<K,V> p; (p = advance()) != null; )
5988     r = reducer.apply(r, transformer.apply(p.val));
5989 dl 1.82 result = r;
5990     CountedCompleter<?> c;
5991     for (c = firstComplete(); c != null; c = c.nextComplete()) {
5992 dl 1.102 @SuppressWarnings("unchecked") MapReduceValuesToIntTask<K,V>
5993 dl 1.82 t = (MapReduceValuesToIntTask<K,V>)c,
5994     s = t.rights;
5995     while (s != null) {
5996     t.result = reducer.apply(t.result, s.result);
5997     s = t.rights = s.nextRight;
5998     }
5999 dl 1.52 }
6000 dl 1.71 }
6001 dl 1.52 }
6002     }
6003    
6004 dl 1.102 @SuppressWarnings("serial")
6005     static final class MapReduceEntriesToIntTask<K,V>
6006     extends BulkTask<K,V,Integer> {
6007 dl 1.52 final ObjectToInt<Map.Entry<K,V>> transformer;
6008     final IntByIntToInt reducer;
6009     final int basis;
6010     int result;
6011 dl 1.61 MapReduceEntriesToIntTask<K,V> rights, nextRight;
6012 dl 1.52 MapReduceEntriesToIntTask
6013 dl 1.102 (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
6014 dl 1.61 MapReduceEntriesToIntTask<K,V> nextRight,
6015 dl 1.52 ObjectToInt<Map.Entry<K,V>> transformer,
6016     int basis,
6017     IntByIntToInt reducer) {
6018 dl 1.102 super(p, b, i, f, t); this.nextRight = nextRight;
6019 dl 1.52 this.transformer = transformer;
6020     this.basis = basis; this.reducer = reducer;
6021     }
6022 dl 1.79 public final Integer getRawResult() { return result; }
6023 dl 1.102 public final void compute() {
6024 dl 1.82 final ObjectToInt<Map.Entry<K,V>> transformer;
6025     final IntByIntToInt reducer;
6026     if ((transformer = this.transformer) != null &&
6027     (reducer = this.reducer) != null) {
6028     int r = this.basis;
6029 dl 1.102 for (int i = baseIndex, f, h; batch > 0 &&
6030     (h = ((f = baseLimit) + i) >>> 1) > i;) {
6031     addToPendingCount(1);
6032 dl 1.82 (rights = new MapReduceEntriesToIntTask<K,V>
6033 dl 1.102 (this, batch >>>= 1, baseLimit = h, f, tab,
6034     rights, transformer, r, reducer)).fork();
6035     }
6036     for (Node<K,V> p; (p = advance()) != null; )
6037     r = reducer.apply(r, transformer.apply(p));
6038 dl 1.82 result = r;
6039     CountedCompleter<?> c;
6040     for (c = firstComplete(); c != null; c = c.nextComplete()) {
6041 dl 1.102 @SuppressWarnings("unchecked") MapReduceEntriesToIntTask<K,V>
6042 dl 1.82 t = (MapReduceEntriesToIntTask<K,V>)c,
6043     s = t.rights;
6044     while (s != null) {
6045     t.result = reducer.apply(t.result, s.result);
6046     s = t.rights = s.nextRight;
6047     }
6048 dl 1.52 }
6049     }
6050     }
6051     }
6052    
6053 dl 1.102 @SuppressWarnings("serial")
6054     static final class MapReduceMappingsToIntTask<K,V>
6055     extends BulkTask<K,V,Integer> {
6056 dl 1.52 final ObjectByObjectToInt<? super K, ? super V> transformer;
6057     final IntByIntToInt reducer;
6058     final int basis;
6059     int result;
6060 dl 1.61 MapReduceMappingsToIntTask<K,V> rights, nextRight;
6061 dl 1.52 MapReduceMappingsToIntTask
6062 dl 1.102 (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
6063 dl 1.79 MapReduceMappingsToIntTask<K,V> nextRight,
6064 dl 1.52 ObjectByObjectToInt<? super K, ? super V> transformer,
6065     int basis,
6066     IntByIntToInt reducer) {
6067 dl 1.102 super(p, b, i, f, t); this.nextRight = nextRight;
6068 dl 1.52 this.transformer = transformer;
6069     this.basis = basis; this.reducer = reducer;
6070     }
6071 dl 1.79 public final Integer getRawResult() { return result; }
6072 dl 1.102 public final void compute() {
6073 dl 1.82 final ObjectByObjectToInt<? super K, ? super V> transformer;
6074     final IntByIntToInt reducer;
6075     if ((transformer = this.transformer) != null &&
6076     (reducer = this.reducer) != null) {
6077     int r = this.basis;
6078 dl 1.102 for (int i = baseIndex, f, h; batch > 0 &&
6079     (h = ((f = baseLimit) + i) >>> 1) > i;) {
6080     addToPendingCount(1);
6081 dl 1.82 (rights = new MapReduceMappingsToIntTask<K,V>
6082 dl 1.102 (this, batch >>>= 1, baseLimit = h, f, tab,
6083     rights, transformer, r, reducer)).fork();
6084     }
6085     for (Node<K,V> p; (p = advance()) != null; )
6086     r = reducer.apply(r, transformer.apply(p.key, p.val));
6087 dl 1.82 result = r;
6088     CountedCompleter<?> c;
6089     for (c = firstComplete(); c != null; c = c.nextComplete()) {
6090 dl 1.102 @SuppressWarnings("unchecked") MapReduceMappingsToIntTask<K,V>
6091 dl 1.82 t = (MapReduceMappingsToIntTask<K,V>)c,
6092     s = t.rights;
6093     while (s != null) {
6094     t.result = reducer.apply(t.result, s.result);
6095     s = t.rights = s.nextRight;
6096     }
6097 dl 1.52 }
6098     }
6099     }
6100     }
6101    
6102 dl 1.102 /* ---------------- Counters -------------- */
6103    
6104     // Adapted from LongAdder and Striped64.
6105     // See their internal docs for explanation.
6106    
6107     // A padded cell for distributing counts
6108     static final class CounterCell {
6109     volatile long p0, p1, p2, p3, p4, p5, p6;
6110     volatile long value;
6111     volatile long q0, q1, q2, q3, q4, q5, q6;
6112     CounterCell(long x) { value = x; }
6113     }
6114    
6115     /**
6116     * Holder for the thread-local hash code determining which
6117     * CounterCell to use. The code is initialized via the
6118     * counterHashCodeGenerator, but may be moved upon collisions.
6119     */
6120     static final class CounterHashCode {
6121     int code;
6122     }
6123    
6124     /**
6125 jsr166 1.105 * Generates initial value for per-thread CounterHashCodes.
6126 dl 1.102 */
6127     static final AtomicInteger counterHashCodeGenerator = new AtomicInteger();
6128    
6129     /**
6130     * Increment for counterHashCodeGenerator. See class ThreadLocal
6131     * for explanation.
6132     */
6133     static final int SEED_INCREMENT = 0x61c88647;
6134    
6135     /**
6136     * Per-thread counter hash codes. Shared across all instances.
6137     */
6138     static final ThreadLocal<CounterHashCode> threadCounterHashCode =
6139     new ThreadLocal<CounterHashCode>();
6140    
6141    
6142     final long sumCount() {
6143     CounterCell[] as = counterCells; CounterCell a;
6144     long sum = baseCount;
6145     if (as != null) {
6146     for (int i = 0; i < as.length; ++i) {
6147     if ((a = as[i]) != null)
6148     sum += a.value;
6149     }
6150     }
6151     return sum;
6152     }
6153    
6154     // See LongAdder version for explanation
6155     private final void fullAddCount(long x, CounterHashCode hc,
6156     boolean wasUncontended) {
6157     int h;
6158     if (hc == null) {
6159     hc = new CounterHashCode();
6160     int s = counterHashCodeGenerator.addAndGet(SEED_INCREMENT);
6161     h = hc.code = (s == 0) ? 1 : s; // Avoid zero
6162     threadCounterHashCode.set(hc);
6163     }
6164     else
6165     h = hc.code;
6166     boolean collide = false; // True if last slot nonempty
6167     for (;;) {
6168     CounterCell[] as; CounterCell a; int n; long v;
6169     if ((as = counterCells) != null && (n = as.length) > 0) {
6170     if ((a = as[(n - 1) & h]) == null) {
6171     if (cellsBusy == 0) { // Try to attach new Cell
6172     CounterCell r = new CounterCell(x); // Optimistic create
6173     if (cellsBusy == 0 &&
6174     U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {
6175     boolean created = false;
6176     try { // Recheck under lock
6177     CounterCell[] rs; int m, j;
6178     if ((rs = counterCells) != null &&
6179     (m = rs.length) > 0 &&
6180     rs[j = (m - 1) & h] == null) {
6181     rs[j] = r;
6182     created = true;
6183     }
6184     } finally {
6185     cellsBusy = 0;
6186     }
6187     if (created)
6188     break;
6189     continue; // Slot is now non-empty
6190     }
6191     }
6192     collide = false;
6193     }
6194     else if (!wasUncontended) // CAS already known to fail
6195     wasUncontended = true; // Continue after rehash
6196     else if (U.compareAndSwapLong(a, CELLVALUE, v = a.value, v + x))
6197     break;
6198     else if (counterCells != as || n >= NCPU)
6199     collide = false; // At max size or stale
6200     else if (!collide)
6201     collide = true;
6202     else if (cellsBusy == 0 &&
6203     U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {
6204     try {
6205     if (counterCells == as) {// Expand table unless stale
6206     CounterCell[] rs = new CounterCell[n << 1];
6207     for (int i = 0; i < n; ++i)
6208     rs[i] = as[i];
6209     counterCells = rs;
6210     }
6211     } finally {
6212     cellsBusy = 0;
6213     }
6214     collide = false;
6215     continue; // Retry with expanded table
6216     }
6217     h ^= h << 13; // Rehash
6218     h ^= h >>> 17;
6219     h ^= h << 5;
6220     }
6221     else if (cellsBusy == 0 && counterCells == as &&
6222     U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) {
6223     boolean init = false;
6224     try { // Initialize table
6225     if (counterCells == as) {
6226     CounterCell[] rs = new CounterCell[2];
6227     rs[h & 1] = new CounterCell(x);
6228     counterCells = rs;
6229     init = true;
6230     }
6231     } finally {
6232     cellsBusy = 0;
6233     }
6234     if (init)
6235     break;
6236     }
6237     else if (U.compareAndSwapLong(this, BASECOUNT, v = baseCount, v + x))
6238     break; // Fall back on using base
6239     }
6240     hc.code = h; // Record index for next time
6241     }
6242    
6243 dl 1.52 // Unsafe mechanics
6244 dl 1.82 private static final sun.misc.Unsafe U;
6245     private static final long SIZECTL;
6246     private static final long TRANSFERINDEX;
6247     private static final long BASECOUNT;
6248 dl 1.102 private static final long CELLSBUSY;
6249 dl 1.82 private static final long CELLVALUE;
6250 dl 1.52 private static final long ABASE;
6251     private static final int ASHIFT;
6252    
6253     static {
6254     try {
6255 dl 1.82 U = getUnsafe();
6256 dl 1.52 Class<?> k = ConcurrentHashMapV8.class;
6257 dl 1.82 SIZECTL = U.objectFieldOffset
6258 dl 1.52 (k.getDeclaredField("sizeCtl"));
6259 dl 1.82 TRANSFERINDEX = U.objectFieldOffset
6260     (k.getDeclaredField("transferIndex"));
6261     BASECOUNT = U.objectFieldOffset
6262     (k.getDeclaredField("baseCount"));
6263 dl 1.102 CELLSBUSY = U.objectFieldOffset
6264     (k.getDeclaredField("cellsBusy"));
6265 dl 1.82 Class<?> ck = CounterCell.class;
6266     CELLVALUE = U.objectFieldOffset
6267     (ck.getDeclaredField("value"));
6268 jsr166 1.101 Class<?> ak = Node[].class;
6269     ABASE = U.arrayBaseOffset(ak);
6270     int scale = U.arrayIndexScale(ak);
6271 jsr166 1.89 if ((scale & (scale - 1)) != 0)
6272     throw new Error("data type scale not a power of two");
6273     ASHIFT = 31 - Integer.numberOfLeadingZeros(scale);
6274 dl 1.52 } catch (Exception e) {
6275     throw new Error(e);
6276     }
6277     }
6278    
6279     /**
6280     * Returns a sun.misc.Unsafe. Suitable for use in a 3rd party package.
6281     * Replace with a simple call to Unsafe.getUnsafe when integrating
6282     * into a jdk.
6283     *
6284     * @return a sun.misc.Unsafe
6285     */
6286     private static sun.misc.Unsafe getUnsafe() {
6287     try {
6288     return sun.misc.Unsafe.getUnsafe();
6289 jsr166 1.87 } catch (SecurityException tryReflectionInstead) {}
6290     try {
6291     return java.security.AccessController.doPrivileged
6292     (new java.security.PrivilegedExceptionAction<sun.misc.Unsafe>() {
6293     public sun.misc.Unsafe run() throws Exception {
6294     Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class;
6295     for (java.lang.reflect.Field f : k.getDeclaredFields()) {
6296     f.setAccessible(true);
6297     Object x = f.get(null);
6298     if (k.isInstance(x))
6299     return k.cast(x);
6300     }
6301     throw new NoSuchFieldError("the Unsafe");
6302     }});
6303     } catch (java.security.PrivilegedActionException e) {
6304     throw new RuntimeException("Could not initialize intrinsics",
6305     e.getCause());
6306 dl 1.52 }
6307     }
6308 dl 1.1 }