@@ -154,6 +154,8 @@ final class HashMap[K, +V] private[immutable] (private[immutable] val rootNode:
154
154
}
155
155
}
156
156
157
+ override def transform [W ](f : (K , V ) => W ) = new HashMap (rootNode.transform(f), cachedJavaKeySetHashCode)
158
+
157
159
override def filterImpl (pred : ((K , V )) => Boolean , flipped : Boolean ): HashMap [K , V ] = {
158
160
// This method has been preemptively overridden in order to ensure that an optimizing implementation may be included
159
161
// in a minor release without breaking binary compatibility.
@@ -173,15 +175,6 @@ final class HashMap[K, +V] private[immutable] (private[immutable] val rootNode:
173
175
super .removeAll(keys)
174
176
}
175
177
176
- override def transform [W ](f : (K , V ) => W ): HashMap [K , W ] = {
177
- // This method has been preemptively overridden in order to ensure that an optimizing implementation may be included
178
- // in a minor release without breaking binary compatibility.
179
- //
180
- // In particular, `transform` could be optimized to traverse the trie node-by-node, swapping out the values of each
181
- // key with the result of applying `f`.
182
- super .transform(f)
183
- }
184
-
185
178
override def partition (p : ((K , V )) => Boolean ): (HashMap [K , V ], HashMap [K , V ]) = {
186
179
// This method has been preemptively overridden in order to ensure that an optimizing implementation may be included
187
180
// in a minor release without breaking binary compatibility.
@@ -255,6 +248,7 @@ final class HashMap[K, +V] private[immutable] (private[immutable] val rootNode:
255
248
// checks.
256
249
super .span(p)
257
250
}
251
+
258
252
}
259
253
260
254
private [immutable] object MapNode {
@@ -303,6 +297,8 @@ private[immutable] sealed abstract class MapNode[K, +V] extends Node[MapNode[K,
303
297
304
298
def foreach [U ](f : ((K , V )) => U ): Unit
305
299
300
+ def transform [W ](f : (K , V ) => W ): MapNode [K , W ]
301
+
306
302
def copy (): MapNode [K , V ]
307
303
}
308
304
@@ -655,6 +651,26 @@ private final class BitmapIndexedMapNode[K, +V](
655
651
}
656
652
}
657
653
654
+ override def transform [W ](f : (K , V ) => W ): BitmapIndexedMapNode [K , W ] = {
655
+ val newContent = content.clone()
656
+ val _payloadArity = payloadArity
657
+ val _nodeArity = nodeArity
658
+ val newContentLength = newContent.length
659
+ var i = 0
660
+ while (i < _payloadArity) {
661
+ newContent(TupleLength * i + 1 ) = f(getKey(i), getValue(i))
662
+ i += 1
663
+ }
664
+
665
+ var j = 0
666
+ while (j < _nodeArity) {
667
+ newContent(newContentLength - j - 1 ) = getNode(j).transform(f)
668
+ j += 1
669
+ }
670
+
671
+ new BitmapIndexedMapNode [K , W ](dataMap, nodeMap, newContent, originalHashes, size)
672
+ }
673
+
658
674
override def equals (that : Any ): Boolean =
659
675
that match {
660
676
case node : BitmapIndexedMapNode [K , V ] =>
@@ -801,6 +817,16 @@ private final class HashCollisionMapNode[K, +V ](
801
817
802
818
def foreach [U ](f : ((K , V )) => U ): Unit = content.foreach(f)
803
819
820
+ override def transform [W ](f : (K , V ) => W ): HashCollisionMapNode [K , W ] = {
821
+ val newContent = Vector .newBuilder[(K , W )]
822
+ val contentIter = content.iterator
823
+ while (contentIter.hasNext) {
824
+ val (k, v) = contentIter.next()
825
+ newContent.addOne((k, f(k, v)))
826
+ }
827
+ new HashCollisionMapNode (originalHash, hash, newContent.result())
828
+ }
829
+
804
830
override def equals (that : Any ): Boolean =
805
831
that match {
806
832
case node : HashCollisionMapNode [K , V ] =>
0 commit comments