|
12 | 12 |
|
13 | 13 | package scala.collection.compat |
14 | 14 |
|
15 | | -import scala.collection.LinearSeq |
| 15 | +import scala.reflect.ClassTag |
16 | 16 | import scala.collection.generic.CanBuildFrom |
17 | 17 | import scala.collection.mutable.Builder |
18 | 18 | import scala.collection.{immutable => i, mutable => m} |
19 | 19 |
|
20 | 20 | /* builder optimized for a single ++= call, which returns identity on result if possible |
21 | 21 | * and defers to the underlying builder if not. |
22 | 22 | */ |
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]] |
26 | 26 | var ruined = false |
27 | 27 |
|
28 | 28 | 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 | + } |
69 | 41 | } |
70 | 42 |
|
71 | 43 | final def +=(elem: A): this.type = { |
72 | | - collection = null |
| 44 | + collection = null.asInstanceOf[CC[A]] |
73 | 45 | ruined = true |
74 | 46 | that += elem |
75 | 47 | this |
76 | 48 | } |
77 | 49 | final def clear(): Unit = { |
78 | | - collection = null |
| 50 | + collection = null.asInstanceOf[CC[A]] |
79 | 51 | if (ruined) that.clear() |
80 | 52 | } |
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 |
82 | 54 | } |
83 | 55 |
|
84 | 56 | private[compat] object CompatImpl { |
|
0 commit comments