forked from scala/scala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashMap.scala
587 lines (502 loc) · 19.2 KB
/
HashMap.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
/*
* Scala (https://www.scala-lang.org)
*
* Copyright EPFL and Lightbend, Inc.
*
* Licensed under Apache License 2.0
* (http://www.apache.org/licenses/LICENSE-2.0).
*
* See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*/
package scala.collection
package mutable
import scala.annotation.tailrec
import scala.collection.Stepper.EfficientSplit
import scala.collection.generic.DefaultSerializationProxy
/** This class implements mutable maps using a hashtable.
*
* @see [[http://docs.scala-lang.org/overviews/collections/concrete-mutable-collection-classes.html#hash-tables "Scala's Collection Library overview"]]
* section on `Hash Tables` for more information.
*
* @tparam K the type of the keys contained in this hash map.
* @tparam V the type of the values assigned to keys in this hash map.
*
* @define Coll `mutable.HashMap`
* @define coll mutable hash map
* @define mayNotTerminateInf
* @define willNotTerminateInf
*/
@deprecatedInheritance("HashMap wil be made final; use .withDefault for the common use case of computing a default value", "2.13.0")
class HashMap[K, V](initialCapacity: Int, loadFactor: Double)
extends AbstractMap[K, V]
with MapOps[K, V, HashMap, HashMap[K, V]]
with StrictOptimizedIterableOps[(K, V), Iterable, HashMap[K, V]]
with StrictOptimizedMapOps[K, V, HashMap, HashMap[K, V]]
with MapFactoryDefaults[K, V, HashMap, Iterable]
with Serializable {
def this() = this(HashMap.defaultInitialCapacity, HashMap.defaultLoadFactor)
import HashMap.Node
/** The actual hash table. */
private[this] var table = new Array[Node[K, V]](tableSizeFor(initialCapacity))
/** The next size value at which to resize (capacity * load factor). */
private[this] var threshold: Int = newThreshold(table.length)
private[this] var contentSize = 0
override def size: Int = contentSize
/** Performs the inverse operation of improveHash. In this case, it happens to be identical to improveHash*/
@`inline` private[collection] def unimproveHash(improvedHash: Int): Int = improveHash(improvedHash)
/** Computes the improved hash of an original (`any.##`) hash. */
@`inline` private[this] def improveHash(originalHash: Int): Int = {
// Improve the hash by xoring the high 16 bits into the low 16 bits just in case entropy is skewed towards the
// high-value bits. We only use the lowest bits to determine the hash bucket. This is the same improvement
// algorithm as in java.util.HashMap.
//
// This function is also its own inverse. That is, for all ints i, improveHash(improveHash(i)) = i
// this allows us to retrieve the original hash when we need it, for instance when appending to an immutable.HashMap
// and that is why unimproveHash simply forwards to this method
originalHash ^ (originalHash >>> 16)
}
/** Computes the improved hash of this key */
@`inline` private[this] def computeHash(o: K): Int = improveHash(o.##)
@`inline` private[this] def index(hash: Int) = hash & (table.length - 1)
override def contains(key: K): Boolean = findNode(key) ne null
@`inline` private[this] def findNode(key: K): Node[K, V] = {
val hash = computeHash(key)
table(index(hash)) match {
case null => null
case nd => nd.findNode(key, hash)
}
}
override def sizeHint(size: Int): Unit = {
val target = tableSizeFor(((size + 1).toDouble / loadFactor).toInt)
if(target > table.length) growTable(target)
}
override def addAll(xs: IterableOnce[(K, V)]): this.type = {
sizeHint(xs.knownSize)
xs match {
case hm: immutable.HashMap[K, V] =>
hm.foreachWithHash((k, v, h) => put0(k, v, improveHash(h), getOld = false))
this
case hm: mutable.HashMap[K, V] =>
val iter = hm.nodeIterator
while (iter.hasNext) {
val next = iter.next()
put0(next.key, next.value, next.hash, getOld = false)
}
this
case _ => super.addAll(xs)
}
}
// Override updateWith for performance, so we can do the update while hashing
// the input key only once and performing one lookup into the hash table
override def updateWith(key: K)(remappingFunction: Option[V] => Option[V]): Option[V] = {
if (getClass != classOf[HashMap[_, _]]) {
// subclasses of HashMap might customise `get` ...
super.updateWith(key)(remappingFunction)
} else {
val hash = computeHash(key)
val indexedHash = index(hash)
var foundNode: Node[K, V] = null
var previousNode: Node[K, V] = null
table(indexedHash) match {
case null =>
case nd =>
@tailrec
def findNode(prev: Node[K, V], nd: Node[K, V], k: K, h: Int): Unit = {
if (h == nd.hash && k == nd.key) {
previousNode = prev
foundNode = nd
}
else if ((nd.next eq null) || (nd.hash > h)) ()
else findNode(nd, nd.next, k, h)
}
findNode(null, nd, key, hash)
}
val previousValue = foundNode match {
case null => None
case nd => Some(nd.value)
}
val nextValue = remappingFunction(previousValue)
(previousValue, nextValue) match {
case (None, None) => // do nothing
case (Some(_), None) =>
if (previousNode != null) previousNode.next = foundNode.next
else table(indexedHash) = foundNode.next
contentSize -= 1
case (None, Some(value)) => put0(key, value, false, hash, indexedHash)
case (Some(_), Some(newValue)) => foundNode.value = newValue
}
nextValue
}
}
override def subtractAll(xs: IterableOnce[K]): this.type = {
if (size == 0) {
return this
}
xs match {
case hs: immutable.HashSet[K] =>
hs.foreachWithHashWhile { (k, h) =>
remove0(k, improveHash(h))
size > 0
}
this
case hs: mutable.HashSet[K] =>
val iter = hs.nodeIterator
while (iter.hasNext) {
val next = iter.next()
remove0(next.key, next.hash)
if (size == 0) return this
}
this
case _ => super.subtractAll(xs)
}
}
/** Adds a key-value pair to this map
*
* @param key the key to add
* @param value the value to add
* @param hash the **improved** hashcode of `key` (see computeHash)
* @param getOld if true, then the previous value for `key` will be returned, otherwise, false
*/
private[this] def put0(key: K, value: V, hash: Int, getOld: Boolean): Some[V] = {
if(contentSize + 1 >= threshold) growTable(table.length * 2)
val idx = index(hash)
put0(key, value, getOld, hash, idx)
}
private[this] def put0(key: K, value: V, getOld: Boolean): Some[V] = {
if(contentSize + 1 >= threshold) growTable(table.length * 2)
val hash = computeHash(key)
val idx = index(hash)
put0(key, value, getOld, hash, idx)
}
private[this] def put0(key: K, value: V, getOld: Boolean, hash: Int, idx: Int): Some[V] = {
table(idx) match {
case null =>
table(idx) = new Node[K, V](key, hash, value, null)
case old =>
var prev: Node[K, V] = null
var n = old
while((n ne null) && n.hash <= hash) {
if(n.hash == hash && key == n.key) {
val old = n.value
n.value = value
return if(getOld) Some(old) else null
}
prev = n
n = n.next
}
if(prev eq null) table(idx) = new Node(key, hash, value, old)
else prev.next = new Node(key, hash, value, prev.next)
}
contentSize += 1
null
}
private def remove0(elem: K) : Node[K, V] = remove0(elem, computeHash(elem))
/** Removes a key from this map if it exists
*
* @param elem the element to remove
* @param hash the **improved** hashcode of `element` (see computeHash)
* @return the node that contained element if it was present, otherwise null
*/
private[this] def remove0(elem: K, hash: Int) : Node[K, V] = {
val idx = index(hash)
table(idx) match {
case null => null
case nd if nd.hash == hash && nd.key == elem =>
// first element matches
table(idx) = nd.next
contentSize -= 1
nd
case nd =>
// find an element that matches
var prev = nd
var next = nd.next
while((next ne null) && next.hash <= hash) {
if(next.hash == hash && next.key == elem) {
prev.next = next.next
contentSize -= 1
return next
}
prev = next
next = next.next
}
null
}
}
private[this] abstract class HashMapIterator[A] extends AbstractIterator[A] {
private[this] var i = 0
private[this] var node: Node[K, V] = null
private[this] val len = table.length
protected[this] def extract(nd: Node[K, V]): A
def hasNext: Boolean = {
if(node ne null) true
else {
while(i < len) {
val n = table(i)
i += 1
if(n ne null) { node = n; return true }
}
false
}
}
def next(): A =
if(!hasNext) Iterator.empty.next()
else {
val r = extract(node)
node = node.next
r
}
}
override def iterator: Iterator[(K, V)] =
if(size == 0) Iterator.empty
else new HashMapIterator[(K, V)] {
protected[this] def extract(nd: Node[K, V]) = (nd.key, nd.value)
}
override def keysIterator: Iterator[K] =
if(size == 0) Iterator.empty
else new HashMapIterator[K] {
protected[this] def extract(nd: Node[K, V]) = nd.key
}
override def valuesIterator: Iterator[V] =
if(size == 0) Iterator.empty
else new HashMapIterator[V] {
protected[this] def extract(nd: Node[K, V]) = nd.value
}
/** Returns an iterator over the nodes stored in this HashMap */
private[collection] def nodeIterator: Iterator[Node[K, V]] =
if(size == 0) Iterator.empty
else new HashMapIterator[Node[K, V]] {
protected[this] def extract(nd: Node[K, V]) = nd
}
override def stepper[S <: Stepper[_]](implicit shape: StepperShape[(K, V), S]): S with EfficientSplit =
shape.
parUnbox(new convert.impl.AnyTableStepper[(K, V), Node[K, V]](size, table, _.next, node => (node.key, node.value), 0, table.length)).
asInstanceOf[S with EfficientSplit]
override def keyStepper[S <: Stepper[_]](implicit shape: StepperShape[K, S]): S with EfficientSplit = {
import convert.impl._
val s = shape.shape match {
case StepperShape.IntShape => new IntTableStepper[Node[K, V]] (size, table, _.next, _.key.asInstanceOf[Int], 0, table.length)
case StepperShape.LongShape => new LongTableStepper[Node[K, V]] (size, table, _.next, _.key.asInstanceOf[Long], 0, table.length)
case StepperShape.DoubleShape => new DoubleTableStepper[Node[K, V]](size, table, _.next, _.key.asInstanceOf[Double], 0, table.length)
case _ => shape.parUnbox(new AnyTableStepper[K, Node[K, V]](size, table, _.next, _.key, 0, table.length))
}
s.asInstanceOf[S with EfficientSplit]
}
override def valueStepper[S <: Stepper[_]](implicit shape: StepperShape[V, S]): S with EfficientSplit = {
import convert.impl._
val s = shape.shape match {
case StepperShape.IntShape => new IntTableStepper[Node[K, V]] (size, table, _.next, _.value.asInstanceOf[Int], 0, table.length)
case StepperShape.LongShape => new LongTableStepper[Node[K, V]] (size, table, _.next, _.value.asInstanceOf[Long], 0, table.length)
case StepperShape.DoubleShape => new DoubleTableStepper[Node[K, V]](size, table, _.next, _.value.asInstanceOf[Double], 0, table.length)
case _ => shape.parUnbox(new AnyTableStepper[V, Node[K, V]](size, table, _.next, _.value, 0, table.length))
}
s.asInstanceOf[S with EfficientSplit]
}
private[this] def growTable(newlen: Int) = {
var oldlen = table.length
threshold = newThreshold(newlen)
if(size == 0) table = new Array(newlen)
else {
table = java.util.Arrays.copyOf(table, newlen)
val preLow: Node[K, V] = new Node(null.asInstanceOf[K], 0, null.asInstanceOf[V], null)
val preHigh: Node[K, V] = new Node(null.asInstanceOf[K], 0, null.asInstanceOf[V], null)
// Split buckets until the new length has been reached. This could be done more
// efficiently when growing an already filled table to more than double the size.
while(oldlen < newlen) {
var i = 0
while (i < oldlen) {
val old = table(i)
if(old ne null) {
preLow.next = null
preHigh.next = null
var lastLow: Node[K, V] = preLow
var lastHigh: Node[K, V] = preHigh
var n = old
while(n ne null) {
val next = n.next
if((n.hash & oldlen) == 0) { // keep low
lastLow.next = n
lastLow = n
} else { // move to high
lastHigh.next = n
lastHigh = n
}
n = next
}
lastLow.next = null
if(old ne preLow.next) table(i) = preLow.next
if(preHigh.next ne null) {
table(i + oldlen) = preHigh.next
lastHigh.next = null
}
}
i += 1
}
oldlen *= 2
}
}
}
private[this] def tableSizeFor(capacity: Int) =
(Integer.highestOneBit((capacity-1).max(4))*2).min(1 << 30)
private[this] def newThreshold(size: Int) = (size.toDouble * loadFactor).toInt
override def clear(): Unit = {
java.util.Arrays.fill(table.asInstanceOf[Array[AnyRef]], null)
contentSize = 0
}
def get(key: K): Option[V] = findNode(key) match {
case null => None
case nd => Some(nd.value)
}
@throws[NoSuchElementException]
override def apply(key: K): V = findNode(key) match {
case null => default(key)
case nd => nd.value
}
override def getOrElse[V1 >: V](key: K, default: => V1): V1 = {
if (getClass != classOf[HashMap[_, _]]) {
// subclasses of HashMap might customise `get` ...
super.getOrElse(key, default)
} else {
// .. but in the common case, we can avoid the Option boxing.
val nd = findNode(key)
if (nd eq null) default else nd.value
}
}
override def getOrElseUpdate(key: K, defaultValue: => V): V = {
if (getClass != classOf[HashMap[_, _]]) {
// subclasses of HashMap might customise `get` ...
super.getOrElseUpdate(key, defaultValue)
} else {
val hash = computeHash(key)
val idx = index(hash)
val nd = table(idx) match {
case null => null
case nd => nd.findNode(key, hash)
}
if(nd != null) nd.value
else {
val table0 = table
val default = defaultValue
if(contentSize + 1 >= threshold) growTable(table.length * 2)
// Avoid recomputing index if the `defaultValue()` or new element hasn't triggered a table resize.
val newIdx = if (table0 eq table) idx else index(hash)
put0(key, default, false, hash, newIdx)
default
}
}
}
override def put(key: K, value: V): Option[V] = put0(key, value, true) match {
case null => None
case sm => sm
}
override def remove(key: K): Option[V] = remove0(key) match {
case null => None
case nd => Some(nd.value)
}
override def update(key: K, value: V): Unit = put0(key, value, false)
def addOne(elem: (K, V)): this.type = { put0(elem._1, elem._2, false); this }
def subtractOne(elem: K): this.type = { remove0(elem); this }
override def knownSize: Int = size
override def isEmpty: Boolean = size == 0
override def foreach[U](f: ((K, V)) => U): Unit = {
val len = table.length
var i = 0
while(i < len) {
val n = table(i)
if(n ne null) n.foreach(f)
i += 1
}
}
protected[this] def writeReplace(): AnyRef = new DefaultSerializationProxy(new mutable.HashMap.DeserializationFactory[K, V](table.length, loadFactor), this)
override def filterInPlace(p: (K, V) => Boolean): this.type = {
if (nonEmpty) {
var bucket = 0
while (bucket < table.length) {
var head = table(bucket)
while ((head ne null) && !p(head.key, head.value)) {
head = head.next
contentSize -= 1
}
if (head ne null) {
var prev = head
var next = head.next
while (next ne null) {
if (p(next.key, next.value)) {
prev = next
} else {
prev.next = next.next
contentSize -= 1
}
next = next.next
}
}
table(bucket) = head
bucket += 1
}
}
this
}
// TODO in 2.14: rename to `mapValuesInPlace` and override the base version (not binary compatible)
private[mutable] def mapValuesInPlaceImpl(f: (K, V) => V): this.type = {
val len = table.length
var i = 0
while (i < len) {
var n = table(i)
while (n ne null) {
n.value = f(n.key, n.value)
n = n.next
}
i += 1
}
this
}
override def mapFactory: MapFactory[HashMap] = HashMap
override protected[this] def stringPrefix = "HashMap"
}
/**
* $factoryInfo
* @define Coll `mutable.HashMap`
* @define coll mutable hash map
*/
@SerialVersionUID(3L)
object HashMap extends MapFactory[HashMap] {
def empty[K, V]: HashMap[K, V] = new HashMap[K, V]
def from[K, V](it: collection.IterableOnce[(K, V)]): HashMap[K, V] = {
val k = it.knownSize
val cap = if(k > 0) ((k + 1).toDouble / defaultLoadFactor).toInt else defaultInitialCapacity
new HashMap[K, V](cap, defaultLoadFactor).addAll(it)
}
def newBuilder[K, V]: Builder[(K, V), HashMap[K, V]] = newBuilder(defaultInitialCapacity, defaultLoadFactor)
def newBuilder[K, V](initialCapacity: Int, loadFactor: Double): Builder[(K, V), HashMap[K, V]] =
new GrowableBuilder[(K, V), HashMap[K, V]](new HashMap[K, V](initialCapacity, loadFactor)) {
override def sizeHint(size: Int) = elems.sizeHint(size)
}
/** The default load factor for the hash table */
final def defaultLoadFactor: Double = 0.75
/** The default initial capacity for the hash table */
final def defaultInitialCapacity: Int = 16
@SerialVersionUID(3L)
private final class DeserializationFactory[K, V](val tableLength: Int, val loadFactor: Double) extends Factory[(K, V), HashMap[K, V]] with Serializable {
def fromSpecific(it: IterableOnce[(K, V)]): HashMap[K, V] = new HashMap[K, V](tableLength, loadFactor).addAll(it)
def newBuilder: Builder[(K, V), HashMap[K, V]] = HashMap.newBuilder(tableLength, loadFactor)
}
private[collection] final class Node[K, V](_key: K, _hash: Int, private[this] var _value: V, private[this] var _next: Node[K, V]) {
def key: K = _key
def hash: Int = _hash
def value: V = _value
def value_= (v: V): Unit = _value = v
def next: Node[K, V] = _next
def next_= (n: Node[K, V]): Unit = _next = n
@tailrec
def findNode(k: K, h: Int): Node[K, V] =
if(h == _hash && k == _key) this
else if((_next eq null) || (_hash > h)) null
else _next.findNode(k, h)
@tailrec
def foreach[U](f: ((K, V)) => U): Unit = {
f((_key, _value))
if(_next ne null) _next.foreach(f)
}
override def toString = s"Node($key, $value, $hash) -> $next"
}
}