diff options
author | Martin Odersky <odersky@gmail.com> | 2012-08-16 20:03:14 +0200 |
---|---|---|
committer | Grzegorz Kossakowski <grzegorz.kossakowski@gmail.com> | 2012-08-20 08:11:06 +0100 |
commit | 1ee7ffb6fd2de6e7194a4eb89601d98503b50048 (patch) | |
tree | a7395953072397beb1435c4907cf4e23cbb4e469 /src/library/scala/collection/LinearSeqOptimized.scala | |
parent | 0cde930b192acc73d1e0b5951b3300c286ae4dd2 (diff) | |
download | scala-1ee7ffb6fd2de6e7194a4eb89601d98503b50048.tar.gz scala-1ee7ffb6fd2de6e7194a4eb89601d98503b50048.tar.bz2 scala-1ee7ffb6fd2de6e7194a4eb89601d98503b50048.zip |
Optimizations to cut down on #closures created
Driven by profile data.
Diffstat (limited to 'src/library/scala/collection/LinearSeqOptimized.scala')
-rwxr-xr-x | src/library/scala/collection/LinearSeqOptimized.scala | 59 |
1 files changed, 58 insertions, 1 deletions
diff --git a/src/library/scala/collection/LinearSeqOptimized.scala b/src/library/scala/collection/LinearSeqOptimized.scala index 5e0bd010a6..2a06b9d0bd 100755 --- a/src/library/scala/collection/LinearSeqOptimized.scala +++ b/src/library/scala/collection/LinearSeqOptimized.scala @@ -93,6 +93,16 @@ trait LinearSeqOptimized[+A, +Repr <: LinearSeqOptimized[A, Repr]] extends Linea cnt } + override /*SeqLike*/ + def contains(elem: Any): Boolean = { + var these = this + while (!these.isEmpty) { + if (these.head == elem) return true + these = these.tail + } + false + } + override /*IterableLike*/ def find(p: A => Boolean): Option[A] = { var these = this @@ -113,7 +123,54 @@ trait LinearSeqOptimized[+A, +Repr <: LinearSeqOptimized[A, Repr]] extends Linea } acc } - + + override /*TraversableLike*/ + def map[B, That](f: A => B)(implicit bf: CanBuildFrom[Repr, B, That]): That = { + val b = bf(repr) + b.sizeHint(this) + var these = this + while (!these.isEmpty) { + b += f(these.head) + these = these.tail + } + b.result + } + + override /*TraversableLike*/ + def flatMap[B, That](f: A => GenTraversableOnce[B])(implicit bf: CanBuildFrom[Repr, B, That]): That = { + val b = bf(repr) + var these = this + while (!these.isEmpty) { + b ++= f(these.head).seq + these = these.tail + } + b.result + } + + override /*TraversableLike*/ + def filter(p: A => Boolean): Repr = { + val b = newBuilder + var these = this + while (!these.isEmpty) { + val x = these.head + if (p(x)) b += x + these = these.tail + } + b.result + } + + override /*TraversableLike*/ + def filterNot(p: A => Boolean): Repr = { + val b = newBuilder + var these = this + while (!these.isEmpty) { + val x = these.head + if (!p(x)) b += x + these = these.tail + } + b.result + } + override /*IterableLike*/ def foldRight[B](z: B)(f: (A, B) => B): B = if (this.isEmpty) z |