Skip to content

Commit bd67c1b

Browse files
committed
adjust for 2.12 key semantics in TreeMap & TreeSet.
leave comment to explain the areas that need attention in the forward port to 2.13
1 parent 439ccfd commit bd67c1b

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

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

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ private[collection] object NewRedBlackTree {
151151
mutableBalanceLeft(tree, mutableUpd(tree.left, k))
152152
else if (cmp > 0)
153153
mutableBalanceRight(tree, mutableUpd(tree.right, k))
154+
else if (k != tree.key)
155+
tree.mutableWithK(k)
156+
//Note - in 2.13 remove the above else clause
157+
// due to the different handling of key equality
154158
else tree
155159
}
156160
}
@@ -166,6 +170,10 @@ private[collection] object NewRedBlackTree {
166170
mutableBalanceLeft(tree, mutableUpd(tree.left, k, v))
167171
else if (cmp > 0)
168172
mutableBalanceRight(tree, mutableUpd(tree.right, k, v))
173+
else if (k != tree.key)
174+
tree.mutableWithKV(k,v)
175+
//Note - in 2.13 remove the above else clause
176+
// due to the different handling of key equality
169177
else tree.mutableWithV(v)
170178
}
171179
}
@@ -569,12 +577,32 @@ private[collection] object NewRedBlackTree {
569577
// }
570578
// else new Tree(_key, _value, _left, _right, initialRedCount)
571579
// }
572-
def mutableWithV[B1 >: B](value: B1): Tree[A, B1] = {
573-
if (value.asInstanceOf[AnyRef] eq _value.asInstanceOf[AnyRef]) this
580+
//Note - in 2.13 remove his method
581+
//due to the handling of keys in 2.13 we never replace a key
582+
def mutableWithK[B1 >: B](newKey: A): Tree[A, B1] = {
583+
if (newKey.asInstanceOf[AnyRef] eq _key.asInstanceOf[AnyRef]) this
574584
else if (mutable) {
585+
_key = newKey
586+
this
587+
} else new Tree(newKey, _value.asInstanceOf[AnyRef], _left, _right, _count)
588+
}
589+
def mutableWithV[B1 >: B](newValue: B1): Tree[A, B1] = {
590+
if (newValue.asInstanceOf[AnyRef] eq _value.asInstanceOf[AnyRef]) this
591+
else if (mutable) {
592+
_value = newValue.asInstanceOf[AnyRef]
593+
this
594+
} else new Tree(_key, newValue.asInstanceOf[AnyRef], _left, _right, _count)
595+
}
596+
//Note - in 2.13 remove his method
597+
//due to the handling of keys in 2.13 we never replace a key
598+
def mutableWithKV[B1 >: B](newKey: A, newValue: B1): Tree[A, B1] = {
599+
if ((newKey.asInstanceOf[AnyRef] eq _key.asInstanceOf[AnyRef]) &&
600+
(newValue.asInstanceOf[AnyRef] eq _value.asInstanceOf[AnyRef])) this
601+
else if (mutable) {
602+
_key = newKey
575603
_value = value.asInstanceOf[AnyRef]
576604
this
577-
} else new Tree(_key, value.asInstanceOf[AnyRef], _left, _right, _count)
605+
} else new Tree(newKey, newValue.asInstanceOf[AnyRef], _left, _right, _count)
578606
}
579607
def mutableWithLeft[B1 >: B](newLeft: Tree[A, B1]): Tree[A, B1] = {
580608
if (_left eq newLeft) this

0 commit comments

Comments
 (0)