Skip to content

Commit 984f684

Browse files
author
Martijn Hoekstra
committed
duplicate logic for LinearSeq
1 parent 1d8ef07 commit 984f684

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212

1313
package scala.collection.compat
1414

15+
import scala.collection.LinearSeq
1516
import scala.collection.generic.CanBuildFrom
1617
import scala.collection.mutable.Builder
1718
import scala.collection.{immutable => i, mutable => m}
1819

1920
/* builder optimized for a single ++= call, which returns identity on result if possible
2021
* and defers to the underlying builder if not.
2122
*/
22-
private final class IdentityPreservingSeqBuilder[A, C <: Seq[A]](that: Builder[A, C])
23+
private final class IdentityPreservingSeqBuilder[A](that: Builder[A, Seq[A]])
2324
extends Builder[A, Seq[A]] {
2425
var collection: Seq[A] = null
2526
var ruined = false
@@ -50,6 +51,36 @@ private final class IdentityPreservingSeqBuilder[A, C <: Seq[A]](that: Builder[A
5051
final def result(): Seq[A] = if(ruined) that.result() else if (collection eq null) Nil else collection
5152
}
5253

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+
5384
private[compat] object CompatImpl {
5485
def simpleCBF[A, C](f: => Builder[A, C]): CanBuildFrom[Any, A, C] = new CanBuildFrom[Any, A, C] {
5586
def apply(from: Any): Builder[A, C] = apply()

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ package scala.collection.compat
1414

1515
import scala.collection.generic._
1616
import scala.reflect.ClassTag
17-
import scala.collection.{MapLike, GenTraversable, BitSet, IterableView}
17+
import scala.collection.{LinearSeq, MapLike, GenTraversable, BitSet, IterableView}
1818
import scala.collection.{immutable => i, mutable => m}
1919
import scala.{collection => c}
2020

@@ -46,7 +46,8 @@ 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[A]](Seq.newBuilder[A]).asInstanceOf[m.Builder[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]]]
5051
else fact.newBuilder[A]
5152
simpleCBF(builder)
5253
}

compat/src/test/scala/test/scala/collection/CollectionTest.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import org.junit.Test
1717

1818
import scala.collection.compat._
1919
import scala.collection.immutable.BitSet
20+
import scala.collection.LinearSeq
2021

2122
class CollectionTest {
2223
@Test
@@ -44,6 +45,7 @@ class CollectionTest {
4445
// Stream.to(Seq) doesn't evaluate the stream
4546
val strm = 1 #:: {throw new Exception("not lazy")} #:: Stream.empty[Int]
4647
val strmsq: Seq[Int] = strm.to(Seq)
48+
var strmln: LinearSeq[Int] = strm.to(LinearSeq)
4749
}
4850

4951
@Test

0 commit comments

Comments
 (0)