1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
< |
* Expert Group and released to the public domain. Use, modify, and |
4 |
< |
* redistribute this code in any way without acknowledgement. |
3 |
> |
* Expert Group and released to the public domain, as explained at |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
|
package java.util.concurrent; |
8 |
< |
import java.util.concurrent.locks.*; |
9 |
< |
import java.util.*; |
8 |
> |
import java.util.concurrent.ForkJoinPool; |
9 |
> |
import java.util.concurrent.CountedCompleter; |
10 |
> |
import java.util.function.*; |
11 |
> |
import java.util.Spliterator; |
12 |
> |
import java.util.stream.Stream; |
13 |
> |
import java.util.stream.Streams; |
14 |
> |
|
15 |
> |
import java.util.Comparator; |
16 |
> |
import java.util.Arrays; |
17 |
> |
import java.util.Map; |
18 |
> |
import java.util.Set; |
19 |
> |
import java.util.Collection; |
20 |
> |
import java.util.AbstractMap; |
21 |
> |
import java.util.AbstractSet; |
22 |
> |
import java.util.AbstractCollection; |
23 |
> |
import java.util.Hashtable; |
24 |
> |
import java.util.HashMap; |
25 |
> |
import java.util.Iterator; |
26 |
> |
import java.util.Enumeration; |
27 |
> |
import java.util.ConcurrentModificationException; |
28 |
> |
import java.util.NoSuchElementException; |
29 |
> |
import java.util.concurrent.ConcurrentMap; |
30 |
> |
import java.util.concurrent.locks.AbstractQueuedSynchronizer; |
31 |
> |
import java.util.concurrent.atomic.AtomicInteger; |
32 |
> |
import java.util.concurrent.atomic.AtomicReference; |
33 |
|
import java.io.Serializable; |
11 |
– |
import java.io.IOException; |
12 |
– |
import java.io.ObjectInputStream; |
13 |
– |
import java.io.ObjectOutputStream; |
34 |
|
|
35 |
|
/** |
36 |
|
* A hash table supporting full concurrency of retrievals and |
37 |
< |
* adjustable expected concurrency for updates. This class obeys the |
38 |
< |
* same functional specification as |
39 |
< |
* <tt>java.util.Hashtable</tt>. However, even though all operations |
40 |
< |
* are thread-safe, retrieval operations do <em>not</em> entail |
41 |
< |
* locking, and there is <em>not</em> any support for locking the |
42 |
< |
* entire table in a way that prevents all access. This class is |
43 |
< |
* fully interoperable with Hashtable in programs that rely on its |
37 |
> |
* high expected concurrency for updates. This class obeys the |
38 |
> |
* same functional specification as {@link java.util.Hashtable}, and |
39 |
> |
* includes versions of methods corresponding to each method of |
40 |
> |
* {@code Hashtable}. However, even though all operations are |
41 |
> |
* thread-safe, retrieval operations do <em>not</em> entail locking, |
42 |
> |
* and there is <em>not</em> any support for locking the entire table |
43 |
> |
* in a way that prevents all access. This class is fully |
44 |
> |
* interoperable with {@code Hashtable} in programs that rely on its |
45 |
|
* thread safety but not on its synchronization details. |
46 |
|
* |
47 |
< |
* <p> Retrieval operations (including <tt>get</tt>) ordinarily |
48 |
< |
* overlap with update operations (including <tt>put</tt> and |
49 |
< |
* <tt>remove</tt>). Retrievals reflect the results of the most |
47 |
> |
* <p>Retrieval operations (including {@code get}) generally do not |
48 |
> |
* block, so may overlap with update operations (including {@code put} |
49 |
> |
* and {@code remove}). Retrievals reflect the results of the most |
50 |
|
* recently <em>completed</em> update operations holding upon their |
51 |
< |
* onset. For aggregate operations such as <tt>putAll</tt> and |
52 |
< |
* <tt>clear</tt>, concurrent retrievals may reflect insertion or |
53 |
< |
* removal of only some entries. Similarly, Iterators and |
54 |
< |
* Enumerations return elements reflecting the state of the hash table |
55 |
< |
* at some point at or since the creation of the iterator/enumeration. |
56 |
< |
* They do <em>not</em> throw ConcurrentModificationException. |
57 |
< |
* However, Iterators are designed to be used by only one thread at a |
58 |
< |
* time. |
59 |
< |
* |
60 |
< |
* <p> The allowed concurrency among update operations is controlled |
61 |
< |
* by the optional <tt>segments</tt> constructor argument (default |
62 |
< |
* 16). The table is divided into this many independent parts, each of |
63 |
< |
* which can be updated concurrently. Because placement in hash tables |
64 |
< |
* is essentially random, the actual concurrency will vary. As a rough |
65 |
< |
* rule of thumb, you should choose at least as many segments as you |
66 |
< |
* expect concurrent threads. However, using more segments than you |
46 |
< |
* need can waste space and time. Using a value of 1 for |
47 |
< |
* <tt>segments</tt> results in a table that is concurrently readable |
48 |
< |
* but can only be updated by one thread at a time. |
51 |
> |
* onset. (More formally, an update operation for a given key bears a |
52 |
> |
* <em>happens-before</em> relation with any (non-null) retrieval for |
53 |
> |
* that key reporting the updated value.) For aggregate operations |
54 |
> |
* such as {@code putAll} and {@code clear}, concurrent retrievals may |
55 |
> |
* reflect insertion or removal of only some entries. Similarly, |
56 |
> |
* Iterators and Enumerations return elements reflecting the state of |
57 |
> |
* the hash table at some point at or since the creation of the |
58 |
> |
* iterator/enumeration. They do <em>not</em> throw {@link |
59 |
> |
* ConcurrentModificationException}. However, iterators are designed |
60 |
> |
* to be used by only one thread at a time. Bear in mind that the |
61 |
> |
* results of aggregate status methods including {@code size}, {@code |
62 |
> |
* isEmpty}, and {@code containsValue} are typically useful only when |
63 |
> |
* a map is not undergoing concurrent updates in other threads. |
64 |
> |
* Otherwise the results of these methods reflect transient states |
65 |
> |
* that may be adequate for monitoring or estimation purposes, but not |
66 |
> |
* for program control. |
67 |
|
* |
68 |
< |
* <p> Like Hashtable but unlike java.util.HashMap, this class does |
69 |
< |
* NOT allow <tt>null</tt> to be used as a key or value. |
68 |
> |
* <p>The table is dynamically expanded when there are too many |
69 |
> |
* collisions (i.e., keys that have distinct hash codes but fall into |
70 |
> |
* the same slot modulo the table size), with the expected average |
71 |
> |
* effect of maintaining roughly two bins per mapping (corresponding |
72 |
> |
* to a 0.75 load factor threshold for resizing). There may be much |
73 |
> |
* variance around this average as mappings are added and removed, but |
74 |
> |
* overall, this maintains a commonly accepted time/space tradeoff for |
75 |
> |
* hash tables. However, resizing this or any other kind of hash |
76 |
> |
* table may be a relatively slow operation. When possible, it is a |
77 |
> |
* good idea to provide a size estimate as an optional {@code |
78 |
> |
* initialCapacity} constructor argument. An additional optional |
79 |
> |
* {@code loadFactor} constructor argument provides a further means of |
80 |
> |
* customizing initial table capacity by specifying the table density |
81 |
> |
* to be used in calculating the amount of space to allocate for the |
82 |
> |
* given number of elements. Also, for compatibility with previous |
83 |
> |
* versions of this class, constructors may optionally specify an |
84 |
> |
* expected {@code concurrencyLevel} as an additional hint for |
85 |
> |
* internal sizing. Note that using many keys with exactly the same |
86 |
> |
* {@code hashCode()} is a sure way to slow down performance of any |
87 |
> |
* hash table. |
88 |
> |
* |
89 |
> |
* <p>A {@link Set} projection of a ConcurrentHashMap may be created |
90 |
> |
* (using {@link #newKeySet()} or {@link #newKeySet(int)}), or viewed |
91 |
> |
* (using {@link #keySet(Object)} when only keys are of interest, and the |
92 |
> |
* mapped values are (perhaps transiently) not used or all take the |
93 |
> |
* same mapping value. |
94 |
> |
* |
95 |
> |
* <p>A ConcurrentHashMap can be used as scalable frequency map (a |
96 |
> |
* form of histogram or multiset) by using {@link |
97 |
> |
* java.util.concurrent.atomic.LongAdder} values and initializing via |
98 |
> |
* {@link #computeIfAbsent computeIfAbsent}. For example, to add a count |
99 |
> |
* to a {@code ConcurrentHashMap<String,LongAdder> freqs}, you can use |
100 |
> |
* {@code freqs.computeIfAbsent(k -> new LongAdder()).increment();} |
101 |
> |
* |
102 |
> |
* <p>This class and its views and iterators implement all of the |
103 |
> |
* <em>optional</em> methods of the {@link Map} and {@link Iterator} |
104 |
> |
* interfaces. |
105 |
> |
* |
106 |
> |
* <p>Like {@link Hashtable} but unlike {@link HashMap}, this class |
107 |
> |
* does <em>not</em> allow {@code null} to be used as a key or value. |
108 |
> |
* |
109 |
> |
* <p>ConcurrentHashMaps support sequential and parallel operations |
110 |
> |
* bulk operations. (Parallel forms use the {@link |
111 |
> |
* ForkJoinPool#commonPool()}). Tasks that may be used in other |
112 |
> |
* contexts are available in class {@link ForkJoinTasks}. These |
113 |
> |
* operations are designed to be safely, and often sensibly, applied |
114 |
> |
* even with maps that are being concurrently updated by other |
115 |
> |
* threads; for example, when computing a snapshot summary of the |
116 |
> |
* values in a shared registry. There are three kinds of operation, |
117 |
> |
* each with four forms, accepting functions with Keys, Values, |
118 |
> |
* Entries, and (Key, Value) arguments and/or return values. Because |
119 |
> |
* the elements of a ConcurrentHashMap are not ordered in any |
120 |
> |
* particular way, and may be processed in different orders in |
121 |
> |
* different parallel executions, the correctness of supplied |
122 |
> |
* functions should not depend on any ordering, or on any other |
123 |
> |
* objects or values that may transiently change while computation is |
124 |
> |
* in progress; and except for forEach actions, should ideally be |
125 |
> |
* side-effect-free. |
126 |
> |
* |
127 |
> |
* <ul> |
128 |
> |
* <li> forEach: Perform a given action on each element. |
129 |
> |
* A variant form applies a given transformation on each element |
130 |
> |
* before performing the action.</li> |
131 |
> |
* |
132 |
> |
* <li> search: Return the first available non-null result of |
133 |
> |
* applying a given function on each element; skipping further |
134 |
> |
* search when a result is found.</li> |
135 |
> |
* |
136 |
> |
* <li> reduce: Accumulate each element. The supplied reduction |
137 |
> |
* function cannot rely on ordering (more formally, it should be |
138 |
> |
* both associative and commutative). There are five variants: |
139 |
> |
* |
140 |
> |
* <ul> |
141 |
> |
* |
142 |
> |
* <li> Plain reductions. (There is not a form of this method for |
143 |
> |
* (key, value) function arguments since there is no corresponding |
144 |
> |
* return type.)</li> |
145 |
> |
* |
146 |
> |
* <li> Mapped reductions that accumulate the results of a given |
147 |
> |
* function applied to each element.</li> |
148 |
> |
* |
149 |
> |
* <li> Reductions to scalar doubles, longs, and ints, using a |
150 |
> |
* given basis value.</li> |
151 |
> |
* |
152 |
> |
* </ul> |
153 |
> |
* </li> |
154 |
> |
* </ul> |
155 |
> |
* |
156 |
> |
* <p>The concurrency properties of bulk operations follow |
157 |
> |
* from those of ConcurrentHashMap: Any non-null result returned |
158 |
> |
* from {@code get(key)} and related access methods bears a |
159 |
> |
* happens-before relation with the associated insertion or |
160 |
> |
* update. The result of any bulk operation reflects the |
161 |
> |
* composition of these per-element relations (but is not |
162 |
> |
* necessarily atomic with respect to the map as a whole unless it |
163 |
> |
* is somehow known to be quiescent). Conversely, because keys |
164 |
> |
* and values in the map are never null, null serves as a reliable |
165 |
> |
* atomic indicator of the current lack of any result. To |
166 |
> |
* maintain this property, null serves as an implicit basis for |
167 |
> |
* all non-scalar reduction operations. For the double, long, and |
168 |
> |
* int versions, the basis should be one that, when combined with |
169 |
> |
* any other value, returns that other value (more formally, it |
170 |
> |
* should be the identity element for the reduction). Most common |
171 |
> |
* reductions have these properties; for example, computing a sum |
172 |
> |
* with basis 0 or a minimum with basis MAX_VALUE. |
173 |
> |
* |
174 |
> |
* <p>Search and transformation functions provided as arguments |
175 |
> |
* should similarly return null to indicate the lack of any result |
176 |
> |
* (in which case it is not used). In the case of mapped |
177 |
> |
* reductions, this also enables transformations to serve as |
178 |
> |
* filters, returning null (or, in the case of primitive |
179 |
> |
* specializations, the identity basis) if the element should not |
180 |
> |
* be combined. You can create compound transformations and |
181 |
> |
* filterings by composing them yourself under this "null means |
182 |
> |
* there is nothing there now" rule before using them in search or |
183 |
> |
* reduce operations. |
184 |
> |
* |
185 |
> |
* <p>Methods accepting and/or returning Entry arguments maintain |
186 |
> |
* key-value associations. They may be useful for example when |
187 |
> |
* finding the key for the greatest value. Note that "plain" Entry |
188 |
> |
* arguments can be supplied using {@code new |
189 |
> |
* AbstractMap.SimpleEntry(k,v)}. |
190 |
> |
* |
191 |
> |
* <p>Bulk operations may complete abruptly, throwing an |
192 |
> |
* exception encountered in the application of a supplied |
193 |
> |
* function. Bear in mind when handling such exceptions that other |
194 |
> |
* concurrently executing functions could also have thrown |
195 |
> |
* exceptions, or would have done so if the first exception had |
196 |
> |
* not occurred. |
197 |
> |
* |
198 |
> |
* <p>Speedups for parallel compared to sequential forms are common |
199 |
> |
* but not guaranteed. Parallel operations involving brief functions |
200 |
> |
* on small maps may execute more slowly than sequential forms if the |
201 |
> |
* underlying work to parallelize the computation is more expensive |
202 |
> |
* than the computation itself. Similarly, parallelization may not |
203 |
> |
* lead to much actual parallelism if all processors are busy |
204 |
> |
* performing unrelated tasks. |
205 |
> |
* |
206 |
> |
* <p>All arguments to all task methods must be non-null. |
207 |
> |
* |
208 |
> |
* <p>This class is a member of the |
209 |
> |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
210 |
> |
* Java Collections Framework</a>. |
211 |
|
* |
212 |
|
* @since 1.5 |
213 |
|
* @author Doug Lea |
214 |
+ |
* @param <K> the type of keys maintained by this map |
215 |
+ |
* @param <V> the type of mapped values |
216 |
|
*/ |
217 |
< |
public class ConcurrentHashMap<K, V> extends AbstractMap<K, V> |
218 |
< |
implements ConcurrentMap<K, V>, Cloneable, Serializable { |
217 |
> |
public class ConcurrentHashMap<K,V> |
218 |
> |
implements ConcurrentMap<K,V>, Serializable { |
219 |
> |
private static final long serialVersionUID = 7249069246763182397L; |
220 |
|
|
221 |
|
/* |
222 |
< |
* The basic strategy is to subdivide the table among Segments, |
223 |
< |
* each of which itself is a concurrently readable hash table. |
222 |
> |
* Overview: |
223 |
> |
* |
224 |
> |
* The primary design goal of this hash table is to maintain |
225 |
> |
* concurrent readability (typically method get(), but also |
226 |
> |
* iterators and related methods) while minimizing update |
227 |
> |
* contention. Secondary goals are to keep space consumption about |
228 |
> |
* the same or better than java.util.HashMap, and to support high |
229 |
> |
* initial insertion rates on an empty table by many threads. |
230 |
> |
* |
231 |
> |
* Each key-value mapping is held in a Node. Because Node key |
232 |
> |
* fields can contain special values, they are defined using plain |
233 |
> |
* Object types (not type "K"). This leads to a lot of explicit |
234 |
> |
* casting (and many explicit warning suppressions to tell |
235 |
> |
* compilers not to complain about it). It also allows some of the |
236 |
> |
* public methods to be factored into a smaller number of internal |
237 |
> |
* methods (although sadly not so for the five variants of |
238 |
> |
* put-related operations). The validation-based approach |
239 |
> |
* explained below leads to a lot of code sprawl because |
240 |
> |
* retry-control precludes factoring into smaller methods. |
241 |
> |
* |
242 |
> |
* The table is lazily initialized to a power-of-two size upon the |
243 |
> |
* first insertion. Each bin in the table normally contains a |
244 |
> |
* list of Nodes (most often, the list has only zero or one Node). |
245 |
> |
* Table accesses require volatile/atomic reads, writes, and |
246 |
> |
* CASes. Because there is no other way to arrange this without |
247 |
> |
* adding further indirections, we use intrinsics |
248 |
> |
* (sun.misc.Unsafe) operations. The lists of nodes within bins |
249 |
> |
* are always accurately traversable under volatile reads, so long |
250 |
> |
* as lookups check hash code and non-nullness of value before |
251 |
> |
* checking key equality. |
252 |
> |
* |
253 |
> |
* We use the top (sign) bit of Node hash fields for control |
254 |
> |
* purposes -- it is available anyway because of addressing |
255 |
> |
* constraints. Nodes with negative hash fields are forwarding |
256 |
> |
* nodes to either TreeBins or resized tables. The lower 31 bits |
257 |
> |
* of each normal Node's hash field contain a transformation of |
258 |
> |
* the key's hash code. |
259 |
> |
* |
260 |
> |
* Insertion (via put or its variants) of the first node in an |
261 |
> |
* empty bin is performed by just CASing it to the bin. This is |
262 |
> |
* by far the most common case for put operations under most |
263 |
> |
* key/hash distributions. Other update operations (insert, |
264 |
> |
* delete, and replace) require locks. We do not want to waste |
265 |
> |
* the space required to associate a distinct lock object with |
266 |
> |
* each bin, so instead use the first node of a bin list itself as |
267 |
> |
* a lock. Locking support for these locks relies on builtin |
268 |
> |
* "synchronized" monitors. |
269 |
> |
* |
270 |
> |
* Using the first node of a list as a lock does not by itself |
271 |
> |
* suffice though: When a node is locked, any update must first |
272 |
> |
* validate that it is still the first node after locking it, and |
273 |
> |
* retry if not. Because new nodes are always appended to lists, |
274 |
> |
* once a node is first in a bin, it remains first until deleted |
275 |
> |
* or the bin becomes invalidated (upon resizing). However, |
276 |
> |
* operations that only conditionally update may inspect nodes |
277 |
> |
* until the point of update. This is a converse of sorts to the |
278 |
> |
* lazy locking technique described by Herlihy & Shavit. |
279 |
> |
* |
280 |
> |
* The main disadvantage of per-bin locks is that other update |
281 |
> |
* operations on other nodes in a bin list protected by the same |
282 |
> |
* lock can stall, for example when user equals() or mapping |
283 |
> |
* functions take a long time. However, statistically, under |
284 |
> |
* random hash codes, this is not a common problem. Ideally, the |
285 |
> |
* frequency of nodes in bins follows a Poisson distribution |
286 |
> |
* (http://en.wikipedia.org/wiki/Poisson_distribution) with a |
287 |
> |
* parameter of about 0.5 on average, given the resizing threshold |
288 |
> |
* of 0.75, although with a large variance because of resizing |
289 |
> |
* granularity. Ignoring variance, the expected occurrences of |
290 |
> |
* list size k are (exp(-0.5) * pow(0.5, k) / factorial(k)). The |
291 |
> |
* first values are: |
292 |
> |
* |
293 |
> |
* 0: 0.60653066 |
294 |
> |
* 1: 0.30326533 |
295 |
> |
* 2: 0.07581633 |
296 |
> |
* 3: 0.01263606 |
297 |
> |
* 4: 0.00157952 |
298 |
> |
* 5: 0.00015795 |
299 |
> |
* 6: 0.00001316 |
300 |
> |
* 7: 0.00000094 |
301 |
> |
* 8: 0.00000006 |
302 |
> |
* more: less than 1 in ten million |
303 |
> |
* |
304 |
> |
* Lock contention probability for two threads accessing distinct |
305 |
> |
* elements is roughly 1 / (8 * #elements) under random hashes. |
306 |
> |
* |
307 |
> |
* Actual hash code distributions encountered in practice |
308 |
> |
* sometimes deviate significantly from uniform randomness. This |
309 |
> |
* includes the case when N > (1<<30), so some keys MUST collide. |
310 |
> |
* Similarly for dumb or hostile usages in which multiple keys are |
311 |
> |
* designed to have identical hash codes. Also, although we guard |
312 |
> |
* against the worst effects of this (see method spread), sets of |
313 |
> |
* hashes may differ only in bits that do not impact their bin |
314 |
> |
* index for a given power-of-two mask. So we use a secondary |
315 |
> |
* strategy that applies when the number of nodes in a bin exceeds |
316 |
> |
* a threshold, and at least one of the keys implements |
317 |
> |
* Comparable. These TreeBins use a balanced tree to hold nodes |
318 |
> |
* (a specialized form of red-black trees), bounding search time |
319 |
> |
* to O(log N). Each search step in a TreeBin is around twice as |
320 |
> |
* slow as in a regular list, but given that N cannot exceed |
321 |
> |
* (1<<64) (before running out of addresses) this bounds search |
322 |
> |
* steps, lock hold times, etc, to reasonable constants (roughly |
323 |
> |
* 100 nodes inspected per operation worst case) so long as keys |
324 |
> |
* are Comparable (which is very common -- String, Long, etc). |
325 |
> |
* TreeBin nodes (TreeNodes) also maintain the same "next" |
326 |
> |
* traversal pointers as regular nodes, so can be traversed in |
327 |
> |
* iterators in the same way. |
328 |
> |
* |
329 |
> |
* The table is resized when occupancy exceeds a percentage |
330 |
> |
* threshold (nominally, 0.75, but see below). Any thread |
331 |
> |
* noticing an overfull bin may assist in resizing after the |
332 |
> |
* initiating thread allocates and sets up the replacement |
333 |
> |
* array. However, rather than stalling, these other threads may |
334 |
> |
* proceed with insertions etc. The use of TreeBins shields us |
335 |
> |
* from the worst case effects of overfilling while resizes are in |
336 |
> |
* progress. Resizing proceeds by transferring bins, one by one, |
337 |
> |
* from the table to the next table. To enable concurrency, the |
338 |
> |
* next table must be (incrementally) prefilled with place-holders |
339 |
> |
* serving as reverse forwarders to the old table. Because we are |
340 |
> |
* using power-of-two expansion, the elements from each bin must |
341 |
> |
* either stay at same index, or move with a power of two |
342 |
> |
* offset. We eliminate unnecessary node creation by catching |
343 |
> |
* cases where old nodes can be reused because their next fields |
344 |
> |
* won't change. On average, only about one-sixth of them need |
345 |
> |
* cloning when a table doubles. The nodes they replace will be |
346 |
> |
* garbage collectable as soon as they are no longer referenced by |
347 |
> |
* any reader thread that may be in the midst of concurrently |
348 |
> |
* traversing table. Upon transfer, the old table bin contains |
349 |
> |
* only a special forwarding node (with hash field "MOVED") that |
350 |
> |
* contains the next table as its key. On encountering a |
351 |
> |
* forwarding node, access and update operations restart, using |
352 |
> |
* the new table. |
353 |
> |
* |
354 |
> |
* Each bin transfer requires its bin lock, which can stall |
355 |
> |
* waiting for locks while resizing. However, because other |
356 |
> |
* threads can join in and help resize rather than contend for |
357 |
> |
* locks, average aggregate waits become shorter as resizing |
358 |
> |
* progresses. The transfer operation must also ensure that all |
359 |
> |
* accessible bins in both the old and new table are usable by any |
360 |
> |
* traversal. This is arranged by proceeding from the last bin |
361 |
> |
* (table.length - 1) up towards the first. Upon seeing a |
362 |
> |
* forwarding node, traversals (see class Traverser) arrange to |
363 |
> |
* move to the new table without revisiting nodes. However, to |
364 |
> |
* ensure that no intervening nodes are skipped, bin splitting can |
365 |
> |
* only begin after the associated reverse-forwarders are in |
366 |
> |
* place. |
367 |
> |
* |
368 |
> |
* The traversal scheme also applies to partial traversals of |
369 |
> |
* ranges of bins (via an alternate Traverser constructor) |
370 |
> |
* to support partitioned aggregate operations. Also, read-only |
371 |
> |
* operations give up if ever forwarded to a null table, which |
372 |
> |
* provides support for shutdown-style clearing, which is also not |
373 |
> |
* currently implemented. |
374 |
> |
* |
375 |
> |
* Lazy table initialization minimizes footprint until first use, |
376 |
> |
* and also avoids resizings when the first operation is from a |
377 |
> |
* putAll, constructor with map argument, or deserialization. |
378 |
> |
* These cases attempt to override the initial capacity settings, |
379 |
> |
* but harmlessly fail to take effect in cases of races. |
380 |
> |
* |
381 |
> |
* The element count is maintained using a specialization of |
382 |
> |
* LongAdder. We need to incorporate a specialization rather than |
383 |
> |
* just use a LongAdder in order to access implicit |
384 |
> |
* contention-sensing that leads to creation of multiple |
385 |
> |
* Cells. The counter mechanics avoid contention on |
386 |
> |
* updates but can encounter cache thrashing if read too |
387 |
> |
* frequently during concurrent access. To avoid reading so often, |
388 |
> |
* resizing under contention is attempted only upon adding to a |
389 |
> |
* bin already holding two or more nodes. Under uniform hash |
390 |
> |
* distributions, the probability of this occurring at threshold |
391 |
> |
* is around 13%, meaning that only about 1 in 8 puts check |
392 |
> |
* threshold (and after resizing, many fewer do so). The bulk |
393 |
> |
* putAll operation further reduces contention by only committing |
394 |
> |
* count updates upon these size checks. |
395 |
> |
* |
396 |
> |
* Maintaining API and serialization compatibility with previous |
397 |
> |
* versions of this class introduces several oddities. Mainly: We |
398 |
> |
* leave untouched but unused constructor arguments refering to |
399 |
> |
* concurrencyLevel. We accept a loadFactor constructor argument, |
400 |
> |
* but apply it only to initial table capacity (which is the only |
401 |
> |
* time that we can guarantee to honor it.) We also declare an |
402 |
> |
* unused "Segment" class that is instantiated in minimal form |
403 |
> |
* only when serializing. |
404 |
|
*/ |
405 |
|
|
406 |
|
/* ---------------- Constants -------------- */ |
407 |
|
|
408 |
|
/** |
409 |
< |
* The default initial number of table slots for this table (32). |
410 |
< |
* Used when not otherwise specified in constructor. |
409 |
> |
* The largest possible table capacity. This value must be |
410 |
> |
* exactly 1<<30 to stay within Java array allocation and indexing |
411 |
> |
* bounds for power of two table sizes, and is further required |
412 |
> |
* because the top two bits of 32bit hash fields are used for |
413 |
> |
* control purposes. |
414 |
|
*/ |
415 |
< |
private static int DEFAULT_INITIAL_CAPACITY = 16; |
415 |
> |
private static final int MAXIMUM_CAPACITY = 1 << 30; |
416 |
|
|
417 |
|
/** |
418 |
< |
* The maximum capacity, used if a higher value is implicitly |
419 |
< |
* specified by either of the constructors with arguments. MUST |
75 |
< |
* be a power of two <= 1<<30. |
418 |
> |
* The default initial table capacity. Must be a power of 2 |
419 |
> |
* (i.e., at least 1) and at most MAXIMUM_CAPACITY. |
420 |
|
*/ |
421 |
< |
static final int MAXIMUM_CAPACITY = 1 << 30; |
421 |
> |
private static final int DEFAULT_CAPACITY = 16; |
422 |
|
|
423 |
|
/** |
424 |
< |
* The default load factor for this table. Used when not |
425 |
< |
* otherwise specified in constructor. |
424 |
> |
* The largest possible (non-power of two) array size. |
425 |
> |
* Needed by toArray and related methods. |
426 |
|
*/ |
427 |
< |
static final float DEFAULT_LOAD_FACTOR = 0.75f; |
427 |
> |
static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; |
428 |
|
|
429 |
|
/** |
430 |
< |
* The default number of concurrency control segments. |
431 |
< |
**/ |
432 |
< |
private static final int DEFAULT_SEGMENTS = 16; |
430 |
> |
* The default concurrency level for this table. Unused but |
431 |
> |
* defined for compatibility with previous versions of this class. |
432 |
> |
*/ |
433 |
> |
private static final int DEFAULT_CONCURRENCY_LEVEL = 16; |
434 |
> |
|
435 |
> |
/** |
436 |
> |
* The load factor for this table. Overrides of this value in |
437 |
> |
* constructors affect only the initial table capacity. The |
438 |
> |
* actual floating point value isn't normally used -- it is |
439 |
> |
* simpler to use expressions such as {@code n - (n >>> 2)} for |
440 |
> |
* the associated resizing threshold. |
441 |
> |
*/ |
442 |
> |
private static final float LOAD_FACTOR = 0.75f; |
443 |
> |
|
444 |
> |
/** |
445 |
> |
* The bin count threshold for using a tree rather than list for a |
446 |
> |
* bin. The value reflects the approximate break-even point for |
447 |
> |
* using tree-based operations. |
448 |
> |
*/ |
449 |
> |
private static final int TREE_THRESHOLD = 8; |
450 |
> |
|
451 |
> |
/** |
452 |
> |
* Minimum number of rebinnings per transfer step. Ranges are |
453 |
> |
* subdivided to allow multiple resizer threads. This value |
454 |
> |
* serves as a lower bound to avoid resizers encountering |
455 |
> |
* excessive memory contention. The value should be at least |
456 |
> |
* DEFAULT_CAPACITY. |
457 |
> |
*/ |
458 |
> |
private static final int MIN_TRANSFER_STRIDE = 16; |
459 |
> |
|
460 |
> |
/* |
461 |
> |
* Encodings for Node hash fields. See above for explanation. |
462 |
> |
*/ |
463 |
> |
static final int MOVED = 0x80000000; // hash field for forwarding nodes |
464 |
> |
static final int HASH_BITS = 0x7fffffff; // usable bits of normal node hash |
465 |
> |
|
466 |
> |
/** Number of CPUS, to place bounds on some sizings */ |
467 |
> |
static final int NCPU = Runtime.getRuntime().availableProcessors(); |
468 |
> |
|
469 |
> |
/* ---------------- Counters -------------- */ |
470 |
> |
|
471 |
> |
// Adapted from LongAdder and Striped64. |
472 |
> |
// See their internal docs for explanation. |
473 |
> |
|
474 |
> |
// A padded cell for distributing counts |
475 |
> |
static final class Cell { |
476 |
> |
volatile long p0, p1, p2, p3, p4, p5, p6; |
477 |
> |
volatile long value; |
478 |
> |
volatile long q0, q1, q2, q3, q4, q5, q6; |
479 |
> |
Cell(long x) { value = x; } |
480 |
> |
} |
481 |
|
|
482 |
|
/* ---------------- Fields -------------- */ |
483 |
|
|
484 |
|
/** |
485 |
< |
* Mask value for indexing into segments. The upper bits of a |
486 |
< |
* key's hash code are used to choose the segment. |
487 |
< |
**/ |
488 |
< |
private final int segmentMask; |
485 |
> |
* The array of bins. Lazily initialized upon first insertion. |
486 |
> |
* Size is always a power of two. Accessed directly by iterators. |
487 |
> |
*/ |
488 |
> |
transient volatile Node<V>[] table; |
489 |
|
|
490 |
|
/** |
491 |
< |
* Shift value for indexing within segments. |
492 |
< |
**/ |
493 |
< |
private final int segmentShift; |
491 |
> |
* The next table to use; non-null only while resizing. |
492 |
> |
*/ |
493 |
> |
private transient volatile Node<V>[] nextTable; |
494 |
|
|
495 |
|
/** |
496 |
< |
* The segments, each of which is a specialized hash table |
496 |
> |
* Base counter value, used mainly when there is no contention, |
497 |
> |
* but also as a fallback during table initialization |
498 |
> |
* races. Updated via CAS. |
499 |
|
*/ |
500 |
< |
private final Segment[] segments; |
500 |
> |
private transient volatile long baseCount; |
501 |
|
|
502 |
< |
private transient Set<K> keySet; |
503 |
< |
private transient Set<Map.Entry<K,V>> entrySet; |
504 |
< |
private transient Collection<V> values; |
502 |
> |
/** |
503 |
> |
* Table initialization and resizing control. When negative, the |
504 |
> |
* table is being initialized or resized: -1 for initialization, |
505 |
> |
* else -(1 + the number of active resizing threads). Otherwise, |
506 |
> |
* when table is null, holds the initial table size to use upon |
507 |
> |
* creation, or 0 for default. After initialization, holds the |
508 |
> |
* next element count value upon which to resize the table. |
509 |
> |
*/ |
510 |
> |
private transient volatile int sizeCtl; |
511 |
|
|
512 |
< |
/* ---------------- Small Utilities -------------- */ |
512 |
> |
/** |
513 |
> |
* The next table index (plus one) to split while resizing. |
514 |
> |
*/ |
515 |
> |
private transient volatile int transferIndex; |
516 |
|
|
517 |
|
/** |
518 |
< |
* Return a hash code for non-null Object x. |
116 |
< |
* Uses the same hash code spreader as most other j.u hash tables. |
117 |
< |
* @param x the object serving as a key |
118 |
< |
* @return the hash code |
518 |
> |
* The least available table index to split while resizing. |
519 |
|
*/ |
520 |
< |
private static int hash(Object x) { |
521 |
< |
int h = x.hashCode(); |
522 |
< |
h += ~(h << 9); |
523 |
< |
h ^= (h >>> 14); |
524 |
< |
h += (h << 4); |
525 |
< |
h ^= (h >>> 10); |
526 |
< |
return h; |
520 |
> |
private transient volatile int transferOrigin; |
521 |
> |
|
522 |
> |
/** |
523 |
> |
* Spinlock (locked via CAS) used when resizing and/or creating Cells. |
524 |
> |
*/ |
525 |
> |
private transient volatile int cellsBusy; |
526 |
> |
|
527 |
> |
/** |
528 |
> |
* Table of counter cells. When non-null, size is a power of 2. |
529 |
> |
*/ |
530 |
> |
private transient volatile Cell[] counterCells; |
531 |
> |
|
532 |
> |
// views |
533 |
> |
private transient KeySetView<K,V> keySet; |
534 |
> |
private transient ValuesView<K,V> values; |
535 |
> |
private transient EntrySetView<K,V> entrySet; |
536 |
> |
|
537 |
> |
/** For serialization compatibility. Null unless serialized; see below */ |
538 |
> |
private Segment<K,V>[] segments; |
539 |
> |
|
540 |
> |
/* ---------------- Table element access -------------- */ |
541 |
> |
|
542 |
> |
/* |
543 |
> |
* Volatile access methods are used for table elements as well as |
544 |
> |
* elements of in-progress next table while resizing. Uses are |
545 |
> |
* null checked by callers, and implicitly bounds-checked, relying |
546 |
> |
* on the invariants that tab arrays have non-zero size, and all |
547 |
> |
* indices are masked with (tab.length - 1) which is never |
548 |
> |
* negative and always less than length. Note that, to be correct |
549 |
> |
* wrt arbitrary concurrency errors by users, bounds checks must |
550 |
> |
* operate on local variables, which accounts for some odd-looking |
551 |
> |
* inline assignments below. |
552 |
> |
*/ |
553 |
> |
|
554 |
> |
@SuppressWarnings("unchecked") static final <V> Node<V> tabAt |
555 |
> |
(Node<V>[] tab, int i) { // used by Traverser |
556 |
> |
return (Node<V>)U.getObjectVolatile(tab, ((long)i << ASHIFT) + ABASE); |
557 |
> |
} |
558 |
> |
|
559 |
> |
private static final <V> boolean casTabAt |
560 |
> |
(Node<V>[] tab, int i, Node<V> c, Node<V> v) { |
561 |
> |
return U.compareAndSwapObject(tab, ((long)i << ASHIFT) + ABASE, c, v); |
562 |
|
} |
563 |
|
|
564 |
+ |
private static final <V> void setTabAt |
565 |
+ |
(Node<V>[] tab, int i, Node<V> v) { |
566 |
+ |
U.putObjectVolatile(tab, ((long)i << ASHIFT) + ABASE, v); |
567 |
+ |
} |
568 |
+ |
|
569 |
+ |
/* ---------------- Nodes -------------- */ |
570 |
+ |
|
571 |
|
/** |
572 |
< |
* Return the segment that should be used for key with given hash |
572 |
> |
* Key-value entry. Note that this is never exported out as a |
573 |
> |
* user-visible Map.Entry (see MapEntry below). Nodes with a hash |
574 |
> |
* field of MOVED are special, and do not contain user keys or |
575 |
> |
* values. Otherwise, keys are never null, and null val fields |
576 |
> |
* indicate that a node is in the process of being deleted or |
577 |
> |
* created. For purposes of read-only access, a key may be read |
578 |
> |
* before a val, but can only be used after checking val to be |
579 |
> |
* non-null. |
580 |
|
*/ |
581 |
< |
private Segment<K,V> segmentFor(int hash) { |
582 |
< |
return (Segment<K,V>) segments[(hash >>> segmentShift) & segmentMask]; |
581 |
> |
static class Node<V> { |
582 |
> |
final int hash; |
583 |
> |
final Object key; |
584 |
> |
volatile V val; |
585 |
> |
volatile Node<V> next; |
586 |
> |
|
587 |
> |
Node(int hash, Object key, V val, Node<V> next) { |
588 |
> |
this.hash = hash; |
589 |
> |
this.key = key; |
590 |
> |
this.val = val; |
591 |
> |
this.next = next; |
592 |
> |
} |
593 |
|
} |
594 |
|
|
595 |
< |
/* ---------------- Inner Classes -------------- */ |
595 |
> |
/* ---------------- TreeBins -------------- */ |
596 |
|
|
597 |
|
/** |
598 |
< |
* Segments are specialized versions of hash tables. This |
599 |
< |
* subclasses from ReentrantLock opportunistically, just to |
600 |
< |
* simplify some locking and avoid separate construction. |
601 |
< |
**/ |
602 |
< |
private static final class Segment<K,V> extends ReentrantLock implements Serializable { |
603 |
< |
/* |
604 |
< |
* Segments maintain a table of entry lists that are ALWAYS |
605 |
< |
* kept in a consistent state, so can be read without locking. |
606 |
< |
* Next fields of nodes are immutable (final). All list |
607 |
< |
* additions are performed at the front of each bin. This |
608 |
< |
* makes it easy to check changes, and also fast to traverse. |
609 |
< |
* When nodes would otherwise be changed, new nodes are |
610 |
< |
* created to replace them. This works well for hash tables |
611 |
< |
* since the bin lists tend to be short. (The average length |
153 |
< |
* is less than two for the default load factor threshold.) |
154 |
< |
* |
155 |
< |
* Read operations can thus proceed without locking, but rely |
156 |
< |
* on a memory barrier to ensure that completed write |
157 |
< |
* operations performed by other threads are |
158 |
< |
* noticed. Conveniently, the "count" field, tracking the |
159 |
< |
* number of elements, can also serve as the volatile variable |
160 |
< |
* providing proper read/write barriers. This is convenient |
161 |
< |
* because this field needs to be read in many read operations |
162 |
< |
* anyway. The use of volatiles for this purpose is only |
163 |
< |
* guaranteed to work in accord with reuirements in |
164 |
< |
* multithreaded environments when run on JVMs conforming to |
165 |
< |
* the clarified JSR133 memory model specification. This true |
166 |
< |
* for hotspot as of release 1.4. |
167 |
< |
* |
168 |
< |
* Implementors note. The basic rules for all this are: |
169 |
< |
* |
170 |
< |
* - All unsynchronized read operations must first read the |
171 |
< |
* "count" field, and should not look at table entries if |
172 |
< |
* it is 0. |
173 |
< |
* |
174 |
< |
* - All synchronized write operations should write to |
175 |
< |
* the "count" field after updating. The operations must not |
176 |
< |
* take any action that could even momentarily cause |
177 |
< |
* a concurrent read operation to see inconsistent |
178 |
< |
* data. This is made easier by the nature of the read |
179 |
< |
* operations in Map. For example, no operation |
180 |
< |
* can reveal that the table has grown but the threshold |
181 |
< |
* has not yet been updated, so there are no atomicity |
182 |
< |
* requirements for this with respect to reads. |
183 |
< |
* |
184 |
< |
* As a guide, all critical volatile reads and writes are marked |
185 |
< |
* in code comments. |
186 |
< |
*/ |
598 |
> |
* Nodes for use in TreeBins |
599 |
> |
*/ |
600 |
> |
static final class TreeNode<V> extends Node<V> { |
601 |
> |
TreeNode<V> parent; // red-black tree links |
602 |
> |
TreeNode<V> left; |
603 |
> |
TreeNode<V> right; |
604 |
> |
TreeNode<V> prev; // needed to unlink next upon deletion |
605 |
> |
boolean red; |
606 |
> |
|
607 |
> |
TreeNode(int hash, Object key, V val, Node<V> next, TreeNode<V> parent) { |
608 |
> |
super(hash, key, val, next); |
609 |
> |
this.parent = parent; |
610 |
> |
} |
611 |
> |
} |
612 |
|
|
613 |
< |
/** |
614 |
< |
* The number of elements in this segment's region. |
615 |
< |
**/ |
616 |
< |
transient volatile int count; |
613 |
> |
/** |
614 |
> |
* A specialized form of red-black tree for use in bins |
615 |
> |
* whose size exceeds a threshold. |
616 |
> |
* |
617 |
> |
* TreeBins use a special form of comparison for search and |
618 |
> |
* related operations (which is the main reason we cannot use |
619 |
> |
* existing collections such as TreeMaps). TreeBins contain |
620 |
> |
* Comparable elements, but may contain others, as well as |
621 |
> |
* elements that are Comparable but not necessarily Comparable<T> |
622 |
> |
* for the same T, so we cannot invoke compareTo among them. To |
623 |
> |
* handle this, the tree is ordered primarily by hash value, then |
624 |
> |
* by getClass().getName() order, and then by Comparator order |
625 |
> |
* among elements of the same class. On lookup at a node, if |
626 |
> |
* elements are not comparable or compare as 0, both left and |
627 |
> |
* right children may need to be searched in the case of tied hash |
628 |
> |
* values. (This corresponds to the full list search that would be |
629 |
> |
* necessary if all elements were non-Comparable and had tied |
630 |
> |
* hashes.) The red-black balancing code is updated from |
631 |
> |
* pre-jdk-collections |
632 |
> |
* (http://gee.cs.oswego.edu/dl/classes/collections/RBCell.java) |
633 |
> |
* based in turn on Cormen, Leiserson, and Rivest "Introduction to |
634 |
> |
* Algorithms" (CLR). |
635 |
> |
* |
636 |
> |
* TreeBins also maintain a separate locking discipline than |
637 |
> |
* regular bins. Because they are forwarded via special MOVED |
638 |
> |
* nodes at bin heads (which can never change once established), |
639 |
> |
* we cannot use those nodes as locks. Instead, TreeBin |
640 |
> |
* extends AbstractQueuedSynchronizer to support a simple form of |
641 |
> |
* read-write lock. For update operations and table validation, |
642 |
> |
* the exclusive form of lock behaves in the same way as bin-head |
643 |
> |
* locks. However, lookups use shared read-lock mechanics to allow |
644 |
> |
* multiple readers in the absence of writers. Additionally, |
645 |
> |
* these lookups do not ever block: While the lock is not |
646 |
> |
* available, they proceed along the slow traversal path (via |
647 |
> |
* next-pointers) until the lock becomes available or the list is |
648 |
> |
* exhausted, whichever comes first. (These cases are not fast, |
649 |
> |
* but maximize aggregate expected throughput.) The AQS mechanics |
650 |
> |
* for doing this are straightforward. The lock state is held as |
651 |
> |
* AQS getState(). Read counts are negative; the write count (1) |
652 |
> |
* is positive. There are no signalling preferences among readers |
653 |
> |
* and writers. Since we don't need to export full Lock API, we |
654 |
> |
* just override the minimal AQS methods and use them directly. |
655 |
> |
*/ |
656 |
> |
static final class TreeBin<V> extends AbstractQueuedSynchronizer { |
657 |
> |
private static final long serialVersionUID = 2249069246763182397L; |
658 |
> |
transient TreeNode<V> root; // root of tree |
659 |
> |
transient TreeNode<V> first; // head of next-pointer list |
660 |
> |
|
661 |
> |
/* AQS overrides */ |
662 |
> |
public final boolean isHeldExclusively() { return getState() > 0; } |
663 |
> |
public final boolean tryAcquire(int ignore) { |
664 |
> |
if (compareAndSetState(0, 1)) { |
665 |
> |
setExclusiveOwnerThread(Thread.currentThread()); |
666 |
> |
return true; |
667 |
> |
} |
668 |
> |
return false; |
669 |
> |
} |
670 |
> |
public final boolean tryRelease(int ignore) { |
671 |
> |
setExclusiveOwnerThread(null); |
672 |
> |
setState(0); |
673 |
> |
return true; |
674 |
> |
} |
675 |
> |
public final int tryAcquireShared(int ignore) { |
676 |
> |
for (int c;;) { |
677 |
> |
if ((c = getState()) > 0) |
678 |
> |
return -1; |
679 |
> |
if (compareAndSetState(c, c -1)) |
680 |
> |
return 1; |
681 |
> |
} |
682 |
> |
} |
683 |
> |
public final boolean tryReleaseShared(int ignore) { |
684 |
> |
int c; |
685 |
> |
do {} while (!compareAndSetState(c = getState(), c + 1)); |
686 |
> |
return c == -1; |
687 |
> |
} |
688 |
> |
|
689 |
> |
/** From CLR */ |
690 |
> |
private void rotateLeft(TreeNode<V> p) { |
691 |
> |
if (p != null) { |
692 |
> |
TreeNode<V> r = p.right, pp, rl; |
693 |
> |
if ((rl = p.right = r.left) != null) |
694 |
> |
rl.parent = p; |
695 |
> |
if ((pp = r.parent = p.parent) == null) |
696 |
> |
root = r; |
697 |
> |
else if (pp.left == p) |
698 |
> |
pp.left = r; |
699 |
> |
else |
700 |
> |
pp.right = r; |
701 |
> |
r.left = p; |
702 |
> |
p.parent = r; |
703 |
> |
} |
704 |
> |
} |
705 |
> |
|
706 |
> |
/** From CLR */ |
707 |
> |
private void rotateRight(TreeNode<V> p) { |
708 |
> |
if (p != null) { |
709 |
> |
TreeNode<V> l = p.left, pp, lr; |
710 |
> |
if ((lr = p.left = l.right) != null) |
711 |
> |
lr.parent = p; |
712 |
> |
if ((pp = l.parent = p.parent) == null) |
713 |
> |
root = l; |
714 |
> |
else if (pp.right == p) |
715 |
> |
pp.right = l; |
716 |
> |
else |
717 |
> |
pp.left = l; |
718 |
> |
l.right = p; |
719 |
> |
p.parent = l; |
720 |
> |
} |
721 |
> |
} |
722 |
|
|
723 |
|
/** |
724 |
< |
* The table is rehashed when its size exceeds this threshold. |
725 |
< |
* (The value of this field is always (int)(capacity * |
196 |
< |
* loadFactor).) |
724 |
> |
* Returns the TreeNode (or null if not found) for the given key |
725 |
> |
* starting at given root. |
726 |
|
*/ |
727 |
< |
private transient int threshold; |
727 |
> |
@SuppressWarnings("unchecked") final TreeNode<V> getTreeNode |
728 |
> |
(int h, Object k, TreeNode<V> p) { |
729 |
> |
Class<?> c = k.getClass(); |
730 |
> |
while (p != null) { |
731 |
> |
int dir, ph; Object pk; Class<?> pc; |
732 |
> |
if ((ph = p.hash) == h) { |
733 |
> |
if ((pk = p.key) == k || k.equals(pk)) |
734 |
> |
return p; |
735 |
> |
if (c != (pc = pk.getClass()) || |
736 |
> |
!(k instanceof Comparable) || |
737 |
> |
(dir = ((Comparable)k).compareTo((Comparable)pk)) == 0) { |
738 |
> |
if ((dir = (c == pc) ? 0 : |
739 |
> |
c.getName().compareTo(pc.getName())) == 0) { |
740 |
> |
TreeNode<V> r = null, pl, pr; // check both sides |
741 |
> |
if ((pr = p.right) != null && h >= pr.hash && |
742 |
> |
(r = getTreeNode(h, k, pr)) != null) |
743 |
> |
return r; |
744 |
> |
else if ((pl = p.left) != null && h <= pl.hash) |
745 |
> |
dir = -1; |
746 |
> |
else // nothing there |
747 |
> |
return null; |
748 |
> |
} |
749 |
> |
} |
750 |
> |
} |
751 |
> |
else |
752 |
> |
dir = (h < ph) ? -1 : 1; |
753 |
> |
p = (dir > 0) ? p.right : p.left; |
754 |
> |
} |
755 |
> |
return null; |
756 |
> |
} |
757 |
|
|
758 |
|
/** |
759 |
< |
* The per-segment table |
759 |
> |
* Wrapper for getTreeNode used by CHM.get. Tries to obtain |
760 |
> |
* read-lock to call getTreeNode, but during failure to get |
761 |
> |
* lock, searches along next links. |
762 |
|
*/ |
763 |
< |
transient HashEntry[] table; |
763 |
> |
final V getValue(int h, Object k) { |
764 |
> |
Node<V> r = null; |
765 |
> |
int c = getState(); // Must read lock state first |
766 |
> |
for (Node<V> e = first; e != null; e = e.next) { |
767 |
> |
if (c <= 0 && compareAndSetState(c, c - 1)) { |
768 |
> |
try { |
769 |
> |
r = getTreeNode(h, k, root); |
770 |
> |
} finally { |
771 |
> |
releaseShared(0); |
772 |
> |
} |
773 |
> |
break; |
774 |
> |
} |
775 |
> |
else if (e.hash == h && k.equals(e.key)) { |
776 |
> |
r = e; |
777 |
> |
break; |
778 |
> |
} |
779 |
> |
else |
780 |
> |
c = getState(); |
781 |
> |
} |
782 |
> |
return r == null ? null : r.val; |
783 |
> |
} |
784 |
|
|
785 |
|
/** |
786 |
< |
* The load factor for the hash table. Even though this value |
787 |
< |
* is same for all segments, it is replicated to avoid needing |
208 |
< |
* links to outer object. |
209 |
< |
* @serial |
786 |
> |
* Finds or adds a node. |
787 |
> |
* @return null if added |
788 |
|
*/ |
789 |
< |
private final float loadFactor; |
789 |
> |
@SuppressWarnings("unchecked") final TreeNode<V> putTreeNode |
790 |
> |
(int h, Object k, V v) { |
791 |
> |
Class<?> c = k.getClass(); |
792 |
> |
TreeNode<V> pp = root, p = null; |
793 |
> |
int dir = 0; |
794 |
> |
while (pp != null) { // find existing node or leaf to insert at |
795 |
> |
int ph; Object pk; Class<?> pc; |
796 |
> |
p = pp; |
797 |
> |
if ((ph = p.hash) == h) { |
798 |
> |
if ((pk = p.key) == k || k.equals(pk)) |
799 |
> |
return p; |
800 |
> |
if (c != (pc = pk.getClass()) || |
801 |
> |
!(k instanceof Comparable) || |
802 |
> |
(dir = ((Comparable)k).compareTo((Comparable)pk)) == 0) { |
803 |
> |
TreeNode<V> s = null, r = null, pr; |
804 |
> |
if ((dir = (c == pc) ? 0 : |
805 |
> |
c.getName().compareTo(pc.getName())) == 0) { |
806 |
> |
if ((pr = p.right) != null && h >= pr.hash && |
807 |
> |
(r = getTreeNode(h, k, pr)) != null) |
808 |
> |
return r; |
809 |
> |
else // continue left |
810 |
> |
dir = -1; |
811 |
> |
} |
812 |
> |
else if ((pr = p.right) != null && h >= pr.hash) |
813 |
> |
s = pr; |
814 |
> |
if (s != null && (r = getTreeNode(h, k, s)) != null) |
815 |
> |
return r; |
816 |
> |
} |
817 |
> |
} |
818 |
> |
else |
819 |
> |
dir = (h < ph) ? -1 : 1; |
820 |
> |
pp = (dir > 0) ? p.right : p.left; |
821 |
> |
} |
822 |
|
|
823 |
< |
Segment(int initialCapacity, float lf) { |
824 |
< |
loadFactor = lf; |
825 |
< |
setTable(new HashEntry[initialCapacity]); |
823 |
> |
TreeNode<V> f = first; |
824 |
> |
TreeNode<V> x = first = new TreeNode<V>(h, k, v, f, p); |
825 |
> |
if (p == null) |
826 |
> |
root = x; |
827 |
> |
else { // attach and rebalance; adapted from CLR |
828 |
> |
TreeNode<V> xp, xpp; |
829 |
> |
if (f != null) |
830 |
> |
f.prev = x; |
831 |
> |
if (dir <= 0) |
832 |
> |
p.left = x; |
833 |
> |
else |
834 |
> |
p.right = x; |
835 |
> |
x.red = true; |
836 |
> |
while (x != null && (xp = x.parent) != null && xp.red && |
837 |
> |
(xpp = xp.parent) != null) { |
838 |
> |
TreeNode<V> xppl = xpp.left; |
839 |
> |
if (xp == xppl) { |
840 |
> |
TreeNode<V> y = xpp.right; |
841 |
> |
if (y != null && y.red) { |
842 |
> |
y.red = false; |
843 |
> |
xp.red = false; |
844 |
> |
xpp.red = true; |
845 |
> |
x = xpp; |
846 |
> |
} |
847 |
> |
else { |
848 |
> |
if (x == xp.right) { |
849 |
> |
rotateLeft(x = xp); |
850 |
> |
xpp = (xp = x.parent) == null ? null : xp.parent; |
851 |
> |
} |
852 |
> |
if (xp != null) { |
853 |
> |
xp.red = false; |
854 |
> |
if (xpp != null) { |
855 |
> |
xpp.red = true; |
856 |
> |
rotateRight(xpp); |
857 |
> |
} |
858 |
> |
} |
859 |
> |
} |
860 |
> |
} |
861 |
> |
else { |
862 |
> |
TreeNode<V> y = xppl; |
863 |
> |
if (y != null && y.red) { |
864 |
> |
y.red = false; |
865 |
> |
xp.red = false; |
866 |
> |
xpp.red = true; |
867 |
> |
x = xpp; |
868 |
> |
} |
869 |
> |
else { |
870 |
> |
if (x == xp.left) { |
871 |
> |
rotateRight(x = xp); |
872 |
> |
xpp = (xp = x.parent) == null ? null : xp.parent; |
873 |
> |
} |
874 |
> |
if (xp != null) { |
875 |
> |
xp.red = false; |
876 |
> |
if (xpp != null) { |
877 |
> |
xpp.red = true; |
878 |
> |
rotateLeft(xpp); |
879 |
> |
} |
880 |
> |
} |
881 |
> |
} |
882 |
> |
} |
883 |
> |
} |
884 |
> |
TreeNode<V> r = root; |
885 |
> |
if (r != null && r.red) |
886 |
> |
r.red = false; |
887 |
> |
} |
888 |
> |
return null; |
889 |
|
} |
890 |
|
|
891 |
|
/** |
892 |
< |
* Set table to new HashEntry array. |
893 |
< |
* Call only while holding lock or in constructor. |
894 |
< |
**/ |
895 |
< |
private void setTable(HashEntry[] newTable) { |
896 |
< |
table = newTable; |
897 |
< |
threshold = (int)(newTable.length * loadFactor); |
898 |
< |
count = count; // write-volatile |
892 |
> |
* Removes the given node, that must be present before this |
893 |
> |
* call. This is messier than typical red-black deletion code |
894 |
> |
* because we cannot swap the contents of an interior node |
895 |
> |
* with a leaf successor that is pinned by "next" pointers |
896 |
> |
* that are accessible independently of lock. So instead we |
897 |
> |
* swap the tree linkages. |
898 |
> |
*/ |
899 |
> |
final void deleteTreeNode(TreeNode<V> p) { |
900 |
> |
TreeNode<V> next = (TreeNode<V>)p.next; // unlink traversal pointers |
901 |
> |
TreeNode<V> pred = p.prev; |
902 |
> |
if (pred == null) |
903 |
> |
first = next; |
904 |
> |
else |
905 |
> |
pred.next = next; |
906 |
> |
if (next != null) |
907 |
> |
next.prev = pred; |
908 |
> |
TreeNode<V> replacement; |
909 |
> |
TreeNode<V> pl = p.left; |
910 |
> |
TreeNode<V> pr = p.right; |
911 |
> |
if (pl != null && pr != null) { |
912 |
> |
TreeNode<V> s = pr, sl; |
913 |
> |
while ((sl = s.left) != null) // find successor |
914 |
> |
s = sl; |
915 |
> |
boolean c = s.red; s.red = p.red; p.red = c; // swap colors |
916 |
> |
TreeNode<V> sr = s.right; |
917 |
> |
TreeNode<V> pp = p.parent; |
918 |
> |
if (s == pr) { // p was s's direct parent |
919 |
> |
p.parent = s; |
920 |
> |
s.right = p; |
921 |
> |
} |
922 |
> |
else { |
923 |
> |
TreeNode<V> sp = s.parent; |
924 |
> |
if ((p.parent = sp) != null) { |
925 |
> |
if (s == sp.left) |
926 |
> |
sp.left = p; |
927 |
> |
else |
928 |
> |
sp.right = p; |
929 |
> |
} |
930 |
> |
if ((s.right = pr) != null) |
931 |
> |
pr.parent = s; |
932 |
> |
} |
933 |
> |
p.left = null; |
934 |
> |
if ((p.right = sr) != null) |
935 |
> |
sr.parent = p; |
936 |
> |
if ((s.left = pl) != null) |
937 |
> |
pl.parent = s; |
938 |
> |
if ((s.parent = pp) == null) |
939 |
> |
root = s; |
940 |
> |
else if (p == pp.left) |
941 |
> |
pp.left = s; |
942 |
> |
else |
943 |
> |
pp.right = s; |
944 |
> |
replacement = sr; |
945 |
> |
} |
946 |
> |
else |
947 |
> |
replacement = (pl != null) ? pl : pr; |
948 |
> |
TreeNode<V> pp = p.parent; |
949 |
> |
if (replacement == null) { |
950 |
> |
if (pp == null) { |
951 |
> |
root = null; |
952 |
> |
return; |
953 |
> |
} |
954 |
> |
replacement = p; |
955 |
> |
} |
956 |
> |
else { |
957 |
> |
replacement.parent = pp; |
958 |
> |
if (pp == null) |
959 |
> |
root = replacement; |
960 |
> |
else if (p == pp.left) |
961 |
> |
pp.left = replacement; |
962 |
> |
else |
963 |
> |
pp.right = replacement; |
964 |
> |
p.left = p.right = p.parent = null; |
965 |
> |
} |
966 |
> |
if (!p.red) { // rebalance, from CLR |
967 |
> |
TreeNode<V> x = replacement; |
968 |
> |
while (x != null) { |
969 |
> |
TreeNode<V> xp, xpl; |
970 |
> |
if (x.red || (xp = x.parent) == null) { |
971 |
> |
x.red = false; |
972 |
> |
break; |
973 |
> |
} |
974 |
> |
if (x == (xpl = xp.left)) { |
975 |
> |
TreeNode<V> sib = xp.right; |
976 |
> |
if (sib != null && sib.red) { |
977 |
> |
sib.red = false; |
978 |
> |
xp.red = true; |
979 |
> |
rotateLeft(xp); |
980 |
> |
sib = (xp = x.parent) == null ? null : xp.right; |
981 |
> |
} |
982 |
> |
if (sib == null) |
983 |
> |
x = xp; |
984 |
> |
else { |
985 |
> |
TreeNode<V> sl = sib.left, sr = sib.right; |
986 |
> |
if ((sr == null || !sr.red) && |
987 |
> |
(sl == null || !sl.red)) { |
988 |
> |
sib.red = true; |
989 |
> |
x = xp; |
990 |
> |
} |
991 |
> |
else { |
992 |
> |
if (sr == null || !sr.red) { |
993 |
> |
if (sl != null) |
994 |
> |
sl.red = false; |
995 |
> |
sib.red = true; |
996 |
> |
rotateRight(sib); |
997 |
> |
sib = (xp = x.parent) == null ? |
998 |
> |
null : xp.right; |
999 |
> |
} |
1000 |
> |
if (sib != null) { |
1001 |
> |
sib.red = (xp == null) ? false : xp.red; |
1002 |
> |
if ((sr = sib.right) != null) |
1003 |
> |
sr.red = false; |
1004 |
> |
} |
1005 |
> |
if (xp != null) { |
1006 |
> |
xp.red = false; |
1007 |
> |
rotateLeft(xp); |
1008 |
> |
} |
1009 |
> |
x = root; |
1010 |
> |
} |
1011 |
> |
} |
1012 |
> |
} |
1013 |
> |
else { // symmetric |
1014 |
> |
TreeNode<V> sib = xpl; |
1015 |
> |
if (sib != null && sib.red) { |
1016 |
> |
sib.red = false; |
1017 |
> |
xp.red = true; |
1018 |
> |
rotateRight(xp); |
1019 |
> |
sib = (xp = x.parent) == null ? null : xp.left; |
1020 |
> |
} |
1021 |
> |
if (sib == null) |
1022 |
> |
x = xp; |
1023 |
> |
else { |
1024 |
> |
TreeNode<V> sl = sib.left, sr = sib.right; |
1025 |
> |
if ((sl == null || !sl.red) && |
1026 |
> |
(sr == null || !sr.red)) { |
1027 |
> |
sib.red = true; |
1028 |
> |
x = xp; |
1029 |
> |
} |
1030 |
> |
else { |
1031 |
> |
if (sl == null || !sl.red) { |
1032 |
> |
if (sr != null) |
1033 |
> |
sr.red = false; |
1034 |
> |
sib.red = true; |
1035 |
> |
rotateLeft(sib); |
1036 |
> |
sib = (xp = x.parent) == null ? |
1037 |
> |
null : xp.left; |
1038 |
> |
} |
1039 |
> |
if (sib != null) { |
1040 |
> |
sib.red = (xp == null) ? false : xp.red; |
1041 |
> |
if ((sl = sib.left) != null) |
1042 |
> |
sl.red = false; |
1043 |
> |
} |
1044 |
> |
if (xp != null) { |
1045 |
> |
xp.red = false; |
1046 |
> |
rotateRight(xp); |
1047 |
> |
} |
1048 |
> |
x = root; |
1049 |
> |
} |
1050 |
> |
} |
1051 |
> |
} |
1052 |
> |
} |
1053 |
> |
} |
1054 |
> |
if (p == replacement && (pp = p.parent) != null) { |
1055 |
> |
if (p == pp.left) // detach pointers |
1056 |
> |
pp.left = null; |
1057 |
> |
else if (p == pp.right) |
1058 |
> |
pp.right = null; |
1059 |
> |
p.parent = null; |
1060 |
> |
} |
1061 |
|
} |
1062 |
+ |
} |
1063 |
|
|
1064 |
< |
/* Specialized implementations of map methods */ |
1064 |
> |
/* ---------------- Collision reduction methods -------------- */ |
1065 |
|
|
1066 |
< |
V get(K key, int hash) { |
1067 |
< |
if (count != 0) { // read-volatile |
1068 |
< |
HashEntry[] tab = table; |
1069 |
< |
int index = hash & (tab.length - 1); |
1070 |
< |
HashEntry<K,V> e = (HashEntry<K,V>) tab[index]; |
1071 |
< |
while (e != null) { |
1072 |
< |
if (e.hash == hash && key.equals(e.key)) |
1073 |
< |
return e.value; |
1074 |
< |
e = e.next; |
1066 |
> |
/** |
1067 |
> |
* Spreads higher bits to lower, and also forces top bit to 0. |
1068 |
> |
* Because the table uses power-of-two masking, sets of hashes |
1069 |
> |
* that vary only in bits above the current mask will always |
1070 |
> |
* collide. (Among known examples are sets of Float keys holding |
1071 |
> |
* consecutive whole numbers in small tables.) To counter this, |
1072 |
> |
* we apply a transform that spreads the impact of higher bits |
1073 |
> |
* downward. There is a tradeoff between speed, utility, and |
1074 |
> |
* quality of bit-spreading. Because many common sets of hashes |
1075 |
> |
* are already reasonably distributed across bits (so don't benefit |
1076 |
> |
* from spreading), and because we use trees to handle large sets |
1077 |
> |
* of collisions in bins, we don't need excessively high quality. |
1078 |
> |
*/ |
1079 |
> |
private static final int spread(int h) { |
1080 |
> |
h ^= (h >>> 18) ^ (h >>> 12); |
1081 |
> |
return (h ^ (h >>> 10)) & HASH_BITS; |
1082 |
> |
} |
1083 |
> |
|
1084 |
> |
/** |
1085 |
> |
* Replaces a list bin with a tree bin if key is comparable. Call |
1086 |
> |
* only when locked. |
1087 |
> |
*/ |
1088 |
> |
private final void replaceWithTreeBin(Node<V>[] tab, int index, Object key) { |
1089 |
> |
if (key instanceof Comparable) { |
1090 |
> |
TreeBin<V> t = new TreeBin<V>(); |
1091 |
> |
for (Node<V> e = tabAt(tab, index); e != null; e = e.next) |
1092 |
> |
t.putTreeNode(e.hash, e.key, e.val); |
1093 |
> |
setTabAt(tab, index, new Node<V>(MOVED, t, null, null)); |
1094 |
> |
} |
1095 |
> |
} |
1096 |
> |
|
1097 |
> |
/* ---------------- Internal access and update methods -------------- */ |
1098 |
> |
|
1099 |
> |
/** Implementation for get and containsKey */ |
1100 |
> |
@SuppressWarnings("unchecked") private final V internalGet(Object k) { |
1101 |
> |
int h = spread(k.hashCode()); |
1102 |
> |
retry: for (Node<V>[] tab = table; tab != null;) { |
1103 |
> |
Node<V> e; Object ek; V ev; int eh; // locals to read fields once |
1104 |
> |
for (e = tabAt(tab, (tab.length - 1) & h); e != null; e = e.next) { |
1105 |
> |
if ((eh = e.hash) < 0) { |
1106 |
> |
if ((ek = e.key) instanceof TreeBin) // search TreeBin |
1107 |
> |
return ((TreeBin<V>)ek).getValue(h, k); |
1108 |
> |
else { // restart with new table |
1109 |
> |
tab = (Node<V>[])ek; |
1110 |
> |
continue retry; |
1111 |
> |
} |
1112 |
|
} |
1113 |
+ |
else if (eh == h && (ev = e.val) != null && |
1114 |
+ |
((ek = e.key) == k || k.equals(ek))) |
1115 |
+ |
return ev; |
1116 |
|
} |
1117 |
< |
return null; |
1117 |
> |
break; |
1118 |
|
} |
1119 |
+ |
return null; |
1120 |
+ |
} |
1121 |
|
|
1122 |
< |
boolean containsKey(Object key, int hash) { |
1123 |
< |
if (count != 0) { // read-volatile |
1124 |
< |
HashEntry[] tab = table; |
1125 |
< |
int index = hash & (tab.length - 1); |
1126 |
< |
HashEntry<K,V> e = (HashEntry<K,V>) tab[index]; |
1127 |
< |
while (e != null) { |
1128 |
< |
if (e.hash == hash && key.equals(e.key)) |
1129 |
< |
return true; |
1130 |
< |
e = e.next; |
1122 |
> |
/** |
1123 |
> |
* Implementation for the four public remove/replace methods: |
1124 |
> |
* Replaces node value with v, conditional upon match of cv if |
1125 |
> |
* non-null. If resulting value is null, delete. |
1126 |
> |
*/ |
1127 |
> |
@SuppressWarnings("unchecked") private final V internalReplace |
1128 |
> |
(Object k, V v, Object cv) { |
1129 |
> |
int h = spread(k.hashCode()); |
1130 |
> |
V oldVal = null; |
1131 |
> |
for (Node<V>[] tab = table;;) { |
1132 |
> |
Node<V> f; int i, fh; Object fk; |
1133 |
> |
if (tab == null || |
1134 |
> |
(f = tabAt(tab, i = (tab.length - 1) & h)) == null) |
1135 |
> |
break; |
1136 |
> |
else if ((fh = f.hash) < 0) { |
1137 |
> |
if ((fk = f.key) instanceof TreeBin) { |
1138 |
> |
TreeBin<V> t = (TreeBin<V>)fk; |
1139 |
> |
boolean validated = false; |
1140 |
> |
boolean deleted = false; |
1141 |
> |
t.acquire(0); |
1142 |
> |
try { |
1143 |
> |
if (tabAt(tab, i) == f) { |
1144 |
> |
validated = true; |
1145 |
> |
TreeNode<V> p = t.getTreeNode(h, k, t.root); |
1146 |
> |
if (p != null) { |
1147 |
> |
V pv = p.val; |
1148 |
> |
if (cv == null || cv == pv || cv.equals(pv)) { |
1149 |
> |
oldVal = pv; |
1150 |
> |
if ((p.val = v) == null) { |
1151 |
> |
deleted = true; |
1152 |
> |
t.deleteTreeNode(p); |
1153 |
> |
} |
1154 |
> |
} |
1155 |
> |
} |
1156 |
> |
} |
1157 |
> |
} finally { |
1158 |
> |
t.release(0); |
1159 |
> |
} |
1160 |
> |
if (validated) { |
1161 |
> |
if (deleted) |
1162 |
> |
addCount(-1L, -1); |
1163 |
> |
break; |
1164 |
> |
} |
1165 |
> |
} |
1166 |
> |
else |
1167 |
> |
tab = (Node<V>[])fk; |
1168 |
> |
} |
1169 |
> |
else if (fh != h && f.next == null) // precheck |
1170 |
> |
break; // rules out possible existence |
1171 |
> |
else { |
1172 |
> |
boolean validated = false; |
1173 |
> |
boolean deleted = false; |
1174 |
> |
synchronized (f) { |
1175 |
> |
if (tabAt(tab, i) == f) { |
1176 |
> |
validated = true; |
1177 |
> |
for (Node<V> e = f, pred = null;;) { |
1178 |
> |
Object ek; V ev; |
1179 |
> |
if (e.hash == h && |
1180 |
> |
((ev = e.val) != null) && |
1181 |
> |
((ek = e.key) == k || k.equals(ek))) { |
1182 |
> |
if (cv == null || cv == ev || cv.equals(ev)) { |
1183 |
> |
oldVal = ev; |
1184 |
> |
if ((e.val = v) == null) { |
1185 |
> |
deleted = true; |
1186 |
> |
Node<V> en = e.next; |
1187 |
> |
if (pred != null) |
1188 |
> |
pred.next = en; |
1189 |
> |
else |
1190 |
> |
setTabAt(tab, i, en); |
1191 |
> |
} |
1192 |
> |
} |
1193 |
> |
break; |
1194 |
> |
} |
1195 |
> |
pred = e; |
1196 |
> |
if ((e = e.next) == null) |
1197 |
> |
break; |
1198 |
> |
} |
1199 |
> |
} |
1200 |
> |
} |
1201 |
> |
if (validated) { |
1202 |
> |
if (deleted) |
1203 |
> |
addCount(-1L, -1); |
1204 |
> |
break; |
1205 |
|
} |
1206 |
|
} |
255 |
– |
return false; |
1207 |
|
} |
1208 |
+ |
return oldVal; |
1209 |
+ |
} |
1210 |
|
|
1211 |
< |
boolean containsValue(Object value) { |
1212 |
< |
if (count != 0) { // read-volatile |
1213 |
< |
HashEntry[] tab = table; |
1214 |
< |
int len = tab.length; |
1215 |
< |
for (int i = 0 ; i < len; i++) |
1216 |
< |
for (HashEntry<K,V> e = (HashEntry<K,V>)tab[i] ; e != null ; e = e.next) |
1217 |
< |
if (value.equals(e.value)) |
1218 |
< |
return true; |
1211 |
> |
/* |
1212 |
> |
* Internal versions of insertion methods |
1213 |
> |
* All have the same basic structure as the first (internalPut): |
1214 |
> |
* 1. If table uninitialized, create |
1215 |
> |
* 2. If bin empty, try to CAS new node |
1216 |
> |
* 3. If bin stale, use new table |
1217 |
> |
* 4. if bin converted to TreeBin, validate and relay to TreeBin methods |
1218 |
> |
* 5. Lock and validate; if valid, scan and add or update |
1219 |
> |
* |
1220 |
> |
* The putAll method differs mainly in attempting to pre-allocate |
1221 |
> |
* enough table space, and also more lazily performs count updates |
1222 |
> |
* and checks. |
1223 |
> |
* |
1224 |
> |
* Most of the function-accepting methods can't be factored nicely |
1225 |
> |
* because they require different functional forms, so instead |
1226 |
> |
* sprawl out similar mechanics. |
1227 |
> |
*/ |
1228 |
> |
|
1229 |
> |
/** Implementation for put and putIfAbsent */ |
1230 |
> |
@SuppressWarnings("unchecked") private final V internalPut |
1231 |
> |
(K k, V v, boolean onlyIfAbsent) { |
1232 |
> |
if (k == null || v == null) throw new NullPointerException(); |
1233 |
> |
int h = spread(k.hashCode()); |
1234 |
> |
int len = 0; |
1235 |
> |
for (Node<V>[] tab = table;;) { |
1236 |
> |
int i, fh; Node<V> f; Object fk; V fv; |
1237 |
> |
if (tab == null) |
1238 |
> |
tab = initTable(); |
1239 |
> |
else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) { |
1240 |
> |
if (casTabAt(tab, i, null, new Node<V>(h, k, v, null))) |
1241 |
> |
break; // no lock when adding to empty bin |
1242 |
> |
} |
1243 |
> |
else if ((fh = f.hash) < 0) { |
1244 |
> |
if ((fk = f.key) instanceof TreeBin) { |
1245 |
> |
TreeBin<V> t = (TreeBin<V>)fk; |
1246 |
> |
V oldVal = null; |
1247 |
> |
t.acquire(0); |
1248 |
> |
try { |
1249 |
> |
if (tabAt(tab, i) == f) { |
1250 |
> |
len = 2; |
1251 |
> |
TreeNode<V> p = t.putTreeNode(h, k, v); |
1252 |
> |
if (p != null) { |
1253 |
> |
oldVal = p.val; |
1254 |
> |
if (!onlyIfAbsent) |
1255 |
> |
p.val = v; |
1256 |
> |
} |
1257 |
> |
} |
1258 |
> |
} finally { |
1259 |
> |
t.release(0); |
1260 |
> |
} |
1261 |
> |
if (len != 0) { |
1262 |
> |
if (oldVal != null) |
1263 |
> |
return oldVal; |
1264 |
> |
break; |
1265 |
> |
} |
1266 |
> |
} |
1267 |
> |
else |
1268 |
> |
tab = (Node<V>[])fk; |
1269 |
> |
} |
1270 |
> |
else if (onlyIfAbsent && fh == h && (fv = f.val) != null && |
1271 |
> |
((fk = f.key) == k || k.equals(fk))) // peek while nearby |
1272 |
> |
return fv; |
1273 |
> |
else { |
1274 |
> |
V oldVal = null; |
1275 |
> |
synchronized (f) { |
1276 |
> |
if (tabAt(tab, i) == f) { |
1277 |
> |
len = 1; |
1278 |
> |
for (Node<V> e = f;; ++len) { |
1279 |
> |
Object ek; V ev; |
1280 |
> |
if (e.hash == h && |
1281 |
> |
(ev = e.val) != null && |
1282 |
> |
((ek = e.key) == k || k.equals(ek))) { |
1283 |
> |
oldVal = ev; |
1284 |
> |
if (!onlyIfAbsent) |
1285 |
> |
e.val = v; |
1286 |
> |
break; |
1287 |
> |
} |
1288 |
> |
Node<V> last = e; |
1289 |
> |
if ((e = e.next) == null) { |
1290 |
> |
last.next = new Node<V>(h, k, v, null); |
1291 |
> |
if (len >= TREE_THRESHOLD) |
1292 |
> |
replaceWithTreeBin(tab, i, k); |
1293 |
> |
break; |
1294 |
> |
} |
1295 |
> |
} |
1296 |
> |
} |
1297 |
> |
} |
1298 |
> |
if (len != 0) { |
1299 |
> |
if (oldVal != null) |
1300 |
> |
return oldVal; |
1301 |
> |
break; |
1302 |
> |
} |
1303 |
|
} |
267 |
– |
return false; |
1304 |
|
} |
1305 |
+ |
addCount(1L, len); |
1306 |
+ |
return null; |
1307 |
+ |
} |
1308 |
|
|
1309 |
< |
V put(K key, int hash, V value, boolean onlyIfAbsent) { |
1310 |
< |
lock(); |
1311 |
< |
try { |
1312 |
< |
int c = count; |
1313 |
< |
HashEntry[] tab = table; |
1314 |
< |
int index = hash & (tab.length - 1); |
1315 |
< |
HashEntry<K,V> first = (HashEntry<K,V>) tab[index]; |
1316 |
< |
|
1317 |
< |
for (HashEntry<K,V> e = first; e != null; e = (HashEntry<K,V>) e.next) { |
1318 |
< |
if (e.hash == hash && key.equals(e.key)) { |
1319 |
< |
V oldValue = e.value; |
1320 |
< |
if (!onlyIfAbsent) |
1321 |
< |
e.value = value; |
1322 |
< |
count = c; // write-volatile |
1323 |
< |
return oldValue; |
1309 |
> |
/** Implementation for computeIfAbsent */ |
1310 |
> |
@SuppressWarnings("unchecked") private final V internalComputeIfAbsent |
1311 |
> |
(K k, Function<? super K, ? extends V> mf) { |
1312 |
> |
if (k == null || mf == null) |
1313 |
> |
throw new NullPointerException(); |
1314 |
> |
int h = spread(k.hashCode()); |
1315 |
> |
V val = null; |
1316 |
> |
int len = 0; |
1317 |
> |
for (Node<V>[] tab = table;;) { |
1318 |
> |
Node<V> f; int i; Object fk; |
1319 |
> |
if (tab == null) |
1320 |
> |
tab = initTable(); |
1321 |
> |
else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) { |
1322 |
> |
Node<V> node = new Node<V>(h, k, null, null); |
1323 |
> |
synchronized (node) { |
1324 |
> |
if (casTabAt(tab, i, null, node)) { |
1325 |
> |
len = 1; |
1326 |
> |
try { |
1327 |
> |
if ((val = mf.apply(k)) != null) |
1328 |
> |
node.val = val; |
1329 |
> |
} finally { |
1330 |
> |
if (val == null) |
1331 |
> |
setTabAt(tab, i, null); |
1332 |
> |
} |
1333 |
> |
} |
1334 |
> |
} |
1335 |
> |
if (len != 0) |
1336 |
> |
break; |
1337 |
> |
} |
1338 |
> |
else if (f.hash < 0) { |
1339 |
> |
if ((fk = f.key) instanceof TreeBin) { |
1340 |
> |
TreeBin<V> t = (TreeBin<V>)fk; |
1341 |
> |
boolean added = false; |
1342 |
> |
t.acquire(0); |
1343 |
> |
try { |
1344 |
> |
if (tabAt(tab, i) == f) { |
1345 |
> |
len = 1; |
1346 |
> |
TreeNode<V> p = t.getTreeNode(h, k, t.root); |
1347 |
> |
if (p != null) |
1348 |
> |
val = p.val; |
1349 |
> |
else if ((val = mf.apply(k)) != null) { |
1350 |
> |
added = true; |
1351 |
> |
len = 2; |
1352 |
> |
t.putTreeNode(h, k, val); |
1353 |
> |
} |
1354 |
> |
} |
1355 |
> |
} finally { |
1356 |
> |
t.release(0); |
1357 |
> |
} |
1358 |
> |
if (len != 0) { |
1359 |
> |
if (!added) |
1360 |
> |
return val; |
1361 |
> |
break; |
1362 |
> |
} |
1363 |
> |
} |
1364 |
> |
else |
1365 |
> |
tab = (Node<V>[])fk; |
1366 |
> |
} |
1367 |
> |
else { |
1368 |
> |
for (Node<V> e = f; e != null; e = e.next) { // prescan |
1369 |
> |
Object ek; V ev; |
1370 |
> |
if (e.hash == h && (ev = e.val) != null && |
1371 |
> |
((ek = e.key) == k || k.equals(ek))) |
1372 |
> |
return ev; |
1373 |
> |
} |
1374 |
> |
boolean added = false; |
1375 |
> |
synchronized (f) { |
1376 |
> |
if (tabAt(tab, i) == f) { |
1377 |
> |
len = 1; |
1378 |
> |
for (Node<V> e = f;; ++len) { |
1379 |
> |
Object ek; V ev; |
1380 |
> |
if (e.hash == h && |
1381 |
> |
(ev = e.val) != null && |
1382 |
> |
((ek = e.key) == k || k.equals(ek))) { |
1383 |
> |
val = ev; |
1384 |
> |
break; |
1385 |
> |
} |
1386 |
> |
Node<V> last = e; |
1387 |
> |
if ((e = e.next) == null) { |
1388 |
> |
if ((val = mf.apply(k)) != null) { |
1389 |
> |
added = true; |
1390 |
> |
last.next = new Node<V>(h, k, val, null); |
1391 |
> |
if (len >= TREE_THRESHOLD) |
1392 |
> |
replaceWithTreeBin(tab, i, k); |
1393 |
> |
} |
1394 |
> |
break; |
1395 |
> |
} |
1396 |
> |
} |
1397 |
|
} |
1398 |
|
} |
1399 |
+ |
if (len != 0) { |
1400 |
+ |
if (!added) |
1401 |
+ |
return val; |
1402 |
+ |
break; |
1403 |
+ |
} |
1404 |
+ |
} |
1405 |
+ |
} |
1406 |
+ |
if (val != null) |
1407 |
+ |
addCount(1L, len); |
1408 |
+ |
return val; |
1409 |
+ |
} |
1410 |
|
|
1411 |
< |
tab[index] = new HashEntry<K,V>(hash, key, value, first); |
1412 |
< |
++c; |
1413 |
< |
count = c; // write-volatile |
1414 |
< |
if (c > threshold) |
1415 |
< |
setTable(rehash(tab)); |
1416 |
< |
return null; |
1417 |
< |
} |
1418 |
< |
finally { |
1419 |
< |
unlock(); |
1411 |
> |
/** Implementation for compute */ |
1412 |
> |
@SuppressWarnings("unchecked") private final V internalCompute |
1413 |
> |
(K k, boolean onlyIfPresent, |
1414 |
> |
BiFunction<? super K, ? super V, ? extends V> mf) { |
1415 |
> |
if (k == null || mf == null) |
1416 |
> |
throw new NullPointerException(); |
1417 |
> |
int h = spread(k.hashCode()); |
1418 |
> |
V val = null; |
1419 |
> |
int delta = 0; |
1420 |
> |
int len = 0; |
1421 |
> |
for (Node<V>[] tab = table;;) { |
1422 |
> |
Node<V> f; int i, fh; Object fk; |
1423 |
> |
if (tab == null) |
1424 |
> |
tab = initTable(); |
1425 |
> |
else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) { |
1426 |
> |
if (onlyIfPresent) |
1427 |
> |
break; |
1428 |
> |
Node<V> node = new Node<V>(h, k, null, null); |
1429 |
> |
synchronized (node) { |
1430 |
> |
if (casTabAt(tab, i, null, node)) { |
1431 |
> |
try { |
1432 |
> |
len = 1; |
1433 |
> |
if ((val = mf.apply(k, null)) != null) { |
1434 |
> |
node.val = val; |
1435 |
> |
delta = 1; |
1436 |
> |
} |
1437 |
> |
} finally { |
1438 |
> |
if (delta == 0) |
1439 |
> |
setTabAt(tab, i, null); |
1440 |
> |
} |
1441 |
> |
} |
1442 |
> |
} |
1443 |
> |
if (len != 0) |
1444 |
> |
break; |
1445 |
> |
} |
1446 |
> |
else if ((fh = f.hash) < 0) { |
1447 |
> |
if ((fk = f.key) instanceof TreeBin) { |
1448 |
> |
TreeBin<V> t = (TreeBin<V>)fk; |
1449 |
> |
t.acquire(0); |
1450 |
> |
try { |
1451 |
> |
if (tabAt(tab, i) == f) { |
1452 |
> |
len = 1; |
1453 |
> |
TreeNode<V> p = t.getTreeNode(h, k, t.root); |
1454 |
> |
if (p == null && onlyIfPresent) |
1455 |
> |
break; |
1456 |
> |
V pv = (p == null) ? null : p.val; |
1457 |
> |
if ((val = mf.apply(k, pv)) != null) { |
1458 |
> |
if (p != null) |
1459 |
> |
p.val = val; |
1460 |
> |
else { |
1461 |
> |
len = 2; |
1462 |
> |
delta = 1; |
1463 |
> |
t.putTreeNode(h, k, val); |
1464 |
> |
} |
1465 |
> |
} |
1466 |
> |
else if (p != null) { |
1467 |
> |
delta = -1; |
1468 |
> |
t.deleteTreeNode(p); |
1469 |
> |
} |
1470 |
> |
} |
1471 |
> |
} finally { |
1472 |
> |
t.release(0); |
1473 |
> |
} |
1474 |
> |
if (len != 0) |
1475 |
> |
break; |
1476 |
> |
} |
1477 |
> |
else |
1478 |
> |
tab = (Node<V>[])fk; |
1479 |
> |
} |
1480 |
> |
else { |
1481 |
> |
synchronized (f) { |
1482 |
> |
if (tabAt(tab, i) == f) { |
1483 |
> |
len = 1; |
1484 |
> |
for (Node<V> e = f, pred = null;; ++len) { |
1485 |
> |
Object ek; V ev; |
1486 |
> |
if (e.hash == h && |
1487 |
> |
(ev = e.val) != null && |
1488 |
> |
((ek = e.key) == k || k.equals(ek))) { |
1489 |
> |
val = mf.apply(k, ev); |
1490 |
> |
if (val != null) |
1491 |
> |
e.val = val; |
1492 |
> |
else { |
1493 |
> |
delta = -1; |
1494 |
> |
Node<V> en = e.next; |
1495 |
> |
if (pred != null) |
1496 |
> |
pred.next = en; |
1497 |
> |
else |
1498 |
> |
setTabAt(tab, i, en); |
1499 |
> |
} |
1500 |
> |
break; |
1501 |
> |
} |
1502 |
> |
pred = e; |
1503 |
> |
if ((e = e.next) == null) { |
1504 |
> |
if (!onlyIfPresent && |
1505 |
> |
(val = mf.apply(k, null)) != null) { |
1506 |
> |
pred.next = new Node<V>(h, k, val, null); |
1507 |
> |
delta = 1; |
1508 |
> |
if (len >= TREE_THRESHOLD) |
1509 |
> |
replaceWithTreeBin(tab, i, k); |
1510 |
> |
} |
1511 |
> |
break; |
1512 |
> |
} |
1513 |
> |
} |
1514 |
> |
} |
1515 |
> |
} |
1516 |
> |
if (len != 0) |
1517 |
> |
break; |
1518 |
|
} |
1519 |
|
} |
1520 |
+ |
if (delta != 0) |
1521 |
+ |
addCount((long)delta, len); |
1522 |
+ |
return val; |
1523 |
+ |
} |
1524 |
|
|
1525 |
< |
private HashEntry[] rehash(HashEntry[] oldTable) { |
1526 |
< |
int oldCapacity = oldTable.length; |
1527 |
< |
if (oldCapacity >= MAXIMUM_CAPACITY) |
1528 |
< |
return oldTable; |
1529 |
< |
|
1530 |
< |
/* |
1531 |
< |
* Reclassify nodes in each list to new Map. Because we are |
1532 |
< |
* using power-of-two expansion, the elements from each bin |
1533 |
< |
* must either stay at same index, or move with a power of two |
1534 |
< |
* offset. We eliminate unnecessary node creation by catching |
1535 |
< |
* cases where old nodes can be reused because their next |
1536 |
< |
* fields won't change. Statistically, at the default |
1537 |
< |
* threshhold, only about one-sixth of them need cloning when |
1538 |
< |
* a table doubles. The nodes they replace will be garbage |
1539 |
< |
* collectable as soon as they are no longer referenced by any |
1540 |
< |
* reader thread that may be in the midst of traversing table |
1541 |
< |
* right now. |
1542 |
< |
*/ |
1543 |
< |
|
1544 |
< |
HashEntry[] newTable = new HashEntry[oldCapacity << 1]; |
1545 |
< |
int sizeMask = newTable.length - 1; |
1546 |
< |
for (int i = 0; i < oldCapacity ; i++) { |
1547 |
< |
// We need to guarantee that any existing reads of old Map can |
1548 |
< |
// proceed. So we cannot yet null out each bin. |
1549 |
< |
HashEntry<K,V> e = (HashEntry<K,V>)oldTable[i]; |
1550 |
< |
|
1551 |
< |
if (e != null) { |
1552 |
< |
HashEntry<K,V> next = e.next; |
1553 |
< |
int idx = e.hash & sizeMask; |
1554 |
< |
|
1555 |
< |
// Single node on list |
1556 |
< |
if (next == null) |
1557 |
< |
newTable[idx] = e; |
1525 |
> |
/** Implementation for merge */ |
1526 |
> |
@SuppressWarnings("unchecked") private final V internalMerge |
1527 |
> |
(K k, V v, BiFunction<? super V, ? super V, ? extends V> mf) { |
1528 |
> |
if (k == null || v == null || mf == null) |
1529 |
> |
throw new NullPointerException(); |
1530 |
> |
int h = spread(k.hashCode()); |
1531 |
> |
V val = null; |
1532 |
> |
int delta = 0; |
1533 |
> |
int len = 0; |
1534 |
> |
for (Node<V>[] tab = table;;) { |
1535 |
> |
int i; Node<V> f; Object fk; V fv; |
1536 |
> |
if (tab == null) |
1537 |
> |
tab = initTable(); |
1538 |
> |
else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null) { |
1539 |
> |
if (casTabAt(tab, i, null, new Node<V>(h, k, v, null))) { |
1540 |
> |
delta = 1; |
1541 |
> |
val = v; |
1542 |
> |
break; |
1543 |
> |
} |
1544 |
> |
} |
1545 |
> |
else if (f.hash < 0) { |
1546 |
> |
if ((fk = f.key) instanceof TreeBin) { |
1547 |
> |
TreeBin<V> t = (TreeBin<V>)fk; |
1548 |
> |
t.acquire(0); |
1549 |
> |
try { |
1550 |
> |
if (tabAt(tab, i) == f) { |
1551 |
> |
len = 1; |
1552 |
> |
TreeNode<V> p = t.getTreeNode(h, k, t.root); |
1553 |
> |
val = (p == null) ? v : mf.apply(p.val, v); |
1554 |
> |
if (val != null) { |
1555 |
> |
if (p != null) |
1556 |
> |
p.val = val; |
1557 |
> |
else { |
1558 |
> |
len = 2; |
1559 |
> |
delta = 1; |
1560 |
> |
t.putTreeNode(h, k, val); |
1561 |
> |
} |
1562 |
> |
} |
1563 |
> |
else if (p != null) { |
1564 |
> |
delta = -1; |
1565 |
> |
t.deleteTreeNode(p); |
1566 |
> |
} |
1567 |
> |
} |
1568 |
> |
} finally { |
1569 |
> |
t.release(0); |
1570 |
> |
} |
1571 |
> |
if (len != 0) |
1572 |
> |
break; |
1573 |
> |
} |
1574 |
> |
else |
1575 |
> |
tab = (Node<V>[])fk; |
1576 |
> |
} |
1577 |
> |
else { |
1578 |
> |
synchronized (f) { |
1579 |
> |
if (tabAt(tab, i) == f) { |
1580 |
> |
len = 1; |
1581 |
> |
for (Node<V> e = f, pred = null;; ++len) { |
1582 |
> |
Object ek; V ev; |
1583 |
> |
if (e.hash == h && |
1584 |
> |
(ev = e.val) != null && |
1585 |
> |
((ek = e.key) == k || k.equals(ek))) { |
1586 |
> |
val = mf.apply(ev, v); |
1587 |
> |
if (val != null) |
1588 |
> |
e.val = val; |
1589 |
> |
else { |
1590 |
> |
delta = -1; |
1591 |
> |
Node<V> en = e.next; |
1592 |
> |
if (pred != null) |
1593 |
> |
pred.next = en; |
1594 |
> |
else |
1595 |
> |
setTabAt(tab, i, en); |
1596 |
> |
} |
1597 |
> |
break; |
1598 |
> |
} |
1599 |
> |
pred = e; |
1600 |
> |
if ((e = e.next) == null) { |
1601 |
> |
val = v; |
1602 |
> |
pred.next = new Node<V>(h, k, val, null); |
1603 |
> |
delta = 1; |
1604 |
> |
if (len >= TREE_THRESHOLD) |
1605 |
> |
replaceWithTreeBin(tab, i, k); |
1606 |
> |
break; |
1607 |
> |
} |
1608 |
> |
} |
1609 |
> |
} |
1610 |
> |
} |
1611 |
> |
if (len != 0) |
1612 |
> |
break; |
1613 |
> |
} |
1614 |
> |
} |
1615 |
> |
if (delta != 0) |
1616 |
> |
addCount((long)delta, len); |
1617 |
> |
return val; |
1618 |
> |
} |
1619 |
|
|
1620 |
+ |
/** Implementation for putAll */ |
1621 |
+ |
@SuppressWarnings("unchecked") private final void internalPutAll |
1622 |
+ |
(Map<? extends K, ? extends V> m) { |
1623 |
+ |
tryPresize(m.size()); |
1624 |
+ |
long delta = 0L; // number of uncommitted additions |
1625 |
+ |
boolean npe = false; // to throw exception on exit for nulls |
1626 |
+ |
try { // to clean up counts on other exceptions |
1627 |
+ |
for (Map.Entry<?, ? extends V> entry : m.entrySet()) { |
1628 |
+ |
Object k; V v; |
1629 |
+ |
if (entry == null || (k = entry.getKey()) == null || |
1630 |
+ |
(v = entry.getValue()) == null) { |
1631 |
+ |
npe = true; |
1632 |
+ |
break; |
1633 |
+ |
} |
1634 |
+ |
int h = spread(k.hashCode()); |
1635 |
+ |
for (Node<V>[] tab = table;;) { |
1636 |
+ |
int i; Node<V> f; int fh; Object fk; |
1637 |
+ |
if (tab == null) |
1638 |
+ |
tab = initTable(); |
1639 |
+ |
else if ((f = tabAt(tab, i = (tab.length - 1) & h)) == null){ |
1640 |
+ |
if (casTabAt(tab, i, null, new Node<V>(h, k, v, null))) { |
1641 |
+ |
++delta; |
1642 |
+ |
break; |
1643 |
+ |
} |
1644 |
+ |
} |
1645 |
+ |
else if ((fh = f.hash) < 0) { |
1646 |
+ |
if ((fk = f.key) instanceof TreeBin) { |
1647 |
+ |
TreeBin<V> t = (TreeBin<V>)fk; |
1648 |
+ |
boolean validated = false; |
1649 |
+ |
t.acquire(0); |
1650 |
+ |
try { |
1651 |
+ |
if (tabAt(tab, i) == f) { |
1652 |
+ |
validated = true; |
1653 |
+ |
TreeNode<V> p = t.getTreeNode(h, k, t.root); |
1654 |
+ |
if (p != null) |
1655 |
+ |
p.val = v; |
1656 |
+ |
else { |
1657 |
+ |
t.putTreeNode(h, k, v); |
1658 |
+ |
++delta; |
1659 |
+ |
} |
1660 |
+ |
} |
1661 |
+ |
} finally { |
1662 |
+ |
t.release(0); |
1663 |
+ |
} |
1664 |
+ |
if (validated) |
1665 |
+ |
break; |
1666 |
+ |
} |
1667 |
+ |
else |
1668 |
+ |
tab = (Node<V>[])fk; |
1669 |
+ |
} |
1670 |
|
else { |
1671 |
< |
// Reuse trailing consecutive sequence at same slot |
1672 |
< |
HashEntry<K,V> lastRun = e; |
1673 |
< |
int lastIdx = idx; |
1674 |
< |
for (HashEntry<K,V> last = next; |
1675 |
< |
last != null; |
1676 |
< |
last = last.next) { |
1677 |
< |
int k = last.hash & sizeMask; |
1678 |
< |
if (k != lastIdx) { |
1679 |
< |
lastIdx = k; |
1680 |
< |
lastRun = last; |
1671 |
> |
int len = 0; |
1672 |
> |
synchronized (f) { |
1673 |
> |
if (tabAt(tab, i) == f) { |
1674 |
> |
len = 1; |
1675 |
> |
for (Node<V> e = f;; ++len) { |
1676 |
> |
Object ek; V ev; |
1677 |
> |
if (e.hash == h && |
1678 |
> |
(ev = e.val) != null && |
1679 |
> |
((ek = e.key) == k || k.equals(ek))) { |
1680 |
> |
e.val = v; |
1681 |
> |
break; |
1682 |
> |
} |
1683 |
> |
Node<V> last = e; |
1684 |
> |
if ((e = e.next) == null) { |
1685 |
> |
++delta; |
1686 |
> |
last.next = new Node<V>(h, k, v, null); |
1687 |
> |
if (len >= TREE_THRESHOLD) |
1688 |
> |
replaceWithTreeBin(tab, i, k); |
1689 |
> |
break; |
1690 |
> |
} |
1691 |
> |
} |
1692 |
> |
} |
1693 |
> |
} |
1694 |
> |
if (len != 0) { |
1695 |
> |
if (len > 1) { |
1696 |
> |
addCount(delta, len); |
1697 |
> |
delta = 0L; |
1698 |
|
} |
1699 |
+ |
break; |
1700 |
|
} |
1701 |
< |
newTable[lastIdx] = lastRun; |
1701 |
> |
} |
1702 |
> |
} |
1703 |
> |
} |
1704 |
> |
} finally { |
1705 |
> |
if (delta != 0L) |
1706 |
> |
addCount(delta, 2); |
1707 |
> |
} |
1708 |
> |
if (npe) |
1709 |
> |
throw new NullPointerException(); |
1710 |
> |
} |
1711 |
|
|
1712 |
< |
// Clone all remaining nodes |
1713 |
< |
for (HashEntry<K,V> p = e; p != lastRun; p = p.next) { |
1714 |
< |
int k = p.hash & sizeMask; |
1715 |
< |
newTable[k] = new HashEntry<K,V>(p.hash, |
1716 |
< |
p.key, |
1717 |
< |
p.value, |
1718 |
< |
(HashEntry<K,V>) newTable[k]); |
1712 |
> |
/** |
1713 |
> |
* Implementation for clear. Steps through each bin, removing all |
1714 |
> |
* nodes. |
1715 |
> |
*/ |
1716 |
> |
@SuppressWarnings("unchecked") private final void internalClear() { |
1717 |
> |
long delta = 0L; // negative number of deletions |
1718 |
> |
int i = 0; |
1719 |
> |
Node<V>[] tab = table; |
1720 |
> |
while (tab != null && i < tab.length) { |
1721 |
> |
Node<V> f = tabAt(tab, i); |
1722 |
> |
if (f == null) |
1723 |
> |
++i; |
1724 |
> |
else if (f.hash < 0) { |
1725 |
> |
Object fk; |
1726 |
> |
if ((fk = f.key) instanceof TreeBin) { |
1727 |
> |
TreeBin<V> t = (TreeBin<V>)fk; |
1728 |
> |
t.acquire(0); |
1729 |
> |
try { |
1730 |
> |
if (tabAt(tab, i) == f) { |
1731 |
> |
for (Node<V> p = t.first; p != null; p = p.next) { |
1732 |
> |
if (p.val != null) { // (currently always true) |
1733 |
> |
p.val = null; |
1734 |
> |
--delta; |
1735 |
> |
} |
1736 |
> |
} |
1737 |
> |
t.first = null; |
1738 |
> |
t.root = null; |
1739 |
> |
++i; |
1740 |
|
} |
1741 |
+ |
} finally { |
1742 |
+ |
t.release(0); |
1743 |
+ |
} |
1744 |
+ |
} |
1745 |
+ |
else |
1746 |
+ |
tab = (Node<V>[])fk; |
1747 |
+ |
} |
1748 |
+ |
else { |
1749 |
+ |
synchronized (f) { |
1750 |
+ |
if (tabAt(tab, i) == f) { |
1751 |
+ |
for (Node<V> e = f; e != null; e = e.next) { |
1752 |
+ |
if (e.val != null) { // (currently always true) |
1753 |
+ |
e.val = null; |
1754 |
+ |
--delta; |
1755 |
+ |
} |
1756 |
+ |
} |
1757 |
+ |
setTabAt(tab, i, null); |
1758 |
+ |
++i; |
1759 |
|
} |
1760 |
|
} |
1761 |
|
} |
360 |
– |
return newTable; |
1762 |
|
} |
1763 |
+ |
if (delta != 0L) |
1764 |
+ |
addCount(delta, -1); |
1765 |
+ |
} |
1766 |
|
|
1767 |
< |
/** |
364 |
< |
* Remove; match on key only if value null, else match both. |
365 |
< |
*/ |
366 |
< |
V remove(Object key, int hash, Object value) { |
367 |
< |
lock(); |
368 |
< |
try { |
369 |
< |
int c = count; |
370 |
< |
HashEntry[] tab = table; |
371 |
< |
int index = hash & (tab.length - 1); |
372 |
< |
HashEntry<K,V> first = (HashEntry<K,V>)tab[index]; |
1767 |
> |
/* ---------------- Table Initialization and Resizing -------------- */ |
1768 |
|
|
1769 |
< |
HashEntry<K,V> e = first; |
1770 |
< |
for (;;) { |
1771 |
< |
if (e == null) |
1772 |
< |
return null; |
1773 |
< |
if (e.hash == hash && key.equals(e.key)) |
1774 |
< |
break; |
1775 |
< |
e = e.next; |
1769 |
> |
/** |
1770 |
> |
* Returns a power of two table size for the given desired capacity. |
1771 |
> |
* See Hackers Delight, sec 3.2 |
1772 |
> |
*/ |
1773 |
> |
private static final int tableSizeFor(int c) { |
1774 |
> |
int n = c - 1; |
1775 |
> |
n |= n >>> 1; |
1776 |
> |
n |= n >>> 2; |
1777 |
> |
n |= n >>> 4; |
1778 |
> |
n |= n >>> 8; |
1779 |
> |
n |= n >>> 16; |
1780 |
> |
return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1; |
1781 |
> |
} |
1782 |
> |
|
1783 |
> |
/** |
1784 |
> |
* Initializes table, using the size recorded in sizeCtl. |
1785 |
> |
*/ |
1786 |
> |
@SuppressWarnings("unchecked") private final Node<V>[] initTable() { |
1787 |
> |
Node<V>[] tab; int sc; |
1788 |
> |
while ((tab = table) == null) { |
1789 |
> |
if ((sc = sizeCtl) < 0) |
1790 |
> |
Thread.yield(); // lost initialization race; just spin |
1791 |
> |
else if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) { |
1792 |
> |
try { |
1793 |
> |
if ((tab = table) == null) { |
1794 |
> |
int n = (sc > 0) ? sc : DEFAULT_CAPACITY; |
1795 |
> |
@SuppressWarnings("rawtypes") Node[] tb = new Node[n]; |
1796 |
> |
table = tab = (Node<V>[])tb; |
1797 |
> |
sc = n - (n >>> 2); |
1798 |
> |
} |
1799 |
> |
} finally { |
1800 |
> |
sizeCtl = sc; |
1801 |
|
} |
1802 |
+ |
break; |
1803 |
+ |
} |
1804 |
+ |
} |
1805 |
+ |
return tab; |
1806 |
+ |
} |
1807 |
|
|
1808 |
< |
V oldValue = e.value; |
1809 |
< |
if (value != null && !value.equals(oldValue)) |
1810 |
< |
return null; |
1811 |
< |
|
1812 |
< |
// All entries following removed node can stay in list, but |
1813 |
< |
// all preceeding ones need to be cloned. |
1814 |
< |
HashEntry<K,V> newFirst = e.next; |
1815 |
< |
for (HashEntry<K,V> p = first; p != e; p = p.next) |
1816 |
< |
newFirst = new HashEntry<K,V>(p.hash, p.key, |
1817 |
< |
p.value, newFirst); |
1818 |
< |
tab[index] = newFirst; |
1819 |
< |
count = c-1; // write-volatile |
1820 |
< |
return oldValue; |
1808 |
> |
/** |
1809 |
> |
* Adds to count, and if table is too small and not already |
1810 |
> |
* resizing, initiates transfer. If already resizing, helps |
1811 |
> |
* perform transfer if work is available. Rechecks occupancy |
1812 |
> |
* after a transfer to see if another resize is already needed |
1813 |
> |
* because resizings are lagging additions. |
1814 |
> |
* |
1815 |
> |
* @param x the count to add |
1816 |
> |
* @param check if <0, don't check resize, if <= 1 only check if uncontended |
1817 |
> |
*/ |
1818 |
> |
private final void addCount(long x, int check) { |
1819 |
> |
Cell[] as; long b, s; |
1820 |
> |
if ((as = counterCells) != null || |
1821 |
> |
!U.compareAndSwapLong(this, BASECOUNT, b = baseCount, s = b + x)) { |
1822 |
> |
Cell a; long v; int m; |
1823 |
> |
boolean uncontended = true; |
1824 |
> |
if (as == null || (m = as.length - 1) < 0 || |
1825 |
> |
(a = as[ThreadLocalRandom.getProbe() & m]) == null || |
1826 |
> |
!(uncontended = |
1827 |
> |
U.compareAndSwapLong(a, CELLVALUE, v = a.value, v + x))) { |
1828 |
> |
fullAddCount(x, uncontended); |
1829 |
> |
return; |
1830 |
|
} |
1831 |
< |
finally { |
1832 |
< |
unlock(); |
1831 |
> |
if (check <= 1) |
1832 |
> |
return; |
1833 |
> |
s = sumCount(); |
1834 |
> |
} |
1835 |
> |
if (check >= 0) { |
1836 |
> |
Node<V>[] tab, nt; int sc; |
1837 |
> |
while (s >= (long)(sc = sizeCtl) && (tab = table) != null && |
1838 |
> |
tab.length < MAXIMUM_CAPACITY) { |
1839 |
> |
if (sc < 0) { |
1840 |
> |
if (sc == -1 || transferIndex <= transferOrigin || |
1841 |
> |
(nt = nextTable) == null) |
1842 |
> |
break; |
1843 |
> |
if (U.compareAndSwapInt(this, SIZECTL, sc, sc - 1)) |
1844 |
> |
transfer(tab, nt); |
1845 |
> |
} |
1846 |
> |
else if (U.compareAndSwapInt(this, SIZECTL, sc, -2)) |
1847 |
> |
transfer(tab, null); |
1848 |
> |
s = sumCount(); |
1849 |
|
} |
1850 |
|
} |
1851 |
+ |
} |
1852 |
|
|
1853 |
< |
void clear() { |
1854 |
< |
lock(); |
1853 |
> |
/** |
1854 |
> |
* Tries to presize table to accommodate the given number of elements. |
1855 |
> |
* |
1856 |
> |
* @param size number of elements (doesn't need to be perfectly accurate) |
1857 |
> |
*/ |
1858 |
> |
@SuppressWarnings("unchecked") private final void tryPresize(int size) { |
1859 |
> |
int c = (size >= (MAXIMUM_CAPACITY >>> 1)) ? MAXIMUM_CAPACITY : |
1860 |
> |
tableSizeFor(size + (size >>> 1) + 1); |
1861 |
> |
int sc; |
1862 |
> |
while ((sc = sizeCtl) >= 0) { |
1863 |
> |
Node<V>[] tab = table; int n; |
1864 |
> |
if (tab == null || (n = tab.length) == 0) { |
1865 |
> |
n = (sc > c) ? sc : c; |
1866 |
> |
if (U.compareAndSwapInt(this, SIZECTL, sc, -1)) { |
1867 |
> |
try { |
1868 |
> |
if (table == tab) { |
1869 |
> |
@SuppressWarnings("rawtypes") Node[] tb = new Node[n]; |
1870 |
> |
table = (Node<V>[])tb; |
1871 |
> |
sc = n - (n >>> 2); |
1872 |
> |
} |
1873 |
> |
} finally { |
1874 |
> |
sizeCtl = sc; |
1875 |
> |
} |
1876 |
> |
} |
1877 |
> |
} |
1878 |
> |
else if (c <= sc || n >= MAXIMUM_CAPACITY) |
1879 |
> |
break; |
1880 |
> |
else if (tab == table && |
1881 |
> |
U.compareAndSwapInt(this, SIZECTL, sc, -2)) |
1882 |
> |
transfer(tab, null); |
1883 |
> |
} |
1884 |
> |
} |
1885 |
> |
|
1886 |
> |
/** |
1887 |
> |
* Moves and/or copies the nodes in each bin to new table. See |
1888 |
> |
* above for explanation. |
1889 |
> |
*/ |
1890 |
> |
@SuppressWarnings("unchecked") private final void transfer |
1891 |
> |
(Node<V>[] tab, Node<V>[] nextTab) { |
1892 |
> |
int n = tab.length, stride; |
1893 |
> |
if ((stride = (NCPU > 1) ? (n >>> 3) / NCPU : n) < MIN_TRANSFER_STRIDE) |
1894 |
> |
stride = MIN_TRANSFER_STRIDE; // subdivide range |
1895 |
> |
if (nextTab == null) { // initiating |
1896 |
|
try { |
1897 |
< |
HashEntry[] tab = table; |
1898 |
< |
for (int i = 0; i < tab.length ; i++) |
1899 |
< |
tab[i] = null; |
1900 |
< |
count = 0; // write-volatile |
1897 |
> |
@SuppressWarnings("rawtypes") Node[] tb = new Node[n << 1]; |
1898 |
> |
nextTab = (Node<V>[])tb; |
1899 |
> |
} catch (Throwable ex) { // try to cope with OOME |
1900 |
> |
sizeCtl = Integer.MAX_VALUE; |
1901 |
> |
return; |
1902 |
|
} |
1903 |
< |
finally { |
1904 |
< |
unlock(); |
1903 |
> |
nextTable = nextTab; |
1904 |
> |
transferOrigin = n; |
1905 |
> |
transferIndex = n; |
1906 |
> |
Node<V> rev = new Node<V>(MOVED, tab, null, null); |
1907 |
> |
for (int k = n; k > 0;) { // progressively reveal ready slots |
1908 |
> |
int nextk = (k > stride) ? k - stride : 0; |
1909 |
> |
for (int m = nextk; m < k; ++m) |
1910 |
> |
nextTab[m] = rev; |
1911 |
> |
for (int m = n + nextk; m < n + k; ++m) |
1912 |
> |
nextTab[m] = rev; |
1913 |
> |
U.putOrderedInt(this, TRANSFERORIGIN, k = nextk); |
1914 |
|
} |
1915 |
|
} |
1916 |
+ |
int nextn = nextTab.length; |
1917 |
+ |
Node<V> fwd = new Node<V>(MOVED, nextTab, null, null); |
1918 |
+ |
boolean advance = true; |
1919 |
+ |
for (int i = 0, bound = 0;;) { |
1920 |
+ |
int nextIndex, nextBound; Node<V> f; Object fk; |
1921 |
+ |
while (advance) { |
1922 |
+ |
if (--i >= bound) |
1923 |
+ |
advance = false; |
1924 |
+ |
else if ((nextIndex = transferIndex) <= transferOrigin) { |
1925 |
+ |
i = -1; |
1926 |
+ |
advance = false; |
1927 |
+ |
} |
1928 |
+ |
else if (U.compareAndSwapInt |
1929 |
+ |
(this, TRANSFERINDEX, nextIndex, |
1930 |
+ |
nextBound = (nextIndex > stride ? |
1931 |
+ |
nextIndex - stride : 0))) { |
1932 |
+ |
bound = nextBound; |
1933 |
+ |
i = nextIndex - 1; |
1934 |
+ |
advance = false; |
1935 |
+ |
} |
1936 |
+ |
} |
1937 |
+ |
if (i < 0 || i >= n || i + n >= nextn) { |
1938 |
+ |
for (int sc;;) { |
1939 |
+ |
if (U.compareAndSwapInt(this, SIZECTL, sc = sizeCtl, ++sc)) { |
1940 |
+ |
if (sc == -1) { |
1941 |
+ |
nextTable = null; |
1942 |
+ |
table = nextTab; |
1943 |
+ |
sizeCtl = (n << 1) - (n >>> 1); |
1944 |
+ |
} |
1945 |
+ |
return; |
1946 |
+ |
} |
1947 |
+ |
} |
1948 |
+ |
} |
1949 |
+ |
else if ((f = tabAt(tab, i)) == null) { |
1950 |
+ |
if (casTabAt(tab, i, null, fwd)) { |
1951 |
+ |
setTabAt(nextTab, i, null); |
1952 |
+ |
setTabAt(nextTab, i + n, null); |
1953 |
+ |
advance = true; |
1954 |
+ |
} |
1955 |
+ |
} |
1956 |
+ |
else if (f.hash >= 0) { |
1957 |
+ |
synchronized (f) { |
1958 |
+ |
if (tabAt(tab, i) == f) { |
1959 |
+ |
int runBit = f.hash & n; |
1960 |
+ |
Node<V> lastRun = f, lo = null, hi = null; |
1961 |
+ |
for (Node<V> p = f.next; p != null; p = p.next) { |
1962 |
+ |
int b = p.hash & n; |
1963 |
+ |
if (b != runBit) { |
1964 |
+ |
runBit = b; |
1965 |
+ |
lastRun = p; |
1966 |
+ |
} |
1967 |
+ |
} |
1968 |
+ |
if (runBit == 0) |
1969 |
+ |
lo = lastRun; |
1970 |
+ |
else |
1971 |
+ |
hi = lastRun; |
1972 |
+ |
for (Node<V> p = f; p != lastRun; p = p.next) { |
1973 |
+ |
int ph = p.hash; |
1974 |
+ |
Object pk = p.key; V pv = p.val; |
1975 |
+ |
if ((ph & n) == 0) |
1976 |
+ |
lo = new Node<V>(ph, pk, pv, lo); |
1977 |
+ |
else |
1978 |
+ |
hi = new Node<V>(ph, pk, pv, hi); |
1979 |
+ |
} |
1980 |
+ |
setTabAt(nextTab, i, lo); |
1981 |
+ |
setTabAt(nextTab, i + n, hi); |
1982 |
+ |
setTabAt(tab, i, fwd); |
1983 |
+ |
advance = true; |
1984 |
+ |
} |
1985 |
+ |
} |
1986 |
+ |
} |
1987 |
+ |
else if ((fk = f.key) instanceof TreeBin) { |
1988 |
+ |
TreeBin<V> t = (TreeBin<V>)fk; |
1989 |
+ |
t.acquire(0); |
1990 |
+ |
try { |
1991 |
+ |
if (tabAt(tab, i) == f) { |
1992 |
+ |
TreeBin<V> lt = new TreeBin<V>(); |
1993 |
+ |
TreeBin<V> ht = new TreeBin<V>(); |
1994 |
+ |
int lc = 0, hc = 0; |
1995 |
+ |
for (Node<V> e = t.first; e != null; e = e.next) { |
1996 |
+ |
int h = e.hash; |
1997 |
+ |
Object k = e.key; V v = e.val; |
1998 |
+ |
if ((h & n) == 0) { |
1999 |
+ |
++lc; |
2000 |
+ |
lt.putTreeNode(h, k, v); |
2001 |
+ |
} |
2002 |
+ |
else { |
2003 |
+ |
++hc; |
2004 |
+ |
ht.putTreeNode(h, k, v); |
2005 |
+ |
} |
2006 |
+ |
} |
2007 |
+ |
Node<V> ln, hn; // throw away trees if too small |
2008 |
+ |
if (lc < TREE_THRESHOLD) { |
2009 |
+ |
ln = null; |
2010 |
+ |
for (Node<V> p = lt.first; p != null; p = p.next) |
2011 |
+ |
ln = new Node<V>(p.hash, p.key, p.val, ln); |
2012 |
+ |
} |
2013 |
+ |
else |
2014 |
+ |
ln = new Node<V>(MOVED, lt, null, null); |
2015 |
+ |
setTabAt(nextTab, i, ln); |
2016 |
+ |
if (hc < TREE_THRESHOLD) { |
2017 |
+ |
hn = null; |
2018 |
+ |
for (Node<V> p = ht.first; p != null; p = p.next) |
2019 |
+ |
hn = new Node<V>(p.hash, p.key, p.val, hn); |
2020 |
+ |
} |
2021 |
+ |
else |
2022 |
+ |
hn = new Node<V>(MOVED, ht, null, null); |
2023 |
+ |
setTabAt(nextTab, i + n, hn); |
2024 |
+ |
setTabAt(tab, i, fwd); |
2025 |
+ |
advance = true; |
2026 |
+ |
} |
2027 |
+ |
} finally { |
2028 |
+ |
t.release(0); |
2029 |
+ |
} |
2030 |
+ |
} |
2031 |
+ |
else |
2032 |
+ |
advance = true; // already processed |
2033 |
+ |
} |
2034 |
+ |
} |
2035 |
+ |
|
2036 |
+ |
/* ---------------- Counter support -------------- */ |
2037 |
+ |
|
2038 |
+ |
final long sumCount() { |
2039 |
+ |
Cell[] as = counterCells; Cell a; |
2040 |
+ |
long sum = baseCount; |
2041 |
+ |
if (as != null) { |
2042 |
+ |
for (int i = 0; i < as.length; ++i) { |
2043 |
+ |
if ((a = as[i]) != null) |
2044 |
+ |
sum += a.value; |
2045 |
+ |
} |
2046 |
+ |
} |
2047 |
+ |
return sum; |
2048 |
|
} |
2049 |
|
|
2050 |
+ |
// See LongAdder version for explanation |
2051 |
+ |
private final void fullAddCount(long x, boolean wasUncontended) { |
2052 |
+ |
int h; |
2053 |
+ |
if ((h = ThreadLocalRandom.getProbe()) == 0) { |
2054 |
+ |
ThreadLocalRandom.localInit(); // force initialization |
2055 |
+ |
h = ThreadLocalRandom.getProbe(); |
2056 |
+ |
wasUncontended = true; |
2057 |
+ |
} |
2058 |
+ |
boolean collide = false; // True if last slot nonempty |
2059 |
+ |
for (;;) { |
2060 |
+ |
Cell[] as; Cell a; int n; long v; |
2061 |
+ |
if ((as = counterCells) != null && (n = as.length) > 0) { |
2062 |
+ |
if ((a = as[(n - 1) & h]) == null) { |
2063 |
+ |
if (cellsBusy == 0) { // Try to attach new Cell |
2064 |
+ |
Cell r = new Cell(x); // Optimistic create |
2065 |
+ |
if (cellsBusy == 0 && |
2066 |
+ |
U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) { |
2067 |
+ |
boolean created = false; |
2068 |
+ |
try { // Recheck under lock |
2069 |
+ |
Cell[] rs; int m, j; |
2070 |
+ |
if ((rs = counterCells) != null && |
2071 |
+ |
(m = rs.length) > 0 && |
2072 |
+ |
rs[j = (m - 1) & h] == null) { |
2073 |
+ |
rs[j] = r; |
2074 |
+ |
created = true; |
2075 |
+ |
} |
2076 |
+ |
} finally { |
2077 |
+ |
cellsBusy = 0; |
2078 |
+ |
} |
2079 |
+ |
if (created) |
2080 |
+ |
break; |
2081 |
+ |
continue; // Slot is now non-empty |
2082 |
+ |
} |
2083 |
+ |
} |
2084 |
+ |
collide = false; |
2085 |
+ |
} |
2086 |
+ |
else if (!wasUncontended) // CAS already known to fail |
2087 |
+ |
wasUncontended = true; // Continue after rehash |
2088 |
+ |
else if (U.compareAndSwapLong(a, CELLVALUE, v = a.value, v + x)) |
2089 |
+ |
break; |
2090 |
+ |
else if (counterCells != as || n >= NCPU) |
2091 |
+ |
collide = false; // At max size or stale |
2092 |
+ |
else if (!collide) |
2093 |
+ |
collide = true; |
2094 |
+ |
else if (cellsBusy == 0 && |
2095 |
+ |
U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) { |
2096 |
+ |
try { |
2097 |
+ |
if (counterCells == as) {// Expand table unless stale |
2098 |
+ |
Cell[] rs = new Cell[n << 1]; |
2099 |
+ |
for (int i = 0; i < n; ++i) |
2100 |
+ |
rs[i] = as[i]; |
2101 |
+ |
counterCells = rs; |
2102 |
+ |
} |
2103 |
+ |
} finally { |
2104 |
+ |
cellsBusy = 0; |
2105 |
+ |
} |
2106 |
+ |
collide = false; |
2107 |
+ |
continue; // Retry with expanded table |
2108 |
+ |
} |
2109 |
+ |
h = ThreadLocalRandom.advanceProbe(h); |
2110 |
+ |
} |
2111 |
+ |
else if (cellsBusy == 0 && counterCells == as && |
2112 |
+ |
U.compareAndSwapInt(this, CELLSBUSY, 0, 1)) { |
2113 |
+ |
boolean init = false; |
2114 |
+ |
try { // Initialize table |
2115 |
+ |
if (counterCells == as) { |
2116 |
+ |
Cell[] rs = new Cell[2]; |
2117 |
+ |
rs[h & 1] = new Cell(x); |
2118 |
+ |
counterCells = rs; |
2119 |
+ |
init = true; |
2120 |
+ |
} |
2121 |
+ |
} finally { |
2122 |
+ |
cellsBusy = 0; |
2123 |
+ |
} |
2124 |
+ |
if (init) |
2125 |
+ |
break; |
2126 |
+ |
} |
2127 |
+ |
else if (U.compareAndSwapLong(this, BASECOUNT, v = baseCount, v + x)) |
2128 |
+ |
break; // Fall back on using base |
2129 |
+ |
} |
2130 |
+ |
} |
2131 |
+ |
|
2132 |
+ |
/* ----------------Table Traversal -------------- */ |
2133 |
+ |
|
2134 |
|
/** |
2135 |
< |
* ConcurrentReaderHashMap list entry. |
2135 |
> |
* Encapsulates traversal for methods such as containsValue; also |
2136 |
> |
* serves as a base class for other iterators and bulk tasks. |
2137 |
> |
* |
2138 |
> |
* At each step, the iterator snapshots the key ("nextKey") and |
2139 |
> |
* value ("nextVal") of a valid node (i.e., one that, at point of |
2140 |
> |
* snapshot, has a non-null user value). Because val fields can |
2141 |
> |
* change (including to null, indicating deletion), field nextVal |
2142 |
> |
* might not be accurate at point of use, but still maintains the |
2143 |
> |
* weak consistency property of holding a value that was once |
2144 |
> |
* valid. To support iterator.remove, the nextKey field is not |
2145 |
> |
* updated (nulled out) when the iterator cannot advance. |
2146 |
> |
* |
2147 |
> |
* Internal traversals directly access these fields, as in: |
2148 |
> |
* {@code while (it.advanceValue() != null) { process(it.nextKey); }} |
2149 |
> |
* |
2150 |
> |
* Exported iterators must track whether the iterator has advanced |
2151 |
> |
* (in hasNext vs next) (by setting/checking/nulling field |
2152 |
> |
* nextVal), and then extract key, value, or key-value pairs as |
2153 |
> |
* return values of next(). |
2154 |
> |
* |
2155 |
> |
* The iterator visits once each still-valid node that was |
2156 |
> |
* reachable upon iterator construction. It might miss some that |
2157 |
> |
* were added to a bin after the bin was visited, which is OK wrt |
2158 |
> |
* consistency guarantees. Maintaining this property in the face |
2159 |
> |
* of possible ongoing resizes requires a fair amount of |
2160 |
> |
* bookkeeping state that is difficult to optimize away amidst |
2161 |
> |
* volatile accesses. Even so, traversal maintains reasonable |
2162 |
> |
* throughput. |
2163 |
> |
* |
2164 |
> |
* Normally, iteration proceeds bin-by-bin traversing lists. |
2165 |
> |
* However, if the table has been resized, then all future steps |
2166 |
> |
* must traverse both the bin at the current index as well as at |
2167 |
> |
* (index + baseSize); and so on for further resizings. To |
2168 |
> |
* paranoically cope with potential sharing by users of iterators |
2169 |
> |
* across threads, iteration terminates if a bounds checks fails |
2170 |
> |
* for a table read. |
2171 |
> |
* |
2172 |
> |
* This class supports both Spliterator-based traversal and |
2173 |
> |
* CountedCompleter-based bulk tasks. The same "batch" field is |
2174 |
> |
* used, but in slightly different ways, in the two cases. For |
2175 |
> |
* Spliterators, it is a saturating (at Integer.MAX_VALUE) |
2176 |
> |
* estimate of element coverage. For CHM tasks, it is a pre-scaled |
2177 |
> |
* size that halves down to zero for leaf tasks, that is only |
2178 |
> |
* computed upon execution of the task. (Tasks can be submitted to |
2179 |
> |
* any pool, of any size, so we don't know scale factors until |
2180 |
> |
* running.) |
2181 |
> |
* |
2182 |
> |
* This class extends CountedCompleter to streamline parallel |
2183 |
> |
* iteration in bulk operations. This adds only a few fields of |
2184 |
> |
* space overhead, which is small enough in cases where it is not |
2185 |
> |
* needed to not worry about it. Because CountedCompleter is |
2186 |
> |
* Serializable, but iterators need not be, we need to add warning |
2187 |
> |
* suppressions. |
2188 |
|
*/ |
2189 |
< |
private static class HashEntry<K,V> implements Entry<K,V> { |
2190 |
< |
private final K key; |
2191 |
< |
private V value; |
2192 |
< |
private final int hash; |
2193 |
< |
private final HashEntry<K,V> next; |
2189 |
> |
@SuppressWarnings("serial") static class Traverser<K,V,R> |
2190 |
> |
extends CountedCompleter<R> { |
2191 |
> |
final ConcurrentHashMap<K,V> map; |
2192 |
> |
Node<V> next; // the next entry to use |
2193 |
> |
K nextKey; // cached key field of next |
2194 |
> |
V nextVal; // cached val field of next |
2195 |
> |
Node<V>[] tab; // current table; updated if resized |
2196 |
> |
int index; // index of bin to use next |
2197 |
> |
int baseIndex; // current index of initial table |
2198 |
> |
int baseLimit; // index bound for initial table |
2199 |
> |
int baseSize; // initial table size |
2200 |
> |
int batch; // split control |
2201 |
> |
/** Creates iterator for all entries in the table. */ |
2202 |
> |
Traverser(ConcurrentHashMap<K,V> map) { |
2203 |
> |
this.map = map; |
2204 |
> |
Node<V>[] t; |
2205 |
> |
if ((t = tab = map.table) != null) |
2206 |
> |
baseLimit = baseSize = t.length; |
2207 |
> |
} |
2208 |
|
|
2209 |
< |
HashEntry(int hash, K key, V value, HashEntry<K,V> next) { |
2210 |
< |
this.value = value; |
2211 |
< |
this.hash = hash; |
2212 |
< |
this.key = key; |
2213 |
< |
this.next = next; |
2209 |
> |
/** Task constructor */ |
2210 |
> |
Traverser(ConcurrentHashMap<K,V> map, Traverser<K,V,?> it, int batch) { |
2211 |
> |
super(it); |
2212 |
> |
this.map = map; |
2213 |
> |
this.batch = batch; // -1 if unknown |
2214 |
> |
if (it == null) { |
2215 |
> |
Node<V>[] t; |
2216 |
> |
if ((t = tab = map.table) != null) |
2217 |
> |
baseLimit = baseSize = t.length; |
2218 |
> |
} |
2219 |
> |
else { // split parent |
2220 |
> |
this.tab = it.tab; |
2221 |
> |
this.baseSize = it.baseSize; |
2222 |
> |
int hi = this.baseLimit = it.baseLimit; |
2223 |
> |
it.baseLimit = this.index = this.baseIndex = |
2224 |
> |
(hi + it.baseIndex + 1) >>> 1; |
2225 |
> |
} |
2226 |
> |
} |
2227 |
> |
|
2228 |
> |
/** Spliterator constructor */ |
2229 |
> |
Traverser(ConcurrentHashMap<K,V> map, Traverser<K,V,?> it) { |
2230 |
> |
super(it); |
2231 |
> |
this.map = map; |
2232 |
> |
if (it == null) { |
2233 |
> |
Node<V>[] t; |
2234 |
> |
if ((t = tab = map.table) != null) |
2235 |
> |
baseLimit = baseSize = t.length; |
2236 |
> |
long n = map.sumCount(); |
2237 |
> |
batch = ((n > (long)Integer.MAX_VALUE) ? Integer.MAX_VALUE : |
2238 |
> |
(int)n); |
2239 |
> |
} |
2240 |
> |
else { |
2241 |
> |
this.tab = it.tab; |
2242 |
> |
this.baseSize = it.baseSize; |
2243 |
> |
int hi = this.baseLimit = it.baseLimit; |
2244 |
> |
it.baseLimit = this.index = this.baseIndex = |
2245 |
> |
(hi + it.baseIndex + 1) >>> 1; |
2246 |
> |
this.batch = it.batch >>>= 1; |
2247 |
> |
} |
2248 |
|
} |
2249 |
|
|
2250 |
< |
public K getKey() { |
2251 |
< |
return key; |
2250 |
> |
/** |
2251 |
> |
* Advances next; returns nextVal or null if terminated. |
2252 |
> |
* See above for explanation. |
2253 |
> |
*/ |
2254 |
> |
@SuppressWarnings("unchecked") final V advanceValue() { |
2255 |
> |
V v; |
2256 |
> |
Node<V> e = next; |
2257 |
> |
outer: do { |
2258 |
> |
if (e != null) // advance past used/skipped node |
2259 |
> |
e = e.next; |
2260 |
> |
while (e == null) { // get to next non-null bin |
2261 |
> |
Node<V>[] t; int b, i, n; Object ek; // must use locals |
2262 |
> |
if ((t = tab) == null || (n = t.length) <= (i = index) || |
2263 |
> |
i < 0 || (b = baseIndex) >= baseLimit) { |
2264 |
> |
v = null; |
2265 |
> |
break outer; |
2266 |
> |
} |
2267 |
> |
if ((e = tabAt(t, i)) != null && e.hash < 0) { |
2268 |
> |
if ((ek = e.key) instanceof TreeBin) |
2269 |
> |
e = ((TreeBin<V>)ek).first; |
2270 |
> |
else { |
2271 |
> |
tab = (Node<V>[])ek; |
2272 |
> |
continue; // restarts due to null val |
2273 |
> |
} |
2274 |
> |
} // visit upper slots if present |
2275 |
> |
index = (i += baseSize) < n ? i : (baseIndex = b + 1); |
2276 |
> |
} |
2277 |
> |
nextKey = (K)e.key; // skip deleted or special nodes |
2278 |
> |
} while ((v = e.val) == null); |
2279 |
> |
next = e; |
2280 |
> |
return nextVal = v; |
2281 |
|
} |
2282 |
|
|
2283 |
< |
public V getValue() { |
2284 |
< |
return value; |
2283 |
> |
// Same idea, but with fewer accesses for key traversals |
2284 |
> |
@SuppressWarnings("unchecked") final K advanceKey() { |
2285 |
> |
K k; |
2286 |
> |
Node<V> e = next; |
2287 |
> |
outer: do { |
2288 |
> |
if (e != null) |
2289 |
> |
e = e.next; |
2290 |
> |
while (e == null) { |
2291 |
> |
Node<V>[] t; int b, i, n; Object ek; |
2292 |
> |
if ((t = tab) == null || (n = t.length) <= (i = index) || |
2293 |
> |
i < 0 || (b = baseIndex) >= baseLimit) { |
2294 |
> |
nextVal = null; |
2295 |
> |
k = null; |
2296 |
> |
break outer; |
2297 |
> |
} |
2298 |
> |
if ((e = tabAt(t, i)) != null && e.hash < 0) { |
2299 |
> |
if ((ek = e.key) instanceof TreeBin) |
2300 |
> |
e = ((TreeBin<V>)ek).first; |
2301 |
> |
else { |
2302 |
> |
tab = (Node<V>[])ek; |
2303 |
> |
continue; |
2304 |
> |
} |
2305 |
> |
} |
2306 |
> |
index = (i += baseSize) < n ? i : (baseIndex = b + 1); |
2307 |
> |
} |
2308 |
> |
k = (K)e.key; |
2309 |
> |
} while ((nextVal = e.val) == null); |
2310 |
> |
next = e; |
2311 |
> |
return nextKey = k; |
2312 |
|
} |
2313 |
|
|
2314 |
< |
public V setValue(V newValue) { |
2315 |
< |
// We aren't required to, and don't provide any |
2316 |
< |
// visibility barriers for setting value. |
2317 |
< |
if (newValue == null) |
2318 |
< |
throw new NullPointerException(); |
445 |
< |
V oldValue = this.value; |
446 |
< |
this.value = newValue; |
447 |
< |
return oldValue; |
2314 |
> |
public final void remove() { |
2315 |
> |
K k = nextKey; |
2316 |
> |
if (k == null && (advanceValue() == null || (k = nextKey) == null)) |
2317 |
> |
throw new IllegalStateException(); |
2318 |
> |
map.internalReplace(k, null, null); |
2319 |
|
} |
2320 |
|
|
2321 |
< |
public boolean equals(Object o) { |
2322 |
< |
if (!(o instanceof Entry)) |
452 |
< |
return false; |
453 |
< |
Entry<K,V> e = (Entry<K,V>)o; |
454 |
< |
return (key.equals(e.getKey()) && value.equals(e.getValue())); |
2321 |
> |
public final boolean hasNext() { |
2322 |
> |
return nextVal != null || advanceValue() != null; |
2323 |
|
} |
2324 |
|
|
2325 |
< |
public int hashCode() { |
2326 |
< |
return key.hashCode() ^ value.hashCode(); |
2325 |
> |
public final boolean hasMoreElements() { return hasNext(); } |
2326 |
> |
|
2327 |
> |
public void compute() { } // default no-op CountedCompleter body |
2328 |
> |
|
2329 |
> |
/** |
2330 |
> |
* Returns a batch value > 0 if this task should (and must) be |
2331 |
> |
* split, if so, adding to pending count, and in any case |
2332 |
> |
* updating batch value. The initial batch value is approx |
2333 |
> |
* exp2 of the number of times (minus one) to split task by |
2334 |
> |
* two before executing leaf action. This value is faster to |
2335 |
> |
* compute and more convenient to use as a guide to splitting |
2336 |
> |
* than is the depth, since it is used while dividing by two |
2337 |
> |
* anyway. |
2338 |
> |
*/ |
2339 |
> |
final int preSplit() { |
2340 |
> |
int b; ForkJoinPool pool; |
2341 |
> |
if ((b = batch) < 0) { // force initialization |
2342 |
> |
int sp = (((pool = getPool()) == null) ? |
2343 |
> |
ForkJoinPool.getCommonPoolParallelism() : |
2344 |
> |
pool.getParallelism()) << 3; // slack of 8 |
2345 |
> |
long n = map.sumCount(); |
2346 |
> |
b = (n <= 0L) ? 0 : (n < (long)sp) ? (int)n : sp; |
2347 |
> |
} |
2348 |
> |
b = (b <= 1 || baseIndex == baseLimit) ? 0 : (b >>> 1); |
2349 |
> |
if ((batch = b) > 0) |
2350 |
> |
addToPendingCount(1); |
2351 |
> |
return b; |
2352 |
|
} |
2353 |
|
|
2354 |
< |
public String toString() { |
2355 |
< |
return key + "=" + value; |
2354 |
> |
// spliterator support |
2355 |
> |
|
2356 |
> |
public long estimateSize() { |
2357 |
> |
return batch; |
2358 |
|
} |
464 |
– |
} |
2359 |
|
|
2360 |
+ |
} |
2361 |
|
|
2362 |
|
/* ---------------- Public operations -------------- */ |
2363 |
|
|
2364 |
|
/** |
2365 |
< |
* Constructs a new, empty map with the specified initial |
2366 |
< |
* capacity and the specified load factor. |
2365 |
> |
* Creates a new, empty map with the default initial table size (16). |
2366 |
> |
*/ |
2367 |
> |
public ConcurrentHashMap() { |
2368 |
> |
} |
2369 |
> |
|
2370 |
> |
/** |
2371 |
> |
* Creates a new, empty map with an initial table size |
2372 |
> |
* accommodating the specified number of elements without the need |
2373 |
> |
* to dynamically resize. |
2374 |
|
* |
2375 |
< |
* @param initialCapacity the initial capacity. The actual |
2376 |
< |
* initial capacity is rounded up to the nearest power of two. |
2377 |
< |
* @param loadFactor the load factor threshold, used to control resizing. |
2378 |
< |
* @param segments the number of concurrently accessible segments. the |
477 |
< |
* actual number of segments is rounded to the next power of two. |
478 |
< |
* @throws IllegalArgumentException if the initial capacity is |
479 |
< |
* negative or the load factor or number of segments are |
480 |
< |
* nonpositive. |
2375 |
> |
* @param initialCapacity The implementation performs internal |
2376 |
> |
* sizing to accommodate this many elements. |
2377 |
> |
* @throws IllegalArgumentException if the initial capacity of |
2378 |
> |
* elements is negative |
2379 |
|
*/ |
2380 |
< |
public ConcurrentHashMap(int initialCapacity, |
2381 |
< |
float loadFactor, int segments) { |
484 |
< |
if (!(loadFactor > 0) || initialCapacity < 0 || segments <= 0) |
2380 |
> |
public ConcurrentHashMap(int initialCapacity) { |
2381 |
> |
if (initialCapacity < 0) |
2382 |
|
throw new IllegalArgumentException(); |
2383 |
+ |
int cap = ((initialCapacity >= (MAXIMUM_CAPACITY >>> 1)) ? |
2384 |
+ |
MAXIMUM_CAPACITY : |
2385 |
+ |
tableSizeFor(initialCapacity + (initialCapacity >>> 1) + 1)); |
2386 |
+ |
this.sizeCtl = cap; |
2387 |
+ |
} |
2388 |
|
|
2389 |
< |
// Find power-of-two sizes best matching arguments |
2390 |
< |
int sshift = 0; |
2391 |
< |
int ssize = 1; |
2392 |
< |
while (ssize < segments) { |
2393 |
< |
++sshift; |
2394 |
< |
ssize <<= 1; |
2395 |
< |
} |
2396 |
< |
segmentShift = 32 - sshift; |
495 |
< |
segmentMask = ssize - 1; |
496 |
< |
this.segments = new Segment[ssize]; |
497 |
< |
|
498 |
< |
if (initialCapacity > MAXIMUM_CAPACITY) |
499 |
< |
initialCapacity = MAXIMUM_CAPACITY; |
500 |
< |
int c = initialCapacity / ssize; |
501 |
< |
if (c * ssize < initialCapacity) |
502 |
< |
++c; |
503 |
< |
int cap = 1; |
504 |
< |
while (cap < c) |
505 |
< |
cap <<= 1; |
506 |
< |
|
507 |
< |
for (int i = 0; i < this.segments.length; ++i) |
508 |
< |
this.segments[i] = new Segment<K,V>(cap, loadFactor); |
2389 |
> |
/** |
2390 |
> |
* Creates a new map with the same mappings as the given map. |
2391 |
> |
* |
2392 |
> |
* @param m the map |
2393 |
> |
*/ |
2394 |
> |
public ConcurrentHashMap(Map<? extends K, ? extends V> m) { |
2395 |
> |
this.sizeCtl = DEFAULT_CAPACITY; |
2396 |
> |
internalPutAll(m); |
2397 |
|
} |
2398 |
|
|
2399 |
|
/** |
2400 |
< |
* Constructs a new, empty map with the specified initial |
2401 |
< |
* capacity, and with default load factor and segments. |
2400 |
> |
* Creates a new, empty map with an initial table size based on |
2401 |
> |
* the given number of elements ({@code initialCapacity}) and |
2402 |
> |
* initial table density ({@code loadFactor}). |
2403 |
|
* |
2404 |
< |
* @param initialCapacity the initial capacity of the |
2405 |
< |
* ConcurrentHashMap. |
2404 |
> |
* @param initialCapacity the initial capacity. The implementation |
2405 |
> |
* performs internal sizing to accommodate this many elements, |
2406 |
> |
* given the specified load factor. |
2407 |
> |
* @param loadFactor the load factor (table density) for |
2408 |
> |
* establishing the initial table size |
2409 |
|
* @throws IllegalArgumentException if the initial capacity of |
2410 |
< |
* elements is negative. |
2410 |
> |
* elements is negative or the load factor is nonpositive |
2411 |
> |
* |
2412 |
> |
* @since 1.6 |
2413 |
|
*/ |
2414 |
< |
public ConcurrentHashMap(int initialCapacity) { |
2415 |
< |
this(initialCapacity, DEFAULT_LOAD_FACTOR, DEFAULT_SEGMENTS); |
2414 |
> |
public ConcurrentHashMap(int initialCapacity, float loadFactor) { |
2415 |
> |
this(initialCapacity, loadFactor, 1); |
2416 |
|
} |
2417 |
|
|
2418 |
|
/** |
2419 |
< |
* Constructs a new, empty map with a default initial capacity, |
2420 |
< |
* load factor, and number of segments. |
2419 |
> |
* Creates a new, empty map with an initial table size based on |
2420 |
> |
* the given number of elements ({@code initialCapacity}), table |
2421 |
> |
* density ({@code loadFactor}), and number of concurrently |
2422 |
> |
* updating threads ({@code concurrencyLevel}). |
2423 |
> |
* |
2424 |
> |
* @param initialCapacity the initial capacity. The implementation |
2425 |
> |
* performs internal sizing to accommodate this many elements, |
2426 |
> |
* given the specified load factor. |
2427 |
> |
* @param loadFactor the load factor (table density) for |
2428 |
> |
* establishing the initial table size |
2429 |
> |
* @param concurrencyLevel the estimated number of concurrently |
2430 |
> |
* updating threads. The implementation may use this value as |
2431 |
> |
* a sizing hint. |
2432 |
> |
* @throws IllegalArgumentException if the initial capacity is |
2433 |
> |
* negative or the load factor or concurrencyLevel are |
2434 |
> |
* nonpositive |
2435 |
|
*/ |
2436 |
< |
public ConcurrentHashMap() { |
2437 |
< |
this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_SEGMENTS); |
2436 |
> |
public ConcurrentHashMap(int initialCapacity, |
2437 |
> |
float loadFactor, int concurrencyLevel) { |
2438 |
> |
if (!(loadFactor > 0.0f) || initialCapacity < 0 || concurrencyLevel <= 0) |
2439 |
> |
throw new IllegalArgumentException(); |
2440 |
> |
if (initialCapacity < concurrencyLevel) // Use at least as many bins |
2441 |
> |
initialCapacity = concurrencyLevel; // as estimated threads |
2442 |
> |
long size = (long)(1.0 + (long)initialCapacity / loadFactor); |
2443 |
> |
int cap = (size >= (long)MAXIMUM_CAPACITY) ? |
2444 |
> |
MAXIMUM_CAPACITY : tableSizeFor((int)size); |
2445 |
> |
this.sizeCtl = cap; |
2446 |
|
} |
2447 |
|
|
2448 |
|
/** |
2449 |
< |
* Constructs a new map with the same mappings as the given map. The |
2450 |
< |
* map is created with a capacity of twice the number of mappings in |
2451 |
< |
* the given map or 11 (whichever is greater), and a default load factor. |
2449 |
> |
* Creates a new {@link Set} backed by a ConcurrentHashMap |
2450 |
> |
* from the given type to {@code Boolean.TRUE}. |
2451 |
> |
* |
2452 |
> |
* @return the new set |
2453 |
|
*/ |
2454 |
< |
public <A extends K, B extends V> ConcurrentHashMap(Map<A,B> t) { |
2455 |
< |
this(Math.max((int) (t.size() / DEFAULT_LOAD_FACTOR) + 1, |
2456 |
< |
11), |
540 |
< |
DEFAULT_LOAD_FACTOR, DEFAULT_SEGMENTS); |
541 |
< |
putAll(t); |
2454 |
> |
public static <K> KeySetView<K,Boolean> newKeySet() { |
2455 |
> |
return new KeySetView<K,Boolean> |
2456 |
> |
(new ConcurrentHashMap<K,Boolean>(), Boolean.TRUE); |
2457 |
|
} |
2458 |
|
|
2459 |
< |
// inherit Map javadoc |
2460 |
< |
public int size() { |
2461 |
< |
int c = 0; |
2462 |
< |
for (int i = 0; i < segments.length; ++i) |
2463 |
< |
c += segments[i].count; |
2464 |
< |
return c; |
2459 |
> |
/** |
2460 |
> |
* Creates a new {@link Set} backed by a ConcurrentHashMap |
2461 |
> |
* from the given type to {@code Boolean.TRUE}. |
2462 |
> |
* |
2463 |
> |
* @param initialCapacity The implementation performs internal |
2464 |
> |
* sizing to accommodate this many elements. |
2465 |
> |
* @throws IllegalArgumentException if the initial capacity of |
2466 |
> |
* elements is negative |
2467 |
> |
* @return the new set |
2468 |
> |
*/ |
2469 |
> |
public static <K> KeySetView<K,Boolean> newKeySet(int initialCapacity) { |
2470 |
> |
return new KeySetView<K,Boolean> |
2471 |
> |
(new ConcurrentHashMap<K,Boolean>(initialCapacity), Boolean.TRUE); |
2472 |
|
} |
2473 |
|
|
2474 |
< |
// inherit Map javadoc |
2474 |
> |
/** |
2475 |
> |
* {@inheritDoc} |
2476 |
> |
*/ |
2477 |
|
public boolean isEmpty() { |
2478 |
< |
for (int i = 0; i < segments.length; ++i) |
2479 |
< |
if (segments[i].count != 0) |
2480 |
< |
return false; |
2481 |
< |
return true; |
2478 |
> |
return sumCount() <= 0L; // ignore transient negative values |
2479 |
> |
} |
2480 |
> |
|
2481 |
> |
/** |
2482 |
> |
* {@inheritDoc} |
2483 |
> |
*/ |
2484 |
> |
public int size() { |
2485 |
> |
long n = sumCount(); |
2486 |
> |
return ((n < 0L) ? 0 : |
2487 |
> |
(n > (long)Integer.MAX_VALUE) ? Integer.MAX_VALUE : |
2488 |
> |
(int)n); |
2489 |
|
} |
2490 |
|
|
2491 |
|
/** |
2492 |
< |
* Returns the value to which the specified key is mapped in this table. |
2492 |
> |
* Returns the number of mappings. This method should be used |
2493 |
> |
* instead of {@link #size} because a ConcurrentHashMap may |
2494 |
> |
* contain more mappings than can be represented as an int. The |
2495 |
> |
* value returned is an estimate; the actual count may differ if |
2496 |
> |
* there are concurrent insertions or removals. |
2497 |
> |
* |
2498 |
> |
* @return the number of mappings |
2499 |
> |
*/ |
2500 |
> |
public long mappingCount() { |
2501 |
> |
long n = sumCount(); |
2502 |
> |
return (n < 0L) ? 0L : n; // ignore transient negative values |
2503 |
> |
} |
2504 |
> |
|
2505 |
> |
/** |
2506 |
> |
* Returns the value to which the specified key is mapped, |
2507 |
> |
* or {@code null} if this map contains no mapping for the key. |
2508 |
> |
* |
2509 |
> |
* <p>More formally, if this map contains a mapping from a key |
2510 |
> |
* {@code k} to a value {@code v} such that {@code key.equals(k)}, |
2511 |
> |
* then this method returns {@code v}; otherwise it returns |
2512 |
> |
* {@code null}. (There can be at most one such mapping.) |
2513 |
|
* |
2514 |
< |
* @param key a key in the table. |
564 |
< |
* @return the value to which the key is mapped in this table; |
565 |
< |
* <code>null</code> if the key is not mapped to any value in |
566 |
< |
* this table. |
567 |
< |
* @throws NullPointerException if the key is |
568 |
< |
* <code>null</code>. |
569 |
< |
* @see #put(Object, Object) |
2514 |
> |
* @throws NullPointerException if the specified key is null |
2515 |
|
*/ |
2516 |
|
public V get(Object key) { |
2517 |
< |
int hash = hash(key); // throws NullPointerException if key null |
2518 |
< |
return segmentFor(hash).get((K) key, hash); |
2517 |
> |
return internalGet(key); |
2518 |
> |
} |
2519 |
> |
|
2520 |
> |
/** |
2521 |
> |
* Returns the value to which the specified key is mapped, |
2522 |
> |
* or the given defaultValue if this map contains no mapping for the key. |
2523 |
> |
* |
2524 |
> |
* @param key the key |
2525 |
> |
* @param defaultValue the value to return if this map contains |
2526 |
> |
* no mapping for the given key |
2527 |
> |
* @return the mapping for the key, if present; else the defaultValue |
2528 |
> |
* @throws NullPointerException if the specified key is null |
2529 |
> |
*/ |
2530 |
> |
public V getValueOrDefault(Object key, V defaultValue) { |
2531 |
> |
V v; |
2532 |
> |
return (v = internalGet(key)) == null ? defaultValue : v; |
2533 |
|
} |
2534 |
|
|
2535 |
|
/** |
2536 |
|
* Tests if the specified object is a key in this table. |
2537 |
|
* |
2538 |
< |
* @param key possible key. |
2539 |
< |
* @return <code>true</code> if and only if the specified object |
2540 |
< |
* is a key in this table, as determined by the |
2541 |
< |
* <tt>equals</tt> method; <code>false</code> otherwise. |
2542 |
< |
* @throws NullPointerException if the key is |
584 |
< |
* <code>null</code>. |
585 |
< |
* @see #contains(Object) |
2538 |
> |
* @param key possible key |
2539 |
> |
* @return {@code true} if and only if the specified object |
2540 |
> |
* is a key in this table, as determined by the |
2541 |
> |
* {@code equals} method; {@code false} otherwise |
2542 |
> |
* @throws NullPointerException if the specified key is null |
2543 |
|
*/ |
2544 |
|
public boolean containsKey(Object key) { |
2545 |
< |
int hash = hash(key); // throws NullPointerException if key null |
589 |
< |
return segmentFor(hash).containsKey(key, hash); |
2545 |
> |
return internalGet(key) != null; |
2546 |
|
} |
2547 |
|
|
2548 |
|
/** |
2549 |
< |
* Returns <tt>true</tt> if this map maps one or more keys to the |
2550 |
< |
* specified value. Note: This method requires a full internal |
2551 |
< |
* traversal of the hash table, and so is much slower than |
596 |
< |
* method <tt>containsKey</tt>. |
2549 |
> |
* Returns {@code true} if this map maps one or more keys to the |
2550 |
> |
* specified value. Note: This method may require a full traversal |
2551 |
> |
* of the map, and is much slower than method {@code containsKey}. |
2552 |
|
* |
2553 |
< |
* @param value value whose presence in this map is to be tested. |
2554 |
< |
* @return <tt>true</tt> if this map maps one or more keys to the |
2555 |
< |
* specified value. |
2556 |
< |
* @throws NullPointerException if the value is <code>null</code>. |
2553 |
> |
* @param value value whose presence in this map is to be tested |
2554 |
> |
* @return {@code true} if this map maps one or more keys to the |
2555 |
> |
* specified value |
2556 |
> |
* @throws NullPointerException if the specified value is null |
2557 |
|
*/ |
2558 |
|
public boolean containsValue(Object value) { |
2559 |
|
if (value == null) |
2560 |
|
throw new NullPointerException(); |
2561 |
< |
|
2562 |
< |
for (int i = 0; i < segments.length; ++i) { |
2563 |
< |
if (segments[i].containsValue(value)) |
2561 |
> |
V v; |
2562 |
> |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
2563 |
> |
while ((v = it.advanceValue()) != null) { |
2564 |
> |
if (v == value || value.equals(v)) |
2565 |
|
return true; |
2566 |
|
} |
2567 |
|
return false; |
2568 |
|
} |
2569 |
+ |
|
2570 |
|
/** |
2571 |
< |
* Tests if some key maps into the specified value in this table. |
2572 |
< |
* This operation is more expensive than the <code>containsKey</code> |
2573 |
< |
* method.<p> |
2574 |
< |
* |
2575 |
< |
* Note that this method is identical in functionality to containsValue, |
2576 |
< |
* (which is part of the Map interface in the collections framework). |
2577 |
< |
* |
2578 |
< |
* @param value a value to search for. |
2579 |
< |
* @return <code>true</code> if and only if some key maps to the |
2580 |
< |
* <code>value</code> argument in this table as |
2581 |
< |
* determined by the <tt>equals</tt> method; |
2582 |
< |
* <code>false</code> otherwise. |
2583 |
< |
* @throws NullPointerException if the value is <code>null</code>. |
627 |
< |
* @see #containsKey(Object) |
628 |
< |
* @see #containsValue(Object) |
629 |
< |
* @see Map |
2571 |
> |
* Legacy method testing if some key maps into the specified value |
2572 |
> |
* in this table. This method is identical in functionality to |
2573 |
> |
* {@link #containsValue(Object)}, and exists solely to ensure |
2574 |
> |
* full compatibility with class {@link java.util.Hashtable}, |
2575 |
> |
* which supported this method prior to introduction of the |
2576 |
> |
* Java Collections framework. |
2577 |
> |
* |
2578 |
> |
* @param value a value to search for |
2579 |
> |
* @return {@code true} if and only if some key maps to the |
2580 |
> |
* {@code value} argument in this table as |
2581 |
> |
* determined by the {@code equals} method; |
2582 |
> |
* {@code false} otherwise |
2583 |
> |
* @throws NullPointerException if the specified value is null |
2584 |
|
*/ |
2585 |
< |
public boolean contains(Object value) { |
2585 |
> |
@Deprecated public boolean contains(Object value) { |
2586 |
|
return containsValue(value); |
2587 |
|
} |
2588 |
|
|
2589 |
|
/** |
2590 |
< |
* Maps the specified <code>key</code> to the specified |
2591 |
< |
* <code>value</code> in this table. Neither the key nor the |
638 |
< |
* value can be <code>null</code>. <p> |
2590 |
> |
* Maps the specified key to the specified value in this table. |
2591 |
> |
* Neither the key nor the value can be null. |
2592 |
|
* |
2593 |
< |
* The value can be retrieved by calling the <code>get</code> method |
2593 |
> |
* <p>The value can be retrieved by calling the {@code get} method |
2594 |
|
* with a key that is equal to the original key. |
2595 |
|
* |
2596 |
< |
* @param key the table key. |
2597 |
< |
* @param value the value. |
2598 |
< |
* @return the previous value of the specified key in this table, |
2599 |
< |
* or <code>null</code> if it did not have one. |
2600 |
< |
* @throws NullPointerException if the key or value is |
648 |
< |
* <code>null</code>. |
649 |
< |
* @see Object#equals(Object) |
650 |
< |
* @see #get(Object) |
2596 |
> |
* @param key key with which the specified value is to be associated |
2597 |
> |
* @param value value to be associated with the specified key |
2598 |
> |
* @return the previous value associated with {@code key}, or |
2599 |
> |
* {@code null} if there was no mapping for {@code key} |
2600 |
> |
* @throws NullPointerException if the specified key or value is null |
2601 |
|
*/ |
2602 |
|
public V put(K key, V value) { |
2603 |
< |
if (value == null) |
654 |
< |
throw new NullPointerException(); |
655 |
< |
int hash = hash(key); |
656 |
< |
return segmentFor(hash).put(key, hash, value, false); |
2603 |
> |
return internalPut(key, value, false); |
2604 |
|
} |
2605 |
|
|
2606 |
|
/** |
2607 |
< |
* If the specified key is not already associated |
661 |
< |
* with a value, associate it with the given value. |
662 |
< |
* This is equivalent to |
663 |
< |
* <pre> |
664 |
< |
* if (!map.containsKey(key)) map.put(key, value); |
665 |
< |
* return get(key); |
666 |
< |
* </pre> |
667 |
< |
* Except that the action is performed atomically. |
668 |
< |
* @param key key with which the specified value is to be associated. |
669 |
< |
* @param value value to be associated with the specified key. |
670 |
< |
* @return previous value associated with specified key, or <tt>null</tt> |
671 |
< |
* if there was no mapping for key. A <tt>null</tt> return can |
672 |
< |
* also indicate that the map previously associated <tt>null</tt> |
673 |
< |
* with the specified key, if the implementation supports |
674 |
< |
* <tt>null</tt> values. |
675 |
< |
* |
676 |
< |
* @throws NullPointerException this map does not permit <tt>null</tt> |
677 |
< |
* keys or values, and the specified key or value is |
678 |
< |
* <tt>null</tt>. |
2607 |
> |
* {@inheritDoc} |
2608 |
|
* |
2609 |
< |
**/ |
2609 |
> |
* @return the previous value associated with the specified key, |
2610 |
> |
* or {@code null} if there was no mapping for the key |
2611 |
> |
* @throws NullPointerException if the specified key or value is null |
2612 |
> |
*/ |
2613 |
|
public V putIfAbsent(K key, V value) { |
2614 |
< |
if (value == null) |
683 |
< |
throw new NullPointerException(); |
684 |
< |
int hash = hash(key); |
685 |
< |
return segmentFor(hash).put(key, hash, value, true); |
2614 |
> |
return internalPut(key, value, true); |
2615 |
|
} |
2616 |
|
|
688 |
– |
|
2617 |
|
/** |
2618 |
|
* Copies all of the mappings from the specified map to this one. |
691 |
– |
* |
2619 |
|
* These mappings replace any mappings that this map had for any of the |
2620 |
< |
* keys currently in the specified Map. |
2620 |
> |
* keys currently in the specified map. |
2621 |
|
* |
2622 |
< |
* @param t Mappings to be stored in this map. |
2622 |
> |
* @param m mappings to be stored in this map |
2623 |
|
*/ |
2624 |
< |
public void putAll(Map<? extends K, ? extends V> t) { |
2625 |
< |
Iterator<Map.Entry<? extends K, ? extends V>> it = t.entrySet().iterator(); |
699 |
< |
while (it.hasNext()) { |
700 |
< |
Entry<? extends K, ? extends V> e = it.next(); |
701 |
< |
put(e.getKey(), e.getValue()); |
702 |
< |
} |
2624 |
> |
public void putAll(Map<? extends K, ? extends V> m) { |
2625 |
> |
internalPutAll(m); |
2626 |
|
} |
2627 |
|
|
2628 |
|
/** |
2629 |
< |
* Removes the key (and its corresponding value) from this |
2630 |
< |
* table. This method does nothing if the key is not in the table. |
2629 |
> |
* If the specified key is not already associated with a value (or |
2630 |
> |
* is mapped to {@code null}), attempts to compute its value using |
2631 |
> |
* the given mapping function and enters it into this map unless |
2632 |
> |
* {@code null}. The entire method invocation is performed |
2633 |
> |
* atomically, so the function is applied at most once per key. |
2634 |
> |
* Some attempted update operations on this map by other threads |
2635 |
> |
* may be blocked while computation is in progress, so the |
2636 |
> |
* computation should be short and simple, and must not attempt to |
2637 |
> |
* update any other mappings of this Map. |
2638 |
|
* |
2639 |
< |
* @param key the key that needs to be removed. |
2640 |
< |
* @return the value to which the key had been mapped in this table, |
2641 |
< |
* or <code>null</code> if the key did not have a mapping. |
2642 |
< |
* @throws NullPointerException if the key is |
2643 |
< |
* <code>null</code>. |
2639 |
> |
* @param key key with which the specified value is to be associated |
2640 |
> |
* @param mappingFunction the function to compute a value |
2641 |
> |
* @return the current (existing or computed) value associated with |
2642 |
> |
* the specified key, or null if the computed value is null |
2643 |
> |
* @throws NullPointerException if the specified key or mappingFunction |
2644 |
> |
* is null |
2645 |
> |
* @throws IllegalStateException if the computation detectably |
2646 |
> |
* attempts a recursive update to this map that would |
2647 |
> |
* otherwise never complete |
2648 |
> |
* @throws RuntimeException or Error if the mappingFunction does so, |
2649 |
> |
* in which case the mapping is left unestablished |
2650 |
|
*/ |
2651 |
< |
public V remove(Object key) { |
2652 |
< |
int hash = hash(key); |
2653 |
< |
return segmentFor(hash).remove(key, hash, null); |
2651 |
> |
public V computeIfAbsent |
2652 |
> |
(K key, Function<? super K, ? extends V> mappingFunction) { |
2653 |
> |
return internalComputeIfAbsent(key, mappingFunction); |
2654 |
|
} |
2655 |
|
|
2656 |
|
/** |
2657 |
< |
* Removes the (key, value) pair from this |
2658 |
< |
* table. This method does nothing if the key is not in the table, |
2659 |
< |
* or if the key is associated with a different value. |
2660 |
< |
* |
2661 |
< |
* @param key the key that needs to be removed. |
2662 |
< |
* @param value the associated value. If the value is null, |
2663 |
< |
* it means "any value". |
2664 |
< |
* @return the value to which the key had been mapped in this table, |
2665 |
< |
* or <code>null</code> if the key did not have a mapping. |
2666 |
< |
* @throws NullPointerException if the key is |
2667 |
< |
* <code>null</code>. |
2657 |
> |
* If the value for the specified key is present and non-null, |
2658 |
> |
* attempts to compute a new mapping given the key and its current |
2659 |
> |
* mapped value. The entire method invocation is performed |
2660 |
> |
* atomically. Some attempted update operations on this map by |
2661 |
> |
* other threads may be blocked while computation is in progress, |
2662 |
> |
* so the computation should be short and simple, and must not |
2663 |
> |
* attempt to update any other mappings of this Map. |
2664 |
> |
* |
2665 |
> |
* @param key key with which the specified value is to be associated |
2666 |
> |
* @param remappingFunction the function to compute a value |
2667 |
> |
* @return the new value associated with the specified key, or null if none |
2668 |
> |
* @throws NullPointerException if the specified key or remappingFunction |
2669 |
> |
* is null |
2670 |
> |
* @throws IllegalStateException if the computation detectably |
2671 |
> |
* attempts a recursive update to this map that would |
2672 |
> |
* otherwise never complete |
2673 |
> |
* @throws RuntimeException or Error if the remappingFunction does so, |
2674 |
> |
* in which case the mapping is unchanged |
2675 |
|
*/ |
2676 |
< |
public boolean remove(Object key, Object value) { |
2677 |
< |
int hash = hash(key); |
2678 |
< |
return segmentFor(hash).remove(key, hash, value) != null; |
2676 |
> |
public V computeIfPresent |
2677 |
> |
(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
2678 |
> |
return internalCompute(key, true, remappingFunction); |
2679 |
|
} |
2680 |
|
|
2681 |
|
/** |
2682 |
< |
* Removes all mappings from this map. |
2682 |
> |
* Attempts to compute a mapping for the specified key and its |
2683 |
> |
* current mapped value (or {@code null} if there is no current |
2684 |
> |
* mapping). The entire method invocation is performed atomically. |
2685 |
> |
* Some attempted update operations on this map by other threads |
2686 |
> |
* may be blocked while computation is in progress, so the |
2687 |
> |
* computation should be short and simple, and must not attempt to |
2688 |
> |
* update any other mappings of this Map. |
2689 |
> |
* |
2690 |
> |
* @param key key with which the specified value is to be associated |
2691 |
> |
* @param remappingFunction the function to compute a value |
2692 |
> |
* @return the new value associated with the specified key, or null if none |
2693 |
> |
* @throws NullPointerException if the specified key or remappingFunction |
2694 |
> |
* is null |
2695 |
> |
* @throws IllegalStateException if the computation detectably |
2696 |
> |
* attempts a recursive update to this map that would |
2697 |
> |
* otherwise never complete |
2698 |
> |
* @throws RuntimeException or Error if the remappingFunction does so, |
2699 |
> |
* in which case the mapping is unchanged |
2700 |
|
*/ |
2701 |
< |
public void clear() { |
2702 |
< |
for (int i = 0; i < segments.length; ++i) |
2703 |
< |
segments[i].clear(); |
2701 |
> |
public V compute |
2702 |
> |
(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) { |
2703 |
> |
return internalCompute(key, false, remappingFunction); |
2704 |
|
} |
2705 |
|
|
2706 |
+ |
/** |
2707 |
+ |
* If the specified key is not already associated with a |
2708 |
+ |
* (non-null) value, associates it with the given value. |
2709 |
+ |
* Otherwise, replaces the value with the results of the given |
2710 |
+ |
* remapping function, or removes if {@code null}. The entire |
2711 |
+ |
* method invocation is performed atomically. Some attempted |
2712 |
+ |
* update operations on this map by other threads may be blocked |
2713 |
+ |
* while computation is in progress, so the computation should be |
2714 |
+ |
* short and simple, and must not attempt to update any other |
2715 |
+ |
* mappings of this Map. |
2716 |
+ |
* |
2717 |
+ |
* @param key key with which the specified value is to be associated |
2718 |
+ |
* @param value the value to use if absent |
2719 |
+ |
* @param remappingFunction the function to recompute a value if present |
2720 |
+ |
* @return the new value associated with the specified key, or null if none |
2721 |
+ |
* @throws NullPointerException if the specified key or the |
2722 |
+ |
* remappingFunction is null |
2723 |
+ |
* @throws RuntimeException or Error if the remappingFunction does so, |
2724 |
+ |
* in which case the mapping is unchanged |
2725 |
+ |
*/ |
2726 |
+ |
public V merge |
2727 |
+ |
(K key, V value, |
2728 |
+ |
BiFunction<? super V, ? super V, ? extends V> remappingFunction) { |
2729 |
+ |
return internalMerge(key, value, remappingFunction); |
2730 |
+ |
} |
2731 |
|
|
2732 |
|
/** |
2733 |
< |
* Returns a shallow copy of this |
2734 |
< |
* <tt>ConcurrentHashMap</tt> instance: the keys and |
750 |
< |
* values themselves are not cloned. |
2733 |
> |
* Removes the key (and its corresponding value) from this map. |
2734 |
> |
* This method does nothing if the key is not in the map. |
2735 |
|
* |
2736 |
< |
* @return a shallow copy of this map. |
2736 |
> |
* @param key the key that needs to be removed |
2737 |
> |
* @return the previous value associated with {@code key}, or |
2738 |
> |
* {@code null} if there was no mapping for {@code key} |
2739 |
> |
* @throws NullPointerException if the specified key is null |
2740 |
|
*/ |
2741 |
< |
public Object clone() { |
2742 |
< |
// We cannot call super.clone, since it would share final |
2743 |
< |
// segments array, and there's no way to reassign finals. |
2741 |
> |
public V remove(Object key) { |
2742 |
> |
return internalReplace(key, null, null); |
2743 |
> |
} |
2744 |
|
|
2745 |
< |
float lf = segments[0].loadFactor; |
2746 |
< |
int segs = segments.length; |
2747 |
< |
int cap = (int)(size() / lf); |
2748 |
< |
if (cap < segs) cap = segs; |
2749 |
< |
ConcurrentHashMap<K,V> t = new ConcurrentHashMap<K,V>(cap, lf, segs); |
2750 |
< |
t.putAll(this); |
2751 |
< |
return t; |
2745 |
> |
/** |
2746 |
> |
* {@inheritDoc} |
2747 |
> |
* |
2748 |
> |
* @throws NullPointerException if the specified key is null |
2749 |
> |
*/ |
2750 |
> |
public boolean remove(Object key, Object value) { |
2751 |
> |
if (key == null) |
2752 |
> |
throw new NullPointerException(); |
2753 |
> |
return value != null && internalReplace(key, null, value) != null; |
2754 |
|
} |
2755 |
|
|
2756 |
|
/** |
2757 |
< |
* Returns a set view of the keys contained in this map. The set is |
2758 |
< |
* backed by the map, so changes to the map are reflected in the set, and |
2759 |
< |
* vice-versa. The set supports element removal, which removes the |
2760 |
< |
* corresponding mapping from this map, via the <tt>Iterator.remove</tt>, |
2761 |
< |
* <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt>, and |
2762 |
< |
* <tt>clear</tt> operations. It does not support the <tt>add</tt> or |
2763 |
< |
* <tt>addAll</tt> operations. |
2764 |
< |
* The returned <tt>iterator</tt> is a "weakly consistent" iterator that |
2765 |
< |
* will never throw {@link java.util.ConcurrentModificationException}, |
2766 |
< |
* and guarantees to traverse elements as they existed upon |
2767 |
< |
* construction of the iterator, and may (but is not guaranteed to) |
2768 |
< |
* reflect any modifications subsequent to construction. |
2757 |
> |
* {@inheritDoc} |
2758 |
> |
* |
2759 |
> |
* @throws NullPointerException if any of the arguments are null |
2760 |
> |
*/ |
2761 |
> |
public boolean replace(K key, V oldValue, V newValue) { |
2762 |
> |
if (key == null || oldValue == null || newValue == null) |
2763 |
> |
throw new NullPointerException(); |
2764 |
> |
return internalReplace(key, newValue, oldValue) != null; |
2765 |
> |
} |
2766 |
> |
|
2767 |
> |
/** |
2768 |
> |
* {@inheritDoc} |
2769 |
|
* |
2770 |
< |
* @return a set view of the keys contained in this map. |
2770 |
> |
* @return the previous value associated with the specified key, |
2771 |
> |
* or {@code null} if there was no mapping for the key |
2772 |
> |
* @throws NullPointerException if the specified key or value is null |
2773 |
|
*/ |
2774 |
< |
public Set<K> keySet() { |
2775 |
< |
Set<K> ks = keySet; |
2776 |
< |
return (ks != null) ? ks : (keySet = new KeySet()); |
2774 |
> |
public V replace(K key, V value) { |
2775 |
> |
if (key == null || value == null) |
2776 |
> |
throw new NullPointerException(); |
2777 |
> |
return internalReplace(key, value, null); |
2778 |
|
} |
2779 |
|
|
2780 |
+ |
/** |
2781 |
+ |
* Removes all of the mappings from this map. |
2782 |
+ |
*/ |
2783 |
+ |
public void clear() { |
2784 |
+ |
internalClear(); |
2785 |
+ |
} |
2786 |
|
|
2787 |
|
/** |
2788 |
< |
* Returns a collection view of the values contained in this map. The |
2789 |
< |
* collection is backed by the map, so changes to the map are reflected in |
2790 |
< |
* the collection, and vice-versa. The collection supports element |
2791 |
< |
* removal, which removes the corresponding mapping from this map, via the |
2792 |
< |
* <tt>Iterator.remove</tt>, <tt>Collection.remove</tt>, |
2793 |
< |
* <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt> operations. |
2794 |
< |
* It does not support the <tt>add</tt> or <tt>addAll</tt> operations. |
2795 |
< |
* The returned <tt>iterator</tt> is a "weakly consistent" iterator that |
2796 |
< |
* will never throw {@link java.util.ConcurrentModificationException}, |
2797 |
< |
* and guarantees to traverse elements as they existed upon |
2798 |
< |
* construction of the iterator, and may (but is not guaranteed to) |
2799 |
< |
* reflect any modifications subsequent to construction. |
2788 |
> |
* Returns a {@link Set} view of the keys contained in this map. |
2789 |
> |
* The set is backed by the map, so changes to the map are |
2790 |
> |
* reflected in the set, and vice-versa. |
2791 |
> |
* |
2792 |
> |
* @return the set view |
2793 |
> |
*/ |
2794 |
> |
public KeySetView<K,V> keySet() { |
2795 |
> |
KeySetView<K,V> ks = keySet; |
2796 |
> |
return (ks != null) ? ks : (keySet = new KeySetView<K,V>(this, null)); |
2797 |
> |
} |
2798 |
> |
|
2799 |
> |
/** |
2800 |
> |
* Returns a {@link Set} view of the keys in this map, using the |
2801 |
> |
* given common mapped value for any additions (i.e., {@link |
2802 |
> |
* Collection#add} and {@link Collection#addAll(Collection)}). |
2803 |
> |
* This is of course only appropriate if it is acceptable to use |
2804 |
> |
* the same value for all additions from this view. |
2805 |
|
* |
2806 |
< |
* @return a collection view of the values contained in this map. |
2806 |
> |
* @param mappedValue the mapped value to use for any additions |
2807 |
> |
* @return the set view |
2808 |
> |
* @throws NullPointerException if the mappedValue is null |
2809 |
|
*/ |
2810 |
< |
public Collection<V> values() { |
2811 |
< |
Collection<V> vs = values; |
2812 |
< |
return (vs != null) ? vs : (values = new Values()); |
2810 |
> |
public KeySetView<K,V> keySet(V mappedValue) { |
2811 |
> |
if (mappedValue == null) |
2812 |
> |
throw new NullPointerException(); |
2813 |
> |
return new KeySetView<K,V>(this, mappedValue); |
2814 |
|
} |
2815 |
|
|
2816 |
+ |
/** |
2817 |
+ |
* Returns a {@link Collection} view of the values contained in this map. |
2818 |
+ |
* The collection is backed by the map, so changes to the map are |
2819 |
+ |
* reflected in the collection, and vice-versa. |
2820 |
+ |
* |
2821 |
+ |
* @return the collection view |
2822 |
+ |
*/ |
2823 |
+ |
public ValuesView<K,V> values() { |
2824 |
+ |
ValuesView<K,V> vs = values; |
2825 |
+ |
return (vs != null) ? vs : (values = new ValuesView<K,V>(this)); |
2826 |
+ |
} |
2827 |
|
|
2828 |
|
/** |
2829 |
< |
* Returns a collection view of the mappings contained in this map. Each |
2830 |
< |
* element in the returned collection is a <tt>Map.Entry</tt>. The |
2831 |
< |
* collection is backed by the map, so changes to the map are reflected in |
2832 |
< |
* the collection, and vice-versa. The collection supports element |
2833 |
< |
* removal, which removes the corresponding mapping from the map, via the |
2834 |
< |
* <tt>Iterator.remove</tt>, <tt>Collection.remove</tt>, |
2835 |
< |
* <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt> operations. |
2836 |
< |
* It does not support the <tt>add</tt> or <tt>addAll</tt> operations. |
2837 |
< |
* The returned <tt>iterator</tt> is a "weakly consistent" iterator that |
2838 |
< |
* will never throw {@link java.util.ConcurrentModificationException}, |
2829 |
> |
* Returns a {@link Set} view of the mappings contained in this map. |
2830 |
> |
* The set is backed by the map, so changes to the map are |
2831 |
> |
* reflected in the set, and vice-versa. The set supports element |
2832 |
> |
* removal, which removes the corresponding mapping from the map, |
2833 |
> |
* via the {@code Iterator.remove}, {@code Set.remove}, |
2834 |
> |
* {@code removeAll}, {@code retainAll}, and {@code clear} |
2835 |
> |
* operations. It does not support the {@code add} or |
2836 |
> |
* {@code addAll} operations. |
2837 |
> |
* |
2838 |
> |
* <p>The view's {@code iterator} is a "weakly consistent" iterator |
2839 |
> |
* that will never throw {@link ConcurrentModificationException}, |
2840 |
|
* and guarantees to traverse elements as they existed upon |
2841 |
|
* construction of the iterator, and may (but is not guaranteed to) |
2842 |
|
* reflect any modifications subsequent to construction. |
2843 |
|
* |
2844 |
< |
* @return a collection view of the mappings contained in this map. |
2844 |
> |
* @return the set view |
2845 |
|
*/ |
2846 |
|
public Set<Map.Entry<K,V>> entrySet() { |
2847 |
< |
Set<Map.Entry<K,V>> es = entrySet; |
2848 |
< |
return (es != null) ? es : (entrySet = new EntrySet()); |
2847 |
> |
EntrySetView<K,V> es = entrySet; |
2848 |
> |
return (es != null) ? es : (entrySet = new EntrySetView<K,V>(this)); |
2849 |
|
} |
2850 |
|
|
833 |
– |
|
2851 |
|
/** |
2852 |
|
* Returns an enumeration of the keys in this table. |
2853 |
|
* |
2854 |
< |
* @return an enumeration of the keys in this table. |
2855 |
< |
* @see Enumeration |
839 |
< |
* @see #elements() |
840 |
< |
* @see #keySet() |
841 |
< |
* @see Map |
2854 |
> |
* @return an enumeration of the keys in this table |
2855 |
> |
* @see #keySet() |
2856 |
|
*/ |
2857 |
|
public Enumeration<K> keys() { |
2858 |
< |
return new KeyIterator(); |
2858 |
> |
return new KeyIterator<K,V>(this); |
2859 |
|
} |
2860 |
|
|
2861 |
|
/** |
2862 |
|
* Returns an enumeration of the values in this table. |
849 |
– |
* Use the Enumeration methods on the returned object to fetch the elements |
850 |
– |
* sequentially. |
2863 |
|
* |
2864 |
< |
* @return an enumeration of the values in this table. |
2865 |
< |
* @see java.util.Enumeration |
854 |
< |
* @see #keys() |
855 |
< |
* @see #values() |
856 |
< |
* @see Map |
2864 |
> |
* @return an enumeration of the values in this table |
2865 |
> |
* @see #values() |
2866 |
|
*/ |
2867 |
|
public Enumeration<V> elements() { |
2868 |
< |
return new ValueIterator(); |
2868 |
> |
return new ValueIterator<K,V>(this); |
2869 |
|
} |
2870 |
|
|
2871 |
< |
/* ---------------- Iterator Support -------------- */ |
2871 |
> |
/** |
2872 |
> |
* Returns the hash code value for this {@link Map}, i.e., |
2873 |
> |
* the sum of, for each key-value pair in the map, |
2874 |
> |
* {@code key.hashCode() ^ value.hashCode()}. |
2875 |
> |
* |
2876 |
> |
* @return the hash code value for this map |
2877 |
> |
*/ |
2878 |
> |
public int hashCode() { |
2879 |
> |
int h = 0; |
2880 |
> |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
2881 |
> |
V v; |
2882 |
> |
while ((v = it.advanceValue()) != null) { |
2883 |
> |
h += it.nextKey.hashCode() ^ v.hashCode(); |
2884 |
> |
} |
2885 |
> |
return h; |
2886 |
> |
} |
2887 |
|
|
2888 |
< |
private abstract class HashIterator { |
2889 |
< |
private int nextSegmentIndex; |
2890 |
< |
private int nextTableIndex; |
2891 |
< |
private HashEntry[] currentTable; |
2892 |
< |
private HashEntry<K, V> nextEntry; |
2893 |
< |
private HashEntry<K, V> lastReturned; |
2888 |
> |
/** |
2889 |
> |
* Returns a string representation of this map. The string |
2890 |
> |
* representation consists of a list of key-value mappings (in no |
2891 |
> |
* particular order) enclosed in braces ("{@code {}}"). Adjacent |
2892 |
> |
* mappings are separated by the characters {@code ", "} (comma |
2893 |
> |
* and space). Each key-value mapping is rendered as the key |
2894 |
> |
* followed by an equals sign ("{@code =}") followed by the |
2895 |
> |
* associated value. |
2896 |
> |
* |
2897 |
> |
* @return a string representation of this map |
2898 |
> |
*/ |
2899 |
> |
public String toString() { |
2900 |
> |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
2901 |
> |
StringBuilder sb = new StringBuilder(); |
2902 |
> |
sb.append('{'); |
2903 |
> |
V v; |
2904 |
> |
if ((v = it.advanceValue()) != null) { |
2905 |
> |
for (;;) { |
2906 |
> |
K k = it.nextKey; |
2907 |
> |
sb.append(k == this ? "(this Map)" : k); |
2908 |
> |
sb.append('='); |
2909 |
> |
sb.append(v == this ? "(this Map)" : v); |
2910 |
> |
if ((v = it.advanceValue()) == null) |
2911 |
> |
break; |
2912 |
> |
sb.append(',').append(' '); |
2913 |
> |
} |
2914 |
> |
} |
2915 |
> |
return sb.append('}').toString(); |
2916 |
> |
} |
2917 |
|
|
2918 |
< |
private HashIterator() { |
2919 |
< |
nextSegmentIndex = segments.length - 1; |
2920 |
< |
nextTableIndex = -1; |
2921 |
< |
advance(); |
2918 |
> |
/** |
2919 |
> |
* Compares the specified object with this map for equality. |
2920 |
> |
* Returns {@code true} if the given object is a map with the same |
2921 |
> |
* mappings as this map. This operation may return misleading |
2922 |
> |
* results if either map is concurrently modified during execution |
2923 |
> |
* of this method. |
2924 |
> |
* |
2925 |
> |
* @param o object to be compared for equality with this map |
2926 |
> |
* @return {@code true} if the specified object is equal to this map |
2927 |
> |
*/ |
2928 |
> |
public boolean equals(Object o) { |
2929 |
> |
if (o != this) { |
2930 |
> |
if (!(o instanceof Map)) |
2931 |
> |
return false; |
2932 |
> |
Map<?,?> m = (Map<?,?>) o; |
2933 |
> |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
2934 |
> |
V val; |
2935 |
> |
while ((val = it.advanceValue()) != null) { |
2936 |
> |
Object v = m.get(it.nextKey); |
2937 |
> |
if (v == null || (v != val && !v.equals(val))) |
2938 |
> |
return false; |
2939 |
> |
} |
2940 |
> |
for (Map.Entry<?,?> e : m.entrySet()) { |
2941 |
> |
Object mk, mv, v; |
2942 |
> |
if ((mk = e.getKey()) == null || |
2943 |
> |
(mv = e.getValue()) == null || |
2944 |
> |
(v = internalGet(mk)) == null || |
2945 |
> |
(mv != v && !mv.equals(v))) |
2946 |
> |
return false; |
2947 |
> |
} |
2948 |
|
} |
2949 |
+ |
return true; |
2950 |
+ |
} |
2951 |
|
|
2952 |
< |
public boolean hasMoreElements() { return hasNext(); } |
2952 |
> |
/* ----------------Iterators -------------- */ |
2953 |
|
|
2954 |
< |
private void advance() { |
2955 |
< |
if (nextEntry != null && (nextEntry = nextEntry.next) != null) |
2956 |
< |
return; |
2954 |
> |
@SuppressWarnings("serial") static final class KeyIterator<K,V> |
2955 |
> |
extends Traverser<K,V,Object> |
2956 |
> |
implements Spliterator<K>, Iterator<K>, Enumeration<K> { |
2957 |
> |
KeyIterator(ConcurrentHashMap<K,V> map) { super(map); } |
2958 |
> |
KeyIterator(ConcurrentHashMap<K,V> map, Traverser<K,V,Object> it) { |
2959 |
> |
super(map, it); |
2960 |
> |
} |
2961 |
> |
public KeyIterator<K,V> trySplit() { |
2962 |
> |
return (baseIndex == baseLimit) ? null : |
2963 |
> |
new KeyIterator<K,V>(map, this); |
2964 |
> |
} |
2965 |
> |
public final K next() { |
2966 |
> |
K k; |
2967 |
> |
if ((k = (nextVal == null) ? advanceKey() : nextKey) == null) |
2968 |
> |
throw new NoSuchElementException(); |
2969 |
> |
nextVal = null; |
2970 |
> |
return k; |
2971 |
> |
} |
2972 |
|
|
2973 |
< |
while (nextTableIndex >= 0) { |
2974 |
< |
if ( (nextEntry = (HashEntry<K,V>)currentTable[nextTableIndex--]) != null) |
2975 |
< |
return; |
2976 |
< |
} |
2973 |
> |
public final K nextElement() { return next(); } |
2974 |
> |
|
2975 |
> |
public Iterator<K> iterator() { return this; } |
2976 |
> |
|
2977 |
> |
public void forEach(Consumer<? super K> action) { |
2978 |
> |
if (action == null) throw new NullPointerException(); |
2979 |
> |
K k; |
2980 |
> |
while ((k = advanceKey()) != null) |
2981 |
> |
action.accept(k); |
2982 |
> |
} |
2983 |
> |
|
2984 |
> |
public boolean tryAdvance(Consumer<? super K> block) { |
2985 |
> |
if (block == null) throw new NullPointerException(); |
2986 |
> |
K k; |
2987 |
> |
if ((k = advanceKey()) == null) |
2988 |
> |
return false; |
2989 |
> |
block.accept(k); |
2990 |
> |
return true; |
2991 |
> |
} |
2992 |
> |
|
2993 |
> |
public int characteristics() { |
2994 |
> |
return Spliterator.DISTINCT | Spliterator.CONCURRENT | |
2995 |
> |
Spliterator.NONNULL; |
2996 |
> |
} |
2997 |
|
|
2998 |
< |
while (nextSegmentIndex >= 0) { |
2999 |
< |
Segment<K,V> seg = (Segment<K,V>)segments[nextSegmentIndex--]; |
3000 |
< |
if (seg.count != 0) { |
3001 |
< |
currentTable = seg.table; |
3002 |
< |
for (int j = currentTable.length - 1; j >= 0; --j) { |
3003 |
< |
if ( (nextEntry = (HashEntry<K,V>)currentTable[j]) != null) { |
3004 |
< |
nextTableIndex = j - 1; |
3005 |
< |
return; |
2998 |
> |
} |
2999 |
> |
|
3000 |
> |
@SuppressWarnings("serial") static final class ValueIterator<K,V> |
3001 |
> |
extends Traverser<K,V,Object> |
3002 |
> |
implements Spliterator<V>, Iterator<V>, Enumeration<V> { |
3003 |
> |
ValueIterator(ConcurrentHashMap<K,V> map) { super(map); } |
3004 |
> |
ValueIterator(ConcurrentHashMap<K,V> map, Traverser<K,V,Object> it) { |
3005 |
> |
super(map, it); |
3006 |
> |
} |
3007 |
> |
public ValueIterator<K,V> trySplit() { |
3008 |
> |
return (baseIndex == baseLimit) ? null : |
3009 |
> |
new ValueIterator<K,V>(map, this); |
3010 |
> |
} |
3011 |
> |
|
3012 |
> |
public final V next() { |
3013 |
> |
V v; |
3014 |
> |
if ((v = nextVal) == null && (v = advanceValue()) == null) |
3015 |
> |
throw new NoSuchElementException(); |
3016 |
> |
nextVal = null; |
3017 |
> |
return v; |
3018 |
> |
} |
3019 |
> |
|
3020 |
> |
public final V nextElement() { return next(); } |
3021 |
> |
|
3022 |
> |
public Iterator<V> iterator() { return this; } |
3023 |
> |
|
3024 |
> |
public void forEach(Consumer<? super V> action) { |
3025 |
> |
if (action == null) throw new NullPointerException(); |
3026 |
> |
V v; |
3027 |
> |
while ((v = advanceValue()) != null) |
3028 |
> |
action.accept(v); |
3029 |
> |
} |
3030 |
> |
|
3031 |
> |
public boolean tryAdvance(Consumer<? super V> block) { |
3032 |
> |
V v; |
3033 |
> |
if (block == null) throw new NullPointerException(); |
3034 |
> |
if ((v = advanceValue()) == null) |
3035 |
> |
return false; |
3036 |
> |
block.accept(v); |
3037 |
> |
return true; |
3038 |
> |
} |
3039 |
> |
|
3040 |
> |
public int characteristics() { |
3041 |
> |
return Spliterator.CONCURRENT | Spliterator.NONNULL; |
3042 |
> |
} |
3043 |
> |
} |
3044 |
> |
|
3045 |
> |
@SuppressWarnings("serial") static final class EntryIterator<K,V> |
3046 |
> |
extends Traverser<K,V,Object> |
3047 |
> |
implements Spliterator<Map.Entry<K,V>>, Iterator<Map.Entry<K,V>> { |
3048 |
> |
EntryIterator(ConcurrentHashMap<K,V> map) { super(map); } |
3049 |
> |
EntryIterator(ConcurrentHashMap<K,V> map, Traverser<K,V,Object> it) { |
3050 |
> |
super(map, it); |
3051 |
> |
} |
3052 |
> |
public EntryIterator<K,V> trySplit() { |
3053 |
> |
return (baseIndex == baseLimit) ? null : |
3054 |
> |
new EntryIterator<K,V>(map, this); |
3055 |
> |
} |
3056 |
> |
|
3057 |
> |
public final Map.Entry<K,V> next() { |
3058 |
> |
V v; |
3059 |
> |
if ((v = nextVal) == null && (v = advanceValue()) == null) |
3060 |
> |
throw new NoSuchElementException(); |
3061 |
> |
K k = nextKey; |
3062 |
> |
nextVal = null; |
3063 |
> |
return new MapEntry<K,V>(k, v, map); |
3064 |
> |
} |
3065 |
> |
|
3066 |
> |
public Iterator<Map.Entry<K,V>> iterator() { return this; } |
3067 |
> |
|
3068 |
> |
public void forEach(Consumer<? super Map.Entry<K,V>> action) { |
3069 |
> |
if (action == null) throw new NullPointerException(); |
3070 |
> |
V v; |
3071 |
> |
while ((v = advanceValue()) != null) |
3072 |
> |
action.accept(entryFor(nextKey, v)); |
3073 |
> |
} |
3074 |
> |
|
3075 |
> |
public boolean tryAdvance(Consumer<? super Map.Entry<K,V>> block) { |
3076 |
> |
V v; |
3077 |
> |
if (block == null) throw new NullPointerException(); |
3078 |
> |
if ((v = advanceValue()) == null) |
3079 |
> |
return false; |
3080 |
> |
block.accept(entryFor(nextKey, v)); |
3081 |
> |
return true; |
3082 |
> |
} |
3083 |
> |
|
3084 |
> |
public int characteristics() { |
3085 |
> |
return Spliterator.DISTINCT | Spliterator.CONCURRENT | |
3086 |
> |
Spliterator.NONNULL; |
3087 |
> |
} |
3088 |
> |
} |
3089 |
> |
|
3090 |
> |
/** |
3091 |
> |
* Exported Entry for iterators |
3092 |
> |
*/ |
3093 |
> |
static final class MapEntry<K,V> implements Map.Entry<K,V> { |
3094 |
> |
final K key; // non-null |
3095 |
> |
V val; // non-null |
3096 |
> |
final ConcurrentHashMap<K,V> map; |
3097 |
> |
MapEntry(K key, V val, ConcurrentHashMap<K,V> map) { |
3098 |
> |
this.key = key; |
3099 |
> |
this.val = val; |
3100 |
> |
this.map = map; |
3101 |
> |
} |
3102 |
> |
public final K getKey() { return key; } |
3103 |
> |
public final V getValue() { return val; } |
3104 |
> |
public final int hashCode() { return key.hashCode() ^ val.hashCode(); } |
3105 |
> |
public final String toString(){ return key + "=" + val; } |
3106 |
> |
|
3107 |
> |
public final boolean equals(Object o) { |
3108 |
> |
Object k, v; Map.Entry<?,?> e; |
3109 |
> |
return ((o instanceof Map.Entry) && |
3110 |
> |
(k = (e = (Map.Entry<?,?>)o).getKey()) != null && |
3111 |
> |
(v = e.getValue()) != null && |
3112 |
> |
(k == key || k.equals(key)) && |
3113 |
> |
(v == val || v.equals(val))); |
3114 |
> |
} |
3115 |
> |
|
3116 |
> |
/** |
3117 |
> |
* Sets our entry's value and writes through to the map. The |
3118 |
> |
* value to return is somewhat arbitrary here. Since we do not |
3119 |
> |
* necessarily track asynchronous changes, the most recent |
3120 |
> |
* "previous" value could be different from what we return (or |
3121 |
> |
* could even have been removed in which case the put will |
3122 |
> |
* re-establish). We do not and cannot guarantee more. |
3123 |
> |
*/ |
3124 |
> |
public final V setValue(V value) { |
3125 |
> |
if (value == null) throw new NullPointerException(); |
3126 |
> |
V v = val; |
3127 |
> |
val = value; |
3128 |
> |
map.put(key, value); |
3129 |
> |
return v; |
3130 |
> |
} |
3131 |
> |
} |
3132 |
> |
|
3133 |
> |
/** |
3134 |
> |
* Returns exportable snapshot entry for the given key and value |
3135 |
> |
* when write-through can't or shouldn't be used. |
3136 |
> |
*/ |
3137 |
> |
static <K,V> AbstractMap.SimpleEntry<K,V> entryFor(K k, V v) { |
3138 |
> |
return new AbstractMap.SimpleEntry<K,V>(k, v); |
3139 |
> |
} |
3140 |
> |
|
3141 |
> |
/* ---------------- Serialization Support -------------- */ |
3142 |
> |
|
3143 |
> |
/** |
3144 |
> |
* Stripped-down version of helper class used in previous version, |
3145 |
> |
* declared for the sake of serialization compatibility |
3146 |
> |
*/ |
3147 |
> |
static class Segment<K,V> implements Serializable { |
3148 |
> |
private static final long serialVersionUID = 2249069246763182397L; |
3149 |
> |
final float loadFactor; |
3150 |
> |
Segment(float lf) { this.loadFactor = lf; } |
3151 |
> |
} |
3152 |
> |
|
3153 |
> |
/** |
3154 |
> |
* Saves the state of the {@code ConcurrentHashMap} instance to a |
3155 |
> |
* stream (i.e., serializes it). |
3156 |
> |
* @param s the stream |
3157 |
> |
* @serialData |
3158 |
> |
* the key (Object) and value (Object) |
3159 |
> |
* for each key-value mapping, followed by a null pair. |
3160 |
> |
* The key-value mappings are emitted in no particular order. |
3161 |
> |
*/ |
3162 |
> |
@SuppressWarnings("unchecked") private void writeObject |
3163 |
> |
(java.io.ObjectOutputStream s) |
3164 |
> |
throws java.io.IOException { |
3165 |
> |
if (segments == null) { // for serialization compatibility |
3166 |
> |
segments = (Segment<K,V>[]) |
3167 |
> |
new Segment<?,?>[DEFAULT_CONCURRENCY_LEVEL]; |
3168 |
> |
for (int i = 0; i < segments.length; ++i) |
3169 |
> |
segments[i] = new Segment<K,V>(LOAD_FACTOR); |
3170 |
> |
} |
3171 |
> |
s.defaultWriteObject(); |
3172 |
> |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
3173 |
> |
V v; |
3174 |
> |
while ((v = it.advanceValue()) != null) { |
3175 |
> |
s.writeObject(it.nextKey); |
3176 |
> |
s.writeObject(v); |
3177 |
> |
} |
3178 |
> |
s.writeObject(null); |
3179 |
> |
s.writeObject(null); |
3180 |
> |
segments = null; // throw away |
3181 |
> |
} |
3182 |
> |
|
3183 |
> |
/** |
3184 |
> |
* Reconstitutes the instance from a stream (that is, deserializes it). |
3185 |
> |
* @param s the stream |
3186 |
> |
*/ |
3187 |
> |
@SuppressWarnings("unchecked") private void readObject |
3188 |
> |
(java.io.ObjectInputStream s) |
3189 |
> |
throws java.io.IOException, ClassNotFoundException { |
3190 |
> |
s.defaultReadObject(); |
3191 |
> |
this.segments = null; // unneeded |
3192 |
> |
|
3193 |
> |
// Create all nodes, then place in table once size is known |
3194 |
> |
long size = 0L; |
3195 |
> |
Node<V> p = null; |
3196 |
> |
for (;;) { |
3197 |
> |
K k = (K) s.readObject(); |
3198 |
> |
V v = (V) s.readObject(); |
3199 |
> |
if (k != null && v != null) { |
3200 |
> |
int h = spread(k.hashCode()); |
3201 |
> |
p = new Node<V>(h, k, v, p); |
3202 |
> |
++size; |
3203 |
> |
} |
3204 |
> |
else |
3205 |
> |
break; |
3206 |
> |
} |
3207 |
> |
if (p != null) { |
3208 |
> |
boolean init = false; |
3209 |
> |
int n; |
3210 |
> |
if (size >= (long)(MAXIMUM_CAPACITY >>> 1)) |
3211 |
> |
n = MAXIMUM_CAPACITY; |
3212 |
> |
else { |
3213 |
> |
int sz = (int)size; |
3214 |
> |
n = tableSizeFor(sz + (sz >>> 1) + 1); |
3215 |
> |
} |
3216 |
> |
int sc = sizeCtl; |
3217 |
> |
boolean collide = false; |
3218 |
> |
if (n > sc && |
3219 |
> |
U.compareAndSwapInt(this, SIZECTL, sc, -1)) { |
3220 |
> |
try { |
3221 |
> |
if (table == null) { |
3222 |
> |
init = true; |
3223 |
> |
@SuppressWarnings("rawtypes") Node[] rt = new Node[n]; |
3224 |
> |
Node<V>[] tab = (Node<V>[])rt; |
3225 |
> |
int mask = n - 1; |
3226 |
> |
while (p != null) { |
3227 |
> |
int j = p.hash & mask; |
3228 |
> |
Node<V> next = p.next; |
3229 |
> |
Node<V> q = p.next = tabAt(tab, j); |
3230 |
> |
setTabAt(tab, j, p); |
3231 |
> |
if (!collide && q != null && q.hash == p.hash) |
3232 |
> |
collide = true; |
3233 |
> |
p = next; |
3234 |
> |
} |
3235 |
> |
table = tab; |
3236 |
> |
addCount(size, -1); |
3237 |
> |
sc = n - (n >>> 2); |
3238 |
> |
} |
3239 |
> |
} finally { |
3240 |
> |
sizeCtl = sc; |
3241 |
> |
} |
3242 |
> |
if (collide) { // rescan and convert to TreeBins |
3243 |
> |
Node<V>[] tab = table; |
3244 |
> |
for (int i = 0; i < tab.length; ++i) { |
3245 |
> |
int c = 0; |
3246 |
> |
for (Node<V> e = tabAt(tab, i); e != null; e = e.next) { |
3247 |
> |
if (++c > TREE_THRESHOLD && |
3248 |
> |
(e.key instanceof Comparable)) { |
3249 |
> |
replaceWithTreeBin(tab, i, e.key); |
3250 |
> |
break; |
3251 |
> |
} |
3252 |
|
} |
3253 |
|
} |
3254 |
|
} |
3255 |
|
} |
3256 |
+ |
if (!init) { // Can only happen if unsafely published. |
3257 |
+ |
while (p != null) { |
3258 |
+ |
internalPut((K)p.key, p.val, false); |
3259 |
+ |
p = p.next; |
3260 |
+ |
} |
3261 |
+ |
} |
3262 |
|
} |
3263 |
+ |
} |
3264 |
|
|
3265 |
< |
public boolean hasNext() { return nextEntry != null; } |
3265 |
> |
// ------------------------------------------------------- |
3266 |
|
|
3267 |
< |
HashEntry<K,V> nextEntry() { |
3268 |
< |
if (nextEntry == null) |
3269 |
< |
throw new NoSuchElementException(); |
3270 |
< |
lastReturned = nextEntry; |
3271 |
< |
advance(); |
3272 |
< |
return lastReturned; |
3267 |
> |
// Sequential bulk operations |
3268 |
> |
|
3269 |
> |
/** |
3270 |
> |
* Performs the given action for each (key, value). |
3271 |
> |
* |
3272 |
> |
* @param action the action |
3273 |
> |
*/ |
3274 |
> |
public void forEachSequentially |
3275 |
> |
(BiConsumer<? super K, ? super V> action) { |
3276 |
> |
if (action == null) throw new NullPointerException(); |
3277 |
> |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
3278 |
> |
V v; |
3279 |
> |
while ((v = it.advanceValue()) != null) |
3280 |
> |
action.accept(it.nextKey, v); |
3281 |
> |
} |
3282 |
> |
|
3283 |
> |
/** |
3284 |
> |
* Performs the given action for each non-null transformation |
3285 |
> |
* of each (key, value). |
3286 |
> |
* |
3287 |
> |
* @param transformer a function returning the transformation |
3288 |
> |
* for an element, or null if there is no transformation (in |
3289 |
> |
* which case the action is not applied) |
3290 |
> |
* @param action the action |
3291 |
> |
*/ |
3292 |
> |
public <U> void forEachSequentially |
3293 |
> |
(BiFunction<? super K, ? super V, ? extends U> transformer, |
3294 |
> |
Consumer<? super U> action) { |
3295 |
> |
if (transformer == null || action == null) |
3296 |
> |
throw new NullPointerException(); |
3297 |
> |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
3298 |
> |
V v; U u; |
3299 |
> |
while ((v = it.advanceValue()) != null) { |
3300 |
> |
if ((u = transformer.apply(it.nextKey, v)) != null) |
3301 |
> |
action.accept(u); |
3302 |
|
} |
3303 |
+ |
} |
3304 |
|
|
3305 |
< |
public void remove() { |
3306 |
< |
if (lastReturned == null) |
3307 |
< |
throw new IllegalStateException(); |
3308 |
< |
ConcurrentHashMap.this.remove(lastReturned.key); |
3309 |
< |
lastReturned = null; |
3305 |
> |
/** |
3306 |
> |
* Returns a non-null result from applying the given search |
3307 |
> |
* function on each (key, value), or null if none. |
3308 |
> |
* |
3309 |
> |
* @param searchFunction a function returning a non-null |
3310 |
> |
* result on success, else null |
3311 |
> |
* @return a non-null result from applying the given search |
3312 |
> |
* function on each (key, value), or null if none |
3313 |
> |
*/ |
3314 |
> |
public <U> U searchSequentially |
3315 |
> |
(BiFunction<? super K, ? super V, ? extends U> searchFunction) { |
3316 |
> |
if (searchFunction == null) throw new NullPointerException(); |
3317 |
> |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
3318 |
> |
V v; U u; |
3319 |
> |
while ((v = it.advanceValue()) != null) { |
3320 |
> |
if ((u = searchFunction.apply(it.nextKey, v)) != null) |
3321 |
> |
return u; |
3322 |
> |
} |
3323 |
> |
return null; |
3324 |
> |
} |
3325 |
> |
|
3326 |
> |
/** |
3327 |
> |
* Returns the result of accumulating the given transformation |
3328 |
> |
* of all (key, value) pairs using the given reducer to |
3329 |
> |
* combine values, or null if none. |
3330 |
> |
* |
3331 |
> |
* @param transformer a function returning the transformation |
3332 |
> |
* for an element, or null if there is no transformation (in |
3333 |
> |
* which case it is not combined) |
3334 |
> |
* @param reducer a commutative associative combining function |
3335 |
> |
* @return the result of accumulating the given transformation |
3336 |
> |
* of all (key, value) pairs |
3337 |
> |
*/ |
3338 |
> |
public <U> U reduceSequentially |
3339 |
> |
(BiFunction<? super K, ? super V, ? extends U> transformer, |
3340 |
> |
BiFunction<? super U, ? super U, ? extends U> reducer) { |
3341 |
> |
if (transformer == null || reducer == null) |
3342 |
> |
throw new NullPointerException(); |
3343 |
> |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
3344 |
> |
U r = null, u; V v; |
3345 |
> |
while ((v = it.advanceValue()) != null) { |
3346 |
> |
if ((u = transformer.apply(it.nextKey, v)) != null) |
3347 |
> |
r = (r == null) ? u : reducer.apply(r, u); |
3348 |
|
} |
3349 |
+ |
return r; |
3350 |
+ |
} |
3351 |
+ |
|
3352 |
+ |
/** |
3353 |
+ |
* Returns the result of accumulating the given transformation |
3354 |
+ |
* of all (key, value) pairs using the given reducer to |
3355 |
+ |
* combine values, and the given basis as an identity value. |
3356 |
+ |
* |
3357 |
+ |
* @param transformer a function returning the transformation |
3358 |
+ |
* for an element |
3359 |
+ |
* @param basis the identity (initial default value) for the reduction |
3360 |
+ |
* @param reducer a commutative associative combining function |
3361 |
+ |
* @return the result of accumulating the given transformation |
3362 |
+ |
* of all (key, value) pairs |
3363 |
+ |
*/ |
3364 |
+ |
public double reduceToDoubleSequentially |
3365 |
+ |
(ToDoubleBiFunction<? super K, ? super V> transformer, |
3366 |
+ |
double basis, |
3367 |
+ |
DoubleBinaryOperator reducer) { |
3368 |
+ |
if (transformer == null || reducer == null) |
3369 |
+ |
throw new NullPointerException(); |
3370 |
+ |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
3371 |
+ |
double r = basis; V v; |
3372 |
+ |
while ((v = it.advanceValue()) != null) |
3373 |
+ |
r = reducer.applyAsDouble(r, transformer.applyAsDouble(it.nextKey, v)); |
3374 |
+ |
return r; |
3375 |
|
} |
3376 |
|
|
3377 |
< |
private class KeyIterator extends HashIterator implements Iterator<K>, Enumeration<K> { |
3378 |
< |
public K next() { return super.nextEntry().key; } |
3379 |
< |
public K nextElement() { return super.nextEntry().key; } |
3377 |
> |
/** |
3378 |
> |
* Returns the result of accumulating the given transformation |
3379 |
> |
* of all (key, value) pairs using the given reducer to |
3380 |
> |
* combine values, and the given basis as an identity value. |
3381 |
> |
* |
3382 |
> |
* @param transformer a function returning the transformation |
3383 |
> |
* for an element |
3384 |
> |
* @param basis the identity (initial default value) for the reduction |
3385 |
> |
* @param reducer a commutative associative combining function |
3386 |
> |
* @return the result of accumulating the given transformation |
3387 |
> |
* of all (key, value) pairs |
3388 |
> |
*/ |
3389 |
> |
public long reduceToLongSequentially |
3390 |
> |
(ToLongBiFunction<? super K, ? super V> transformer, |
3391 |
> |
long basis, |
3392 |
> |
LongBinaryOperator reducer) { |
3393 |
> |
if (transformer == null || reducer == null) |
3394 |
> |
throw new NullPointerException(); |
3395 |
> |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
3396 |
> |
long r = basis; V v; |
3397 |
> |
while ((v = it.advanceValue()) != null) |
3398 |
> |
r = reducer.applyAsLong(r, transformer.applyAsLong(it.nextKey, v)); |
3399 |
> |
return r; |
3400 |
|
} |
3401 |
|
|
3402 |
< |
private class ValueIterator extends HashIterator implements Iterator<V>, Enumeration<V> { |
3403 |
< |
public V next() { return super.nextEntry().value; } |
3404 |
< |
public V nextElement() { return super.nextEntry().value; } |
3402 |
> |
/** |
3403 |
> |
* Returns the result of accumulating the given transformation |
3404 |
> |
* of all (key, value) pairs using the given reducer to |
3405 |
> |
* combine values, and the given basis as an identity value. |
3406 |
> |
* |
3407 |
> |
* @param transformer a function returning the transformation |
3408 |
> |
* for an element |
3409 |
> |
* @param basis the identity (initial default value) for the reduction |
3410 |
> |
* @param reducer a commutative associative combining function |
3411 |
> |
* @return the result of accumulating the given transformation |
3412 |
> |
* of all (key, value) pairs |
3413 |
> |
*/ |
3414 |
> |
public int reduceToIntSequentially |
3415 |
> |
(ToIntBiFunction<? super K, ? super V> transformer, |
3416 |
> |
int basis, |
3417 |
> |
IntBinaryOperator reducer) { |
3418 |
> |
if (transformer == null || reducer == null) |
3419 |
> |
throw new NullPointerException(); |
3420 |
> |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
3421 |
> |
int r = basis; V v; |
3422 |
> |
while ((v = it.advanceValue()) != null) |
3423 |
> |
r = reducer.applyAsInt(r, transformer.applyAsInt(it.nextKey, v)); |
3424 |
> |
return r; |
3425 |
|
} |
3426 |
|
|
3427 |
< |
private class EntryIterator extends HashIterator implements Iterator<Entry<K,V>> { |
3428 |
< |
public Map.Entry<K,V> next() { return super.nextEntry(); } |
3427 |
> |
/** |
3428 |
> |
* Performs the given action for each key. |
3429 |
> |
* |
3430 |
> |
* @param action the action |
3431 |
> |
*/ |
3432 |
> |
public void forEachKeySequentially |
3433 |
> |
(Consumer<? super K> action) { |
3434 |
> |
if (action == null) throw new NullPointerException(); |
3435 |
> |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
3436 |
> |
K k; |
3437 |
> |
while ((k = it.advanceKey()) != null) |
3438 |
> |
action.accept(k); |
3439 |
|
} |
3440 |
|
|
3441 |
< |
private class KeySet extends AbstractSet<K> { |
3442 |
< |
public Iterator<K> iterator() { |
3443 |
< |
return new KeyIterator(); |
3441 |
> |
/** |
3442 |
> |
* Performs the given action for each non-null transformation |
3443 |
> |
* of each key. |
3444 |
> |
* |
3445 |
> |
* @param transformer a function returning the transformation |
3446 |
> |
* for an element, or null if there is no transformation (in |
3447 |
> |
* which case the action is not applied) |
3448 |
> |
* @param action the action |
3449 |
> |
*/ |
3450 |
> |
public <U> void forEachKeySequentially |
3451 |
> |
(Function<? super K, ? extends U> transformer, |
3452 |
> |
Consumer<? super U> action) { |
3453 |
> |
if (transformer == null || action == null) |
3454 |
> |
throw new NullPointerException(); |
3455 |
> |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
3456 |
> |
K k; U u; |
3457 |
> |
while ((k = it.advanceKey()) != null) { |
3458 |
> |
if ((u = transformer.apply(k)) != null) |
3459 |
> |
action.accept(u); |
3460 |
|
} |
3461 |
< |
public int size() { |
3462 |
< |
return ConcurrentHashMap.this.size(); |
3461 |
> |
ForkJoinTasks.forEachKey |
3462 |
> |
(this, transformer, action).invoke(); |
3463 |
> |
} |
3464 |
> |
|
3465 |
> |
/** |
3466 |
> |
* Returns a non-null result from applying the given search |
3467 |
> |
* function on each key, or null if none. |
3468 |
> |
* |
3469 |
> |
* @param searchFunction a function returning a non-null |
3470 |
> |
* result on success, else null |
3471 |
> |
* @return a non-null result from applying the given search |
3472 |
> |
* function on each key, or null if none |
3473 |
> |
*/ |
3474 |
> |
public <U> U searchKeysSequentially |
3475 |
> |
(Function<? super K, ? extends U> searchFunction) { |
3476 |
> |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
3477 |
> |
K k; U u; |
3478 |
> |
while ((k = it.advanceKey()) != null) { |
3479 |
> |
if ((u = searchFunction.apply(k)) != null) |
3480 |
> |
return u; |
3481 |
|
} |
3482 |
< |
public boolean contains(Object o) { |
3483 |
< |
return ConcurrentHashMap.this.containsKey(o); |
3482 |
> |
return null; |
3483 |
> |
} |
3484 |
> |
|
3485 |
> |
/** |
3486 |
> |
* Returns the result of accumulating all keys using the given |
3487 |
> |
* reducer to combine values, or null if none. |
3488 |
> |
* |
3489 |
> |
* @param reducer a commutative associative combining function |
3490 |
> |
* @return the result of accumulating all keys using the given |
3491 |
> |
* reducer to combine values, or null if none |
3492 |
> |
*/ |
3493 |
> |
public K reduceKeysSequentially |
3494 |
> |
(BiFunction<? super K, ? super K, ? extends K> reducer) { |
3495 |
> |
if (reducer == null) throw new NullPointerException(); |
3496 |
> |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
3497 |
> |
K u, r = null; |
3498 |
> |
while ((u = it.advanceKey()) != null) { |
3499 |
> |
r = (r == null) ? u : reducer.apply(r, u); |
3500 |
|
} |
3501 |
< |
public boolean remove(Object o) { |
3502 |
< |
return ConcurrentHashMap.this.remove(o) != null; |
3501 |
> |
return r; |
3502 |
> |
} |
3503 |
> |
|
3504 |
> |
/** |
3505 |
> |
* Returns the result of accumulating the given transformation |
3506 |
> |
* of all keys using the given reducer to combine values, or |
3507 |
> |
* null if none. |
3508 |
> |
* |
3509 |
> |
* @param transformer a function returning the transformation |
3510 |
> |
* for an element, or null if there is no transformation (in |
3511 |
> |
* which case it is not combined) |
3512 |
> |
* @param reducer a commutative associative combining function |
3513 |
> |
* @return the result of accumulating the given transformation |
3514 |
> |
* of all keys |
3515 |
> |
*/ |
3516 |
> |
public <U> U reduceKeysSequentially |
3517 |
> |
(Function<? super K, ? extends U> transformer, |
3518 |
> |
BiFunction<? super U, ? super U, ? extends U> reducer) { |
3519 |
> |
if (transformer == null || reducer == null) |
3520 |
> |
throw new NullPointerException(); |
3521 |
> |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
3522 |
> |
K k; U r = null, u; |
3523 |
> |
while ((k = it.advanceKey()) != null) { |
3524 |
> |
if ((u = transformer.apply(k)) != null) |
3525 |
> |
r = (r == null) ? u : reducer.apply(r, u); |
3526 |
|
} |
3527 |
< |
public void clear() { |
3528 |
< |
ConcurrentHashMap.this.clear(); |
3527 |
> |
return r; |
3528 |
> |
} |
3529 |
> |
|
3530 |
> |
/** |
3531 |
> |
* Returns the result of accumulating the given transformation |
3532 |
> |
* of all keys using the given reducer to combine values, and |
3533 |
> |
* the given basis as an identity value. |
3534 |
> |
* |
3535 |
> |
* @param transformer a function returning the transformation |
3536 |
> |
* for an element |
3537 |
> |
* @param basis the identity (initial default value) for the reduction |
3538 |
> |
* @param reducer a commutative associative combining function |
3539 |
> |
* @return the result of accumulating the given transformation |
3540 |
> |
* of all keys |
3541 |
> |
*/ |
3542 |
> |
public double reduceKeysToDoubleSequentially |
3543 |
> |
(ToDoubleFunction<? super K> transformer, |
3544 |
> |
double basis, |
3545 |
> |
DoubleBinaryOperator reducer) { |
3546 |
> |
if (transformer == null || reducer == null) |
3547 |
> |
throw new NullPointerException(); |
3548 |
> |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
3549 |
> |
double r = basis; |
3550 |
> |
K k; |
3551 |
> |
while ((k = it.advanceKey()) != null) |
3552 |
> |
r = reducer.applyAsDouble(r, transformer.applyAsDouble(k)); |
3553 |
> |
return r; |
3554 |
> |
} |
3555 |
> |
|
3556 |
> |
/** |
3557 |
> |
* Returns the result of accumulating the given transformation |
3558 |
> |
* of all keys using the given reducer to combine values, and |
3559 |
> |
* the given basis as an identity value. |
3560 |
> |
* |
3561 |
> |
* @param transformer a function returning the transformation |
3562 |
> |
* for an element |
3563 |
> |
* @param basis the identity (initial default value) for the reduction |
3564 |
> |
* @param reducer a commutative associative combining function |
3565 |
> |
* @return the result of accumulating the given transformation |
3566 |
> |
* of all keys |
3567 |
> |
*/ |
3568 |
> |
public long reduceKeysToLongSequentially |
3569 |
> |
(ToLongFunction<? super K> transformer, |
3570 |
> |
long basis, |
3571 |
> |
LongBinaryOperator reducer) { |
3572 |
> |
if (transformer == null || reducer == null) |
3573 |
> |
throw new NullPointerException(); |
3574 |
> |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
3575 |
> |
long r = basis; |
3576 |
> |
K k; |
3577 |
> |
while ((k = it.advanceKey()) != null) |
3578 |
> |
r = reducer.applyAsLong(r, transformer.applyAsLong(k)); |
3579 |
> |
return r; |
3580 |
> |
} |
3581 |
> |
|
3582 |
> |
/** |
3583 |
> |
* Returns the result of accumulating the given transformation |
3584 |
> |
* of all keys using the given reducer to combine values, and |
3585 |
> |
* the given basis as an identity value. |
3586 |
> |
* |
3587 |
> |
* @param transformer a function returning the transformation |
3588 |
> |
* for an element |
3589 |
> |
* @param basis the identity (initial default value) for the reduction |
3590 |
> |
* @param reducer a commutative associative combining function |
3591 |
> |
* @return the result of accumulating the given transformation |
3592 |
> |
* of all keys |
3593 |
> |
*/ |
3594 |
> |
public int reduceKeysToIntSequentially |
3595 |
> |
(ToIntFunction<? super K> transformer, |
3596 |
> |
int basis, |
3597 |
> |
IntBinaryOperator reducer) { |
3598 |
> |
if (transformer == null || reducer == null) |
3599 |
> |
throw new NullPointerException(); |
3600 |
> |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
3601 |
> |
int r = basis; |
3602 |
> |
K k; |
3603 |
> |
while ((k = it.advanceKey()) != null) |
3604 |
> |
r = reducer.applyAsInt(r, transformer.applyAsInt(k)); |
3605 |
> |
return r; |
3606 |
> |
} |
3607 |
> |
|
3608 |
> |
/** |
3609 |
> |
* Performs the given action for each value. |
3610 |
> |
* |
3611 |
> |
* @param action the action |
3612 |
> |
*/ |
3613 |
> |
public void forEachValueSequentially(Consumer<? super V> action) { |
3614 |
> |
if (action == null) throw new NullPointerException(); |
3615 |
> |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
3616 |
> |
V v; |
3617 |
> |
while ((v = it.advanceValue()) != null) |
3618 |
> |
action.accept(v); |
3619 |
> |
} |
3620 |
> |
|
3621 |
> |
/** |
3622 |
> |
* Performs the given action for each non-null transformation |
3623 |
> |
* of each value. |
3624 |
> |
* |
3625 |
> |
* @param transformer a function returning the transformation |
3626 |
> |
* for an element, or null if there is no transformation (in |
3627 |
> |
* which case the action is not applied) |
3628 |
> |
* @param action the action |
3629 |
> |
*/ |
3630 |
> |
public <U> void forEachValueSequentially |
3631 |
> |
(Function<? super V, ? extends U> transformer, |
3632 |
> |
Consumer<? super U> action) { |
3633 |
> |
if (transformer == null || action == null) |
3634 |
> |
throw new NullPointerException(); |
3635 |
> |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
3636 |
> |
V v; U u; |
3637 |
> |
while ((v = it.advanceValue()) != null) { |
3638 |
> |
if ((u = transformer.apply(v)) != null) |
3639 |
> |
action.accept(u); |
3640 |
|
} |
3641 |
|
} |
3642 |
|
|
3643 |
< |
private class Values extends AbstractCollection<V> { |
3644 |
< |
public Iterator<V> iterator() { |
3645 |
< |
return new ValueIterator(); |
3643 |
> |
/** |
3644 |
> |
* Returns a non-null result from applying the given search |
3645 |
> |
* function on each value, or null if none. |
3646 |
> |
* |
3647 |
> |
* @param searchFunction a function returning a non-null |
3648 |
> |
* result on success, else null |
3649 |
> |
* @return a non-null result from applying the given search |
3650 |
> |
* function on each value, or null if none |
3651 |
> |
*/ |
3652 |
> |
public <U> U searchValuesSequentially |
3653 |
> |
(Function<? super V, ? extends U> searchFunction) { |
3654 |
> |
if (searchFunction == null) throw new NullPointerException(); |
3655 |
> |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
3656 |
> |
V v; U u; |
3657 |
> |
while ((v = it.advanceValue()) != null) { |
3658 |
> |
if ((u = searchFunction.apply(v)) != null) |
3659 |
> |
return u; |
3660 |
> |
} |
3661 |
> |
return null; |
3662 |
> |
} |
3663 |
> |
|
3664 |
> |
/** |
3665 |
> |
* Returns the result of accumulating all values using the |
3666 |
> |
* given reducer to combine values, or null if none. |
3667 |
> |
* |
3668 |
> |
* @param reducer a commutative associative combining function |
3669 |
> |
* @return the result of accumulating all values |
3670 |
> |
*/ |
3671 |
> |
public V reduceValuesSequentially |
3672 |
> |
(BiFunction<? super V, ? super V, ? extends V> reducer) { |
3673 |
> |
if (reducer == null) throw new NullPointerException(); |
3674 |
> |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
3675 |
> |
V r = null; V v; |
3676 |
> |
while ((v = it.advanceValue()) != null) |
3677 |
> |
r = (r == null) ? v : reducer.apply(r, v); |
3678 |
> |
return r; |
3679 |
> |
} |
3680 |
> |
|
3681 |
> |
/** |
3682 |
> |
* Returns the result of accumulating the given transformation |
3683 |
> |
* of all values using the given reducer to combine values, or |
3684 |
> |
* null if none. |
3685 |
> |
* |
3686 |
> |
* @param transformer a function returning the transformation |
3687 |
> |
* for an element, or null if there is no transformation (in |
3688 |
> |
* which case it is not combined) |
3689 |
> |
* @param reducer a commutative associative combining function |
3690 |
> |
* @return the result of accumulating the given transformation |
3691 |
> |
* of all values |
3692 |
> |
*/ |
3693 |
> |
public <U> U reduceValuesSequentially |
3694 |
> |
(Function<? super V, ? extends U> transformer, |
3695 |
> |
BiFunction<? super U, ? super U, ? extends U> reducer) { |
3696 |
> |
if (transformer == null || reducer == null) |
3697 |
> |
throw new NullPointerException(); |
3698 |
> |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
3699 |
> |
U r = null, u; V v; |
3700 |
> |
while ((v = it.advanceValue()) != null) { |
3701 |
> |
if ((u = transformer.apply(v)) != null) |
3702 |
> |
r = (r == null) ? u : reducer.apply(r, u); |
3703 |
> |
} |
3704 |
> |
return r; |
3705 |
> |
} |
3706 |
> |
|
3707 |
> |
/** |
3708 |
> |
* Returns the result of accumulating the given transformation |
3709 |
> |
* of all values using the given reducer to combine values, |
3710 |
> |
* and the given basis as an identity value. |
3711 |
> |
* |
3712 |
> |
* @param transformer a function returning the transformation |
3713 |
> |
* for an element |
3714 |
> |
* @param basis the identity (initial default value) for the reduction |
3715 |
> |
* @param reducer a commutative associative combining function |
3716 |
> |
* @return the result of accumulating the given transformation |
3717 |
> |
* of all values |
3718 |
> |
*/ |
3719 |
> |
public double reduceValuesToDoubleSequentially |
3720 |
> |
(ToDoubleFunction<? super V> transformer, |
3721 |
> |
double basis, |
3722 |
> |
DoubleBinaryOperator reducer) { |
3723 |
> |
if (transformer == null || reducer == null) |
3724 |
> |
throw new NullPointerException(); |
3725 |
> |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
3726 |
> |
double r = basis; V v; |
3727 |
> |
while ((v = it.advanceValue()) != null) |
3728 |
> |
r = reducer.applyAsDouble(r, transformer.applyAsDouble(v)); |
3729 |
> |
return r; |
3730 |
> |
} |
3731 |
> |
|
3732 |
> |
/** |
3733 |
> |
* Returns the result of accumulating the given transformation |
3734 |
> |
* of all values using the given reducer to combine values, |
3735 |
> |
* and the given basis as an identity value. |
3736 |
> |
* |
3737 |
> |
* @param transformer a function returning the transformation |
3738 |
> |
* for an element |
3739 |
> |
* @param basis the identity (initial default value) for the reduction |
3740 |
> |
* @param reducer a commutative associative combining function |
3741 |
> |
* @return the result of accumulating the given transformation |
3742 |
> |
* of all values |
3743 |
> |
*/ |
3744 |
> |
public long reduceValuesToLongSequentially |
3745 |
> |
(ToLongFunction<? super V> transformer, |
3746 |
> |
long basis, |
3747 |
> |
LongBinaryOperator reducer) { |
3748 |
> |
if (transformer == null || reducer == null) |
3749 |
> |
throw new NullPointerException(); |
3750 |
> |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
3751 |
> |
long r = basis; V v; |
3752 |
> |
while ((v = it.advanceValue()) != null) |
3753 |
> |
r = reducer.applyAsLong(r, transformer.applyAsLong(v)); |
3754 |
> |
return r; |
3755 |
> |
} |
3756 |
> |
|
3757 |
> |
/** |
3758 |
> |
* Returns the result of accumulating the given transformation |
3759 |
> |
* of all values using the given reducer to combine values, |
3760 |
> |
* and the given basis as an identity value. |
3761 |
> |
* |
3762 |
> |
* @param transformer a function returning the transformation |
3763 |
> |
* for an element |
3764 |
> |
* @param basis the identity (initial default value) for the reduction |
3765 |
> |
* @param reducer a commutative associative combining function |
3766 |
> |
* @return the result of accumulating the given transformation |
3767 |
> |
* of all values |
3768 |
> |
*/ |
3769 |
> |
public int reduceValuesToIntSequentially |
3770 |
> |
(ToIntFunction<? super V> transformer, |
3771 |
> |
int basis, |
3772 |
> |
IntBinaryOperator reducer) { |
3773 |
> |
if (transformer == null || reducer == null) |
3774 |
> |
throw new NullPointerException(); |
3775 |
> |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
3776 |
> |
int r = basis; V v; |
3777 |
> |
while ((v = it.advanceValue()) != null) |
3778 |
> |
r = reducer.applyAsInt(r, transformer.applyAsInt(v)); |
3779 |
> |
return r; |
3780 |
> |
} |
3781 |
> |
|
3782 |
> |
/** |
3783 |
> |
* Performs the given action for each entry. |
3784 |
> |
* |
3785 |
> |
* @param action the action |
3786 |
> |
*/ |
3787 |
> |
public void forEachEntrySequentially |
3788 |
> |
(Consumer<? super Map.Entry<K,V>> action) { |
3789 |
> |
if (action == null) throw new NullPointerException(); |
3790 |
> |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
3791 |
> |
V v; |
3792 |
> |
while ((v = it.advanceValue()) != null) |
3793 |
> |
action.accept(entryFor(it.nextKey, v)); |
3794 |
> |
} |
3795 |
> |
|
3796 |
> |
/** |
3797 |
> |
* Performs the given action for each non-null transformation |
3798 |
> |
* of each entry. |
3799 |
> |
* |
3800 |
> |
* @param transformer a function returning the transformation |
3801 |
> |
* for an element, or null if there is no transformation (in |
3802 |
> |
* which case the action is not applied) |
3803 |
> |
* @param action the action |
3804 |
> |
*/ |
3805 |
> |
public <U> void forEachEntrySequentially |
3806 |
> |
(Function<Map.Entry<K,V>, ? extends U> transformer, |
3807 |
> |
Consumer<? super U> action) { |
3808 |
> |
if (transformer == null || action == null) |
3809 |
> |
throw new NullPointerException(); |
3810 |
> |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
3811 |
> |
V v; U u; |
3812 |
> |
while ((v = it.advanceValue()) != null) { |
3813 |
> |
if ((u = transformer.apply(entryFor(it.nextKey, v))) != null) |
3814 |
> |
action.accept(u); |
3815 |
|
} |
3816 |
< |
public int size() { |
3817 |
< |
return ConcurrentHashMap.this.size(); |
3816 |
> |
} |
3817 |
> |
|
3818 |
> |
/** |
3819 |
> |
* Returns a non-null result from applying the given search |
3820 |
> |
* function on each entry, or null if none. |
3821 |
> |
* |
3822 |
> |
* @param searchFunction a function returning a non-null |
3823 |
> |
* result on success, else null |
3824 |
> |
* @return a non-null result from applying the given search |
3825 |
> |
* function on each entry, or null if none |
3826 |
> |
*/ |
3827 |
> |
public <U> U searchEntriesSequentially |
3828 |
> |
(Function<Map.Entry<K,V>, ? extends U> searchFunction) { |
3829 |
> |
if (searchFunction == null) throw new NullPointerException(); |
3830 |
> |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
3831 |
> |
V v; U u; |
3832 |
> |
while ((v = it.advanceValue()) != null) { |
3833 |
> |
if ((u = searchFunction.apply(entryFor(it.nextKey, v))) != null) |
3834 |
> |
return u; |
3835 |
|
} |
3836 |
< |
public boolean contains(Object o) { |
3837 |
< |
return ConcurrentHashMap.this.containsValue(o); |
3836 |
> |
return null; |
3837 |
> |
} |
3838 |
> |
|
3839 |
> |
/** |
3840 |
> |
* Returns the result of accumulating all entries using the |
3841 |
> |
* given reducer to combine values, or null if none. |
3842 |
> |
* |
3843 |
> |
* @param reducer a commutative associative combining function |
3844 |
> |
* @return the result of accumulating all entries |
3845 |
> |
*/ |
3846 |
> |
public Map.Entry<K,V> reduceEntriesSequentially |
3847 |
> |
(BiFunction<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) { |
3848 |
> |
if (reducer == null) throw new NullPointerException(); |
3849 |
> |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
3850 |
> |
Map.Entry<K,V> r = null; V v; |
3851 |
> |
while ((v = it.advanceValue()) != null) { |
3852 |
> |
Map.Entry<K,V> u = entryFor(it.nextKey, v); |
3853 |
> |
r = (r == null) ? u : reducer.apply(r, u); |
3854 |
|
} |
3855 |
< |
public void clear() { |
3856 |
< |
ConcurrentHashMap.this.clear(); |
3855 |
> |
return r; |
3856 |
> |
} |
3857 |
> |
|
3858 |
> |
/** |
3859 |
> |
* Returns the result of accumulating the given transformation |
3860 |
> |
* of all entries using the given reducer to combine values, |
3861 |
> |
* or null if none. |
3862 |
> |
* |
3863 |
> |
* @param transformer a function returning the transformation |
3864 |
> |
* for an element, or null if there is no transformation (in |
3865 |
> |
* which case it is not combined) |
3866 |
> |
* @param reducer a commutative associative combining function |
3867 |
> |
* @return the result of accumulating the given transformation |
3868 |
> |
* of all entries |
3869 |
> |
*/ |
3870 |
> |
public <U> U reduceEntriesSequentially |
3871 |
> |
(Function<Map.Entry<K,V>, ? extends U> transformer, |
3872 |
> |
BiFunction<? super U, ? super U, ? extends U> reducer) { |
3873 |
> |
if (transformer == null || reducer == null) |
3874 |
> |
throw new NullPointerException(); |
3875 |
> |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
3876 |
> |
U r = null, u; V v; |
3877 |
> |
while ((v = it.advanceValue()) != null) { |
3878 |
> |
if ((u = transformer.apply(entryFor(it.nextKey, v))) != null) |
3879 |
> |
r = (r == null) ? u : reducer.apply(r, u); |
3880 |
|
} |
3881 |
+ |
return r; |
3882 |
+ |
} |
3883 |
+ |
|
3884 |
+ |
/** |
3885 |
+ |
* Returns the result of accumulating the given transformation |
3886 |
+ |
* of all entries using the given reducer to combine values, |
3887 |
+ |
* and the given basis as an identity value. |
3888 |
+ |
* |
3889 |
+ |
* @param transformer a function returning the transformation |
3890 |
+ |
* for an element |
3891 |
+ |
* @param basis the identity (initial default value) for the reduction |
3892 |
+ |
* @param reducer a commutative associative combining function |
3893 |
+ |
* @return the result of accumulating the given transformation |
3894 |
+ |
* of all entries |
3895 |
+ |
*/ |
3896 |
+ |
public double reduceEntriesToDoubleSequentially |
3897 |
+ |
(ToDoubleFunction<Map.Entry<K,V>> transformer, |
3898 |
+ |
double basis, |
3899 |
+ |
DoubleBinaryOperator reducer) { |
3900 |
+ |
if (transformer == null || reducer == null) |
3901 |
+ |
throw new NullPointerException(); |
3902 |
+ |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
3903 |
+ |
double r = basis; V v; |
3904 |
+ |
while ((v = it.advanceValue()) != null) |
3905 |
+ |
r = reducer.applyAsDouble(r, transformer.applyAsDouble(entryFor(it.nextKey, v))); |
3906 |
+ |
return r; |
3907 |
+ |
} |
3908 |
+ |
|
3909 |
+ |
/** |
3910 |
+ |
* Returns the result of accumulating the given transformation |
3911 |
+ |
* of all entries using the given reducer to combine values, |
3912 |
+ |
* and the given basis as an identity value. |
3913 |
+ |
* |
3914 |
+ |
* @param transformer a function returning the transformation |
3915 |
+ |
* for an element |
3916 |
+ |
* @param basis the identity (initial default value) for the reduction |
3917 |
+ |
* @param reducer a commutative associative combining function |
3918 |
+ |
* @return the result of accumulating the given transformation |
3919 |
+ |
* of all entries |
3920 |
+ |
*/ |
3921 |
+ |
public long reduceEntriesToLongSequentially |
3922 |
+ |
(ToLongFunction<Map.Entry<K,V>> transformer, |
3923 |
+ |
long basis, |
3924 |
+ |
LongBinaryOperator reducer) { |
3925 |
+ |
if (transformer == null || reducer == null) |
3926 |
+ |
throw new NullPointerException(); |
3927 |
+ |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
3928 |
+ |
long r = basis; V v; |
3929 |
+ |
while ((v = it.advanceValue()) != null) |
3930 |
+ |
r = reducer.applyAsLong(r, transformer.applyAsLong(entryFor(it.nextKey, v))); |
3931 |
+ |
return r; |
3932 |
+ |
} |
3933 |
+ |
|
3934 |
+ |
/** |
3935 |
+ |
* Returns the result of accumulating the given transformation |
3936 |
+ |
* of all entries using the given reducer to combine values, |
3937 |
+ |
* and the given basis as an identity value. |
3938 |
+ |
* |
3939 |
+ |
* @param transformer a function returning the transformation |
3940 |
+ |
* for an element |
3941 |
+ |
* @param basis the identity (initial default value) for the reduction |
3942 |
+ |
* @param reducer a commutative associative combining function |
3943 |
+ |
* @return the result of accumulating the given transformation |
3944 |
+ |
* of all entries |
3945 |
+ |
*/ |
3946 |
+ |
public int reduceEntriesToIntSequentially |
3947 |
+ |
(ToIntFunction<Map.Entry<K,V>> transformer, |
3948 |
+ |
int basis, |
3949 |
+ |
IntBinaryOperator reducer) { |
3950 |
+ |
if (transformer == null || reducer == null) |
3951 |
+ |
throw new NullPointerException(); |
3952 |
+ |
Traverser<K,V,Object> it = new Traverser<K,V,Object>(this); |
3953 |
+ |
int r = basis; V v; |
3954 |
+ |
while ((v = it.advanceValue()) != null) |
3955 |
+ |
r = reducer.applyAsInt(r, transformer.applyAsInt(entryFor(it.nextKey, v))); |
3956 |
+ |
return r; |
3957 |
+ |
} |
3958 |
+ |
|
3959 |
+ |
// Parallel bulk operations |
3960 |
+ |
|
3961 |
+ |
/** |
3962 |
+ |
* Performs the given action for each (key, value). |
3963 |
+ |
* |
3964 |
+ |
* @param action the action |
3965 |
+ |
*/ |
3966 |
+ |
public void forEachInParallel(BiConsumer<? super K,? super V> action) { |
3967 |
+ |
ForkJoinTasks.forEach |
3968 |
+ |
(this, action).invoke(); |
3969 |
+ |
} |
3970 |
+ |
|
3971 |
+ |
/** |
3972 |
+ |
* Performs the given action for each non-null transformation |
3973 |
+ |
* of each (key, value). |
3974 |
+ |
* |
3975 |
+ |
* @param transformer a function returning the transformation |
3976 |
+ |
* for an element, or null if there is no transformation (in |
3977 |
+ |
* which case the action is not applied) |
3978 |
+ |
* @param action the action |
3979 |
+ |
*/ |
3980 |
+ |
public <U> void forEachInParallel |
3981 |
+ |
(BiFunction<? super K, ? super V, ? extends U> transformer, |
3982 |
+ |
Consumer<? super U> action) { |
3983 |
+ |
ForkJoinTasks.forEach |
3984 |
+ |
(this, transformer, action).invoke(); |
3985 |
+ |
} |
3986 |
+ |
|
3987 |
+ |
/** |
3988 |
+ |
* Returns a non-null result from applying the given search |
3989 |
+ |
* function on each (key, value), or null if none. Upon |
3990 |
+ |
* success, further element processing is suppressed and the |
3991 |
+ |
* results of any other parallel invocations of the search |
3992 |
+ |
* function are ignored. |
3993 |
+ |
* |
3994 |
+ |
* @param searchFunction a function returning a non-null |
3995 |
+ |
* result on success, else null |
3996 |
+ |
* @return a non-null result from applying the given search |
3997 |
+ |
* function on each (key, value), or null if none |
3998 |
+ |
*/ |
3999 |
+ |
public <U> U searchInParallel |
4000 |
+ |
(BiFunction<? super K, ? super V, ? extends U> searchFunction) { |
4001 |
+ |
return ForkJoinTasks.search |
4002 |
+ |
(this, searchFunction).invoke(); |
4003 |
+ |
} |
4004 |
+ |
|
4005 |
+ |
/** |
4006 |
+ |
* Returns the result of accumulating the given transformation |
4007 |
+ |
* of all (key, value) pairs using the given reducer to |
4008 |
+ |
* combine values, or null if none. |
4009 |
+ |
* |
4010 |
+ |
* @param transformer a function returning the transformation |
4011 |
+ |
* for an element, or null if there is no transformation (in |
4012 |
+ |
* which case it is not combined) |
4013 |
+ |
* @param reducer a commutative associative combining function |
4014 |
+ |
* @return the result of accumulating the given transformation |
4015 |
+ |
* of all (key, value) pairs |
4016 |
+ |
*/ |
4017 |
+ |
public <U> U reduceInParallel |
4018 |
+ |
(BiFunction<? super K, ? super V, ? extends U> transformer, |
4019 |
+ |
BiFunction<? super U, ? super U, ? extends U> reducer) { |
4020 |
+ |
return ForkJoinTasks.reduce |
4021 |
+ |
(this, transformer, reducer).invoke(); |
4022 |
+ |
} |
4023 |
+ |
|
4024 |
+ |
/** |
4025 |
+ |
* Returns the result of accumulating the given transformation |
4026 |
+ |
* of all (key, value) pairs using the given reducer to |
4027 |
+ |
* combine values, and the given basis as an identity value. |
4028 |
+ |
* |
4029 |
+ |
* @param transformer a function returning the transformation |
4030 |
+ |
* for an element |
4031 |
+ |
* @param basis the identity (initial default value) for the reduction |
4032 |
+ |
* @param reducer a commutative associative combining function |
4033 |
+ |
* @return the result of accumulating the given transformation |
4034 |
+ |
* of all (key, value) pairs |
4035 |
+ |
*/ |
4036 |
+ |
public double reduceToDoubleInParallel |
4037 |
+ |
(ToDoubleBiFunction<? super K, ? super V> transformer, |
4038 |
+ |
double basis, |
4039 |
+ |
DoubleBinaryOperator reducer) { |
4040 |
+ |
return ForkJoinTasks.reduceToDouble |
4041 |
+ |
(this, transformer, basis, reducer).invoke(); |
4042 |
+ |
} |
4043 |
+ |
|
4044 |
+ |
/** |
4045 |
+ |
* Returns the result of accumulating the given transformation |
4046 |
+ |
* of all (key, value) pairs using the given reducer to |
4047 |
+ |
* combine values, and the given basis as an identity value. |
4048 |
+ |
* |
4049 |
+ |
* @param transformer a function returning the transformation |
4050 |
+ |
* for an element |
4051 |
+ |
* @param basis the identity (initial default value) for the reduction |
4052 |
+ |
* @param reducer a commutative associative combining function |
4053 |
+ |
* @return the result of accumulating the given transformation |
4054 |
+ |
* of all (key, value) pairs |
4055 |
+ |
*/ |
4056 |
+ |
public long reduceToLongInParallel |
4057 |
+ |
(ToLongBiFunction<? super K, ? super V> transformer, |
4058 |
+ |
long basis, |
4059 |
+ |
LongBinaryOperator reducer) { |
4060 |
+ |
return ForkJoinTasks.reduceToLong |
4061 |
+ |
(this, transformer, basis, reducer).invoke(); |
4062 |
+ |
} |
4063 |
+ |
|
4064 |
+ |
/** |
4065 |
+ |
* Returns the result of accumulating the given transformation |
4066 |
+ |
* of all (key, value) pairs using the given reducer to |
4067 |
+ |
* combine values, and the given basis as an identity value. |
4068 |
+ |
* |
4069 |
+ |
* @param transformer a function returning the transformation |
4070 |
+ |
* for an element |
4071 |
+ |
* @param basis the identity (initial default value) for the reduction |
4072 |
+ |
* @param reducer a commutative associative combining function |
4073 |
+ |
* @return the result of accumulating the given transformation |
4074 |
+ |
* of all (key, value) pairs |
4075 |
+ |
*/ |
4076 |
+ |
public int reduceToIntInParallel |
4077 |
+ |
(ToIntBiFunction<? super K, ? super V> transformer, |
4078 |
+ |
int basis, |
4079 |
+ |
IntBinaryOperator reducer) { |
4080 |
+ |
return ForkJoinTasks.reduceToInt |
4081 |
+ |
(this, transformer, basis, reducer).invoke(); |
4082 |
+ |
} |
4083 |
+ |
|
4084 |
+ |
/** |
4085 |
+ |
* Performs the given action for each key. |
4086 |
+ |
* |
4087 |
+ |
* @param action the action |
4088 |
+ |
*/ |
4089 |
+ |
public void forEachKeyInParallel(Consumer<? super K> action) { |
4090 |
+ |
ForkJoinTasks.forEachKey |
4091 |
+ |
(this, action).invoke(); |
4092 |
+ |
} |
4093 |
+ |
|
4094 |
+ |
/** |
4095 |
+ |
* Performs the given action for each non-null transformation |
4096 |
+ |
* of each key. |
4097 |
+ |
* |
4098 |
+ |
* @param transformer a function returning the transformation |
4099 |
+ |
* for an element, or null if there is no transformation (in |
4100 |
+ |
* which case the action is not applied) |
4101 |
+ |
* @param action the action |
4102 |
+ |
*/ |
4103 |
+ |
public <U> void forEachKeyInParallel |
4104 |
+ |
(Function<? super K, ? extends U> transformer, |
4105 |
+ |
Consumer<? super U> action) { |
4106 |
+ |
ForkJoinTasks.forEachKey |
4107 |
+ |
(this, transformer, action).invoke(); |
4108 |
+ |
} |
4109 |
+ |
|
4110 |
+ |
/** |
4111 |
+ |
* Returns a non-null result from applying the given search |
4112 |
+ |
* function on each key, or null if none. Upon success, |
4113 |
+ |
* further element processing is suppressed and the results of |
4114 |
+ |
* any other parallel invocations of the search function are |
4115 |
+ |
* ignored. |
4116 |
+ |
* |
4117 |
+ |
* @param searchFunction a function returning a non-null |
4118 |
+ |
* result on success, else null |
4119 |
+ |
* @return a non-null result from applying the given search |
4120 |
+ |
* function on each key, or null if none |
4121 |
+ |
*/ |
4122 |
+ |
public <U> U searchKeysInParallel |
4123 |
+ |
(Function<? super K, ? extends U> searchFunction) { |
4124 |
+ |
return ForkJoinTasks.searchKeys |
4125 |
+ |
(this, searchFunction).invoke(); |
4126 |
+ |
} |
4127 |
+ |
|
4128 |
+ |
/** |
4129 |
+ |
* Returns the result of accumulating all keys using the given |
4130 |
+ |
* reducer to combine values, or null if none. |
4131 |
+ |
* |
4132 |
+ |
* @param reducer a commutative associative combining function |
4133 |
+ |
* @return the result of accumulating all keys using the given |
4134 |
+ |
* reducer to combine values, or null if none |
4135 |
+ |
*/ |
4136 |
+ |
public K reduceKeysInParallel |
4137 |
+ |
(BiFunction<? super K, ? super K, ? extends K> reducer) { |
4138 |
+ |
return ForkJoinTasks.reduceKeys |
4139 |
+ |
(this, reducer).invoke(); |
4140 |
+ |
} |
4141 |
+ |
|
4142 |
+ |
/** |
4143 |
+ |
* Returns the result of accumulating the given transformation |
4144 |
+ |
* of all keys using the given reducer to combine values, or |
4145 |
+ |
* null if none. |
4146 |
+ |
* |
4147 |
+ |
* @param transformer a function returning the transformation |
4148 |
+ |
* for an element, or null if there is no transformation (in |
4149 |
+ |
* which case it is not combined) |
4150 |
+ |
* @param reducer a commutative associative combining function |
4151 |
+ |
* @return the result of accumulating the given transformation |
4152 |
+ |
* of all keys |
4153 |
+ |
*/ |
4154 |
+ |
public <U> U reduceKeysInParallel |
4155 |
+ |
(Function<? super K, ? extends U> transformer, |
4156 |
+ |
BiFunction<? super U, ? super U, ? extends U> reducer) { |
4157 |
+ |
return ForkJoinTasks.reduceKeys |
4158 |
+ |
(this, transformer, reducer).invoke(); |
4159 |
+ |
} |
4160 |
+ |
|
4161 |
+ |
/** |
4162 |
+ |
* Returns the result of accumulating the given transformation |
4163 |
+ |
* of all keys using the given reducer to combine values, and |
4164 |
+ |
* the given basis as an identity value. |
4165 |
+ |
* |
4166 |
+ |
* @param transformer a function returning the transformation |
4167 |
+ |
* for an element |
4168 |
+ |
* @param basis the identity (initial default value) for the reduction |
4169 |
+ |
* @param reducer a commutative associative combining function |
4170 |
+ |
* @return the result of accumulating the given transformation |
4171 |
+ |
* of all keys |
4172 |
+ |
*/ |
4173 |
+ |
public double reduceKeysToDoubleInParallel |
4174 |
+ |
(ToDoubleFunction<? super K> transformer, |
4175 |
+ |
double basis, |
4176 |
+ |
DoubleBinaryOperator reducer) { |
4177 |
+ |
return ForkJoinTasks.reduceKeysToDouble |
4178 |
+ |
(this, transformer, basis, reducer).invoke(); |
4179 |
+ |
} |
4180 |
+ |
|
4181 |
+ |
/** |
4182 |
+ |
* Returns the result of accumulating the given transformation |
4183 |
+ |
* of all keys using the given reducer to combine values, and |
4184 |
+ |
* the given basis as an identity value. |
4185 |
+ |
* |
4186 |
+ |
* @param transformer a function returning the transformation |
4187 |
+ |
* for an element |
4188 |
+ |
* @param basis the identity (initial default value) for the reduction |
4189 |
+ |
* @param reducer a commutative associative combining function |
4190 |
+ |
* @return the result of accumulating the given transformation |
4191 |
+ |
* of all keys |
4192 |
+ |
*/ |
4193 |
+ |
public long reduceKeysToLongInParallel |
4194 |
+ |
(ToLongFunction<? super K> transformer, |
4195 |
+ |
long basis, |
4196 |
+ |
LongBinaryOperator reducer) { |
4197 |
+ |
return ForkJoinTasks.reduceKeysToLong |
4198 |
+ |
(this, transformer, basis, reducer).invoke(); |
4199 |
+ |
} |
4200 |
+ |
|
4201 |
+ |
/** |
4202 |
+ |
* Returns the result of accumulating the given transformation |
4203 |
+ |
* of all keys using the given reducer to combine values, and |
4204 |
+ |
* the given basis as an identity value. |
4205 |
+ |
* |
4206 |
+ |
* @param transformer a function returning the transformation |
4207 |
+ |
* for an element |
4208 |
+ |
* @param basis the identity (initial default value) for the reduction |
4209 |
+ |
* @param reducer a commutative associative combining function |
4210 |
+ |
* @return the result of accumulating the given transformation |
4211 |
+ |
* of all keys |
4212 |
+ |
*/ |
4213 |
+ |
public int reduceKeysToIntInParallel |
4214 |
+ |
(ToIntFunction<? super K> transformer, |
4215 |
+ |
int basis, |
4216 |
+ |
IntBinaryOperator reducer) { |
4217 |
+ |
return ForkJoinTasks.reduceKeysToInt |
4218 |
+ |
(this, transformer, basis, reducer).invoke(); |
4219 |
+ |
} |
4220 |
+ |
|
4221 |
+ |
/** |
4222 |
+ |
* Performs the given action for each value. |
4223 |
+ |
* |
4224 |
+ |
* @param action the action |
4225 |
+ |
*/ |
4226 |
+ |
public void forEachValueInParallel(Consumer<? super V> action) { |
4227 |
+ |
ForkJoinTasks.forEachValue |
4228 |
+ |
(this, action).invoke(); |
4229 |
+ |
} |
4230 |
+ |
|
4231 |
+ |
/** |
4232 |
+ |
* Performs the given action for each non-null transformation |
4233 |
+ |
* of each value. |
4234 |
+ |
* |
4235 |
+ |
* @param transformer a function returning the transformation |
4236 |
+ |
* for an element, or null if there is no transformation (in |
4237 |
+ |
* which case the action is not applied) |
4238 |
+ |
* @param action the action |
4239 |
+ |
*/ |
4240 |
+ |
public <U> void forEachValueInParallel |
4241 |
+ |
(Function<? super V, ? extends U> transformer, |
4242 |
+ |
Consumer<? super U> action) { |
4243 |
+ |
ForkJoinTasks.forEachValue |
4244 |
+ |
(this, transformer, action).invoke(); |
4245 |
+ |
} |
4246 |
+ |
|
4247 |
+ |
/** |
4248 |
+ |
* Returns a non-null result from applying the given search |
4249 |
+ |
* function on each value, or null if none. Upon success, |
4250 |
+ |
* further element processing is suppressed and the results of |
4251 |
+ |
* any other parallel invocations of the search function are |
4252 |
+ |
* ignored. |
4253 |
+ |
* |
4254 |
+ |
* @param searchFunction a function returning a non-null |
4255 |
+ |
* result on success, else null |
4256 |
+ |
* @return a non-null result from applying the given search |
4257 |
+ |
* function on each value, or null if none |
4258 |
+ |
*/ |
4259 |
+ |
public <U> U searchValuesInParallel |
4260 |
+ |
(Function<? super V, ? extends U> searchFunction) { |
4261 |
+ |
return ForkJoinTasks.searchValues |
4262 |
+ |
(this, searchFunction).invoke(); |
4263 |
+ |
} |
4264 |
+ |
|
4265 |
+ |
/** |
4266 |
+ |
* Returns the result of accumulating all values using the |
4267 |
+ |
* given reducer to combine values, or null if none. |
4268 |
+ |
* |
4269 |
+ |
* @param reducer a commutative associative combining function |
4270 |
+ |
* @return the result of accumulating all values |
4271 |
+ |
*/ |
4272 |
+ |
public V reduceValuesInParallel |
4273 |
+ |
(BiFunction<? super V, ? super V, ? extends V> reducer) { |
4274 |
+ |
return ForkJoinTasks.reduceValues |
4275 |
+ |
(this, reducer).invoke(); |
4276 |
+ |
} |
4277 |
+ |
|
4278 |
+ |
/** |
4279 |
+ |
* Returns the result of accumulating the given transformation |
4280 |
+ |
* of all values using the given reducer to combine values, or |
4281 |
+ |
* null if none. |
4282 |
+ |
* |
4283 |
+ |
* @param transformer a function returning the transformation |
4284 |
+ |
* for an element, or null if there is no transformation (in |
4285 |
+ |
* which case it is not combined) |
4286 |
+ |
* @param reducer a commutative associative combining function |
4287 |
+ |
* @return the result of accumulating the given transformation |
4288 |
+ |
* of all values |
4289 |
+ |
*/ |
4290 |
+ |
public <U> U reduceValuesInParallel |
4291 |
+ |
(Function<? super V, ? extends U> transformer, |
4292 |
+ |
BiFunction<? super U, ? super U, ? extends U> reducer) { |
4293 |
+ |
return ForkJoinTasks.reduceValues |
4294 |
+ |
(this, transformer, reducer).invoke(); |
4295 |
+ |
} |
4296 |
+ |
|
4297 |
+ |
/** |
4298 |
+ |
* Returns the result of accumulating the given transformation |
4299 |
+ |
* of all values using the given reducer to combine values, |
4300 |
+ |
* and the given basis as an identity value. |
4301 |
+ |
* |
4302 |
+ |
* @param transformer a function returning the transformation |
4303 |
+ |
* for an element |
4304 |
+ |
* @param basis the identity (initial default value) for the reduction |
4305 |
+ |
* @param reducer a commutative associative combining function |
4306 |
+ |
* @return the result of accumulating the given transformation |
4307 |
+ |
* of all values |
4308 |
+ |
*/ |
4309 |
+ |
public double reduceValuesToDoubleInParallel |
4310 |
+ |
(ToDoubleFunction<? super V> transformer, |
4311 |
+ |
double basis, |
4312 |
+ |
DoubleBinaryOperator reducer) { |
4313 |
+ |
return ForkJoinTasks.reduceValuesToDouble |
4314 |
+ |
(this, transformer, basis, reducer).invoke(); |
4315 |
+ |
} |
4316 |
+ |
|
4317 |
+ |
/** |
4318 |
+ |
* Returns the result of accumulating the given transformation |
4319 |
+ |
* of all values using the given reducer to combine values, |
4320 |
+ |
* and the given basis as an identity value. |
4321 |
+ |
* |
4322 |
+ |
* @param transformer a function returning the transformation |
4323 |
+ |
* for an element |
4324 |
+ |
* @param basis the identity (initial default value) for the reduction |
4325 |
+ |
* @param reducer a commutative associative combining function |
4326 |
+ |
* @return the result of accumulating the given transformation |
4327 |
+ |
* of all values |
4328 |
+ |
*/ |
4329 |
+ |
public long reduceValuesToLongInParallel |
4330 |
+ |
(ToLongFunction<? super V> transformer, |
4331 |
+ |
long basis, |
4332 |
+ |
LongBinaryOperator reducer) { |
4333 |
+ |
return ForkJoinTasks.reduceValuesToLong |
4334 |
+ |
(this, transformer, basis, reducer).invoke(); |
4335 |
+ |
} |
4336 |
+ |
|
4337 |
+ |
/** |
4338 |
+ |
* Returns the result of accumulating the given transformation |
4339 |
+ |
* of all values using the given reducer to combine values, |
4340 |
+ |
* and the given basis as an identity value. |
4341 |
+ |
* |
4342 |
+ |
* @param transformer a function returning the transformation |
4343 |
+ |
* for an element |
4344 |
+ |
* @param basis the identity (initial default value) for the reduction |
4345 |
+ |
* @param reducer a commutative associative combining function |
4346 |
+ |
* @return the result of accumulating the given transformation |
4347 |
+ |
* of all values |
4348 |
+ |
*/ |
4349 |
+ |
public int reduceValuesToIntInParallel |
4350 |
+ |
(ToIntFunction<? super V> transformer, |
4351 |
+ |
int basis, |
4352 |
+ |
IntBinaryOperator reducer) { |
4353 |
+ |
return ForkJoinTasks.reduceValuesToInt |
4354 |
+ |
(this, transformer, basis, reducer).invoke(); |
4355 |
+ |
} |
4356 |
+ |
|
4357 |
+ |
/** |
4358 |
+ |
* Performs the given action for each entry. |
4359 |
+ |
* |
4360 |
+ |
* @param action the action |
4361 |
+ |
*/ |
4362 |
+ |
public void forEachEntryInParallel(Consumer<? super Map.Entry<K,V>> action) { |
4363 |
+ |
ForkJoinTasks.forEachEntry |
4364 |
+ |
(this, action).invoke(); |
4365 |
+ |
} |
4366 |
+ |
|
4367 |
+ |
/** |
4368 |
+ |
* Performs the given action for each non-null transformation |
4369 |
+ |
* of each entry. |
4370 |
+ |
* |
4371 |
+ |
* @param transformer a function returning the transformation |
4372 |
+ |
* for an element, or null if there is no transformation (in |
4373 |
+ |
* which case the action is not applied) |
4374 |
+ |
* @param action the action |
4375 |
+ |
*/ |
4376 |
+ |
public <U> void forEachEntryInParallel |
4377 |
+ |
(Function<Map.Entry<K,V>, ? extends U> transformer, |
4378 |
+ |
Consumer<? super U> action) { |
4379 |
+ |
ForkJoinTasks.forEachEntry |
4380 |
+ |
(this, transformer, action).invoke(); |
4381 |
+ |
} |
4382 |
+ |
|
4383 |
+ |
/** |
4384 |
+ |
* Returns a non-null result from applying the given search |
4385 |
+ |
* function on each entry, or null if none. Upon success, |
4386 |
+ |
* further element processing is suppressed and the results of |
4387 |
+ |
* any other parallel invocations of the search function are |
4388 |
+ |
* ignored. |
4389 |
+ |
* |
4390 |
+ |
* @param searchFunction a function returning a non-null |
4391 |
+ |
* result on success, else null |
4392 |
+ |
* @return a non-null result from applying the given search |
4393 |
+ |
* function on each entry, or null if none |
4394 |
+ |
*/ |
4395 |
+ |
public <U> U searchEntriesInParallel |
4396 |
+ |
(Function<Map.Entry<K,V>, ? extends U> searchFunction) { |
4397 |
+ |
return ForkJoinTasks.searchEntries |
4398 |
+ |
(this, searchFunction).invoke(); |
4399 |
+ |
} |
4400 |
+ |
|
4401 |
+ |
/** |
4402 |
+ |
* Returns the result of accumulating all entries using the |
4403 |
+ |
* given reducer to combine values, or null if none. |
4404 |
+ |
* |
4405 |
+ |
* @param reducer a commutative associative combining function |
4406 |
+ |
* @return the result of accumulating all entries |
4407 |
+ |
*/ |
4408 |
+ |
public Map.Entry<K,V> reduceEntriesInParallel |
4409 |
+ |
(BiFunction<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) { |
4410 |
+ |
return ForkJoinTasks.reduceEntries |
4411 |
+ |
(this, reducer).invoke(); |
4412 |
+ |
} |
4413 |
+ |
|
4414 |
+ |
/** |
4415 |
+ |
* Returns the result of accumulating the given transformation |
4416 |
+ |
* of all entries using the given reducer to combine values, |
4417 |
+ |
* or null if none. |
4418 |
+ |
* |
4419 |
+ |
* @param transformer a function returning the transformation |
4420 |
+ |
* for an element, or null if there is no transformation (in |
4421 |
+ |
* which case it is not combined) |
4422 |
+ |
* @param reducer a commutative associative combining function |
4423 |
+ |
* @return the result of accumulating the given transformation |
4424 |
+ |
* of all entries |
4425 |
+ |
*/ |
4426 |
+ |
public <U> U reduceEntriesInParallel |
4427 |
+ |
(Function<Map.Entry<K,V>, ? extends U> transformer, |
4428 |
+ |
BiFunction<? super U, ? super U, ? extends U> reducer) { |
4429 |
+ |
return ForkJoinTasks.reduceEntries |
4430 |
+ |
(this, transformer, reducer).invoke(); |
4431 |
+ |
} |
4432 |
+ |
|
4433 |
+ |
/** |
4434 |
+ |
* Returns the result of accumulating the given transformation |
4435 |
+ |
* of all entries using the given reducer to combine values, |
4436 |
+ |
* and the given basis as an identity value. |
4437 |
+ |
* |
4438 |
+ |
* @param transformer a function returning the transformation |
4439 |
+ |
* for an element |
4440 |
+ |
* @param basis the identity (initial default value) for the reduction |
4441 |
+ |
* @param reducer a commutative associative combining function |
4442 |
+ |
* @return the result of accumulating the given transformation |
4443 |
+ |
* of all entries |
4444 |
+ |
*/ |
4445 |
+ |
public double reduceEntriesToDoubleInParallel |
4446 |
+ |
(ToDoubleFunction<Map.Entry<K,V>> transformer, |
4447 |
+ |
double basis, |
4448 |
+ |
DoubleBinaryOperator reducer) { |
4449 |
+ |
return ForkJoinTasks.reduceEntriesToDouble |
4450 |
+ |
(this, transformer, basis, reducer).invoke(); |
4451 |
+ |
} |
4452 |
+ |
|
4453 |
+ |
/** |
4454 |
+ |
* Returns the result of accumulating the given transformation |
4455 |
+ |
* of all entries using the given reducer to combine values, |
4456 |
+ |
* and the given basis as an identity value. |
4457 |
+ |
* |
4458 |
+ |
* @param transformer a function returning the transformation |
4459 |
+ |
* for an element |
4460 |
+ |
* @param basis the identity (initial default value) for the reduction |
4461 |
+ |
* @param reducer a commutative associative combining function |
4462 |
+ |
* @return the result of accumulating the given transformation |
4463 |
+ |
* of all entries |
4464 |
+ |
*/ |
4465 |
+ |
public long reduceEntriesToLongInParallel |
4466 |
+ |
(ToLongFunction<Map.Entry<K,V>> transformer, |
4467 |
+ |
long basis, |
4468 |
+ |
LongBinaryOperator reducer) { |
4469 |
+ |
return ForkJoinTasks.reduceEntriesToLong |
4470 |
+ |
(this, transformer, basis, reducer).invoke(); |
4471 |
|
} |
4472 |
|
|
4473 |
< |
private class EntrySet extends AbstractSet<Map.Entry<K,V>> { |
4474 |
< |
public Iterator<Map.Entry<K,V>> iterator() { |
4475 |
< |
return new EntryIterator(); |
4473 |
> |
/** |
4474 |
> |
* Returns the result of accumulating the given transformation |
4475 |
> |
* of all entries using the given reducer to combine values, |
4476 |
> |
* and the given basis as an identity value. |
4477 |
> |
* |
4478 |
> |
* @param transformer a function returning the transformation |
4479 |
> |
* for an element |
4480 |
> |
* @param basis the identity (initial default value) for the reduction |
4481 |
> |
* @param reducer a commutative associative combining function |
4482 |
> |
* @return the result of accumulating the given transformation |
4483 |
> |
* of all entries |
4484 |
> |
*/ |
4485 |
> |
public int reduceEntriesToIntInParallel |
4486 |
> |
(ToIntFunction<Map.Entry<K,V>> transformer, |
4487 |
> |
int basis, |
4488 |
> |
IntBinaryOperator reducer) { |
4489 |
> |
return ForkJoinTasks.reduceEntriesToInt |
4490 |
> |
(this, transformer, basis, reducer).invoke(); |
4491 |
> |
} |
4492 |
> |
|
4493 |
> |
|
4494 |
> |
/* ----------------Views -------------- */ |
4495 |
> |
|
4496 |
> |
/** |
4497 |
> |
* Base class for views. |
4498 |
> |
*/ |
4499 |
> |
abstract static class CHMCollectionView<K,V,E> |
4500 |
> |
implements Collection<E>, java.io.Serializable { |
4501 |
> |
private static final long serialVersionUID = 7249069246763182397L; |
4502 |
> |
final ConcurrentHashMap<K,V> map; |
4503 |
> |
CHMCollectionView(ConcurrentHashMap<K,V> map) { this.map = map; } |
4504 |
> |
|
4505 |
> |
/** |
4506 |
> |
* Returns the map backing this view. |
4507 |
> |
* |
4508 |
> |
* @return the map backing this view |
4509 |
> |
*/ |
4510 |
> |
public ConcurrentHashMap<K,V> getMap() { return map; } |
4511 |
> |
|
4512 |
> |
/** |
4513 |
> |
* Removes all of the elements from this view, by removing all |
4514 |
> |
* the mappings from the map backing this view. |
4515 |
> |
*/ |
4516 |
> |
public final void clear() { map.clear(); } |
4517 |
> |
public final int size() { return map.size(); } |
4518 |
> |
public final boolean isEmpty() { return map.isEmpty(); } |
4519 |
> |
|
4520 |
> |
// implementations below rely on concrete classes supplying these |
4521 |
> |
// abstract methods |
4522 |
> |
/** |
4523 |
> |
* Returns a "weakly consistent" iterator that will never |
4524 |
> |
* throw {@link ConcurrentModificationException}, and |
4525 |
> |
* guarantees to traverse elements as they existed upon |
4526 |
> |
* construction of the iterator, and may (but is not |
4527 |
> |
* guaranteed to) reflect any modifications subsequent to |
4528 |
> |
* construction. |
4529 |
> |
*/ |
4530 |
> |
public abstract Iterator<E> iterator(); |
4531 |
> |
public abstract boolean contains(Object o); |
4532 |
> |
public abstract boolean remove(Object o); |
4533 |
> |
|
4534 |
> |
private static final String oomeMsg = "Required array size too large"; |
4535 |
> |
|
4536 |
> |
public final Object[] toArray() { |
4537 |
> |
long sz = map.mappingCount(); |
4538 |
> |
if (sz > MAX_ARRAY_SIZE) |
4539 |
> |
throw new OutOfMemoryError(oomeMsg); |
4540 |
> |
int n = (int)sz; |
4541 |
> |
Object[] r = new Object[n]; |
4542 |
> |
int i = 0; |
4543 |
> |
for (E e : this) { |
4544 |
> |
if (i == n) { |
4545 |
> |
if (n >= MAX_ARRAY_SIZE) |
4546 |
> |
throw new OutOfMemoryError(oomeMsg); |
4547 |
> |
if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1) |
4548 |
> |
n = MAX_ARRAY_SIZE; |
4549 |
> |
else |
4550 |
> |
n += (n >>> 1) + 1; |
4551 |
> |
r = Arrays.copyOf(r, n); |
4552 |
> |
} |
4553 |
> |
r[i++] = e; |
4554 |
> |
} |
4555 |
> |
return (i == n) ? r : Arrays.copyOf(r, i); |
4556 |
|
} |
4557 |
< |
public boolean contains(Object o) { |
4558 |
< |
if (!(o instanceof Map.Entry)) |
4559 |
< |
return false; |
4560 |
< |
Map.Entry<K,V> e = (Map.Entry<K,V>)o; |
4561 |
< |
V v = ConcurrentHashMap.this.get(e.getKey()); |
4562 |
< |
return v != null && v.equals(e.getValue()); |
4557 |
> |
|
4558 |
> |
@SuppressWarnings("unchecked") |
4559 |
> |
public final <T> T[] toArray(T[] a) { |
4560 |
> |
long sz = map.mappingCount(); |
4561 |
> |
if (sz > MAX_ARRAY_SIZE) |
4562 |
> |
throw new OutOfMemoryError(oomeMsg); |
4563 |
> |
int m = (int)sz; |
4564 |
> |
T[] r = (a.length >= m) ? a : |
4565 |
> |
(T[])java.lang.reflect.Array |
4566 |
> |
.newInstance(a.getClass().getComponentType(), m); |
4567 |
> |
int n = r.length; |
4568 |
> |
int i = 0; |
4569 |
> |
for (E e : this) { |
4570 |
> |
if (i == n) { |
4571 |
> |
if (n >= MAX_ARRAY_SIZE) |
4572 |
> |
throw new OutOfMemoryError(oomeMsg); |
4573 |
> |
if (n >= MAX_ARRAY_SIZE - (MAX_ARRAY_SIZE >>> 1) - 1) |
4574 |
> |
n = MAX_ARRAY_SIZE; |
4575 |
> |
else |
4576 |
> |
n += (n >>> 1) + 1; |
4577 |
> |
r = Arrays.copyOf(r, n); |
4578 |
> |
} |
4579 |
> |
r[i++] = (T)e; |
4580 |
> |
} |
4581 |
> |
if (a == r && i < n) { |
4582 |
> |
r[i] = null; // null-terminate |
4583 |
> |
return r; |
4584 |
> |
} |
4585 |
> |
return (i == n) ? r : Arrays.copyOf(r, i); |
4586 |
|
} |
4587 |
< |
public boolean remove(Object o) { |
4588 |
< |
if (!(o instanceof Map.Entry)) |
4589 |
< |
return false; |
4590 |
< |
Map.Entry<K,V> e = (Map.Entry<K,V>)o; |
4591 |
< |
return ConcurrentHashMap.this.remove(e.getKey(), e.getValue()); |
4587 |
> |
|
4588 |
> |
/** |
4589 |
> |
* Returns a string representation of this collection. |
4590 |
> |
* The string representation consists of the string representations |
4591 |
> |
* of the collection's elements in the order they are returned by |
4592 |
> |
* its iterator, enclosed in square brackets ({@code "[]"}). |
4593 |
> |
* Adjacent elements are separated by the characters {@code ", "} |
4594 |
> |
* (comma and space). Elements are converted to strings as by |
4595 |
> |
* {@link String#valueOf(Object)}. |
4596 |
> |
* |
4597 |
> |
* @return a string representation of this collection |
4598 |
> |
*/ |
4599 |
> |
public final String toString() { |
4600 |
> |
StringBuilder sb = new StringBuilder(); |
4601 |
> |
sb.append('['); |
4602 |
> |
Iterator<E> it = iterator(); |
4603 |
> |
if (it.hasNext()) { |
4604 |
> |
for (;;) { |
4605 |
> |
Object e = it.next(); |
4606 |
> |
sb.append(e == this ? "(this Collection)" : e); |
4607 |
> |
if (!it.hasNext()) |
4608 |
> |
break; |
4609 |
> |
sb.append(',').append(' '); |
4610 |
> |
} |
4611 |
> |
} |
4612 |
> |
return sb.append(']').toString(); |
4613 |
> |
} |
4614 |
> |
|
4615 |
> |
public final boolean containsAll(Collection<?> c) { |
4616 |
> |
if (c != this) { |
4617 |
> |
for (Object e : c) { |
4618 |
> |
if (e == null || !contains(e)) |
4619 |
> |
return false; |
4620 |
> |
} |
4621 |
> |
} |
4622 |
> |
return true; |
4623 |
|
} |
4624 |
< |
public int size() { |
4625 |
< |
return ConcurrentHashMap.this.size(); |
4624 |
> |
|
4625 |
> |
public final boolean removeAll(Collection<?> c) { |
4626 |
> |
boolean modified = false; |
4627 |
> |
for (Iterator<E> it = iterator(); it.hasNext();) { |
4628 |
> |
if (c.contains(it.next())) { |
4629 |
> |
it.remove(); |
4630 |
> |
modified = true; |
4631 |
> |
} |
4632 |
> |
} |
4633 |
> |
return modified; |
4634 |
|
} |
4635 |
< |
public void clear() { |
4636 |
< |
ConcurrentHashMap.this.clear(); |
4635 |
> |
|
4636 |
> |
public final boolean retainAll(Collection<?> c) { |
4637 |
> |
boolean modified = false; |
4638 |
> |
for (Iterator<E> it = iterator(); it.hasNext();) { |
4639 |
> |
if (!c.contains(it.next())) { |
4640 |
> |
it.remove(); |
4641 |
> |
modified = true; |
4642 |
> |
} |
4643 |
> |
} |
4644 |
> |
return modified; |
4645 |
|
} |
4646 |
+ |
|
4647 |
|
} |
4648 |
|
|
4649 |
< |
/* ---------------- Serialization Support -------------- */ |
4649 |
> |
abstract static class CHMSetView<K,V,E> |
4650 |
> |
extends CHMCollectionView<K,V,E> |
4651 |
> |
implements Set<E>, java.io.Serializable { |
4652 |
> |
private static final long serialVersionUID = 7249069246763182397L; |
4653 |
> |
CHMSetView(ConcurrentHashMap<K,V> map) { super(map); } |
4654 |
> |
|
4655 |
> |
// Implement Set API |
4656 |
> |
|
4657 |
> |
/** |
4658 |
> |
* Implements {@link Set#hashCode()}. |
4659 |
> |
* @return the hash code value for this set |
4660 |
> |
*/ |
4661 |
> |
public final int hashCode() { |
4662 |
> |
int h = 0; |
4663 |
> |
for (E e : this) |
4664 |
> |
h += e.hashCode(); |
4665 |
> |
return h; |
4666 |
> |
} |
4667 |
> |
|
4668 |
> |
/** |
4669 |
> |
* Implements {@link Set#equals(Object)}. |
4670 |
> |
* @param o object to be compared for equality with this set |
4671 |
> |
* @return {@code true} if the specified object is equal to this set |
4672 |
> |
*/ |
4673 |
> |
public final boolean equals(Object o) { |
4674 |
> |
Set<?> c; |
4675 |
> |
return ((o instanceof Set) && |
4676 |
> |
((c = (Set<?>)o) == this || |
4677 |
> |
(containsAll(c) && c.containsAll(this)))); |
4678 |
> |
} |
4679 |
> |
} |
4680 |
|
|
4681 |
|
/** |
4682 |
< |
* Save the state of the <tt>ConcurrentHashMap</tt> |
4683 |
< |
* instance to a stream (i.e., |
4684 |
< |
* serialize it). |
4685 |
< |
* @param s the stream |
4686 |
< |
* @serialData |
4687 |
< |
* the key (Object) and value (Object) |
4688 |
< |
* for each key-value mapping, followed by a null pair. |
1002 |
< |
* The key-value mappings are emitted in no particular order. |
4682 |
> |
* A view of a ConcurrentHashMap as a {@link Set} of keys, in |
4683 |
> |
* which additions may optionally be enabled by mapping to a |
4684 |
> |
* common value. This class cannot be directly instantiated. |
4685 |
> |
* See {@link #keySet() keySet()}, |
4686 |
> |
* {@link #keySet(Object) keySet(V)}, |
4687 |
> |
* {@link #newKeySet() newKeySet()}, |
4688 |
> |
* {@link #newKeySet(int) newKeySet(int)}. |
4689 |
|
*/ |
4690 |
< |
private void writeObject(java.io.ObjectOutputStream s) throws IOException { |
4691 |
< |
s.defaultWriteObject(); |
4690 |
> |
public static class KeySetView<K,V> |
4691 |
> |
extends CHMSetView<K,V,K> |
4692 |
> |
implements Set<K>, java.io.Serializable { |
4693 |
> |
private static final long serialVersionUID = 7249069246763182397L; |
4694 |
> |
private final V value; |
4695 |
> |
KeySetView(ConcurrentHashMap<K,V> map, V value) { // non-public |
4696 |
> |
super(map); |
4697 |
> |
this.value = value; |
4698 |
> |
} |
4699 |
|
|
4700 |
< |
for (int k = 0; k < segments.length; ++k) { |
4701 |
< |
Segment<K,V> seg = (Segment<K,V>)segments[k]; |
4702 |
< |
seg.lock(); |
4703 |
< |
try { |
4704 |
< |
HashEntry[] tab = seg.table; |
4705 |
< |
for (int i = 0; i < tab.length; ++i) { |
4706 |
< |
for (HashEntry<K,V> e = (HashEntry<K,V>)tab[i]; e != null; e = e.next) { |
4707 |
< |
s.writeObject(e.key); |
4708 |
< |
s.writeObject(e.value); |
4700 |
> |
/** |
4701 |
> |
* Returns the default mapped value for additions, |
4702 |
> |
* or {@code null} if additions are not supported. |
4703 |
> |
* |
4704 |
> |
* @return the default mapped value for additions, or {@code null} |
4705 |
> |
* if not supported |
4706 |
> |
*/ |
4707 |
> |
public V getMappedValue() { return value; } |
4708 |
> |
|
4709 |
> |
/** |
4710 |
> |
* {@inheritDoc} |
4711 |
> |
* @throws NullPointerException if the specified key is null |
4712 |
> |
*/ |
4713 |
> |
public boolean contains(Object o) { return map.containsKey(o); } |
4714 |
> |
|
4715 |
> |
/** |
4716 |
> |
* Removes the key from this map view, by removing the key (and its |
4717 |
> |
* corresponding value) from the backing map. This method does |
4718 |
> |
* nothing if the key is not in the map. |
4719 |
> |
* |
4720 |
> |
* @param o the key to be removed from the backing map |
4721 |
> |
* @return {@code true} if the backing map contained the specified key |
4722 |
> |
* @throws NullPointerException if the specified key is null |
4723 |
> |
*/ |
4724 |
> |
public boolean remove(Object o) { return map.remove(o) != null; } |
4725 |
> |
|
4726 |
> |
/** |
4727 |
> |
* @return an iterator over the keys of the backing map |
4728 |
> |
*/ |
4729 |
> |
public Iterator<K> iterator() { return new KeyIterator<K,V>(map); } |
4730 |
> |
|
4731 |
> |
/** |
4732 |
> |
* Adds the specified key to this set view by mapping the key to |
4733 |
> |
* the default mapped value in the backing map, if defined. |
4734 |
> |
* |
4735 |
> |
* @param e key to be added |
4736 |
> |
* @return {@code true} if this set changed as a result of the call |
4737 |
> |
* @throws NullPointerException if the specified key is null |
4738 |
> |
* @throws UnsupportedOperationException if no default mapped value |
4739 |
> |
* for additions was provided |
4740 |
> |
*/ |
4741 |
> |
public boolean add(K e) { |
4742 |
> |
V v; |
4743 |
> |
if ((v = value) == null) |
4744 |
> |
throw new UnsupportedOperationException(); |
4745 |
> |
return map.internalPut(e, v, true) == null; |
4746 |
> |
} |
4747 |
> |
|
4748 |
> |
/** |
4749 |
> |
* Adds all of the elements in the specified collection to this set, |
4750 |
> |
* as if by calling {@link #add} on each one. |
4751 |
> |
* |
4752 |
> |
* @param c the elements to be inserted into this set |
4753 |
> |
* @return {@code true} if this set changed as a result of the call |
4754 |
> |
* @throws NullPointerException if the collection or any of its |
4755 |
> |
* elements are {@code null} |
4756 |
> |
* @throws UnsupportedOperationException if no default mapped value |
4757 |
> |
* for additions was provided |
4758 |
> |
*/ |
4759 |
> |
public boolean addAll(Collection<? extends K> c) { |
4760 |
> |
boolean added = false; |
4761 |
> |
V v; |
4762 |
> |
if ((v = value) == null) |
4763 |
> |
throw new UnsupportedOperationException(); |
4764 |
> |
for (K e : c) { |
4765 |
> |
if (map.internalPut(e, v, true) == null) |
4766 |
> |
added = true; |
4767 |
> |
} |
4768 |
> |
return added; |
4769 |
> |
} |
4770 |
> |
|
4771 |
> |
Spliterator<K> spliterator() { |
4772 |
> |
return new KeyIterator<>(map, null); |
4773 |
> |
} |
4774 |
> |
|
4775 |
> |
public Stream<K> stream() { |
4776 |
> |
return Streams.stream(spliterator()); |
4777 |
> |
} |
4778 |
> |
public Stream<K> parallelStream() { |
4779 |
> |
return Streams.parallelStream(spliterator()); |
4780 |
> |
} |
4781 |
> |
} |
4782 |
> |
|
4783 |
> |
/** |
4784 |
> |
* A view of a ConcurrentHashMap as a {@link Collection} of |
4785 |
> |
* values, in which additions are disabled. This class cannot be |
4786 |
> |
* directly instantiated. See {@link #values()}. |
4787 |
> |
* |
4788 |
> |
* <p>The view's {@code iterator} is a "weakly consistent" iterator |
4789 |
> |
* that will never throw {@link ConcurrentModificationException}, |
4790 |
> |
* and guarantees to traverse elements as they existed upon |
4791 |
> |
* construction of the iterator, and may (but is not guaranteed to) |
4792 |
> |
* reflect any modifications subsequent to construction. |
4793 |
> |
*/ |
4794 |
> |
public static final class ValuesView<K,V> |
4795 |
> |
extends CHMCollectionView<K,V,V> |
4796 |
> |
implements Collection<V>, java.io.Serializable { |
4797 |
> |
private static final long serialVersionUID = 2249069246763182397L; |
4798 |
> |
ValuesView(ConcurrentHashMap<K,V> map) { super(map); } |
4799 |
> |
public final boolean contains(Object o) { |
4800 |
> |
return map.containsValue(o); |
4801 |
> |
} |
4802 |
> |
public final boolean remove(Object o) { |
4803 |
> |
if (o != null) { |
4804 |
> |
for (Iterator<V> it = iterator(); it.hasNext();) { |
4805 |
> |
if (o.equals(it.next())) { |
4806 |
> |
it.remove(); |
4807 |
> |
return true; |
4808 |
|
} |
4809 |
|
} |
4810 |
|
} |
4811 |
< |
finally { |
4812 |
< |
seg.unlock(); |
4811 |
> |
return false; |
4812 |
> |
} |
4813 |
> |
|
4814 |
> |
/** |
4815 |
> |
* @return an iterator over the values of the backing map |
4816 |
> |
*/ |
4817 |
> |
public final Iterator<V> iterator() { |
4818 |
> |
return new ValueIterator<K,V>(map); |
4819 |
> |
} |
4820 |
> |
|
4821 |
> |
/** Always throws {@link UnsupportedOperationException}. */ |
4822 |
> |
public final boolean add(V e) { |
4823 |
> |
throw new UnsupportedOperationException(); |
4824 |
> |
} |
4825 |
> |
/** Always throws {@link UnsupportedOperationException}. */ |
4826 |
> |
public final boolean addAll(Collection<? extends V> c) { |
4827 |
> |
throw new UnsupportedOperationException(); |
4828 |
> |
} |
4829 |
> |
|
4830 |
> |
Spliterator<V> spliterator() { |
4831 |
> |
return new ValueIterator<K,V>(map, null); |
4832 |
> |
} |
4833 |
> |
|
4834 |
> |
public Stream<V> stream() { |
4835 |
> |
return Streams.stream(spliterator()); |
4836 |
> |
} |
4837 |
> |
|
4838 |
> |
public Stream<V> parallelStream() { |
4839 |
> |
return Streams.parallelStream(spliterator()); |
4840 |
> |
} |
4841 |
> |
|
4842 |
> |
} |
4843 |
> |
|
4844 |
> |
/** |
4845 |
> |
* A view of a ConcurrentHashMap as a {@link Set} of (key, value) |
4846 |
> |
* entries. This class cannot be directly instantiated. See |
4847 |
> |
* {@link #entrySet()}. |
4848 |
> |
*/ |
4849 |
> |
public static final class EntrySetView<K,V> |
4850 |
> |
extends CHMSetView<K,V,Map.Entry<K,V>> |
4851 |
> |
implements Set<Map.Entry<K,V>>, java.io.Serializable { |
4852 |
> |
private static final long serialVersionUID = 2249069246763182397L; |
4853 |
> |
EntrySetView(ConcurrentHashMap<K,V> map) { super(map); } |
4854 |
> |
|
4855 |
> |
public final boolean contains(Object o) { |
4856 |
> |
Object k, v, r; Map.Entry<?,?> e; |
4857 |
> |
return ((o instanceof Map.Entry) && |
4858 |
> |
(k = (e = (Map.Entry<?,?>)o).getKey()) != null && |
4859 |
> |
(r = map.get(k)) != null && |
4860 |
> |
(v = e.getValue()) != null && |
4861 |
> |
(v == r || v.equals(r))); |
4862 |
> |
} |
4863 |
> |
public final boolean remove(Object o) { |
4864 |
> |
Object k, v; Map.Entry<?,?> e; |
4865 |
> |
return ((o instanceof Map.Entry) && |
4866 |
> |
(k = (e = (Map.Entry<?,?>)o).getKey()) != null && |
4867 |
> |
(v = e.getValue()) != null && |
4868 |
> |
map.remove(k, v)); |
4869 |
> |
} |
4870 |
> |
|
4871 |
> |
/** |
4872 |
> |
* @return an iterator over the entries of the backing map |
4873 |
> |
*/ |
4874 |
> |
public final Iterator<Map.Entry<K,V>> iterator() { |
4875 |
> |
return new EntryIterator<K,V>(map); |
4876 |
> |
} |
4877 |
> |
|
4878 |
> |
/** |
4879 |
> |
* Adds the specified mapping to this view. |
4880 |
> |
* |
4881 |
> |
* @param e mapping to be added |
4882 |
> |
* @return {@code true} if this set changed as a result of the call |
4883 |
> |
* @throws NullPointerException if the entry, its key, or its |
4884 |
> |
* value is null |
4885 |
> |
*/ |
4886 |
> |
public final boolean add(Entry<K,V> e) { |
4887 |
> |
return map.internalPut(e.getKey(), e.getValue(), false) == null; |
4888 |
> |
} |
4889 |
> |
/** |
4890 |
> |
* Adds all of the mappings in the specified collection to this |
4891 |
> |
* set, as if by calling {@link #add(Map.Entry)} on each one. |
4892 |
> |
* @param c the mappings to be inserted into this set |
4893 |
> |
* @return {@code true} if this set changed as a result of the call |
4894 |
> |
* @throws NullPointerException if the collection or any of its |
4895 |
> |
* entries, keys, or values are null |
4896 |
> |
*/ |
4897 |
> |
public final boolean addAll(Collection<? extends Entry<K,V>> c) { |
4898 |
> |
boolean added = false; |
4899 |
> |
for (Entry<K,V> e : c) { |
4900 |
> |
if (add(e)) |
4901 |
> |
added = true; |
4902 |
|
} |
4903 |
+ |
return added; |
4904 |
+ |
} |
4905 |
+ |
|
4906 |
+ |
Spliterator<Map.Entry<K,V>> spliterator() { |
4907 |
+ |
return new EntryIterator<K,V>(map, null); |
4908 |
+ |
} |
4909 |
+ |
|
4910 |
+ |
public Stream<Map.Entry<K,V>> stream() { |
4911 |
+ |
return Streams.stream(spliterator()); |
4912 |
+ |
} |
4913 |
+ |
|
4914 |
+ |
public Stream<Map.Entry<K,V>> parallelStream() { |
4915 |
+ |
return Streams.parallelStream(spliterator()); |
4916 |
|
} |
1023 |
– |
s.writeObject(null); |
1024 |
– |
s.writeObject(null); |
4917 |
|
} |
4918 |
|
|
4919 |
+ |
// --------------------------------------------------------------------- |
4920 |
+ |
|
4921 |
|
/** |
4922 |
< |
* Reconstitute the <tt>ConcurrentHashMap</tt> |
4923 |
< |
* instance from a stream (i.e., |
4924 |
< |
* deserialize it). |
4925 |
< |
* @param s the stream |
4922 |
> |
* Predefined tasks for performing bulk parallel operations on |
4923 |
> |
* ConcurrentHashMaps. These tasks follow the forms and rules used |
4924 |
> |
* for bulk operations. Each method has the same name, but returns |
4925 |
> |
* a task rather than invoking it. These methods may be useful in |
4926 |
> |
* custom applications such as submitting a task without waiting |
4927 |
> |
* for completion, using a custom pool, or combining with other |
4928 |
> |
* tasks. |
4929 |
|
*/ |
4930 |
< |
private void readObject(java.io.ObjectInputStream s) |
4931 |
< |
throws IOException, ClassNotFoundException { |
1035 |
< |
s.defaultReadObject(); |
4930 |
> |
public static class ForkJoinTasks { |
4931 |
> |
private ForkJoinTasks() {} |
4932 |
|
|
4933 |
< |
// Initialize each segment to be minimally sized, and let grow. |
4934 |
< |
for (int i = 0; i < segments.length; ++i) { |
4935 |
< |
segments[i].setTable(new HashEntry[1]); |
4933 |
> |
/** |
4934 |
> |
* Returns a task that when invoked, performs the given |
4935 |
> |
* action for each (key, value) |
4936 |
> |
* |
4937 |
> |
* @param map the map |
4938 |
> |
* @param action the action |
4939 |
> |
* @return the task |
4940 |
> |
*/ |
4941 |
> |
public static <K,V> ForkJoinTask<Void> forEach |
4942 |
> |
(ConcurrentHashMap<K,V> map, |
4943 |
> |
BiConsumer<? super K, ? super V> action) { |
4944 |
> |
if (action == null) throw new NullPointerException(); |
4945 |
> |
return new ForEachMappingTask<K,V>(map, null, -1, action); |
4946 |
|
} |
4947 |
|
|
4948 |
< |
// Read the keys and values, and put the mappings in the table |
4949 |
< |
for (;;) { |
4950 |
< |
K key = (K) s.readObject(); |
4951 |
< |
V value = (V) s.readObject(); |
4952 |
< |
if (key == null) |
4953 |
< |
break; |
4954 |
< |
put(key, value); |
4948 |
> |
/** |
4949 |
> |
* Returns a task that when invoked, performs the given |
4950 |
> |
* action for each non-null transformation of each (key, value) |
4951 |
> |
* |
4952 |
> |
* @param map the map |
4953 |
> |
* @param transformer a function returning the transformation |
4954 |
> |
* for an element, or null if there is no transformation (in |
4955 |
> |
* which case the action is not applied) |
4956 |
> |
* @param action the action |
4957 |
> |
* @return the task |
4958 |
> |
*/ |
4959 |
> |
public static <K,V,U> ForkJoinTask<Void> forEach |
4960 |
> |
(ConcurrentHashMap<K,V> map, |
4961 |
> |
BiFunction<? super K, ? super V, ? extends U> transformer, |
4962 |
> |
Consumer<? super U> action) { |
4963 |
> |
if (transformer == null || action == null) |
4964 |
> |
throw new NullPointerException(); |
4965 |
> |
return new ForEachTransformedMappingTask<K,V,U> |
4966 |
> |
(map, null, -1, transformer, action); |
4967 |
> |
} |
4968 |
> |
|
4969 |
> |
/** |
4970 |
> |
* Returns a task that when invoked, returns a non-null result |
4971 |
> |
* from applying the given search function on each (key, |
4972 |
> |
* value), or null if none. Upon success, further element |
4973 |
> |
* processing is suppressed and the results of any other |
4974 |
> |
* parallel invocations of the search function are ignored. |
4975 |
> |
* |
4976 |
> |
* @param map the map |
4977 |
> |
* @param searchFunction a function returning a non-null |
4978 |
> |
* result on success, else null |
4979 |
> |
* @return the task |
4980 |
> |
*/ |
4981 |
> |
public static <K,V,U> ForkJoinTask<U> search |
4982 |
> |
(ConcurrentHashMap<K,V> map, |
4983 |
> |
BiFunction<? super K, ? super V, ? extends U> searchFunction) { |
4984 |
> |
if (searchFunction == null) throw new NullPointerException(); |
4985 |
> |
return new SearchMappingsTask<K,V,U> |
4986 |
> |
(map, null, -1, searchFunction, |
4987 |
> |
new AtomicReference<U>()); |
4988 |
> |
} |
4989 |
> |
|
4990 |
> |
/** |
4991 |
> |
* Returns a task that when invoked, returns the result of |
4992 |
> |
* accumulating the given transformation of all (key, value) pairs |
4993 |
> |
* using the given reducer to combine values, or null if none. |
4994 |
> |
* |
4995 |
> |
* @param map the map |
4996 |
> |
* @param transformer a function returning the transformation |
4997 |
> |
* for an element, or null if there is no transformation (in |
4998 |
> |
* which case it is not combined) |
4999 |
> |
* @param reducer a commutative associative combining function |
5000 |
> |
* @return the task |
5001 |
> |
*/ |
5002 |
> |
public static <K,V,U> ForkJoinTask<U> reduce |
5003 |
> |
(ConcurrentHashMap<K,V> map, |
5004 |
> |
BiFunction<? super K, ? super V, ? extends U> transformer, |
5005 |
> |
BiFunction<? super U, ? super U, ? extends U> reducer) { |
5006 |
> |
if (transformer == null || reducer == null) |
5007 |
> |
throw new NullPointerException(); |
5008 |
> |
return new MapReduceMappingsTask<K,V,U> |
5009 |
> |
(map, null, -1, null, transformer, reducer); |
5010 |
> |
} |
5011 |
> |
|
5012 |
> |
/** |
5013 |
> |
* Returns a task that when invoked, returns the result of |
5014 |
> |
* accumulating the given transformation of all (key, value) pairs |
5015 |
> |
* using the given reducer to combine values, and the given |
5016 |
> |
* basis as an identity value. |
5017 |
> |
* |
5018 |
> |
* @param map the map |
5019 |
> |
* @param transformer a function returning the transformation |
5020 |
> |
* for an element |
5021 |
> |
* @param basis the identity (initial default value) for the reduction |
5022 |
> |
* @param reducer a commutative associative combining function |
5023 |
> |
* @return the task |
5024 |
> |
*/ |
5025 |
> |
public static <K,V> ForkJoinTask<Double> reduceToDouble |
5026 |
> |
(ConcurrentHashMap<K,V> map, |
5027 |
> |
ToDoubleBiFunction<? super K, ? super V> transformer, |
5028 |
> |
double basis, |
5029 |
> |
DoubleBinaryOperator reducer) { |
5030 |
> |
if (transformer == null || reducer == null) |
5031 |
> |
throw new NullPointerException(); |
5032 |
> |
return new MapReduceMappingsToDoubleTask<K,V> |
5033 |
> |
(map, null, -1, null, transformer, basis, reducer); |
5034 |
> |
} |
5035 |
> |
|
5036 |
> |
/** |
5037 |
> |
* Returns a task that when invoked, returns the result of |
5038 |
> |
* accumulating the given transformation of all (key, value) pairs |
5039 |
> |
* using the given reducer to combine values, and the given |
5040 |
> |
* basis as an identity value. |
5041 |
> |
* |
5042 |
> |
* @param map the map |
5043 |
> |
* @param transformer a function returning the transformation |
5044 |
> |
* for an element |
5045 |
> |
* @param basis the identity (initial default value) for the reduction |
5046 |
> |
* @param reducer a commutative associative combining function |
5047 |
> |
* @return the task |
5048 |
> |
*/ |
5049 |
> |
public static <K,V> ForkJoinTask<Long> reduceToLong |
5050 |
> |
(ConcurrentHashMap<K,V> map, |
5051 |
> |
ToLongBiFunction<? super K, ? super V> transformer, |
5052 |
> |
long basis, |
5053 |
> |
LongBinaryOperator reducer) { |
5054 |
> |
if (transformer == null || reducer == null) |
5055 |
> |
throw new NullPointerException(); |
5056 |
> |
return new MapReduceMappingsToLongTask<K,V> |
5057 |
> |
(map, null, -1, null, transformer, basis, reducer); |
5058 |
> |
} |
5059 |
> |
|
5060 |
> |
/** |
5061 |
> |
* Returns a task that when invoked, returns the result of |
5062 |
> |
* accumulating the given transformation of all (key, value) pairs |
5063 |
> |
* using the given reducer to combine values, and the given |
5064 |
> |
* basis as an identity value. |
5065 |
> |
* |
5066 |
> |
* @param map the map |
5067 |
> |
* @param transformer a function returning the transformation |
5068 |
> |
* for an element |
5069 |
> |
* @param basis the identity (initial default value) for the reduction |
5070 |
> |
* @param reducer a commutative associative combining function |
5071 |
> |
* @return the task |
5072 |
> |
*/ |
5073 |
> |
public static <K,V> ForkJoinTask<Integer> reduceToInt |
5074 |
> |
(ConcurrentHashMap<K,V> map, |
5075 |
> |
ToIntBiFunction<? super K, ? super V> transformer, |
5076 |
> |
int basis, |
5077 |
> |
IntBinaryOperator reducer) { |
5078 |
> |
if (transformer == null || reducer == null) |
5079 |
> |
throw new NullPointerException(); |
5080 |
> |
return new MapReduceMappingsToIntTask<K,V> |
5081 |
> |
(map, null, -1, null, transformer, basis, reducer); |
5082 |
> |
} |
5083 |
> |
|
5084 |
> |
/** |
5085 |
> |
* Returns a task that when invoked, performs the given action |
5086 |
> |
* for each key. |
5087 |
> |
* |
5088 |
> |
* @param map the map |
5089 |
> |
* @param action the action |
5090 |
> |
* @return the task |
5091 |
> |
*/ |
5092 |
> |
public static <K,V> ForkJoinTask<Void> forEachKey |
5093 |
> |
(ConcurrentHashMap<K,V> map, |
5094 |
> |
Consumer<? super K> action) { |
5095 |
> |
if (action == null) throw new NullPointerException(); |
5096 |
> |
return new ForEachKeyTask<K,V>(map, null, -1, action); |
5097 |
> |
} |
5098 |
> |
|
5099 |
> |
/** |
5100 |
> |
* Returns a task that when invoked, performs the given action |
5101 |
> |
* for each non-null transformation of each key. |
5102 |
> |
* |
5103 |
> |
* @param map the map |
5104 |
> |
* @param transformer a function returning the transformation |
5105 |
> |
* for an element, or null if there is no transformation (in |
5106 |
> |
* which case the action is not applied) |
5107 |
> |
* @param action the action |
5108 |
> |
* @return the task |
5109 |
> |
*/ |
5110 |
> |
public static <K,V,U> ForkJoinTask<Void> forEachKey |
5111 |
> |
(ConcurrentHashMap<K,V> map, |
5112 |
> |
Function<? super K, ? extends U> transformer, |
5113 |
> |
Consumer<? super U> action) { |
5114 |
> |
if (transformer == null || action == null) |
5115 |
> |
throw new NullPointerException(); |
5116 |
> |
return new ForEachTransformedKeyTask<K,V,U> |
5117 |
> |
(map, null, -1, transformer, action); |
5118 |
> |
} |
5119 |
> |
|
5120 |
> |
/** |
5121 |
> |
* Returns a task that when invoked, returns a non-null result |
5122 |
> |
* from applying the given search function on each key, or |
5123 |
> |
* null if none. Upon success, further element processing is |
5124 |
> |
* suppressed and the results of any other parallel |
5125 |
> |
* invocations of the search function are ignored. |
5126 |
> |
* |
5127 |
> |
* @param map the map |
5128 |
> |
* @param searchFunction a function returning a non-null |
5129 |
> |
* result on success, else null |
5130 |
> |
* @return the task |
5131 |
> |
*/ |
5132 |
> |
public static <K,V,U> ForkJoinTask<U> searchKeys |
5133 |
> |
(ConcurrentHashMap<K,V> map, |
5134 |
> |
Function<? super K, ? extends U> searchFunction) { |
5135 |
> |
if (searchFunction == null) throw new NullPointerException(); |
5136 |
> |
return new SearchKeysTask<K,V,U> |
5137 |
> |
(map, null, -1, searchFunction, |
5138 |
> |
new AtomicReference<U>()); |
5139 |
> |
} |
5140 |
> |
|
5141 |
> |
/** |
5142 |
> |
* Returns a task that when invoked, returns the result of |
5143 |
> |
* accumulating all keys using the given reducer to combine |
5144 |
> |
* values, or null if none. |
5145 |
> |
* |
5146 |
> |
* @param map the map |
5147 |
> |
* @param reducer a commutative associative combining function |
5148 |
> |
* @return the task |
5149 |
> |
*/ |
5150 |
> |
public static <K,V> ForkJoinTask<K> reduceKeys |
5151 |
> |
(ConcurrentHashMap<K,V> map, |
5152 |
> |
BiFunction<? super K, ? super K, ? extends K> reducer) { |
5153 |
> |
if (reducer == null) throw new NullPointerException(); |
5154 |
> |
return new ReduceKeysTask<K,V> |
5155 |
> |
(map, null, -1, null, reducer); |
5156 |
> |
} |
5157 |
> |
|
5158 |
> |
/** |
5159 |
> |
* Returns a task that when invoked, returns the result of |
5160 |
> |
* accumulating the given transformation of all keys using the given |
5161 |
> |
* reducer to combine values, or null if none. |
5162 |
> |
* |
5163 |
> |
* @param map the map |
5164 |
> |
* @param transformer a function returning the transformation |
5165 |
> |
* for an element, or null if there is no transformation (in |
5166 |
> |
* which case it is not combined) |
5167 |
> |
* @param reducer a commutative associative combining function |
5168 |
> |
* @return the task |
5169 |
> |
*/ |
5170 |
> |
public static <K,V,U> ForkJoinTask<U> reduceKeys |
5171 |
> |
(ConcurrentHashMap<K,V> map, |
5172 |
> |
Function<? super K, ? extends U> transformer, |
5173 |
> |
BiFunction<? super U, ? super U, ? extends U> reducer) { |
5174 |
> |
if (transformer == null || reducer == null) |
5175 |
> |
throw new NullPointerException(); |
5176 |
> |
return new MapReduceKeysTask<K,V,U> |
5177 |
> |
(map, null, -1, null, transformer, reducer); |
5178 |
> |
} |
5179 |
> |
|
5180 |
> |
/** |
5181 |
> |
* Returns a task that when invoked, returns the result of |
5182 |
> |
* accumulating the given transformation of all keys using the given |
5183 |
> |
* reducer to combine values, and the given basis as an |
5184 |
> |
* identity value. |
5185 |
> |
* |
5186 |
> |
* @param map the map |
5187 |
> |
* @param transformer a function returning the transformation |
5188 |
> |
* for an element |
5189 |
> |
* @param basis the identity (initial default value) for the reduction |
5190 |
> |
* @param reducer a commutative associative combining function |
5191 |
> |
* @return the task |
5192 |
> |
*/ |
5193 |
> |
public static <K,V> ForkJoinTask<Double> reduceKeysToDouble |
5194 |
> |
(ConcurrentHashMap<K,V> map, |
5195 |
> |
ToDoubleFunction<? super K> transformer, |
5196 |
> |
double basis, |
5197 |
> |
DoubleBinaryOperator reducer) { |
5198 |
> |
if (transformer == null || reducer == null) |
5199 |
> |
throw new NullPointerException(); |
5200 |
> |
return new MapReduceKeysToDoubleTask<K,V> |
5201 |
> |
(map, null, -1, null, transformer, basis, reducer); |
5202 |
> |
} |
5203 |
> |
|
5204 |
> |
/** |
5205 |
> |
* Returns a task that when invoked, returns the result of |
5206 |
> |
* accumulating the given transformation of all keys using the given |
5207 |
> |
* reducer to combine values, and the given basis as an |
5208 |
> |
* identity value. |
5209 |
> |
* |
5210 |
> |
* @param map the map |
5211 |
> |
* @param transformer a function returning the transformation |
5212 |
> |
* for an element |
5213 |
> |
* @param basis the identity (initial default value) for the reduction |
5214 |
> |
* @param reducer a commutative associative combining function |
5215 |
> |
* @return the task |
5216 |
> |
*/ |
5217 |
> |
public static <K,V> ForkJoinTask<Long> reduceKeysToLong |
5218 |
> |
(ConcurrentHashMap<K,V> map, |
5219 |
> |
ToLongFunction<? super K> transformer, |
5220 |
> |
long basis, |
5221 |
> |
LongBinaryOperator reducer) { |
5222 |
> |
if (transformer == null || reducer == null) |
5223 |
> |
throw new NullPointerException(); |
5224 |
> |
return new MapReduceKeysToLongTask<K,V> |
5225 |
> |
(map, null, -1, null, transformer, basis, reducer); |
5226 |
> |
} |
5227 |
> |
|
5228 |
> |
/** |
5229 |
> |
* Returns a task that when invoked, returns the result of |
5230 |
> |
* accumulating the given transformation of all keys using the given |
5231 |
> |
* reducer to combine values, and the given basis as an |
5232 |
> |
* identity value. |
5233 |
> |
* |
5234 |
> |
* @param map the map |
5235 |
> |
* @param transformer a function returning the transformation |
5236 |
> |
* for an element |
5237 |
> |
* @param basis the identity (initial default value) for the reduction |
5238 |
> |
* @param reducer a commutative associative combining function |
5239 |
> |
* @return the task |
5240 |
> |
*/ |
5241 |
> |
public static <K,V> ForkJoinTask<Integer> reduceKeysToInt |
5242 |
> |
(ConcurrentHashMap<K,V> map, |
5243 |
> |
ToIntFunction<? super K> transformer, |
5244 |
> |
int basis, |
5245 |
> |
IntBinaryOperator reducer) { |
5246 |
> |
if (transformer == null || reducer == null) |
5247 |
> |
throw new NullPointerException(); |
5248 |
> |
return new MapReduceKeysToIntTask<K,V> |
5249 |
> |
(map, null, -1, null, transformer, basis, reducer); |
5250 |
> |
} |
5251 |
> |
|
5252 |
> |
/** |
5253 |
> |
* Returns a task that when invoked, performs the given action |
5254 |
> |
* for each value. |
5255 |
> |
* |
5256 |
> |
* @param map the map |
5257 |
> |
* @param action the action |
5258 |
> |
* @return the task |
5259 |
> |
*/ |
5260 |
> |
public static <K,V> ForkJoinTask<Void> forEachValue |
5261 |
> |
(ConcurrentHashMap<K,V> map, |
5262 |
> |
Consumer<? super V> action) { |
5263 |
> |
if (action == null) throw new NullPointerException(); |
5264 |
> |
return new ForEachValueTask<K,V>(map, null, -1, action); |
5265 |
> |
} |
5266 |
> |
|
5267 |
> |
/** |
5268 |
> |
* Returns a task that when invoked, performs the given action |
5269 |
> |
* for each non-null transformation of each value. |
5270 |
> |
* |
5271 |
> |
* @param map the map |
5272 |
> |
* @param transformer a function returning the transformation |
5273 |
> |
* for an element, or null if there is no transformation (in |
5274 |
> |
* which case the action is not applied) |
5275 |
> |
* @param action the action |
5276 |
> |
* @return the task |
5277 |
> |
*/ |
5278 |
> |
public static <K,V,U> ForkJoinTask<Void> forEachValue |
5279 |
> |
(ConcurrentHashMap<K,V> map, |
5280 |
> |
Function<? super V, ? extends U> transformer, |
5281 |
> |
Consumer<? super U> action) { |
5282 |
> |
if (transformer == null || action == null) |
5283 |
> |
throw new NullPointerException(); |
5284 |
> |
return new ForEachTransformedValueTask<K,V,U> |
5285 |
> |
(map, null, -1, transformer, action); |
5286 |
> |
} |
5287 |
> |
|
5288 |
> |
/** |
5289 |
> |
* Returns a task that when invoked, returns a non-null result |
5290 |
> |
* from applying the given search function on each value, or |
5291 |
> |
* null if none. Upon success, further element processing is |
5292 |
> |
* suppressed and the results of any other parallel |
5293 |
> |
* invocations of the search function are ignored. |
5294 |
> |
* |
5295 |
> |
* @param map the map |
5296 |
> |
* @param searchFunction a function returning a non-null |
5297 |
> |
* result on success, else null |
5298 |
> |
* @return the task |
5299 |
> |
*/ |
5300 |
> |
public static <K,V,U> ForkJoinTask<U> searchValues |
5301 |
> |
(ConcurrentHashMap<K,V> map, |
5302 |
> |
Function<? super V, ? extends U> searchFunction) { |
5303 |
> |
if (searchFunction == null) throw new NullPointerException(); |
5304 |
> |
return new SearchValuesTask<K,V,U> |
5305 |
> |
(map, null, -1, searchFunction, |
5306 |
> |
new AtomicReference<U>()); |
5307 |
> |
} |
5308 |
> |
|
5309 |
> |
/** |
5310 |
> |
* Returns a task that when invoked, returns the result of |
5311 |
> |
* accumulating all values using the given reducer to combine |
5312 |
> |
* values, or null if none. |
5313 |
> |
* |
5314 |
> |
* @param map the map |
5315 |
> |
* @param reducer a commutative associative combining function |
5316 |
> |
* @return the task |
5317 |
> |
*/ |
5318 |
> |
public static <K,V> ForkJoinTask<V> reduceValues |
5319 |
> |
(ConcurrentHashMap<K,V> map, |
5320 |
> |
BiFunction<? super V, ? super V, ? extends V> reducer) { |
5321 |
> |
if (reducer == null) throw new NullPointerException(); |
5322 |
> |
return new ReduceValuesTask<K,V> |
5323 |
> |
(map, null, -1, null, reducer); |
5324 |
> |
} |
5325 |
> |
|
5326 |
> |
/** |
5327 |
> |
* Returns a task that when invoked, returns the result of |
5328 |
> |
* accumulating the given transformation of all values using the |
5329 |
> |
* given reducer to combine values, or null if none. |
5330 |
> |
* |
5331 |
> |
* @param map the map |
5332 |
> |
* @param transformer a function returning the transformation |
5333 |
> |
* for an element, or null if there is no transformation (in |
5334 |
> |
* which case it is not combined) |
5335 |
> |
* @param reducer a commutative associative combining function |
5336 |
> |
* @return the task |
5337 |
> |
*/ |
5338 |
> |
public static <K,V,U> ForkJoinTask<U> reduceValues |
5339 |
> |
(ConcurrentHashMap<K,V> map, |
5340 |
> |
Function<? super V, ? extends U> transformer, |
5341 |
> |
BiFunction<? super U, ? super U, ? extends U> reducer) { |
5342 |
> |
if (transformer == null || reducer == null) |
5343 |
> |
throw new NullPointerException(); |
5344 |
> |
return new MapReduceValuesTask<K,V,U> |
5345 |
> |
(map, null, -1, null, transformer, reducer); |
5346 |
> |
} |
5347 |
> |
|
5348 |
> |
/** |
5349 |
> |
* Returns a task that when invoked, returns the result of |
5350 |
> |
* accumulating the given transformation of all values using the |
5351 |
> |
* given reducer to combine values, and the given basis as an |
5352 |
> |
* identity value. |
5353 |
> |
* |
5354 |
> |
* @param map the map |
5355 |
> |
* @param transformer a function returning the transformation |
5356 |
> |
* for an element |
5357 |
> |
* @param basis the identity (initial default value) for the reduction |
5358 |
> |
* @param reducer a commutative associative combining function |
5359 |
> |
* @return the task |
5360 |
> |
*/ |
5361 |
> |
public static <K,V> ForkJoinTask<Double> reduceValuesToDouble |
5362 |
> |
(ConcurrentHashMap<K,V> map, |
5363 |
> |
ToDoubleFunction<? super V> transformer, |
5364 |
> |
double basis, |
5365 |
> |
DoubleBinaryOperator reducer) { |
5366 |
> |
if (transformer == null || reducer == null) |
5367 |
> |
throw new NullPointerException(); |
5368 |
> |
return new MapReduceValuesToDoubleTask<K,V> |
5369 |
> |
(map, null, -1, null, transformer, basis, reducer); |
5370 |
> |
} |
5371 |
> |
|
5372 |
> |
/** |
5373 |
> |
* Returns a task that when invoked, returns the result of |
5374 |
> |
* accumulating the given transformation of all values using the |
5375 |
> |
* given reducer to combine values, and the given basis as an |
5376 |
> |
* identity value. |
5377 |
> |
* |
5378 |
> |
* @param map the map |
5379 |
> |
* @param transformer a function returning the transformation |
5380 |
> |
* for an element |
5381 |
> |
* @param basis the identity (initial default value) for the reduction |
5382 |
> |
* @param reducer a commutative associative combining function |
5383 |
> |
* @return the task |
5384 |
> |
*/ |
5385 |
> |
public static <K,V> ForkJoinTask<Long> reduceValuesToLong |
5386 |
> |
(ConcurrentHashMap<K,V> map, |
5387 |
> |
ToLongFunction<? super V> transformer, |
5388 |
> |
long basis, |
5389 |
> |
LongBinaryOperator reducer) { |
5390 |
> |
if (transformer == null || reducer == null) |
5391 |
> |
throw new NullPointerException(); |
5392 |
> |
return new MapReduceValuesToLongTask<K,V> |
5393 |
> |
(map, null, -1, null, transformer, basis, reducer); |
5394 |
> |
} |
5395 |
> |
|
5396 |
> |
/** |
5397 |
> |
* Returns a task that when invoked, returns the result of |
5398 |
> |
* accumulating the given transformation of all values using the |
5399 |
> |
* given reducer to combine values, and the given basis as an |
5400 |
> |
* identity value. |
5401 |
> |
* |
5402 |
> |
* @param map the map |
5403 |
> |
* @param transformer a function returning the transformation |
5404 |
> |
* for an element |
5405 |
> |
* @param basis the identity (initial default value) for the reduction |
5406 |
> |
* @param reducer a commutative associative combining function |
5407 |
> |
* @return the task |
5408 |
> |
*/ |
5409 |
> |
public static <K,V> ForkJoinTask<Integer> reduceValuesToInt |
5410 |
> |
(ConcurrentHashMap<K,V> map, |
5411 |
> |
ToIntFunction<? super V> transformer, |
5412 |
> |
int basis, |
5413 |
> |
IntBinaryOperator reducer) { |
5414 |
> |
if (transformer == null || reducer == null) |
5415 |
> |
throw new NullPointerException(); |
5416 |
> |
return new MapReduceValuesToIntTask<K,V> |
5417 |
> |
(map, null, -1, null, transformer, basis, reducer); |
5418 |
> |
} |
5419 |
> |
|
5420 |
> |
/** |
5421 |
> |
* Returns a task that when invoked, perform the given action |
5422 |
> |
* for each entry. |
5423 |
> |
* |
5424 |
> |
* @param map the map |
5425 |
> |
* @param action the action |
5426 |
> |
* @return the task |
5427 |
> |
*/ |
5428 |
> |
public static <K,V> ForkJoinTask<Void> forEachEntry |
5429 |
> |
(ConcurrentHashMap<K,V> map, |
5430 |
> |
Consumer<? super Map.Entry<K,V>> action) { |
5431 |
> |
if (action == null) throw new NullPointerException(); |
5432 |
> |
return new ForEachEntryTask<K,V>(map, null, -1, action); |
5433 |
> |
} |
5434 |
> |
|
5435 |
> |
/** |
5436 |
> |
* Returns a task that when invoked, perform the given action |
5437 |
> |
* for each non-null transformation of each entry. |
5438 |
> |
* |
5439 |
> |
* @param map the map |
5440 |
> |
* @param transformer a function returning the transformation |
5441 |
> |
* for an element, or null if there is no transformation (in |
5442 |
> |
* which case the action is not applied) |
5443 |
> |
* @param action the action |
5444 |
> |
* @return the task |
5445 |
> |
*/ |
5446 |
> |
public static <K,V,U> ForkJoinTask<Void> forEachEntry |
5447 |
> |
(ConcurrentHashMap<K,V> map, |
5448 |
> |
Function<Map.Entry<K,V>, ? extends U> transformer, |
5449 |
> |
Consumer<? super U> action) { |
5450 |
> |
if (transformer == null || action == null) |
5451 |
> |
throw new NullPointerException(); |
5452 |
> |
return new ForEachTransformedEntryTask<K,V,U> |
5453 |
> |
(map, null, -1, transformer, action); |
5454 |
> |
} |
5455 |
> |
|
5456 |
> |
/** |
5457 |
> |
* Returns a task that when invoked, returns a non-null result |
5458 |
> |
* from applying the given search function on each entry, or |
5459 |
> |
* null if none. Upon success, further element processing is |
5460 |
> |
* suppressed and the results of any other parallel |
5461 |
> |
* invocations of the search function are ignored. |
5462 |
> |
* |
5463 |
> |
* @param map the map |
5464 |
> |
* @param searchFunction a function returning a non-null |
5465 |
> |
* result on success, else null |
5466 |
> |
* @return the task |
5467 |
> |
*/ |
5468 |
> |
public static <K,V,U> ForkJoinTask<U> searchEntries |
5469 |
> |
(ConcurrentHashMap<K,V> map, |
5470 |
> |
Function<Map.Entry<K,V>, ? extends U> searchFunction) { |
5471 |
> |
if (searchFunction == null) throw new NullPointerException(); |
5472 |
> |
return new SearchEntriesTask<K,V,U> |
5473 |
> |
(map, null, -1, searchFunction, |
5474 |
> |
new AtomicReference<U>()); |
5475 |
> |
} |
5476 |
> |
|
5477 |
> |
/** |
5478 |
> |
* Returns a task that when invoked, returns the result of |
5479 |
> |
* accumulating all entries using the given reducer to combine |
5480 |
> |
* values, or null if none. |
5481 |
> |
* |
5482 |
> |
* @param map the map |
5483 |
> |
* @param reducer a commutative associative combining function |
5484 |
> |
* @return the task |
5485 |
> |
*/ |
5486 |
> |
public static <K,V> ForkJoinTask<Map.Entry<K,V>> reduceEntries |
5487 |
> |
(ConcurrentHashMap<K,V> map, |
5488 |
> |
BiFunction<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) { |
5489 |
> |
if (reducer == null) throw new NullPointerException(); |
5490 |
> |
return new ReduceEntriesTask<K,V> |
5491 |
> |
(map, null, -1, null, reducer); |
5492 |
> |
} |
5493 |
> |
|
5494 |
> |
/** |
5495 |
> |
* Returns a task that when invoked, returns the result of |
5496 |
> |
* accumulating the given transformation of all entries using the |
5497 |
> |
* given reducer to combine values, or null if none. |
5498 |
> |
* |
5499 |
> |
* @param map the map |
5500 |
> |
* @param transformer a function returning the transformation |
5501 |
> |
* for an element, or null if there is no transformation (in |
5502 |
> |
* which case it is not combined) |
5503 |
> |
* @param reducer a commutative associative combining function |
5504 |
> |
* @return the task |
5505 |
> |
*/ |
5506 |
> |
public static <K,V,U> ForkJoinTask<U> reduceEntries |
5507 |
> |
(ConcurrentHashMap<K,V> map, |
5508 |
> |
Function<Map.Entry<K,V>, ? extends U> transformer, |
5509 |
> |
BiFunction<? super U, ? super U, ? extends U> reducer) { |
5510 |
> |
if (transformer == null || reducer == null) |
5511 |
> |
throw new NullPointerException(); |
5512 |
> |
return new MapReduceEntriesTask<K,V,U> |
5513 |
> |
(map, null, -1, null, transformer, reducer); |
5514 |
> |
} |
5515 |
> |
|
5516 |
> |
/** |
5517 |
> |
* Returns a task that when invoked, returns the result of |
5518 |
> |
* accumulating the given transformation of all entries using the |
5519 |
> |
* given reducer to combine values, and the given basis as an |
5520 |
> |
* identity value. |
5521 |
> |
* |
5522 |
> |
* @param map the map |
5523 |
> |
* @param transformer a function returning the transformation |
5524 |
> |
* for an element |
5525 |
> |
* @param basis the identity (initial default value) for the reduction |
5526 |
> |
* @param reducer a commutative associative combining function |
5527 |
> |
* @return the task |
5528 |
> |
*/ |
5529 |
> |
public static <K,V> ForkJoinTask<Double> reduceEntriesToDouble |
5530 |
> |
(ConcurrentHashMap<K,V> map, |
5531 |
> |
ToDoubleFunction<Map.Entry<K,V>> transformer, |
5532 |
> |
double basis, |
5533 |
> |
DoubleBinaryOperator reducer) { |
5534 |
> |
if (transformer == null || reducer == null) |
5535 |
> |
throw new NullPointerException(); |
5536 |
> |
return new MapReduceEntriesToDoubleTask<K,V> |
5537 |
> |
(map, null, -1, null, transformer, basis, reducer); |
5538 |
> |
} |
5539 |
> |
|
5540 |
> |
/** |
5541 |
> |
* Returns a task that when invoked, returns the result of |
5542 |
> |
* accumulating the given transformation of all entries using the |
5543 |
> |
* given reducer to combine values, and the given basis as an |
5544 |
> |
* identity value. |
5545 |
> |
* |
5546 |
> |
* @param map the map |
5547 |
> |
* @param transformer a function returning the transformation |
5548 |
> |
* for an element |
5549 |
> |
* @param basis the identity (initial default value) for the reduction |
5550 |
> |
* @param reducer a commutative associative combining function |
5551 |
> |
* @return the task |
5552 |
> |
*/ |
5553 |
> |
public static <K,V> ForkJoinTask<Long> reduceEntriesToLong |
5554 |
> |
(ConcurrentHashMap<K,V> map, |
5555 |
> |
ToLongFunction<Map.Entry<K,V>> transformer, |
5556 |
> |
long basis, |
5557 |
> |
LongBinaryOperator reducer) { |
5558 |
> |
if (transformer == null || reducer == null) |
5559 |
> |
throw new NullPointerException(); |
5560 |
> |
return new MapReduceEntriesToLongTask<K,V> |
5561 |
> |
(map, null, -1, null, transformer, basis, reducer); |
5562 |
> |
} |
5563 |
> |
|
5564 |
> |
/** |
5565 |
> |
* Returns a task that when invoked, returns the result of |
5566 |
> |
* accumulating the given transformation of all entries using the |
5567 |
> |
* given reducer to combine values, and the given basis as an |
5568 |
> |
* identity value. |
5569 |
> |
* |
5570 |
> |
* @param map the map |
5571 |
> |
* @param transformer a function returning the transformation |
5572 |
> |
* for an element |
5573 |
> |
* @param basis the identity (initial default value) for the reduction |
5574 |
> |
* @param reducer a commutative associative combining function |
5575 |
> |
* @return the task |
5576 |
> |
*/ |
5577 |
> |
public static <K,V> ForkJoinTask<Integer> reduceEntriesToInt |
5578 |
> |
(ConcurrentHashMap<K,V> map, |
5579 |
> |
ToIntFunction<Map.Entry<K,V>> transformer, |
5580 |
> |
int basis, |
5581 |
> |
IntBinaryOperator reducer) { |
5582 |
> |
if (transformer == null || reducer == null) |
5583 |
> |
throw new NullPointerException(); |
5584 |
> |
return new MapReduceEntriesToIntTask<K,V> |
5585 |
> |
(map, null, -1, null, transformer, basis, reducer); |
5586 |
> |
} |
5587 |
> |
} |
5588 |
> |
|
5589 |
> |
// ------------------------------------------------------- |
5590 |
> |
|
5591 |
> |
/* |
5592 |
> |
* Task classes. Coded in a regular but ugly format/style to |
5593 |
> |
* simplify checks that each variant differs in the right way from |
5594 |
> |
* others. The null screenings exist because compilers cannot tell |
5595 |
> |
* that we've already null-checked task arguments, so we force |
5596 |
> |
* simplest hoisted bypass to help avoid convoluted traps. |
5597 |
> |
*/ |
5598 |
> |
|
5599 |
> |
@SuppressWarnings("serial") static final class ForEachKeyTask<K,V> |
5600 |
> |
extends Traverser<K,V,Void> { |
5601 |
> |
final Consumer<? super K> action; |
5602 |
> |
ForEachKeyTask |
5603 |
> |
(ConcurrentHashMap<K,V> m, Traverser<K,V,?> p, int b, |
5604 |
> |
Consumer<? super K> action) { |
5605 |
> |
super(m, p, b); |
5606 |
> |
this.action = action; |
5607 |
> |
} |
5608 |
> |
public final void compute() { |
5609 |
> |
final Consumer<? super K> action; |
5610 |
> |
if ((action = this.action) != null) { |
5611 |
> |
for (int b; (b = preSplit()) > 0;) |
5612 |
> |
new ForEachKeyTask<K,V>(map, this, b, action).fork(); |
5613 |
> |
K k; |
5614 |
> |
while ((k = advanceKey()) != null) |
5615 |
> |
action.accept(k); |
5616 |
> |
propagateCompletion(); |
5617 |
> |
} |
5618 |
> |
} |
5619 |
> |
} |
5620 |
> |
|
5621 |
> |
@SuppressWarnings("serial") static final class ForEachValueTask<K,V> |
5622 |
> |
extends Traverser<K,V,Void> { |
5623 |
> |
final Consumer<? super V> action; |
5624 |
> |
ForEachValueTask |
5625 |
> |
(ConcurrentHashMap<K,V> m, Traverser<K,V,?> p, int b, |
5626 |
> |
Consumer<? super V> action) { |
5627 |
> |
super(m, p, b); |
5628 |
> |
this.action = action; |
5629 |
> |
} |
5630 |
> |
public final void compute() { |
5631 |
> |
final Consumer<? super V> action; |
5632 |
> |
if ((action = this.action) != null) { |
5633 |
> |
for (int b; (b = preSplit()) > 0;) |
5634 |
> |
new ForEachValueTask<K,V>(map, this, b, action).fork(); |
5635 |
> |
V v; |
5636 |
> |
while ((v = advanceValue()) != null) |
5637 |
> |
action.accept(v); |
5638 |
> |
propagateCompletion(); |
5639 |
> |
} |
5640 |
> |
} |
5641 |
> |
} |
5642 |
> |
|
5643 |
> |
@SuppressWarnings("serial") static final class ForEachEntryTask<K,V> |
5644 |
> |
extends Traverser<K,V,Void> { |
5645 |
> |
final Consumer<? super Entry<K,V>> action; |
5646 |
> |
ForEachEntryTask |
5647 |
> |
(ConcurrentHashMap<K,V> m, Traverser<K,V,?> p, int b, |
5648 |
> |
Consumer<? super Entry<K,V>> action) { |
5649 |
> |
super(m, p, b); |
5650 |
> |
this.action = action; |
5651 |
> |
} |
5652 |
> |
public final void compute() { |
5653 |
> |
final Consumer<? super Entry<K,V>> action; |
5654 |
> |
if ((action = this.action) != null) { |
5655 |
> |
for (int b; (b = preSplit()) > 0;) |
5656 |
> |
new ForEachEntryTask<K,V>(map, this, b, action).fork(); |
5657 |
> |
V v; |
5658 |
> |
while ((v = advanceValue()) != null) |
5659 |
> |
action.accept(entryFor(nextKey, v)); |
5660 |
> |
propagateCompletion(); |
5661 |
> |
} |
5662 |
> |
} |
5663 |
> |
} |
5664 |
> |
|
5665 |
> |
@SuppressWarnings("serial") static final class ForEachMappingTask<K,V> |
5666 |
> |
extends Traverser<K,V,Void> { |
5667 |
> |
final BiConsumer<? super K, ? super V> action; |
5668 |
> |
ForEachMappingTask |
5669 |
> |
(ConcurrentHashMap<K,V> m, Traverser<K,V,?> p, int b, |
5670 |
> |
BiConsumer<? super K,? super V> action) { |
5671 |
> |
super(m, p, b); |
5672 |
> |
this.action = action; |
5673 |
> |
} |
5674 |
> |
public final void compute() { |
5675 |
> |
final BiConsumer<? super K, ? super V> action; |
5676 |
> |
if ((action = this.action) != null) { |
5677 |
> |
for (int b; (b = preSplit()) > 0;) |
5678 |
> |
new ForEachMappingTask<K,V>(map, this, b, action).fork(); |
5679 |
> |
V v; |
5680 |
> |
while ((v = advanceValue()) != null) |
5681 |
> |
action.accept(nextKey, v); |
5682 |
> |
propagateCompletion(); |
5683 |
> |
} |
5684 |
> |
} |
5685 |
> |
} |
5686 |
> |
|
5687 |
> |
@SuppressWarnings("serial") static final class ForEachTransformedKeyTask<K,V,U> |
5688 |
> |
extends Traverser<K,V,Void> { |
5689 |
> |
final Function<? super K, ? extends U> transformer; |
5690 |
> |
final Consumer<? super U> action; |
5691 |
> |
ForEachTransformedKeyTask |
5692 |
> |
(ConcurrentHashMap<K,V> m, Traverser<K,V,?> p, int b, |
5693 |
> |
Function<? super K, ? extends U> transformer, Consumer<? super U> action) { |
5694 |
> |
super(m, p, b); |
5695 |
> |
this.transformer = transformer; this.action = action; |
5696 |
> |
} |
5697 |
> |
public final void compute() { |
5698 |
> |
final Function<? super K, ? extends U> transformer; |
5699 |
> |
final Consumer<? super U> action; |
5700 |
> |
if ((transformer = this.transformer) != null && |
5701 |
> |
(action = this.action) != null) { |
5702 |
> |
for (int b; (b = preSplit()) > 0;) |
5703 |
> |
new ForEachTransformedKeyTask<K,V,U> |
5704 |
> |
(map, this, b, transformer, action).fork(); |
5705 |
> |
K k; U u; |
5706 |
> |
while ((k = advanceKey()) != null) { |
5707 |
> |
if ((u = transformer.apply(k)) != null) |
5708 |
> |
action.accept(u); |
5709 |
> |
} |
5710 |
> |
propagateCompletion(); |
5711 |
> |
} |
5712 |
> |
} |
5713 |
> |
} |
5714 |
> |
|
5715 |
> |
@SuppressWarnings("serial") static final class ForEachTransformedValueTask<K,V,U> |
5716 |
> |
extends Traverser<K,V,Void> { |
5717 |
> |
final Function<? super V, ? extends U> transformer; |
5718 |
> |
final Consumer<? super U> action; |
5719 |
> |
ForEachTransformedValueTask |
5720 |
> |
(ConcurrentHashMap<K,V> m, Traverser<K,V,?> p, int b, |
5721 |
> |
Function<? super V, ? extends U> transformer, Consumer<? super U> action) { |
5722 |
> |
super(m, p, b); |
5723 |
> |
this.transformer = transformer; this.action = action; |
5724 |
> |
} |
5725 |
> |
public final void compute() { |
5726 |
> |
final Function<? super V, ? extends U> transformer; |
5727 |
> |
final Consumer<? super U> action; |
5728 |
> |
if ((transformer = this.transformer) != null && |
5729 |
> |
(action = this.action) != null) { |
5730 |
> |
for (int b; (b = preSplit()) > 0;) |
5731 |
> |
new ForEachTransformedValueTask<K,V,U> |
5732 |
> |
(map, this, b, transformer, action).fork(); |
5733 |
> |
V v; U u; |
5734 |
> |
while ((v = advanceValue()) != null) { |
5735 |
> |
if ((u = transformer.apply(v)) != null) |
5736 |
> |
action.accept(u); |
5737 |
> |
} |
5738 |
> |
propagateCompletion(); |
5739 |
> |
} |
5740 |
> |
} |
5741 |
> |
} |
5742 |
> |
|
5743 |
> |
@SuppressWarnings("serial") static final class ForEachTransformedEntryTask<K,V,U> |
5744 |
> |
extends Traverser<K,V,Void> { |
5745 |
> |
final Function<Map.Entry<K,V>, ? extends U> transformer; |
5746 |
> |
final Consumer<? super U> action; |
5747 |
> |
ForEachTransformedEntryTask |
5748 |
> |
(ConcurrentHashMap<K,V> m, Traverser<K,V,?> p, int b, |
5749 |
> |
Function<Map.Entry<K,V>, ? extends U> transformer, Consumer<? super U> action) { |
5750 |
> |
super(m, p, b); |
5751 |
> |
this.transformer = transformer; this.action = action; |
5752 |
> |
} |
5753 |
> |
public final void compute() { |
5754 |
> |
final Function<Map.Entry<K,V>, ? extends U> transformer; |
5755 |
> |
final Consumer<? super U> action; |
5756 |
> |
if ((transformer = this.transformer) != null && |
5757 |
> |
(action = this.action) != null) { |
5758 |
> |
for (int b; (b = preSplit()) > 0;) |
5759 |
> |
new ForEachTransformedEntryTask<K,V,U> |
5760 |
> |
(map, this, b, transformer, action).fork(); |
5761 |
> |
V v; U u; |
5762 |
> |
while ((v = advanceValue()) != null) { |
5763 |
> |
if ((u = transformer.apply(entryFor(nextKey, |
5764 |
> |
v))) != null) |
5765 |
> |
action.accept(u); |
5766 |
> |
} |
5767 |
> |
propagateCompletion(); |
5768 |
> |
} |
5769 |
> |
} |
5770 |
> |
} |
5771 |
> |
|
5772 |
> |
@SuppressWarnings("serial") static final class ForEachTransformedMappingTask<K,V,U> |
5773 |
> |
extends Traverser<K,V,Void> { |
5774 |
> |
final BiFunction<? super K, ? super V, ? extends U> transformer; |
5775 |
> |
final Consumer<? super U> action; |
5776 |
> |
ForEachTransformedMappingTask |
5777 |
> |
(ConcurrentHashMap<K,V> m, Traverser<K,V,?> p, int b, |
5778 |
> |
BiFunction<? super K, ? super V, ? extends U> transformer, |
5779 |
> |
Consumer<? super U> action) { |
5780 |
> |
super(m, p, b); |
5781 |
> |
this.transformer = transformer; this.action = action; |
5782 |
> |
} |
5783 |
> |
public final void compute() { |
5784 |
> |
final BiFunction<? super K, ? super V, ? extends U> transformer; |
5785 |
> |
final Consumer<? super U> action; |
5786 |
> |
if ((transformer = this.transformer) != null && |
5787 |
> |
(action = this.action) != null) { |
5788 |
> |
for (int b; (b = preSplit()) > 0;) |
5789 |
> |
new ForEachTransformedMappingTask<K,V,U> |
5790 |
> |
(map, this, b, transformer, action).fork(); |
5791 |
> |
V v; U u; |
5792 |
> |
while ((v = advanceValue()) != null) { |
5793 |
> |
if ((u = transformer.apply(nextKey, v)) != null) |
5794 |
> |
action.accept(u); |
5795 |
> |
} |
5796 |
> |
propagateCompletion(); |
5797 |
> |
} |
5798 |
|
} |
5799 |
|
} |
1051 |
– |
} |
5800 |
|
|
5801 |
+ |
@SuppressWarnings("serial") static final class SearchKeysTask<K,V,U> |
5802 |
+ |
extends Traverser<K,V,U> { |
5803 |
+ |
final Function<? super K, ? extends U> searchFunction; |
5804 |
+ |
final AtomicReference<U> result; |
5805 |
+ |
SearchKeysTask |
5806 |
+ |
(ConcurrentHashMap<K,V> m, Traverser<K,V,?> p, int b, |
5807 |
+ |
Function<? super K, ? extends U> searchFunction, |
5808 |
+ |
AtomicReference<U> result) { |
5809 |
+ |
super(m, p, b); |
5810 |
+ |
this.searchFunction = searchFunction; this.result = result; |
5811 |
+ |
} |
5812 |
+ |
public final U getRawResult() { return result.get(); } |
5813 |
+ |
public final void compute() { |
5814 |
+ |
final Function<? super K, ? extends U> searchFunction; |
5815 |
+ |
final AtomicReference<U> result; |
5816 |
+ |
if ((searchFunction = this.searchFunction) != null && |
5817 |
+ |
(result = this.result) != null) { |
5818 |
+ |
for (int b;;) { |
5819 |
+ |
if (result.get() != null) |
5820 |
+ |
return; |
5821 |
+ |
if ((b = preSplit()) <= 0) |
5822 |
+ |
break; |
5823 |
+ |
new SearchKeysTask<K,V,U> |
5824 |
+ |
(map, this, b, searchFunction, result).fork(); |
5825 |
+ |
} |
5826 |
+ |
while (result.get() == null) { |
5827 |
+ |
K k; U u; |
5828 |
+ |
if ((k = advanceKey()) == null) { |
5829 |
+ |
propagateCompletion(); |
5830 |
+ |
break; |
5831 |
+ |
} |
5832 |
+ |
if ((u = searchFunction.apply(k)) != null) { |
5833 |
+ |
if (result.compareAndSet(null, u)) |
5834 |
+ |
quietlyCompleteRoot(); |
5835 |
+ |
break; |
5836 |
+ |
} |
5837 |
+ |
} |
5838 |
+ |
} |
5839 |
+ |
} |
5840 |
+ |
} |
5841 |
+ |
|
5842 |
+ |
@SuppressWarnings("serial") static final class SearchValuesTask<K,V,U> |
5843 |
+ |
extends Traverser<K,V,U> { |
5844 |
+ |
final Function<? super V, ? extends U> searchFunction; |
5845 |
+ |
final AtomicReference<U> result; |
5846 |
+ |
SearchValuesTask |
5847 |
+ |
(ConcurrentHashMap<K,V> m, Traverser<K,V,?> p, int b, |
5848 |
+ |
Function<? super V, ? extends U> searchFunction, |
5849 |
+ |
AtomicReference<U> result) { |
5850 |
+ |
super(m, p, b); |
5851 |
+ |
this.searchFunction = searchFunction; this.result = result; |
5852 |
+ |
} |
5853 |
+ |
public final U getRawResult() { return result.get(); } |
5854 |
+ |
public final void compute() { |
5855 |
+ |
final Function<? super V, ? extends U> searchFunction; |
5856 |
+ |
final AtomicReference<U> result; |
5857 |
+ |
if ((searchFunction = this.searchFunction) != null && |
5858 |
+ |
(result = this.result) != null) { |
5859 |
+ |
for (int b;;) { |
5860 |
+ |
if (result.get() != null) |
5861 |
+ |
return; |
5862 |
+ |
if ((b = preSplit()) <= 0) |
5863 |
+ |
break; |
5864 |
+ |
new SearchValuesTask<K,V,U> |
5865 |
+ |
(map, this, b, searchFunction, result).fork(); |
5866 |
+ |
} |
5867 |
+ |
while (result.get() == null) { |
5868 |
+ |
V v; U u; |
5869 |
+ |
if ((v = advanceValue()) == null) { |
5870 |
+ |
propagateCompletion(); |
5871 |
+ |
break; |
5872 |
+ |
} |
5873 |
+ |
if ((u = searchFunction.apply(v)) != null) { |
5874 |
+ |
if (result.compareAndSet(null, u)) |
5875 |
+ |
quietlyCompleteRoot(); |
5876 |
+ |
break; |
5877 |
+ |
} |
5878 |
+ |
} |
5879 |
+ |
} |
5880 |
+ |
} |
5881 |
+ |
} |
5882 |
+ |
|
5883 |
+ |
@SuppressWarnings("serial") static final class SearchEntriesTask<K,V,U> |
5884 |
+ |
extends Traverser<K,V,U> { |
5885 |
+ |
final Function<Entry<K,V>, ? extends U> searchFunction; |
5886 |
+ |
final AtomicReference<U> result; |
5887 |
+ |
SearchEntriesTask |
5888 |
+ |
(ConcurrentHashMap<K,V> m, Traverser<K,V,?> p, int b, |
5889 |
+ |
Function<Entry<K,V>, ? extends U> searchFunction, |
5890 |
+ |
AtomicReference<U> result) { |
5891 |
+ |
super(m, p, b); |
5892 |
+ |
this.searchFunction = searchFunction; this.result = result; |
5893 |
+ |
} |
5894 |
+ |
public final U getRawResult() { return result.get(); } |
5895 |
+ |
public final void compute() { |
5896 |
+ |
final Function<Entry<K,V>, ? extends U> searchFunction; |
5897 |
+ |
final AtomicReference<U> result; |
5898 |
+ |
if ((searchFunction = this.searchFunction) != null && |
5899 |
+ |
(result = this.result) != null) { |
5900 |
+ |
for (int b;;) { |
5901 |
+ |
if (result.get() != null) |
5902 |
+ |
return; |
5903 |
+ |
if ((b = preSplit()) <= 0) |
5904 |
+ |
break; |
5905 |
+ |
new SearchEntriesTask<K,V,U> |
5906 |
+ |
(map, this, b, searchFunction, result).fork(); |
5907 |
+ |
} |
5908 |
+ |
while (result.get() == null) { |
5909 |
+ |
V v; U u; |
5910 |
+ |
if ((v = advanceValue()) == null) { |
5911 |
+ |
propagateCompletion(); |
5912 |
+ |
break; |
5913 |
+ |
} |
5914 |
+ |
if ((u = searchFunction.apply(entryFor(nextKey, |
5915 |
+ |
v))) != null) { |
5916 |
+ |
if (result.compareAndSet(null, u)) |
5917 |
+ |
quietlyCompleteRoot(); |
5918 |
+ |
return; |
5919 |
+ |
} |
5920 |
+ |
} |
5921 |
+ |
} |
5922 |
+ |
} |
5923 |
+ |
} |
5924 |
+ |
|
5925 |
+ |
@SuppressWarnings("serial") static final class SearchMappingsTask<K,V,U> |
5926 |
+ |
extends Traverser<K,V,U> { |
5927 |
+ |
final BiFunction<? super K, ? super V, ? extends U> searchFunction; |
5928 |
+ |
final AtomicReference<U> result; |
5929 |
+ |
SearchMappingsTask |
5930 |
+ |
(ConcurrentHashMap<K,V> m, Traverser<K,V,?> p, int b, |
5931 |
+ |
BiFunction<? super K, ? super V, ? extends U> searchFunction, |
5932 |
+ |
AtomicReference<U> result) { |
5933 |
+ |
super(m, p, b); |
5934 |
+ |
this.searchFunction = searchFunction; this.result = result; |
5935 |
+ |
} |
5936 |
+ |
public final U getRawResult() { return result.get(); } |
5937 |
+ |
public final void compute() { |
5938 |
+ |
final BiFunction<? super K, ? super V, ? extends U> searchFunction; |
5939 |
+ |
final AtomicReference<U> result; |
5940 |
+ |
if ((searchFunction = this.searchFunction) != null && |
5941 |
+ |
(result = this.result) != null) { |
5942 |
+ |
for (int b;;) { |
5943 |
+ |
if (result.get() != null) |
5944 |
+ |
return; |
5945 |
+ |
if ((b = preSplit()) <= 0) |
5946 |
+ |
break; |
5947 |
+ |
new SearchMappingsTask<K,V,U> |
5948 |
+ |
(map, this, b, searchFunction, result).fork(); |
5949 |
+ |
} |
5950 |
+ |
while (result.get() == null) { |
5951 |
+ |
V v; U u; |
5952 |
+ |
if ((v = advanceValue()) == null) { |
5953 |
+ |
propagateCompletion(); |
5954 |
+ |
break; |
5955 |
+ |
} |
5956 |
+ |
if ((u = searchFunction.apply(nextKey, v)) != null) { |
5957 |
+ |
if (result.compareAndSet(null, u)) |
5958 |
+ |
quietlyCompleteRoot(); |
5959 |
+ |
break; |
5960 |
+ |
} |
5961 |
+ |
} |
5962 |
+ |
} |
5963 |
+ |
} |
5964 |
+ |
} |
5965 |
+ |
|
5966 |
+ |
@SuppressWarnings("serial") static final class ReduceKeysTask<K,V> |
5967 |
+ |
extends Traverser<K,V,K> { |
5968 |
+ |
final BiFunction<? super K, ? super K, ? extends K> reducer; |
5969 |
+ |
K result; |
5970 |
+ |
ReduceKeysTask<K,V> rights, nextRight; |
5971 |
+ |
ReduceKeysTask |
5972 |
+ |
(ConcurrentHashMap<K,V> m, Traverser<K,V,?> p, int b, |
5973 |
+ |
ReduceKeysTask<K,V> nextRight, |
5974 |
+ |
BiFunction<? super K, ? super K, ? extends K> reducer) { |
5975 |
+ |
super(m, p, b); this.nextRight = nextRight; |
5976 |
+ |
this.reducer = reducer; |
5977 |
+ |
} |
5978 |
+ |
public final K getRawResult() { return result; } |
5979 |
+ |
@SuppressWarnings("unchecked") public final void compute() { |
5980 |
+ |
final BiFunction<? super K, ? super K, ? extends K> reducer; |
5981 |
+ |
if ((reducer = this.reducer) != null) { |
5982 |
+ |
for (int b; (b = preSplit()) > 0;) |
5983 |
+ |
(rights = new ReduceKeysTask<K,V> |
5984 |
+ |
(map, this, b, rights, reducer)).fork(); |
5985 |
+ |
K u, r = null; |
5986 |
+ |
while ((u = advanceKey()) != null) { |
5987 |
+ |
r = (r == null) ? u : u == null ? r : reducer.apply(r, u); |
5988 |
+ |
} |
5989 |
+ |
result = r; |
5990 |
+ |
CountedCompleter<?> c; |
5991 |
+ |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
5992 |
+ |
ReduceKeysTask<K,V> |
5993 |
+ |
t = (ReduceKeysTask<K,V>)c, |
5994 |
+ |
s = t.rights; |
5995 |
+ |
while (s != null) { |
5996 |
+ |
K tr, sr; |
5997 |
+ |
if ((sr = s.result) != null) |
5998 |
+ |
t.result = (((tr = t.result) == null) ? sr : |
5999 |
+ |
reducer.apply(tr, sr)); |
6000 |
+ |
s = t.rights = s.nextRight; |
6001 |
+ |
} |
6002 |
+ |
} |
6003 |
+ |
} |
6004 |
+ |
} |
6005 |
+ |
} |
6006 |
+ |
|
6007 |
+ |
@SuppressWarnings("serial") static final class ReduceValuesTask<K,V> |
6008 |
+ |
extends Traverser<K,V,V> { |
6009 |
+ |
final BiFunction<? super V, ? super V, ? extends V> reducer; |
6010 |
+ |
V result; |
6011 |
+ |
ReduceValuesTask<K,V> rights, nextRight; |
6012 |
+ |
ReduceValuesTask |
6013 |
+ |
(ConcurrentHashMap<K,V> m, Traverser<K,V,?> p, int b, |
6014 |
+ |
ReduceValuesTask<K,V> nextRight, |
6015 |
+ |
BiFunction<? super V, ? super V, ? extends V> reducer) { |
6016 |
+ |
super(m, p, b); this.nextRight = nextRight; |
6017 |
+ |
this.reducer = reducer; |
6018 |
+ |
} |
6019 |
+ |
public final V getRawResult() { return result; } |
6020 |
+ |
@SuppressWarnings("unchecked") public final void compute() { |
6021 |
+ |
final BiFunction<? super V, ? super V, ? extends V> reducer; |
6022 |
+ |
if ((reducer = this.reducer) != null) { |
6023 |
+ |
for (int b; (b = preSplit()) > 0;) |
6024 |
+ |
(rights = new ReduceValuesTask<K,V> |
6025 |
+ |
(map, this, b, rights, reducer)).fork(); |
6026 |
+ |
V r = null, v; |
6027 |
+ |
while ((v = advanceValue()) != null) |
6028 |
+ |
r = (r == null) ? v : reducer.apply(r, v); |
6029 |
+ |
result = r; |
6030 |
+ |
CountedCompleter<?> c; |
6031 |
+ |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
6032 |
+ |
ReduceValuesTask<K,V> |
6033 |
+ |
t = (ReduceValuesTask<K,V>)c, |
6034 |
+ |
s = t.rights; |
6035 |
+ |
while (s != null) { |
6036 |
+ |
V tr, sr; |
6037 |
+ |
if ((sr = s.result) != null) |
6038 |
+ |
t.result = (((tr = t.result) == null) ? sr : |
6039 |
+ |
reducer.apply(tr, sr)); |
6040 |
+ |
s = t.rights = s.nextRight; |
6041 |
+ |
} |
6042 |
+ |
} |
6043 |
+ |
} |
6044 |
+ |
} |
6045 |
+ |
} |
6046 |
+ |
|
6047 |
+ |
@SuppressWarnings("serial") static final class ReduceEntriesTask<K,V> |
6048 |
+ |
extends Traverser<K,V,Map.Entry<K,V>> { |
6049 |
+ |
final BiFunction<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer; |
6050 |
+ |
Map.Entry<K,V> result; |
6051 |
+ |
ReduceEntriesTask<K,V> rights, nextRight; |
6052 |
+ |
ReduceEntriesTask |
6053 |
+ |
(ConcurrentHashMap<K,V> m, Traverser<K,V,?> p, int b, |
6054 |
+ |
ReduceEntriesTask<K,V> nextRight, |
6055 |
+ |
BiFunction<Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) { |
6056 |
+ |
super(m, p, b); this.nextRight = nextRight; |
6057 |
+ |
this.reducer = reducer; |
6058 |
+ |
} |
6059 |
+ |
public final Map.Entry<K,V> getRawResult() { return result; } |
6060 |
+ |
@SuppressWarnings("unchecked") public final void compute() { |
6061 |
+ |
final BiFunction<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer; |
6062 |
+ |
if ((reducer = this.reducer) != null) { |
6063 |
+ |
for (int b; (b = preSplit()) > 0;) |
6064 |
+ |
(rights = new ReduceEntriesTask<K,V> |
6065 |
+ |
(map, this, b, rights, reducer)).fork(); |
6066 |
+ |
Map.Entry<K,V> r = null; |
6067 |
+ |
V v; |
6068 |
+ |
while ((v = advanceValue()) != null) { |
6069 |
+ |
Map.Entry<K,V> u = entryFor(nextKey, v); |
6070 |
+ |
r = (r == null) ? u : reducer.apply(r, u); |
6071 |
+ |
} |
6072 |
+ |
result = r; |
6073 |
+ |
CountedCompleter<?> c; |
6074 |
+ |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
6075 |
+ |
ReduceEntriesTask<K,V> |
6076 |
+ |
t = (ReduceEntriesTask<K,V>)c, |
6077 |
+ |
s = t.rights; |
6078 |
+ |
while (s != null) { |
6079 |
+ |
Map.Entry<K,V> tr, sr; |
6080 |
+ |
if ((sr = s.result) != null) |
6081 |
+ |
t.result = (((tr = t.result) == null) ? sr : |
6082 |
+ |
reducer.apply(tr, sr)); |
6083 |
+ |
s = t.rights = s.nextRight; |
6084 |
+ |
} |
6085 |
+ |
} |
6086 |
+ |
} |
6087 |
+ |
} |
6088 |
+ |
} |
6089 |
+ |
|
6090 |
+ |
@SuppressWarnings("serial") static final class MapReduceKeysTask<K,V,U> |
6091 |
+ |
extends Traverser<K,V,U> { |
6092 |
+ |
final Function<? super K, ? extends U> transformer; |
6093 |
+ |
final BiFunction<? super U, ? super U, ? extends U> reducer; |
6094 |
+ |
U result; |
6095 |
+ |
MapReduceKeysTask<K,V,U> rights, nextRight; |
6096 |
+ |
MapReduceKeysTask |
6097 |
+ |
(ConcurrentHashMap<K,V> m, Traverser<K,V,?> p, int b, |
6098 |
+ |
MapReduceKeysTask<K,V,U> nextRight, |
6099 |
+ |
Function<? super K, ? extends U> transformer, |
6100 |
+ |
BiFunction<? super U, ? super U, ? extends U> reducer) { |
6101 |
+ |
super(m, p, b); this.nextRight = nextRight; |
6102 |
+ |
this.transformer = transformer; |
6103 |
+ |
this.reducer = reducer; |
6104 |
+ |
} |
6105 |
+ |
public final U getRawResult() { return result; } |
6106 |
+ |
@SuppressWarnings("unchecked") public final void compute() { |
6107 |
+ |
final Function<? super K, ? extends U> transformer; |
6108 |
+ |
final BiFunction<? super U, ? super U, ? extends U> reducer; |
6109 |
+ |
if ((transformer = this.transformer) != null && |
6110 |
+ |
(reducer = this.reducer) != null) { |
6111 |
+ |
for (int b; (b = preSplit()) > 0;) |
6112 |
+ |
(rights = new MapReduceKeysTask<K,V,U> |
6113 |
+ |
(map, this, b, rights, transformer, reducer)).fork(); |
6114 |
+ |
K k; U r = null, u; |
6115 |
+ |
while ((k = advanceKey()) != null) { |
6116 |
+ |
if ((u = transformer.apply(k)) != null) |
6117 |
+ |
r = (r == null) ? u : reducer.apply(r, u); |
6118 |
+ |
} |
6119 |
+ |
result = r; |
6120 |
+ |
CountedCompleter<?> c; |
6121 |
+ |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
6122 |
+ |
MapReduceKeysTask<K,V,U> |
6123 |
+ |
t = (MapReduceKeysTask<K,V,U>)c, |
6124 |
+ |
s = t.rights; |
6125 |
+ |
while (s != null) { |
6126 |
+ |
U tr, sr; |
6127 |
+ |
if ((sr = s.result) != null) |
6128 |
+ |
t.result = (((tr = t.result) == null) ? sr : |
6129 |
+ |
reducer.apply(tr, sr)); |
6130 |
+ |
s = t.rights = s.nextRight; |
6131 |
+ |
} |
6132 |
+ |
} |
6133 |
+ |
} |
6134 |
+ |
} |
6135 |
+ |
} |
6136 |
+ |
|
6137 |
+ |
@SuppressWarnings("serial") static final class MapReduceValuesTask<K,V,U> |
6138 |
+ |
extends Traverser<K,V,U> { |
6139 |
+ |
final Function<? super V, ? extends U> transformer; |
6140 |
+ |
final BiFunction<? super U, ? super U, ? extends U> reducer; |
6141 |
+ |
U result; |
6142 |
+ |
MapReduceValuesTask<K,V,U> rights, nextRight; |
6143 |
+ |
MapReduceValuesTask |
6144 |
+ |
(ConcurrentHashMap<K,V> m, Traverser<K,V,?> p, int b, |
6145 |
+ |
MapReduceValuesTask<K,V,U> nextRight, |
6146 |
+ |
Function<? super V, ? extends U> transformer, |
6147 |
+ |
BiFunction<? super U, ? super U, ? extends U> reducer) { |
6148 |
+ |
super(m, p, b); this.nextRight = nextRight; |
6149 |
+ |
this.transformer = transformer; |
6150 |
+ |
this.reducer = reducer; |
6151 |
+ |
} |
6152 |
+ |
public final U getRawResult() { return result; } |
6153 |
+ |
@SuppressWarnings("unchecked") public final void compute() { |
6154 |
+ |
final Function<? super V, ? extends U> transformer; |
6155 |
+ |
final BiFunction<? super U, ? super U, ? extends U> reducer; |
6156 |
+ |
if ((transformer = this.transformer) != null && |
6157 |
+ |
(reducer = this.reducer) != null) { |
6158 |
+ |
for (int b; (b = preSplit()) > 0;) |
6159 |
+ |
(rights = new MapReduceValuesTask<K,V,U> |
6160 |
+ |
(map, this, b, rights, transformer, reducer)).fork(); |
6161 |
+ |
U r = null, u; |
6162 |
+ |
V v; |
6163 |
+ |
while ((v = advanceValue()) != null) { |
6164 |
+ |
if ((u = transformer.apply(v)) != null) |
6165 |
+ |
r = (r == null) ? u : reducer.apply(r, u); |
6166 |
+ |
} |
6167 |
+ |
result = r; |
6168 |
+ |
CountedCompleter<?> c; |
6169 |
+ |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
6170 |
+ |
MapReduceValuesTask<K,V,U> |
6171 |
+ |
t = (MapReduceValuesTask<K,V,U>)c, |
6172 |
+ |
s = t.rights; |
6173 |
+ |
while (s != null) { |
6174 |
+ |
U tr, sr; |
6175 |
+ |
if ((sr = s.result) != null) |
6176 |
+ |
t.result = (((tr = t.result) == null) ? sr : |
6177 |
+ |
reducer.apply(tr, sr)); |
6178 |
+ |
s = t.rights = s.nextRight; |
6179 |
+ |
} |
6180 |
+ |
} |
6181 |
+ |
} |
6182 |
+ |
} |
6183 |
+ |
} |
6184 |
+ |
|
6185 |
+ |
@SuppressWarnings("serial") static final class MapReduceEntriesTask<K,V,U> |
6186 |
+ |
extends Traverser<K,V,U> { |
6187 |
+ |
final Function<Map.Entry<K,V>, ? extends U> transformer; |
6188 |
+ |
final BiFunction<? super U, ? super U, ? extends U> reducer; |
6189 |
+ |
U result; |
6190 |
+ |
MapReduceEntriesTask<K,V,U> rights, nextRight; |
6191 |
+ |
MapReduceEntriesTask |
6192 |
+ |
(ConcurrentHashMap<K,V> m, Traverser<K,V,?> p, int b, |
6193 |
+ |
MapReduceEntriesTask<K,V,U> nextRight, |
6194 |
+ |
Function<Map.Entry<K,V>, ? extends U> transformer, |
6195 |
+ |
BiFunction<? super U, ? super U, ? extends U> reducer) { |
6196 |
+ |
super(m, p, b); this.nextRight = nextRight; |
6197 |
+ |
this.transformer = transformer; |
6198 |
+ |
this.reducer = reducer; |
6199 |
+ |
} |
6200 |
+ |
public final U getRawResult() { return result; } |
6201 |
+ |
@SuppressWarnings("unchecked") public final void compute() { |
6202 |
+ |
final Function<Map.Entry<K,V>, ? extends U> transformer; |
6203 |
+ |
final BiFunction<? super U, ? super U, ? extends U> reducer; |
6204 |
+ |
if ((transformer = this.transformer) != null && |
6205 |
+ |
(reducer = this.reducer) != null) { |
6206 |
+ |
for (int b; (b = preSplit()) > 0;) |
6207 |
+ |
(rights = new MapReduceEntriesTask<K,V,U> |
6208 |
+ |
(map, this, b, rights, transformer, reducer)).fork(); |
6209 |
+ |
U r = null, u; |
6210 |
+ |
V v; |
6211 |
+ |
while ((v = advanceValue()) != null) { |
6212 |
+ |
if ((u = transformer.apply(entryFor(nextKey, |
6213 |
+ |
v))) != null) |
6214 |
+ |
r = (r == null) ? u : reducer.apply(r, u); |
6215 |
+ |
} |
6216 |
+ |
result = r; |
6217 |
+ |
CountedCompleter<?> c; |
6218 |
+ |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
6219 |
+ |
MapReduceEntriesTask<K,V,U> |
6220 |
+ |
t = (MapReduceEntriesTask<K,V,U>)c, |
6221 |
+ |
s = t.rights; |
6222 |
+ |
while (s != null) { |
6223 |
+ |
U tr, sr; |
6224 |
+ |
if ((sr = s.result) != null) |
6225 |
+ |
t.result = (((tr = t.result) == null) ? sr : |
6226 |
+ |
reducer.apply(tr, sr)); |
6227 |
+ |
s = t.rights = s.nextRight; |
6228 |
+ |
} |
6229 |
+ |
} |
6230 |
+ |
} |
6231 |
+ |
} |
6232 |
+ |
} |
6233 |
+ |
|
6234 |
+ |
@SuppressWarnings("serial") static final class MapReduceMappingsTask<K,V,U> |
6235 |
+ |
extends Traverser<K,V,U> { |
6236 |
+ |
final BiFunction<? super K, ? super V, ? extends U> transformer; |
6237 |
+ |
final BiFunction<? super U, ? super U, ? extends U> reducer; |
6238 |
+ |
U result; |
6239 |
+ |
MapReduceMappingsTask<K,V,U> rights, nextRight; |
6240 |
+ |
MapReduceMappingsTask |
6241 |
+ |
(ConcurrentHashMap<K,V> m, Traverser<K,V,?> p, int b, |
6242 |
+ |
MapReduceMappingsTask<K,V,U> nextRight, |
6243 |
+ |
BiFunction<? super K, ? super V, ? extends U> transformer, |
6244 |
+ |
BiFunction<? super U, ? super U, ? extends U> reducer) { |
6245 |
+ |
super(m, p, b); this.nextRight = nextRight; |
6246 |
+ |
this.transformer = transformer; |
6247 |
+ |
this.reducer = reducer; |
6248 |
+ |
} |
6249 |
+ |
public final U getRawResult() { return result; } |
6250 |
+ |
@SuppressWarnings("unchecked") public final void compute() { |
6251 |
+ |
final BiFunction<? super K, ? super V, ? extends U> transformer; |
6252 |
+ |
final BiFunction<? super U, ? super U, ? extends U> reducer; |
6253 |
+ |
if ((transformer = this.transformer) != null && |
6254 |
+ |
(reducer = this.reducer) != null) { |
6255 |
+ |
for (int b; (b = preSplit()) > 0;) |
6256 |
+ |
(rights = new MapReduceMappingsTask<K,V,U> |
6257 |
+ |
(map, this, b, rights, transformer, reducer)).fork(); |
6258 |
+ |
U r = null, u; |
6259 |
+ |
V v; |
6260 |
+ |
while ((v = advanceValue()) != null) { |
6261 |
+ |
if ((u = transformer.apply(nextKey, v)) != null) |
6262 |
+ |
r = (r == null) ? u : reducer.apply(r, u); |
6263 |
+ |
} |
6264 |
+ |
result = r; |
6265 |
+ |
CountedCompleter<?> c; |
6266 |
+ |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
6267 |
+ |
MapReduceMappingsTask<K,V,U> |
6268 |
+ |
t = (MapReduceMappingsTask<K,V,U>)c, |
6269 |
+ |
s = t.rights; |
6270 |
+ |
while (s != null) { |
6271 |
+ |
U tr, sr; |
6272 |
+ |
if ((sr = s.result) != null) |
6273 |
+ |
t.result = (((tr = t.result) == null) ? sr : |
6274 |
+ |
reducer.apply(tr, sr)); |
6275 |
+ |
s = t.rights = s.nextRight; |
6276 |
+ |
} |
6277 |
+ |
} |
6278 |
+ |
} |
6279 |
+ |
} |
6280 |
+ |
} |
6281 |
+ |
|
6282 |
+ |
@SuppressWarnings("serial") static final class MapReduceKeysToDoubleTask<K,V> |
6283 |
+ |
extends Traverser<K,V,Double> { |
6284 |
+ |
final ToDoubleFunction<? super K> transformer; |
6285 |
+ |
final DoubleBinaryOperator reducer; |
6286 |
+ |
final double basis; |
6287 |
+ |
double result; |
6288 |
+ |
MapReduceKeysToDoubleTask<K,V> rights, nextRight; |
6289 |
+ |
MapReduceKeysToDoubleTask |
6290 |
+ |
(ConcurrentHashMap<K,V> m, Traverser<K,V,?> p, int b, |
6291 |
+ |
MapReduceKeysToDoubleTask<K,V> nextRight, |
6292 |
+ |
ToDoubleFunction<? super K> transformer, |
6293 |
+ |
double basis, |
6294 |
+ |
DoubleBinaryOperator reducer) { |
6295 |
+ |
super(m, p, b); this.nextRight = nextRight; |
6296 |
+ |
this.transformer = transformer; |
6297 |
+ |
this.basis = basis; this.reducer = reducer; |
6298 |
+ |
} |
6299 |
+ |
public final Double getRawResult() { return result; } |
6300 |
+ |
@SuppressWarnings("unchecked") public final void compute() { |
6301 |
+ |
final ToDoubleFunction<? super K> transformer; |
6302 |
+ |
final DoubleBinaryOperator reducer; |
6303 |
+ |
if ((transformer = this.transformer) != null && |
6304 |
+ |
(reducer = this.reducer) != null) { |
6305 |
+ |
double r = this.basis; |
6306 |
+ |
for (int b; (b = preSplit()) > 0;) |
6307 |
+ |
(rights = new MapReduceKeysToDoubleTask<K,V> |
6308 |
+ |
(map, this, b, rights, transformer, r, reducer)).fork(); |
6309 |
+ |
K k; |
6310 |
+ |
while ((k = advanceKey()) != null) |
6311 |
+ |
r = reducer.applyAsDouble(r, transformer.applyAsDouble(k)); |
6312 |
+ |
result = r; |
6313 |
+ |
CountedCompleter<?> c; |
6314 |
+ |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
6315 |
+ |
MapReduceKeysToDoubleTask<K,V> |
6316 |
+ |
t = (MapReduceKeysToDoubleTask<K,V>)c, |
6317 |
+ |
s = t.rights; |
6318 |
+ |
while (s != null) { |
6319 |
+ |
t.result = reducer.applyAsDouble(t.result, s.result); |
6320 |
+ |
s = t.rights = s.nextRight; |
6321 |
+ |
} |
6322 |
+ |
} |
6323 |
+ |
} |
6324 |
+ |
} |
6325 |
+ |
} |
6326 |
+ |
|
6327 |
+ |
@SuppressWarnings("serial") static final class MapReduceValuesToDoubleTask<K,V> |
6328 |
+ |
extends Traverser<K,V,Double> { |
6329 |
+ |
final ToDoubleFunction<? super V> transformer; |
6330 |
+ |
final DoubleBinaryOperator reducer; |
6331 |
+ |
final double basis; |
6332 |
+ |
double result; |
6333 |
+ |
MapReduceValuesToDoubleTask<K,V> rights, nextRight; |
6334 |
+ |
MapReduceValuesToDoubleTask |
6335 |
+ |
(ConcurrentHashMap<K,V> m, Traverser<K,V,?> p, int b, |
6336 |
+ |
MapReduceValuesToDoubleTask<K,V> nextRight, |
6337 |
+ |
ToDoubleFunction<? super V> transformer, |
6338 |
+ |
double basis, |
6339 |
+ |
DoubleBinaryOperator reducer) { |
6340 |
+ |
super(m, p, b); this.nextRight = nextRight; |
6341 |
+ |
this.transformer = transformer; |
6342 |
+ |
this.basis = basis; this.reducer = reducer; |
6343 |
+ |
} |
6344 |
+ |
public final Double getRawResult() { return result; } |
6345 |
+ |
@SuppressWarnings("unchecked") public final void compute() { |
6346 |
+ |
final ToDoubleFunction<? super V> transformer; |
6347 |
+ |
final DoubleBinaryOperator reducer; |
6348 |
+ |
if ((transformer = this.transformer) != null && |
6349 |
+ |
(reducer = this.reducer) != null) { |
6350 |
+ |
double r = this.basis; |
6351 |
+ |
for (int b; (b = preSplit()) > 0;) |
6352 |
+ |
(rights = new MapReduceValuesToDoubleTask<K,V> |
6353 |
+ |
(map, this, b, rights, transformer, r, reducer)).fork(); |
6354 |
+ |
V v; |
6355 |
+ |
while ((v = advanceValue()) != null) |
6356 |
+ |
r = reducer.applyAsDouble(r, transformer.applyAsDouble(v)); |
6357 |
+ |
result = r; |
6358 |
+ |
CountedCompleter<?> c; |
6359 |
+ |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
6360 |
+ |
MapReduceValuesToDoubleTask<K,V> |
6361 |
+ |
t = (MapReduceValuesToDoubleTask<K,V>)c, |
6362 |
+ |
s = t.rights; |
6363 |
+ |
while (s != null) { |
6364 |
+ |
t.result = reducer.applyAsDouble(t.result, s.result); |
6365 |
+ |
s = t.rights = s.nextRight; |
6366 |
+ |
} |
6367 |
+ |
} |
6368 |
+ |
} |
6369 |
+ |
} |
6370 |
+ |
} |
6371 |
+ |
|
6372 |
+ |
@SuppressWarnings("serial") static final class MapReduceEntriesToDoubleTask<K,V> |
6373 |
+ |
extends Traverser<K,V,Double> { |
6374 |
+ |
final ToDoubleFunction<Map.Entry<K,V>> transformer; |
6375 |
+ |
final DoubleBinaryOperator reducer; |
6376 |
+ |
final double basis; |
6377 |
+ |
double result; |
6378 |
+ |
MapReduceEntriesToDoubleTask<K,V> rights, nextRight; |
6379 |
+ |
MapReduceEntriesToDoubleTask |
6380 |
+ |
(ConcurrentHashMap<K,V> m, Traverser<K,V,?> p, int b, |
6381 |
+ |
MapReduceEntriesToDoubleTask<K,V> nextRight, |
6382 |
+ |
ToDoubleFunction<Map.Entry<K,V>> transformer, |
6383 |
+ |
double basis, |
6384 |
+ |
DoubleBinaryOperator reducer) { |
6385 |
+ |
super(m, p, b); this.nextRight = nextRight; |
6386 |
+ |
this.transformer = transformer; |
6387 |
+ |
this.basis = basis; this.reducer = reducer; |
6388 |
+ |
} |
6389 |
+ |
public final Double getRawResult() { return result; } |
6390 |
+ |
@SuppressWarnings("unchecked") public final void compute() { |
6391 |
+ |
final ToDoubleFunction<Map.Entry<K,V>> transformer; |
6392 |
+ |
final DoubleBinaryOperator reducer; |
6393 |
+ |
if ((transformer = this.transformer) != null && |
6394 |
+ |
(reducer = this.reducer) != null) { |
6395 |
+ |
double r = this.basis; |
6396 |
+ |
for (int b; (b = preSplit()) > 0;) |
6397 |
+ |
(rights = new MapReduceEntriesToDoubleTask<K,V> |
6398 |
+ |
(map, this, b, rights, transformer, r, reducer)).fork(); |
6399 |
+ |
V v; |
6400 |
+ |
while ((v = advanceValue()) != null) |
6401 |
+ |
r = reducer.applyAsDouble(r, transformer.applyAsDouble(entryFor(nextKey, |
6402 |
+ |
v))); |
6403 |
+ |
result = r; |
6404 |
+ |
CountedCompleter<?> c; |
6405 |
+ |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
6406 |
+ |
MapReduceEntriesToDoubleTask<K,V> |
6407 |
+ |
t = (MapReduceEntriesToDoubleTask<K,V>)c, |
6408 |
+ |
s = t.rights; |
6409 |
+ |
while (s != null) { |
6410 |
+ |
t.result = reducer.applyAsDouble(t.result, s.result); |
6411 |
+ |
s = t.rights = s.nextRight; |
6412 |
+ |
} |
6413 |
+ |
} |
6414 |
+ |
} |
6415 |
+ |
} |
6416 |
+ |
} |
6417 |
+ |
|
6418 |
+ |
@SuppressWarnings("serial") static final class MapReduceMappingsToDoubleTask<K,V> |
6419 |
+ |
extends Traverser<K,V,Double> { |
6420 |
+ |
final ToDoubleBiFunction<? super K, ? super V> transformer; |
6421 |
+ |
final DoubleBinaryOperator reducer; |
6422 |
+ |
final double basis; |
6423 |
+ |
double result; |
6424 |
+ |
MapReduceMappingsToDoubleTask<K,V> rights, nextRight; |
6425 |
+ |
MapReduceMappingsToDoubleTask |
6426 |
+ |
(ConcurrentHashMap<K,V> m, Traverser<K,V,?> p, int b, |
6427 |
+ |
MapReduceMappingsToDoubleTask<K,V> nextRight, |
6428 |
+ |
ToDoubleBiFunction<? super K, ? super V> transformer, |
6429 |
+ |
double basis, |
6430 |
+ |
DoubleBinaryOperator reducer) { |
6431 |
+ |
super(m, p, b); this.nextRight = nextRight; |
6432 |
+ |
this.transformer = transformer; |
6433 |
+ |
this.basis = basis; this.reducer = reducer; |
6434 |
+ |
} |
6435 |
+ |
public final Double getRawResult() { return result; } |
6436 |
+ |
@SuppressWarnings("unchecked") public final void compute() { |
6437 |
+ |
final ToDoubleBiFunction<? super K, ? super V> transformer; |
6438 |
+ |
final DoubleBinaryOperator reducer; |
6439 |
+ |
if ((transformer = this.transformer) != null && |
6440 |
+ |
(reducer = this.reducer) != null) { |
6441 |
+ |
double r = this.basis; |
6442 |
+ |
for (int b; (b = preSplit()) > 0;) |
6443 |
+ |
(rights = new MapReduceMappingsToDoubleTask<K,V> |
6444 |
+ |
(map, this, b, rights, transformer, r, reducer)).fork(); |
6445 |
+ |
V v; |
6446 |
+ |
while ((v = advanceValue()) != null) |
6447 |
+ |
r = reducer.applyAsDouble(r, transformer.applyAsDouble(nextKey, v)); |
6448 |
+ |
result = r; |
6449 |
+ |
CountedCompleter<?> c; |
6450 |
+ |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
6451 |
+ |
MapReduceMappingsToDoubleTask<K,V> |
6452 |
+ |
t = (MapReduceMappingsToDoubleTask<K,V>)c, |
6453 |
+ |
s = t.rights; |
6454 |
+ |
while (s != null) { |
6455 |
+ |
t.result = reducer.applyAsDouble(t.result, s.result); |
6456 |
+ |
s = t.rights = s.nextRight; |
6457 |
+ |
} |
6458 |
+ |
} |
6459 |
+ |
} |
6460 |
+ |
} |
6461 |
+ |
} |
6462 |
+ |
|
6463 |
+ |
@SuppressWarnings("serial") static final class MapReduceKeysToLongTask<K,V> |
6464 |
+ |
extends Traverser<K,V,Long> { |
6465 |
+ |
final ToLongFunction<? super K> transformer; |
6466 |
+ |
final LongBinaryOperator reducer; |
6467 |
+ |
final long basis; |
6468 |
+ |
long result; |
6469 |
+ |
MapReduceKeysToLongTask<K,V> rights, nextRight; |
6470 |
+ |
MapReduceKeysToLongTask |
6471 |
+ |
(ConcurrentHashMap<K,V> m, Traverser<K,V,?> p, int b, |
6472 |
+ |
MapReduceKeysToLongTask<K,V> nextRight, |
6473 |
+ |
ToLongFunction<? super K> transformer, |
6474 |
+ |
long basis, |
6475 |
+ |
LongBinaryOperator reducer) { |
6476 |
+ |
super(m, p, b); this.nextRight = nextRight; |
6477 |
+ |
this.transformer = transformer; |
6478 |
+ |
this.basis = basis; this.reducer = reducer; |
6479 |
+ |
} |
6480 |
+ |
public final Long getRawResult() { return result; } |
6481 |
+ |
@SuppressWarnings("unchecked") public final void compute() { |
6482 |
+ |
final ToLongFunction<? super K> transformer; |
6483 |
+ |
final LongBinaryOperator reducer; |
6484 |
+ |
if ((transformer = this.transformer) != null && |
6485 |
+ |
(reducer = this.reducer) != null) { |
6486 |
+ |
long r = this.basis; |
6487 |
+ |
for (int b; (b = preSplit()) > 0;) |
6488 |
+ |
(rights = new MapReduceKeysToLongTask<K,V> |
6489 |
+ |
(map, this, b, rights, transformer, r, reducer)).fork(); |
6490 |
+ |
K k; |
6491 |
+ |
while ((k = advanceKey()) != null) |
6492 |
+ |
r = reducer.applyAsLong(r, transformer.applyAsLong(k)); |
6493 |
+ |
result = r; |
6494 |
+ |
CountedCompleter<?> c; |
6495 |
+ |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
6496 |
+ |
MapReduceKeysToLongTask<K,V> |
6497 |
+ |
t = (MapReduceKeysToLongTask<K,V>)c, |
6498 |
+ |
s = t.rights; |
6499 |
+ |
while (s != null) { |
6500 |
+ |
t.result = reducer.applyAsLong(t.result, s.result); |
6501 |
+ |
s = t.rights = s.nextRight; |
6502 |
+ |
} |
6503 |
+ |
} |
6504 |
+ |
} |
6505 |
+ |
} |
6506 |
+ |
} |
6507 |
+ |
|
6508 |
+ |
@SuppressWarnings("serial") static final class MapReduceValuesToLongTask<K,V> |
6509 |
+ |
extends Traverser<K,V,Long> { |
6510 |
+ |
final ToLongFunction<? super V> transformer; |
6511 |
+ |
final LongBinaryOperator reducer; |
6512 |
+ |
final long basis; |
6513 |
+ |
long result; |
6514 |
+ |
MapReduceValuesToLongTask<K,V> rights, nextRight; |
6515 |
+ |
MapReduceValuesToLongTask |
6516 |
+ |
(ConcurrentHashMap<K,V> m, Traverser<K,V,?> p, int b, |
6517 |
+ |
MapReduceValuesToLongTask<K,V> nextRight, |
6518 |
+ |
ToLongFunction<? super V> transformer, |
6519 |
+ |
long basis, |
6520 |
+ |
LongBinaryOperator reducer) { |
6521 |
+ |
super(m, p, b); this.nextRight = nextRight; |
6522 |
+ |
this.transformer = transformer; |
6523 |
+ |
this.basis = basis; this.reducer = reducer; |
6524 |
+ |
} |
6525 |
+ |
public final Long getRawResult() { return result; } |
6526 |
+ |
@SuppressWarnings("unchecked") public final void compute() { |
6527 |
+ |
final ToLongFunction<? super V> transformer; |
6528 |
+ |
final LongBinaryOperator reducer; |
6529 |
+ |
if ((transformer = this.transformer) != null && |
6530 |
+ |
(reducer = this.reducer) != null) { |
6531 |
+ |
long r = this.basis; |
6532 |
+ |
for (int b; (b = preSplit()) > 0;) |
6533 |
+ |
(rights = new MapReduceValuesToLongTask<K,V> |
6534 |
+ |
(map, this, b, rights, transformer, r, reducer)).fork(); |
6535 |
+ |
V v; |
6536 |
+ |
while ((v = advanceValue()) != null) |
6537 |
+ |
r = reducer.applyAsLong(r, transformer.applyAsLong(v)); |
6538 |
+ |
result = r; |
6539 |
+ |
CountedCompleter<?> c; |
6540 |
+ |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
6541 |
+ |
MapReduceValuesToLongTask<K,V> |
6542 |
+ |
t = (MapReduceValuesToLongTask<K,V>)c, |
6543 |
+ |
s = t.rights; |
6544 |
+ |
while (s != null) { |
6545 |
+ |
t.result = reducer.applyAsLong(t.result, s.result); |
6546 |
+ |
s = t.rights = s.nextRight; |
6547 |
+ |
} |
6548 |
+ |
} |
6549 |
+ |
} |
6550 |
+ |
} |
6551 |
+ |
} |
6552 |
+ |
|
6553 |
+ |
@SuppressWarnings("serial") static final class MapReduceEntriesToLongTask<K,V> |
6554 |
+ |
extends Traverser<K,V,Long> { |
6555 |
+ |
final ToLongFunction<Map.Entry<K,V>> transformer; |
6556 |
+ |
final LongBinaryOperator reducer; |
6557 |
+ |
final long basis; |
6558 |
+ |
long result; |
6559 |
+ |
MapReduceEntriesToLongTask<K,V> rights, nextRight; |
6560 |
+ |
MapReduceEntriesToLongTask |
6561 |
+ |
(ConcurrentHashMap<K,V> m, Traverser<K,V,?> p, int b, |
6562 |
+ |
MapReduceEntriesToLongTask<K,V> nextRight, |
6563 |
+ |
ToLongFunction<Map.Entry<K,V>> transformer, |
6564 |
+ |
long basis, |
6565 |
+ |
LongBinaryOperator reducer) { |
6566 |
+ |
super(m, p, b); this.nextRight = nextRight; |
6567 |
+ |
this.transformer = transformer; |
6568 |
+ |
this.basis = basis; this.reducer = reducer; |
6569 |
+ |
} |
6570 |
+ |
public final Long getRawResult() { return result; } |
6571 |
+ |
@SuppressWarnings("unchecked") public final void compute() { |
6572 |
+ |
final ToLongFunction<Map.Entry<K,V>> transformer; |
6573 |
+ |
final LongBinaryOperator reducer; |
6574 |
+ |
if ((transformer = this.transformer) != null && |
6575 |
+ |
(reducer = this.reducer) != null) { |
6576 |
+ |
long r = this.basis; |
6577 |
+ |
for (int b; (b = preSplit()) > 0;) |
6578 |
+ |
(rights = new MapReduceEntriesToLongTask<K,V> |
6579 |
+ |
(map, this, b, rights, transformer, r, reducer)).fork(); |
6580 |
+ |
V v; |
6581 |
+ |
while ((v = advanceValue()) != null) |
6582 |
+ |
r = reducer.applyAsLong(r, transformer.applyAsLong(entryFor(nextKey, v))); |
6583 |
+ |
result = r; |
6584 |
+ |
CountedCompleter<?> c; |
6585 |
+ |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
6586 |
+ |
MapReduceEntriesToLongTask<K,V> |
6587 |
+ |
t = (MapReduceEntriesToLongTask<K,V>)c, |
6588 |
+ |
s = t.rights; |
6589 |
+ |
while (s != null) { |
6590 |
+ |
t.result = reducer.applyAsLong(t.result, s.result); |
6591 |
+ |
s = t.rights = s.nextRight; |
6592 |
+ |
} |
6593 |
+ |
} |
6594 |
+ |
} |
6595 |
+ |
} |
6596 |
+ |
} |
6597 |
+ |
|
6598 |
+ |
@SuppressWarnings("serial") static final class MapReduceMappingsToLongTask<K,V> |
6599 |
+ |
extends Traverser<K,V,Long> { |
6600 |
+ |
final ToLongBiFunction<? super K, ? super V> transformer; |
6601 |
+ |
final LongBinaryOperator reducer; |
6602 |
+ |
final long basis; |
6603 |
+ |
long result; |
6604 |
+ |
MapReduceMappingsToLongTask<K,V> rights, nextRight; |
6605 |
+ |
MapReduceMappingsToLongTask |
6606 |
+ |
(ConcurrentHashMap<K,V> m, Traverser<K,V,?> p, int b, |
6607 |
+ |
MapReduceMappingsToLongTask<K,V> nextRight, |
6608 |
+ |
ToLongBiFunction<? super K, ? super V> transformer, |
6609 |
+ |
long basis, |
6610 |
+ |
LongBinaryOperator reducer) { |
6611 |
+ |
super(m, p, b); this.nextRight = nextRight; |
6612 |
+ |
this.transformer = transformer; |
6613 |
+ |
this.basis = basis; this.reducer = reducer; |
6614 |
+ |
} |
6615 |
+ |
public final Long getRawResult() { return result; } |
6616 |
+ |
@SuppressWarnings("unchecked") public final void compute() { |
6617 |
+ |
final ToLongBiFunction<? super K, ? super V> transformer; |
6618 |
+ |
final LongBinaryOperator reducer; |
6619 |
+ |
if ((transformer = this.transformer) != null && |
6620 |
+ |
(reducer = this.reducer) != null) { |
6621 |
+ |
long r = this.basis; |
6622 |
+ |
for (int b; (b = preSplit()) > 0;) |
6623 |
+ |
(rights = new MapReduceMappingsToLongTask<K,V> |
6624 |
+ |
(map, this, b, rights, transformer, r, reducer)).fork(); |
6625 |
+ |
V v; |
6626 |
+ |
while ((v = advanceValue()) != null) |
6627 |
+ |
r = reducer.applyAsLong(r, transformer.applyAsLong(nextKey, v)); |
6628 |
+ |
result = r; |
6629 |
+ |
CountedCompleter<?> c; |
6630 |
+ |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
6631 |
+ |
MapReduceMappingsToLongTask<K,V> |
6632 |
+ |
t = (MapReduceMappingsToLongTask<K,V>)c, |
6633 |
+ |
s = t.rights; |
6634 |
+ |
while (s != null) { |
6635 |
+ |
t.result = reducer.applyAsLong(t.result, s.result); |
6636 |
+ |
s = t.rights = s.nextRight; |
6637 |
+ |
} |
6638 |
+ |
} |
6639 |
+ |
} |
6640 |
+ |
} |
6641 |
+ |
} |
6642 |
+ |
|
6643 |
+ |
@SuppressWarnings("serial") static final class MapReduceKeysToIntTask<K,V> |
6644 |
+ |
extends Traverser<K,V,Integer> { |
6645 |
+ |
final ToIntFunction<? super K> transformer; |
6646 |
+ |
final IntBinaryOperator reducer; |
6647 |
+ |
final int basis; |
6648 |
+ |
int result; |
6649 |
+ |
MapReduceKeysToIntTask<K,V> rights, nextRight; |
6650 |
+ |
MapReduceKeysToIntTask |
6651 |
+ |
(ConcurrentHashMap<K,V> m, Traverser<K,V,?> p, int b, |
6652 |
+ |
MapReduceKeysToIntTask<K,V> nextRight, |
6653 |
+ |
ToIntFunction<? super K> transformer, |
6654 |
+ |
int basis, |
6655 |
+ |
IntBinaryOperator reducer) { |
6656 |
+ |
super(m, p, b); this.nextRight = nextRight; |
6657 |
+ |
this.transformer = transformer; |
6658 |
+ |
this.basis = basis; this.reducer = reducer; |
6659 |
+ |
} |
6660 |
+ |
public final Integer getRawResult() { return result; } |
6661 |
+ |
@SuppressWarnings("unchecked") public final void compute() { |
6662 |
+ |
final ToIntFunction<? super K> transformer; |
6663 |
+ |
final IntBinaryOperator reducer; |
6664 |
+ |
if ((transformer = this.transformer) != null && |
6665 |
+ |
(reducer = this.reducer) != null) { |
6666 |
+ |
int r = this.basis; |
6667 |
+ |
for (int b; (b = preSplit()) > 0;) |
6668 |
+ |
(rights = new MapReduceKeysToIntTask<K,V> |
6669 |
+ |
(map, this, b, rights, transformer, r, reducer)).fork(); |
6670 |
+ |
K k; |
6671 |
+ |
while ((k = advanceKey()) != null) |
6672 |
+ |
r = reducer.applyAsInt(r, transformer.applyAsInt(k)); |
6673 |
+ |
result = r; |
6674 |
+ |
CountedCompleter<?> c; |
6675 |
+ |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
6676 |
+ |
MapReduceKeysToIntTask<K,V> |
6677 |
+ |
t = (MapReduceKeysToIntTask<K,V>)c, |
6678 |
+ |
s = t.rights; |
6679 |
+ |
while (s != null) { |
6680 |
+ |
t.result = reducer.applyAsInt(t.result, s.result); |
6681 |
+ |
s = t.rights = s.nextRight; |
6682 |
+ |
} |
6683 |
+ |
} |
6684 |
+ |
} |
6685 |
+ |
} |
6686 |
+ |
} |
6687 |
+ |
|
6688 |
+ |
@SuppressWarnings("serial") static final class MapReduceValuesToIntTask<K,V> |
6689 |
+ |
extends Traverser<K,V,Integer> { |
6690 |
+ |
final ToIntFunction<? super V> transformer; |
6691 |
+ |
final IntBinaryOperator reducer; |
6692 |
+ |
final int basis; |
6693 |
+ |
int result; |
6694 |
+ |
MapReduceValuesToIntTask<K,V> rights, nextRight; |
6695 |
+ |
MapReduceValuesToIntTask |
6696 |
+ |
(ConcurrentHashMap<K,V> m, Traverser<K,V,?> p, int b, |
6697 |
+ |
MapReduceValuesToIntTask<K,V> nextRight, |
6698 |
+ |
ToIntFunction<? super V> transformer, |
6699 |
+ |
int basis, |
6700 |
+ |
IntBinaryOperator reducer) { |
6701 |
+ |
super(m, p, b); this.nextRight = nextRight; |
6702 |
+ |
this.transformer = transformer; |
6703 |
+ |
this.basis = basis; this.reducer = reducer; |
6704 |
+ |
} |
6705 |
+ |
public final Integer getRawResult() { return result; } |
6706 |
+ |
@SuppressWarnings("unchecked") public final void compute() { |
6707 |
+ |
final ToIntFunction<? super V> transformer; |
6708 |
+ |
final IntBinaryOperator reducer; |
6709 |
+ |
if ((transformer = this.transformer) != null && |
6710 |
+ |
(reducer = this.reducer) != null) { |
6711 |
+ |
int r = this.basis; |
6712 |
+ |
for (int b; (b = preSplit()) > 0;) |
6713 |
+ |
(rights = new MapReduceValuesToIntTask<K,V> |
6714 |
+ |
(map, this, b, rights, transformer, r, reducer)).fork(); |
6715 |
+ |
V v; |
6716 |
+ |
while ((v = advanceValue()) != null) |
6717 |
+ |
r = reducer.applyAsInt(r, transformer.applyAsInt(v)); |
6718 |
+ |
result = r; |
6719 |
+ |
CountedCompleter<?> c; |
6720 |
+ |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
6721 |
+ |
MapReduceValuesToIntTask<K,V> |
6722 |
+ |
t = (MapReduceValuesToIntTask<K,V>)c, |
6723 |
+ |
s = t.rights; |
6724 |
+ |
while (s != null) { |
6725 |
+ |
t.result = reducer.applyAsInt(t.result, s.result); |
6726 |
+ |
s = t.rights = s.nextRight; |
6727 |
+ |
} |
6728 |
+ |
} |
6729 |
+ |
} |
6730 |
+ |
} |
6731 |
+ |
} |
6732 |
+ |
|
6733 |
+ |
@SuppressWarnings("serial") static final class MapReduceEntriesToIntTask<K,V> |
6734 |
+ |
extends Traverser<K,V,Integer> { |
6735 |
+ |
final ToIntFunction<Map.Entry<K,V>> transformer; |
6736 |
+ |
final IntBinaryOperator reducer; |
6737 |
+ |
final int basis; |
6738 |
+ |
int result; |
6739 |
+ |
MapReduceEntriesToIntTask<K,V> rights, nextRight; |
6740 |
+ |
MapReduceEntriesToIntTask |
6741 |
+ |
(ConcurrentHashMap<K,V> m, Traverser<K,V,?> p, int b, |
6742 |
+ |
MapReduceEntriesToIntTask<K,V> nextRight, |
6743 |
+ |
ToIntFunction<Map.Entry<K,V>> transformer, |
6744 |
+ |
int basis, |
6745 |
+ |
IntBinaryOperator reducer) { |
6746 |
+ |
super(m, p, b); this.nextRight = nextRight; |
6747 |
+ |
this.transformer = transformer; |
6748 |
+ |
this.basis = basis; this.reducer = reducer; |
6749 |
+ |
} |
6750 |
+ |
public final Integer getRawResult() { return result; } |
6751 |
+ |
@SuppressWarnings("unchecked") public final void compute() { |
6752 |
+ |
final ToIntFunction<Map.Entry<K,V>> transformer; |
6753 |
+ |
final IntBinaryOperator reducer; |
6754 |
+ |
if ((transformer = this.transformer) != null && |
6755 |
+ |
(reducer = this.reducer) != null) { |
6756 |
+ |
int r = this.basis; |
6757 |
+ |
for (int b; (b = preSplit()) > 0;) |
6758 |
+ |
(rights = new MapReduceEntriesToIntTask<K,V> |
6759 |
+ |
(map, this, b, rights, transformer, r, reducer)).fork(); |
6760 |
+ |
V v; |
6761 |
+ |
while ((v = advanceValue()) != null) |
6762 |
+ |
r = reducer.applyAsInt(r, transformer.applyAsInt(entryFor(nextKey, |
6763 |
+ |
v))); |
6764 |
+ |
result = r; |
6765 |
+ |
CountedCompleter<?> c; |
6766 |
+ |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
6767 |
+ |
MapReduceEntriesToIntTask<K,V> |
6768 |
+ |
t = (MapReduceEntriesToIntTask<K,V>)c, |
6769 |
+ |
s = t.rights; |
6770 |
+ |
while (s != null) { |
6771 |
+ |
t.result = reducer.applyAsInt(t.result, s.result); |
6772 |
+ |
s = t.rights = s.nextRight; |
6773 |
+ |
} |
6774 |
+ |
} |
6775 |
+ |
} |
6776 |
+ |
} |
6777 |
+ |
} |
6778 |
+ |
|
6779 |
+ |
@SuppressWarnings("serial") static final class MapReduceMappingsToIntTask<K,V> |
6780 |
+ |
extends Traverser<K,V,Integer> { |
6781 |
+ |
final ToIntBiFunction<? super K, ? super V> transformer; |
6782 |
+ |
final IntBinaryOperator reducer; |
6783 |
+ |
final int basis; |
6784 |
+ |
int result; |
6785 |
+ |
MapReduceMappingsToIntTask<K,V> rights, nextRight; |
6786 |
+ |
MapReduceMappingsToIntTask |
6787 |
+ |
(ConcurrentHashMap<K,V> m, Traverser<K,V,?> p, int b, |
6788 |
+ |
MapReduceMappingsToIntTask<K,V> nextRight, |
6789 |
+ |
ToIntBiFunction<? super K, ? super V> transformer, |
6790 |
+ |
int basis, |
6791 |
+ |
IntBinaryOperator reducer) { |
6792 |
+ |
super(m, p, b); this.nextRight = nextRight; |
6793 |
+ |
this.transformer = transformer; |
6794 |
+ |
this.basis = basis; this.reducer = reducer; |
6795 |
+ |
} |
6796 |
+ |
public final Integer getRawResult() { return result; } |
6797 |
+ |
@SuppressWarnings("unchecked") public final void compute() { |
6798 |
+ |
final ToIntBiFunction<? super K, ? super V> transformer; |
6799 |
+ |
final IntBinaryOperator reducer; |
6800 |
+ |
if ((transformer = this.transformer) != null && |
6801 |
+ |
(reducer = this.reducer) != null) { |
6802 |
+ |
int r = this.basis; |
6803 |
+ |
for (int b; (b = preSplit()) > 0;) |
6804 |
+ |
(rights = new MapReduceMappingsToIntTask<K,V> |
6805 |
+ |
(map, this, b, rights, transformer, r, reducer)).fork(); |
6806 |
+ |
V v; |
6807 |
+ |
while ((v = advanceValue()) != null) |
6808 |
+ |
r = reducer.applyAsInt(r, transformer.applyAsInt(nextKey, v)); |
6809 |
+ |
result = r; |
6810 |
+ |
CountedCompleter<?> c; |
6811 |
+ |
for (c = firstComplete(); c != null; c = c.nextComplete()) { |
6812 |
+ |
MapReduceMappingsToIntTask<K,V> |
6813 |
+ |
t = (MapReduceMappingsToIntTask<K,V>)c, |
6814 |
+ |
s = t.rights; |
6815 |
+ |
while (s != null) { |
6816 |
+ |
t.result = reducer.applyAsInt(t.result, s.result); |
6817 |
+ |
s = t.rights = s.nextRight; |
6818 |
+ |
} |
6819 |
+ |
} |
6820 |
+ |
} |
6821 |
+ |
} |
6822 |
+ |
} |
6823 |
+ |
|
6824 |
+ |
// Unsafe mechanics |
6825 |
+ |
private static final sun.misc.Unsafe U; |
6826 |
+ |
private static final long SIZECTL; |
6827 |
+ |
private static final long TRANSFERINDEX; |
6828 |
+ |
private static final long TRANSFERORIGIN; |
6829 |
+ |
private static final long BASECOUNT; |
6830 |
+ |
private static final long CELLSBUSY; |
6831 |
+ |
private static final long CELLVALUE; |
6832 |
+ |
private static final long ABASE; |
6833 |
+ |
private static final int ASHIFT; |
6834 |
+ |
|
6835 |
+ |
static { |
6836 |
+ |
try { |
6837 |
+ |
U = sun.misc.Unsafe.getUnsafe(); |
6838 |
+ |
Class<?> k = ConcurrentHashMap.class; |
6839 |
+ |
SIZECTL = U.objectFieldOffset |
6840 |
+ |
(k.getDeclaredField("sizeCtl")); |
6841 |
+ |
TRANSFERINDEX = U.objectFieldOffset |
6842 |
+ |
(k.getDeclaredField("transferIndex")); |
6843 |
+ |
TRANSFERORIGIN = U.objectFieldOffset |
6844 |
+ |
(k.getDeclaredField("transferOrigin")); |
6845 |
+ |
BASECOUNT = U.objectFieldOffset |
6846 |
+ |
(k.getDeclaredField("baseCount")); |
6847 |
+ |
CELLSBUSY = U.objectFieldOffset |
6848 |
+ |
(k.getDeclaredField("cellsBusy")); |
6849 |
+ |
Class<?> ck = Cell.class; |
6850 |
+ |
CELLVALUE = U.objectFieldOffset |
6851 |
+ |
(ck.getDeclaredField("value")); |
6852 |
+ |
Class<?> sc = Node[].class; |
6853 |
+ |
ABASE = U.arrayBaseOffset(sc); |
6854 |
+ |
int scale = U.arrayIndexScale(sc); |
6855 |
+ |
if ((scale & (scale - 1)) != 0) |
6856 |
+ |
throw new Error("data type scale not a power of two"); |
6857 |
+ |
ASHIFT = 31 - Integer.numberOfLeadingZeros(scale); |
6858 |
+ |
} catch (Exception e) { |
6859 |
+ |
throw new Error(e); |
6860 |
+ |
} |
6861 |
+ |
} |
6862 |
+ |
|
6863 |
+ |
} |