summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2010-12-01 00:11:03 +0000
committerPaul Phillips <paulp@improving.org>2010-12-01 00:11:03 +0000
commit1c0d571f6db723ca751eb56555053756299e04a8 (patch)
tree6a0cb3c4d667fb7fb0794f74b6026eb4ae9998d3 /src
parent6b274687b355e5bcd6a49a0ec3b63031a28c87ea (diff)
downloadscala-1c0d571f6db723ca751eb56555053756299e04a8.tar.gz
scala-1c0d571f6db723ca751eb56555053756299e04a8.tar.bz2
scala-1c0d571f6db723ca751eb56555053756299e04a8.zip
Some collections overrides for more efficient t...
Some collections overrides for more efficient toSeq and toBuffer where possible. Closes #2972, review by malayeri.
Diffstat (limited to 'src')
-rw-r--r--src/library/scala/collection/IndexedSeqLike.scala9
-rw-r--r--src/library/scala/collection/IterableLike.scala7
-rw-r--r--src/library/scala/collection/MapLike.scala8
-rw-r--r--src/library/scala/collection/SetLike.scala8
4 files changed, 25 insertions, 7 deletions
diff --git a/src/library/scala/collection/IndexedSeqLike.scala b/src/library/scala/collection/IndexedSeqLike.scala
index a2044ae7ba..92047288fb 100644
--- a/src/library/scala/collection/IndexedSeqLike.scala
+++ b/src/library/scala/collection/IndexedSeqLike.scala
@@ -81,6 +81,15 @@ trait IndexedSeqLike[+A, +Repr] extends SeqLike[A, Repr] { self =>
override /*IterableLike*/
def iterator: Iterator[A] = new Elements(0, length)
+
+ /** Overridden for efficiency */
+ override def toBuffer[A1 >: A]: mutable.Buffer[A1] = {
+ val result = new mutable.ArrayBuffer[A1](size)
+ copyToBuffer(result)
+ result
+ }
+
+
/*
override /*SeqLike*/
def view = new IndexedSeqView[A, Repr] {
diff --git a/src/library/scala/collection/IterableLike.scala b/src/library/scala/collection/IterableLike.scala
index 97787c5867..72dabbf740 100644
--- a/src/library/scala/collection/IterableLike.scala
+++ b/src/library/scala/collection/IterableLike.scala
@@ -353,13 +353,6 @@ self =>
override /*TraversableLike*/ def toStream: Stream[A] = iterator.toStream
- /** Converts this $coll to a sequence.
- *
- * $willNotTerminateInf
- * @return a sequence containing all the elements of this $coll.
- */
- override /*TraversableOnce*/ def toSeq: Seq[A] = toList
-
/** Method called from equality methods, so that user-defined subclasses can
* refuse to be equal to other collections of the same kind.
* @param that The object with which this $coll should be compared
diff --git a/src/library/scala/collection/MapLike.scala b/src/library/scala/collection/MapLike.scala
index ae1d2cc6e8..df3e3c62ff 100644
--- a/src/library/scala/collection/MapLike.scala
+++ b/src/library/scala/collection/MapLike.scala
@@ -306,6 +306,14 @@ self =>
res
}
+ /** Overridden for efficiency. */
+ override def toSeq: Seq[(A, B)] = toBuffer[(A, B)]
+ override def toBuffer[C >: (A, B)]: mutable.Buffer[C] = {
+ val result = new mutable.ArrayBuffer[C](size)
+ copyToBuffer(result)
+ result
+ }
+
/** Appends all bindings of this map to a string builder using start, end, and separator strings.
* The written text begins with the string `start` and ends with the string
* `end`. Inside, the string representations of all bindings of this map
diff --git a/src/library/scala/collection/SetLike.scala b/src/library/scala/collection/SetLike.scala
index 5a3b9734ed..8df0fb9c9d 100644
--- a/src/library/scala/collection/SetLike.scala
+++ b/src/library/scala/collection/SetLike.scala
@@ -73,6 +73,14 @@ self =>
*/
override protected[this] def newBuilder: Builder[A, This] = new AddingBuilder[A, This](empty)
+ /** Overridden for efficiency. */
+ override def toSeq: Seq[A] = toBuffer[A]
+ override def toBuffer[A1 >: A]: mutable.Buffer[A1] = {
+ val result = new mutable.ArrayBuffer[A1](size)
+ copyToBuffer(result)
+ result
+ }
+
// note: this is only overridden here to add the migration annotation,
// which I hope to turn into an Xlint style warning as the migration aspect
// is not central to its importance.