diff options
author | Paul Phillips <paulp@improving.org> | 2011-01-04 01:17:05 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2011-01-04 01:17:05 +0000 |
commit | 266a2ca1c4ed1c655989c2435a9e83650b06f79a (patch) | |
tree | 59a330e06b7d0b575423ce649fb61e832fc59298 /src | |
parent | 7d0e5ac4bb93009c9ae1392bd9a7b3628dee4e2a (diff) | |
download | scala-266a2ca1c4ed1c655989c2435a9e83650b06f79a.tar.gz scala-266a2ca1c4ed1c655989c2435a9e83650b06f79a.tar.bz2 scala-266a2ca1c4ed1c655989c2435a9e83650b06f79a.zip |
Modified generic companion apply to call empty ...
Modified generic companion apply to call empty if there are no
arguments, so something like Set() does not generate unnecessary
garbage. Also found some immutable classes which don't reuse an empty
object for emptiness, and gave them one. No review.
Diffstat (limited to 'src')
4 files changed, 15 insertions, 7 deletions
diff --git a/src/library/scala/collection/generic/GenericCompanion.scala b/src/library/scala/collection/generic/GenericCompanion.scala index cfcd2a94cf..2d2111f76d 100644 --- a/src/library/scala/collection/generic/GenericCompanion.scala +++ b/src/library/scala/collection/generic/GenericCompanion.scala @@ -43,9 +43,11 @@ abstract class GenericCompanion[+CC[X] <: Traversable[X]] { * @return a new $coll with elements `elems` */ def apply[A](elems: A*): CC[A] = { - val b = newBuilder[A] - b ++= elems - b.result + if (elems.isEmpty) empty[A] + else { + val b = newBuilder[A] + b ++= elems + b.result + } } } - diff --git a/src/library/scala/collection/immutable/ListMap.scala b/src/library/scala/collection/immutable/ListMap.scala index 5fced699f3..9ce5410546 100644 --- a/src/library/scala/collection/immutable/ListMap.scala +++ b/src/library/scala/collection/immutable/ListMap.scala @@ -23,7 +23,9 @@ object ListMap extends ImmutableMapFactory[ListMap] { /** $mapCanBuildFromInfo */ implicit def canBuildFrom[A, B]: CanBuildFrom[Coll, (A, B), ListMap[A, B]] = new MapCanBuildFrom[A, B] - def empty[A, B]: ListMap[A, B] = new ListMap + def empty[A, B]: ListMap[A, B] = EmptyListMap.asInstanceOf[ListMap[A, B]] + + private object EmptyListMap extends ListMap[Any, Nothing] { } } /** This class implements immutable maps using a list-based data structure. diff --git a/src/library/scala/collection/immutable/ListSet.scala b/src/library/scala/collection/immutable/ListSet.scala index 8547ba195b..ec0f2ff5a1 100644 --- a/src/library/scala/collection/immutable/ListSet.scala +++ b/src/library/scala/collection/immutable/ListSet.scala @@ -23,9 +23,11 @@ import mutable.{ ListBuffer, Builder } object ListSet extends ImmutableSetFactory[ListSet] { /** setCanBuildFromInfo */ implicit def canBuildFrom[A]: CanBuildFrom[Coll, A, ListSet[A]] = setCanBuildFrom[A] - override def empty[A] = new ListSet[A] + override def empty[A] = EmptyListSet.asInstanceOf[ListSet[A]] override def newBuilder[A]: Builder[A, ListSet[A]] = new ListSetBuilder[A] + private object EmptyListSet extends ListSet[Any] { } + /** A custom builder because forgetfully adding elements one at * a time to a list backed set puts the "squared" in N^2. There is a * temporary space cost, but it's improbable a list backed set could diff --git a/src/library/scala/collection/immutable/Queue.scala b/src/library/scala/collection/immutable/Queue.scala index bd2f27e57e..3e0181f1a4 100644 --- a/src/library/scala/collection/immutable/Queue.scala +++ b/src/library/scala/collection/immutable/Queue.scala @@ -146,9 +146,11 @@ object Queue extends SeqFactory[Queue] { /** $genericCanBuildFromInfo */ implicit def canBuildFrom[A]: CanBuildFrom[Coll, A, Queue[A]] = new GenericCanBuildFrom[A] def newBuilder[A]: Builder[A, Queue[A]] = new ListBuffer[A] mapResult (x => new Queue[A](Nil, x.toList)) - override def empty[A]: Queue[A] = new Queue[A](Nil, Nil) + override def empty[A]: Queue[A] = EmptyQueue.asInstanceOf[Queue[A]] override def apply[A](xs: A*): Queue[A] = new Queue[A](Nil, xs.toList) + private object EmptyQueue extends Queue[Nothing](Nil, Nil) { } + @deprecated("Use Queue.empty instead") val Empty: Queue[Nothing] = Queue() } |