Skip to content

Commit ee6b336

Browse files
author
Martijn Hoekstra
committed
use classtag
1 parent 984f684 commit ee6b336

File tree

2 files changed

+22
-50
lines changed

2 files changed

+22
-50
lines changed

compat/src/main/scala-2.11_2.12/scala/collection/compat/CompatImpl.scala

+19-47
Original file line numberDiff line numberDiff line change
@@ -12,73 +12,45 @@
1212

1313
package scala.collection.compat
1414

15-
import scala.collection.LinearSeq
15+
import scala.reflect.ClassTag
1616
import scala.collection.generic.CanBuildFrom
1717
import scala.collection.mutable.Builder
1818
import scala.collection.{immutable => i, mutable => m}
1919

2020
/* builder optimized for a single ++= call, which returns identity on result if possible
2121
* and defers to the underlying builder if not.
2222
*/
23-
private final class IdentityPreservingSeqBuilder[A](that: Builder[A, Seq[A]])
24-
extends Builder[A, Seq[A]] {
25-
var collection: Seq[A] = null
23+
private final class IdentityPreservingBuilder[A, CC[X] <: TraversableOnce[X]](that: Builder[A, CC[A]])(implicit ct: ClassTag[CC[A]])
24+
extends Builder[A, CC[A]] {
25+
var collection: CC[A] = null.asInstanceOf[CC[A]]
2626
var ruined = false
2727

2828
final override def ++=(elems: TraversableOnce[A]): this.type =
29-
if(!ruined && collection == null && elems.isInstanceOf[Seq[_]]) {
30-
collection = elems.asInstanceOf[Seq[A]]
31-
this
32-
}
33-
else {
34-
ruined = true
35-
if (collection != null) that ++= collection
36-
that ++= elems
37-
collection = null
38-
this
39-
}
40-
41-
final def +=(elem: A): this.type = {
42-
collection = null
43-
ruined = true
44-
that += elem
45-
this
46-
}
47-
final def clear(): Unit = {
48-
collection = null
49-
if (ruined) that.clear()
50-
}
51-
final def result(): Seq[A] = if(ruined) that.result() else if (collection eq null) Nil else collection
52-
}
53-
54-
private final class IdentityPreservingLinearSeqBuilder[A](that: Builder[A, LinearSeq[A]]) extends Builder[A, LinearSeq[A]] {
55-
var collection: LinearSeq[A] = null
56-
var ruined = false
57-
58-
final override def ++=(elems: TraversableOnce[A]): this.type =
59-
if(!ruined && collection == null && elems.isInstanceOf[LinearSeq[_]]) {
60-
collection = elems.asInstanceOf[LinearSeq[A]]
61-
this
62-
}
63-
else {
64-
ruined = true
65-
if (collection != null) that ++= collection
66-
that ++= elems
67-
collection = null
68-
this
29+
elems match {
30+
case ct(ca) if (collection == null && !ruined) => {
31+
collection = ca
32+
this
33+
}
34+
case _ => {
35+
ruined = true
36+
if (collection != null) that ++= collection
37+
that ++= elems
38+
collection = null.asInstanceOf[CC[A]]
39+
this
40+
}
6941
}
7042

7143
final def +=(elem: A): this.type = {
72-
collection = null
44+
collection = null.asInstanceOf[CC[A]]
7345
ruined = true
7446
that += elem
7547
this
7648
}
7749
final def clear(): Unit = {
78-
collection = null
50+
collection = null.asInstanceOf[CC[A]]
7951
if (ruined) that.clear()
8052
}
81-
final def result(): LinearSeq[A] = if(ruined) that.result() else if (collection eq null) Nil else collection
53+
final def result(): CC[A] = if(ruined || (collection == null)) that.result() else collection
8254
}
8355

8456
private[compat] object CompatImpl {

compat/src/main/scala-2.11_2.12/scala/collection/compat/PackageShared.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ private[compat] trait PackageShared {
4646

4747
implicit def genericCompanionToCBF[A, CC[X] <: GenTraversable[X]](
4848
fact: GenericCompanion[CC]): CanBuildFrom[Any, A, CC[A]] = {
49-
val builder = if (fact == Seq) new IdentityPreservingSeqBuilder[A](Seq.newBuilder[A]).asInstanceOf[m.Builder[A, CC[A]]]
50-
else if (fact == LinearSeq) new IdentityPreservingLinearSeqBuilder[A](LinearSeq.newBuilder[A]).asInstanceOf[m.Builder[A, CC[A]]]
49+
val builder = if (fact == Seq) new IdentityPreservingBuilder[A, Seq](Seq.newBuilder[A])
50+
else if (fact == LinearSeq) new IdentityPreservingBuilder[A, LinearSeq](LinearSeq.newBuilder[A])
5151
else fact.newBuilder[A]
52-
simpleCBF(builder)
52+
simpleCBF(builder.asInstanceOf[m.Builder[A, CC[A]]])
5353
}
5454

5555
implicit def sortedSetCompanionToCBF[A: Ordering,

0 commit comments

Comments
 (0)