Skip to content

fix sameCBF in TreeSet and TreeMap #8952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,10 @@ val mimaFilterSettings = Seq {
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.collection.immutable.HashMap#HashTrieMap.size0_="),
ProblemFilters.exclude[FinalMethodProblem]("scala.collection.immutable.HashMap#HashTrieMap.size"),

ProblemFilters.exclude[DirectMissingMethodProblem]("scala.collection.generic.SortedMapFactory#SortedMapCanBuildFrom.factory"),
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.collection.generic.SortedSetFactory#SortedSetCanBuildFrom.factory"),
ProblemFilters.exclude[DirectMissingMethodProblem]("scala.collection.generic.SortedSetFactory#SortedSetCanBuildFrom.ordering"),

//
// scala-relect
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ abstract class SortedMapFactory[CC[A, B] <: SortedMap[A, B] with SortedMapLike[A
new MapBuilder[A, B, CC[A, B]](empty(ord))

class SortedMapCanBuildFrom[A, B](implicit ord: Ordering[A]) extends CanBuildFrom[Coll, (A, B), CC[A, B]] {
private[collection] def factory = SortedMapFactory.this
private[collection] def ordering = ord
def apply(from: Coll) = newBuilder[A, B](ord)
def apply() = newBuilder[A, B]
Expand Down
2 changes: 2 additions & 0 deletions src/library/scala/collection/generic/SortedSetFactory.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ abstract class SortedSetFactory[CC[A] <: SortedSet[A] with SortedSetLike[A, CC[A
implicit def newCanBuildFrom[A](implicit ord : Ordering[A]) : CanBuildFrom[Coll, A, CC[A]] = new SortedSetCanBuildFrom()(ord)

class SortedSetCanBuildFrom[A](implicit ord: Ordering[A]) extends CanBuildFrom[Coll, A, CC[A]] {
private[collection] def factory = SortedSetFactory.this
private[collection] def ordering = ord
def apply(from: Coll) = newBuilder[A](ord)
def apply() = newBuilder[A](ord)
}
Expand Down
22 changes: 15 additions & 7 deletions src/library/scala/collection/immutable/TreeMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,22 @@ final class TreeMap[A, +B] private (tree: RB.Tree[A, B])(implicit val ordering:
val (l, r) = RB.partitionEntries[A, B](tree, (k, v) => p((k, v)))
(newMapOrSelf(l), newMapOrSelf(r))
}

override def transform[W, That](f: (A, B) => W)(implicit bf: CanBuildFrom[TreeMap[A, B], (A, W), That]): That = {
private def sameCBF(bf: CanBuildFrom[_,_,_]): Boolean = {
bf match {
case sm: TreeMap.SortedMapCanBuildFrom[A, B] if sm.ordering == ordering =>
val t2 = RB.transform[A, B, W](tree, f)
if (t2 eq tree) this.asInstanceOf[That]
else new TreeMap(t2).asInstanceOf[That]
case _ => super.transform(f)
case cbf: SortedMapFactory[_]#SortedMapCanBuildFrom[_,_] => {
val factory:AnyRef = cbf.factory
((factory eq TreeMap) || (factory eq immutable.SortedMap) || (factory eq collection.SortedMap)) &&
cbf.ordering == ordering
}
case w: WrappedCanBuildFrom[_,_,_] => sameCBF(w.wrapped)
case _ => false
}
}


override def transform[W, That](f: (A, B) => W)(implicit bf: CanBuildFrom[TreeMap[A, B], (A, W), That]): That = {
if (sameCBF(bf))
newMapOrSelf(RB.transform[A, B, W](tree, f)).asInstanceOf[That]
else super.transform(f)
}
}
9 changes: 6 additions & 3 deletions src/library/scala/collection/immutable/TreeSet.scala
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,13 @@ final class TreeSet[A] private[immutable] (private[immutable] val tree: RB.Tree[
override def firstKey = head
override def lastKey = last


private def sameCBF(bf: CanBuildFrom[_,_,_]): Boolean = {
bf match {
case tsb: TreeSet.TreeSetBuilder[_] if tsb.ordering == ordering => true
case cbf: SortedSetFactory[_]#SortedSetCanBuildFrom[_] => {
val factory:AnyRef = cbf.factory
((factory eq TreeSet) || (factory eq immutable.SortedSet) || (factory eq collection.SortedSet)) &&
cbf.ordering == ordering
}
case w: WrappedCanBuildFrom[_,_,_] => sameCBF(w.wrapped)
case _ => false
}
Expand All @@ -221,7 +224,7 @@ final class TreeSet[A] private[immutable] (private[immutable] val tree: RB.Tree[

private[scala] def addAllImpl[B >: A, That](that: GenTraversableOnce[B])(implicit bf: CanBuildFrom[TreeSet[A], B, That]): That = {
that match {
case ts: TreeSet[A] if ordering == ts.ordering && sameCBF(bf) =>
case ts: TreeSet[A] if sameCBF(bf) =>
newSetOrSelf(RB.union(tree, ts.tree)).asInstanceOf[That]
case _ =>
val b = bf(repr)
Expand Down