diff options
author | Paul Phillips <paulp@improving.org> | 2012-11-30 04:39:08 +0100 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2012-12-27 15:47:28 -0800 |
commit | 3059e3a0c039645158d2e5533e84d00f508ca824 (patch) | |
tree | 5501c2acb463fca76a7c5c27b4f5c7fd70a3c056 /src/library/scala/collection/TraversableLike.scala | |
parent | c53359ecbe135e79d55a6806209a6301bb386ada (diff) | |
download | scala-3059e3a0c039645158d2e5533e84d00f508ca824.tar.gz scala-3059e3a0c039645158d2e5533e84d00f508ca824.tar.bz2 scala-3059e3a0c039645158d2e5533e84d00f508ca824.zip |
Eliminating more allocations in the collections.
Diffstat (limited to 'src/library/scala/collection/TraversableLike.scala')
-rw-r--r-- | src/library/scala/collection/TraversableLike.scala | 17 |
1 files changed, 10 insertions, 7 deletions
diff --git a/src/library/scala/collection/TraversableLike.scala b/src/library/scala/collection/TraversableLike.scala index c1a68b6b16..a55257d128 100644 --- a/src/library/scala/collection/TraversableLike.scala +++ b/src/library/scala/collection/TraversableLike.scala @@ -252,18 +252,21 @@ trait TraversableLike[+A, +Repr] extends Any b.result } + private def filterImpl(p: A => Boolean, isFlipped: Boolean): Repr = { + val b = newBuilder + for (x <- this) + if (p(x) != isFlipped) b += x + + b.result + } + /** Selects all elements of this $coll which satisfy a predicate. * * @param p the predicate used to test elements. * @return a new $coll consisting of all elements of this $coll that satisfy the given * predicate `p`. The order of the elements is preserved. */ - def filter(p: A => Boolean): Repr = { - val b = newBuilder - for (x <- this) - if (p(x)) b += x - b.result - } + def filter(p: A => Boolean): Repr = filterImpl(p, isFlipped = false) /** Selects all elements of this $coll which do not satisfy a predicate. * @@ -271,7 +274,7 @@ trait TraversableLike[+A, +Repr] extends Any * @return a new $coll consisting of all elements of this $coll that do not satisfy the given * predicate `p`. The order of the elements is preserved. */ - def filterNot(p: A => Boolean): Repr = filter(!p(_)) + def filterNot(p: A => Boolean): Repr = filterImpl(p, isFlipped = true) def collect[B, That](pf: PartialFunction[A, B])(implicit bf: CanBuildFrom[Repr, B, That]): That = { val b = bf(repr) |