@@ -190,10 +190,10 @@ final class HashSet[A] private[immutable](private[immutable] val rootNode: Bitma
190
190
* Stops iterating the first time that f returns `false`.*/
191
191
@ `inline` private [collection] def foreachWithHashWhile (f : (A , Int ) => Boolean ): Unit = rootNode.foreachWithHashWhile(f)
192
192
193
- def subsetOf (that : Set [A ]): Boolean = if ( that.isEmpty) true else that match {
193
+ def subsetOf (that : Set [A ]): Boolean = isEmpty || ! that.isEmpty && ( that match {
194
194
case set : HashSet [A ] => rootNode.subsetOf(set.rootNode, 0 )
195
195
case _ => super .subsetOf(that)
196
- }
196
+ })
197
197
198
198
override def equals (that : Any ): Boolean =
199
199
that match {
@@ -606,48 +606,37 @@ private final class BitmapIndexedSetNode[A](
606
606
607
607
if (element0 == element) {
608
608
if (this .payloadArity == 2 && this .nodeArity == 0 ) {
609
- /*
610
- * Create new node with remaining pair. The new node will a) either become the new root
611
- * returned, or b) unwrapped and inlined during returning.
612
- */
609
+ // Create new node with remaining pair. The new node will a) either become the new root
610
+ // returned, or b) unwrapped and inlined during returning.
613
611
val newDataMap = if (shift == 0 ) (dataMap ^ bitpos) else bitposFrom(maskFrom(elementHash, 0 ))
614
- if (index == 0 )
615
- return new BitmapIndexedSetNode [A ](newDataMap, 0 , Array (getPayload(1 )), Array (originalHashes(1 )), size - 1 , improve(originalHashes(1 )))
616
- else
617
- return new BitmapIndexedSetNode [A ](newDataMap, 0 , Array (getPayload(0 )), Array (originalHashes(0 )), size - 1 , improve(originalHashes(0 )))
612
+ if (index == 0 ) new BitmapIndexedSetNode [A ](newDataMap, 0 , Array (getPayload(1 )), Array (originalHashes(1 )), size - 1 , improve(originalHashes(1 )))
613
+ else new BitmapIndexedSetNode [A ](newDataMap, 0 , Array (getPayload(0 )), Array (originalHashes(0 )), size - 1 , improve(originalHashes(0 )))
618
614
}
619
- else return copyAndRemoveValue(bitpos, elementHash)
620
- } else return this
615
+ else copyAndRemoveValue(bitpos, elementHash)
616
+ }
617
+ else this
621
618
}
622
-
623
- if ((nodeMap & bitpos) != 0 ) {
619
+ else if ((nodeMap & bitpos) != 0 ) {
624
620
val index = indexFrom(nodeMap, mask, bitpos)
625
621
val subNode = this .getNode(index)
626
-
627
622
val subNodeNew = subNode.removed(element, originalHash, elementHash, shift + BitPartitionSize )
628
623
629
- if (subNodeNew eq subNode) return this
630
-
631
- // cache just in case subNodeNew is a hashCollision node, in which in which case a little arithmetic is avoided
632
- // in Vector#length
633
- val subNodeNewSize = subNodeNew.size
634
-
635
- if (subNodeNewSize == 1 ) {
636
- if (this .size == subNode.size) {
624
+ if (subNodeNew eq subNode) this
625
+ // if subNodeNew is a hashCollision node, size has cost in Vector#length
626
+ else subNodeNew.size match {
627
+ case 1 =>
637
628
// subNode is the only child (no other data or node children of `this` exist)
638
629
// escalate (singleton or empty) result
639
- return subNodeNew.asInstanceOf [BitmapIndexedSetNode [A ]]
640
- } else {
630
+ if (this .size == subNode.size) subNodeNew.asInstanceOf [BitmapIndexedSetNode [A ]]
641
631
// inline value (move to front)
642
- return copyAndMigrateFromNodeToInline(bitpos, elementHash, subNode, subNodeNew)
643
- }
644
- } else if (subNodeNewSize > 1 ) {
645
- // modify current node (set replacement node )
646
- return copyAndSetNode(bitpos, subNode, subNodeNew)
632
+ else copyAndMigrateFromNodeToInline(bitpos, elementHash, subNode, subNodeNew)
633
+ case subNodeNewSize if subNodeNewSize > 1 =>
634
+ // modify current node (set replacement node)
635
+ copyAndSetNode(bitpos, subNode, subNodeNew )
636
+ case _ => this
647
637
}
648
638
}
649
-
650
- this
639
+ else this
651
640
}
652
641
/** Variant of `removed` which will perform mutation on only the top-level node (`this`), rather than return a new
653
642
* node
@@ -999,7 +988,7 @@ private final class BitmapIndexedSetNode[A](
999
988
val elementHash = improve(elementUnimprovedHash)
1000
989
subNode.contains(payload, elementUnimprovedHash, elementHash, shift + BitPartitionSize )
1001
990
}
1002
- } else {
991
+ } else ((node.dataMap & bitpos) == 0 ) && {
1003
992
// Node x Node
1004
993
val subNode0 = this .getNode(indexFrom(this .nodeMap, bitpos))
1005
994
val subNode1 = node.getNode(indexFrom(node.nodeMap, bitpos))
@@ -1428,6 +1417,8 @@ private final class BitmapIndexedSetNode[A](
1428
1417
override def hashCode (): Int =
1429
1418
throw new UnsupportedOperationException (" Trie nodes do not support hashing." )
1430
1419
1420
+ override def toString : String = f " BitmapIndexedSetNode(size= $size, dataMap= $dataMap%x, nodeMap= $nodeMap%x) " // content=${scala.runtime.ScalaRunTime.stringOf(content)}
1421
+
1431
1422
override def copy (): BitmapIndexedSetNode [A ] = {
1432
1423
val contentClone = content.clone()
1433
1424
val contentLength = contentClone.length
@@ -2077,7 +2068,7 @@ private[collection] final class HashSetBuilder[A] extends ReusableBuilder[A, Has
2077
2068
ensureUnaliased()
2078
2069
val h = elem.##
2079
2070
val im = improve(h)
2080
- update(rootNode, elem, h, im, 0 )
2071
+ update(rootNode, elem, originalHash = h, elementHash = im, shift = 0 )
2081
2072
this
2082
2073
}
2083
2074
0 commit comments