|
12 | 12 |
|
13 | 13 | package scala.collection.compat |
14 | 14 |
|
| 15 | +import scala.collection.LinearSeq |
15 | 16 | import scala.collection.generic.CanBuildFrom |
16 | 17 | import scala.collection.mutable.Builder |
17 | 18 | import scala.collection.{immutable => i, mutable => m} |
18 | 19 |
|
19 | 20 | /* builder optimized for a single ++= call, which returns identity on result if possible |
20 | 21 | * and defers to the underlying builder if not. |
21 | 22 | */ |
22 | | -private final class IdentityPreservingSeqBuilder[A, C <: Seq[A]](that: Builder[A, C]) |
| 23 | +private final class IdentityPreservingSeqBuilder[A](that: Builder[A, Seq[A]]) |
23 | 24 | extends Builder[A, Seq[A]] { |
24 | 25 | var collection: Seq[A] = null |
25 | 26 | var ruined = false |
@@ -50,6 +51,36 @@ private final class IdentityPreservingSeqBuilder[A, C <: Seq[A]](that: Builder[A |
50 | 51 | final def result(): Seq[A] = if(ruined) that.result() else if (collection eq null) Nil else collection |
51 | 52 | } |
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 |
| 69 | + } |
| 70 | + |
| 71 | + final def +=(elem: A): this.type = { |
| 72 | + collection = null |
| 73 | + ruined = true |
| 74 | + that += elem |
| 75 | + this |
| 76 | + } |
| 77 | + final def clear(): Unit = { |
| 78 | + collection = null |
| 79 | + if (ruined) that.clear() |
| 80 | + } |
| 81 | + final def result(): LinearSeq[A] = if(ruined) that.result() else if (collection eq null) Nil else collection |
| 82 | +} |
| 83 | + |
53 | 84 | private[compat] object CompatImpl { |
54 | 85 | def simpleCBF[A, C](f: => Builder[A, C]): CanBuildFrom[Any, A, C] = new CanBuildFrom[Any, A, C] { |
55 | 86 | def apply(from: Any): Builder[A, C] = apply() |
|
0 commit comments