Skip to content

Commit 8a3f3d3

Browse files
committed
[backport] Rework recent, buggy change to immutable.TreeMap
We need to favour keys from the left that equivalent (but ne), while favouring values from the right. (cherry picked from commit 07e2d56)
1 parent 4ad7b1f commit 8a3f3d3

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

library/src/scala/collection/immutable/RedBlackTree.scala

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -820,17 +820,17 @@ private[collection] object RedBlackTree {
820820
} else mkTree(isRedTree(tl) || isRedTree(tr), k, v, tl, tr)
821821
}
822822

823-
private[this] def split[A, B](t: Tree[A, B], k: A)(implicit ordering: Ordering[A]): (Tree[A, B], Tree[A, B], Tree[A, B]) =
824-
if(t eq null) (null, null, null)
823+
private[this] def split[A, B](t: Tree[A, B], k2: A)(implicit ordering: Ordering[A]): (Tree[A, B], Tree[A, B], Tree[A, B], A) =
824+
if(t eq null) (null, null, null, k2)
825825
else {
826-
val cmp = ordering.compare(k, t.key)
827-
if(cmp == 0) (t.left, t, t.right)
826+
val cmp = ordering.compare(k2, t.key)
827+
if(cmp == 0) (t.left, t, t.right, t.key)
828828
else if(cmp < 0) {
829-
val (ll, b, lr) = split(t.left, k)
830-
(ll, b, join(lr, t.key, t.value, t.right))
829+
val (ll, b, lr, k1) = split(t.left, k2)
830+
(ll, b, join(lr, t.key, t.value, t.right), k1)
831831
} else {
832-
val (rl, b, rr) = split(t.right, k)
833-
(join(t.left, t.key, t.value, rl), b, rr)
832+
val (rl, b, rr, k1) = split(t.right, k2)
833+
(join(t.left, t.key, t.value, rl), b, rr, k1)
834834
}
835835
}
836836

@@ -853,28 +853,28 @@ private[collection] object RedBlackTree {
853853
if((t1 eq null) || (t1 eq t2)) t2
854854
else if(t2 eq null) t1
855855
else {
856-
val (l2, _, r2) = split(t2, t1.key)
857-
val tl = _union(t1.left, l2)
858-
val tr = _union(t1.right, r2)
859-
join(tl, t1.key, t1.value, tr)
856+
val (l1, _, r1, k1) = split(t1, t2.key)
857+
val tl = _union(l1, t2.left)
858+
val tr = _union(r1, t2.right)
859+
join(tl, k1, t2.value, tr)
860860
}
861861

862862
private[this] def _intersect[A, B](t1: Tree[A, B], t2: Tree[A, B])(implicit ordering: Ordering[A]): Tree[A, B] =
863863
if((t1 eq null) || (t2 eq null)) null
864864
else if (t1 eq t2) t1
865865
else {
866-
val (l2, b, r2) = split(t2, t1.key)
867-
val tl = _intersect(t1.left, l2)
868-
val tr = _intersect(t1.right, r2)
869-
if(b ne null) join(tl, t1.key, t1.value, tr)
866+
val (l1, b, r1, k1) = split(t1, t2.key)
867+
val tl = _intersect(l1, t2.left)
868+
val tr = _intersect(r1, t2.right)
869+
if(b ne null) join(tl, k1, t2.value, tr)
870870
else join2(tl, tr)
871871
}
872872

873873
private[this] def _difference[A, B](t1: Tree[A, B], t2: Tree[A, B])(implicit ordering: Ordering[A]): Tree[A, B] =
874874
if((t1 eq null) || (t2 eq null)) t1
875875
else if (t1 eq t2) null
876876
else {
877-
val (l1, _, r1) = split(t1, t2.key)
877+
val (l1, _, r1, k1) = split(t1, t2.key)
878878
val tl = _difference(l1, t2.left)
879879
val tr = _difference(r1, t2.right)
880880
join2(tl, tr)

0 commit comments

Comments
 (0)