From 83b67aa805fd1329d6bcc54b1c1fa16416437b6f Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Fri, 2 Oct 2009 17:57:16 +0000 Subject: Sequence->Seq --- lib/scala-compiler.jar.desired.sha1 | 2 +- lib/scala-library.jar.desired.sha1 | 2 +- src/compiler/scala/tools/nsc/Global.scala | 2 + src/compiler/scala/tools/nsc/doc/DocUtil.scala | 2 +- src/compiler/scala/tools/nsc/io/Path.scala | 2 +- src/compiler/scala/tools/nsc/io/ZipArchive.scala | 2 +- .../scala/tools/nsc/symtab/Definitions.scala | 4 +- src/compiler/scala/tools/nsc/symtab/Scopes.scala | 2 +- src/compiler/scala/tools/nsc/symtab/StdNames.scala | 2 +- src/compiler/scala/tools/nsc/symtab/Types.scala | 2 +- src/library/scala/Array.scala | 4 +- src/library/scala/collection/IterableLike.scala | 2 - src/library/scala/collection/Iterator.scala | 12 +- src/library/scala/collection/LinearSeq.scala | 43 ++ src/library/scala/collection/LinearSeqLike.scala | 405 ++++++++++++++ src/library/scala/collection/LinearSequence.scala | 43 -- .../scala/collection/LinearSequenceLike.scala | 405 -------------- src/library/scala/collection/Seq.scala | 62 +++ src/library/scala/collection/SeqLike.scala | 597 +++++++++++++++++++++ src/library/scala/collection/SeqProxy.scala | 21 + src/library/scala/collection/SeqProxyLike.scala | 63 +++ src/library/scala/collection/SeqView.scala | 28 + src/library/scala/collection/SeqViewLike.scala | 156 ++++++ src/library/scala/collection/Sequence.scala | 62 --- src/library/scala/collection/SequenceLike.scala | 597 --------------------- src/library/scala/collection/SequenceProxy.scala | 21 - .../scala/collection/SequenceProxyLike.scala | 63 --- src/library/scala/collection/SequenceView.scala | 28 - .../scala/collection/SequenceViewLike.scala | 156 ------ src/library/scala/collection/Traversable.scala | 2 +- src/library/scala/collection/TraversableLike.scala | 2 +- .../scala/collection/TraversableProxyLike.scala | 2 +- src/library/scala/collection/Vector.scala | 4 +- src/library/scala/collection/VectorLike.scala | 10 +- src/library/scala/collection/VectorViewLike.scala | 6 +- .../collection/generic/IterableForwarder.scala | 2 +- .../scala/collection/generic/SeqFactory.scala | 27 + .../scala/collection/generic/SeqForwarder.scala | 52 ++ .../scala/collection/generic/SequenceFactory.scala | 27 - .../collection/generic/SequenceForwarder.scala | 52 -- .../collection/generic/TraversableForwarder.scala | 4 +- .../scala/collection/immutable/LinearSeq.scala | 36 ++ .../collection/immutable/LinearSequence.scala | 36 -- src/library/scala/collection/immutable/List.scala | 12 +- src/library/scala/collection/immutable/Queue.scala | 2 +- src/library/scala/collection/immutable/Seq.scala | 36 ++ .../scala/collection/immutable/Sequence.scala | 36 -- src/library/scala/collection/immutable/Stack.scala | 2 +- .../scala/collection/immutable/Stream.scala | 12 +- .../scala/collection/immutable/StringLike.scala | 4 +- .../scala/collection/immutable/Vector.scala | 4 +- .../scala/collection/interfaces/SeqMethods.scala | 58 ++ .../collection/interfaces/SequenceMethods.scala | 58 -- .../collection/interfaces/TraversableMethods.scala | 2 +- .../scala/collection/mutable/ArrayBuffer.scala | 2 +- .../scala/collection/mutable/ArrayStack.scala | 2 +- src/library/scala/collection/mutable/Buffer.scala | 4 +- .../scala/collection/mutable/BufferLike.scala | 4 +- .../collection/mutable/DoubleLinkedList.scala | 4 +- .../collection/mutable/DoubleLinkedListLike.scala | 2 +- .../scala/collection/mutable/GenericArray.scala | 4 +- .../scala/collection/mutable/LinearSeq.scala | 32 ++ .../scala/collection/mutable/LinearSequence.scala | 32 -- .../scala/collection/mutable/LinkedList.scala | 4 +- .../scala/collection/mutable/LinkedListLike.scala | 2 +- .../scala/collection/mutable/ListBuffer.scala | 6 +- .../scala/collection/mutable/MutableList.scala | 4 +- src/library/scala/collection/mutable/Queue.scala | 4 +- .../scala/collection/mutable/ResizableArray.scala | 4 +- src/library/scala/collection/mutable/Seq.scala | 41 ++ .../scala/collection/mutable/Sequence.scala | 41 -- src/library/scala/collection/mutable/Stack.scala | 2 +- .../scala/collection/mutable/StringBuilder.scala | 2 + src/library/scala/collection/mutable/Vector.scala | 4 +- src/library/scala/package.scala | 6 +- src/library/scala/runtime/BoxedArray.scala | 2 +- src/library/scala/runtime/ScalaRunTime.scala | 4 +- src/library/scala/util/Random.scala | 8 +- src/library/scala/util/control/Exception.scala | 2 +- src/library/scala/xml/NodeSeq.scala | 4 +- .../scala/xml/transform/BasicTransformer.scala | 2 +- src/swing/scala/swing/ListView.scala | 4 +- src/swing/scala/swing/Table.scala | 2 +- test/files/neg/bug910.check | 4 +- test/files/neg/t0259.check | 2 +- test/files/pos/implicits.scala | 2 +- test/files/pos/spec-List.scala | 12 +- test/files/run/colltest1.scala | 20 +- test/files/run/sequenceComparisons.scala | 20 +- test/pending/neg/t1545.scala | 16 + test/pending/neg/t1800.scala | 28 + test/pending/neg/t1845.scala | 12 + test/pending/neg/t2080.scala | 17 + test/pending/neg/t2180.scala | 31 ++ test/pending/pos/local-objects.scala | 9 + test/pending/pos/t1722.scala | 10 + test/pending/pos/t1722/C.scala | 9 + test/pending/pos/t1722/Test.scala | 4 + test/pending/pos/t1722/Top.scala | 13 + test/pending/pos/t1756.scala | 30 ++ test/pending/pos/t1786.scala | 13 + test/pending/pos/t1798.scala | 10 + test/pending/pos/t1987.scala | 8 + test/pending/pos/t1996.scala | 19 + test/pending/pos/t2071.scala | 14 + test/pending/pos/t2081.scala | 7 + test/pending/pos/t2099.scala | 27 + test/pending/pos/t2108.scala | 2 + test/pending/pos/t2127.scala | 29 + test/pending/pos/t2130.scala | 44 ++ test/pending/pos/t2162.scala | 19 + test/pending/pos/t2173.scala | 17 + test/pending/pos/t2178.scala | 10 + test/pending/pos/t2179.scala | 6 + test/pending/pos/t2185,.scala | 3 + test/pending/pos/t2188.scala | 9 + test/pending/pos/t2194.scala | 15 + test/pending/pos/t2201.scala | 8 + test/pending/pos/ticket2251.scala | 25 + test/pending/run/t1939.scala | 42 ++ test/pending/run/t1980.scala | 13 + test/pending/run/t2005.scala | 24 + 122 files changed, 2331 insertions(+), 1784 deletions(-) create mode 100644 src/library/scala/collection/LinearSeq.scala create mode 100644 src/library/scala/collection/LinearSeqLike.scala delete mode 100644 src/library/scala/collection/LinearSequence.scala delete mode 100644 src/library/scala/collection/LinearSequenceLike.scala create mode 100644 src/library/scala/collection/Seq.scala create mode 100644 src/library/scala/collection/SeqLike.scala create mode 100644 src/library/scala/collection/SeqProxy.scala create mode 100644 src/library/scala/collection/SeqProxyLike.scala create mode 100644 src/library/scala/collection/SeqView.scala create mode 100644 src/library/scala/collection/SeqViewLike.scala delete mode 100644 src/library/scala/collection/Sequence.scala delete mode 100644 src/library/scala/collection/SequenceLike.scala delete mode 100644 src/library/scala/collection/SequenceProxy.scala delete mode 100644 src/library/scala/collection/SequenceProxyLike.scala delete mode 100644 src/library/scala/collection/SequenceView.scala delete mode 100644 src/library/scala/collection/SequenceViewLike.scala create mode 100644 src/library/scala/collection/generic/SeqFactory.scala create mode 100644 src/library/scala/collection/generic/SeqForwarder.scala delete mode 100644 src/library/scala/collection/generic/SequenceFactory.scala delete mode 100644 src/library/scala/collection/generic/SequenceForwarder.scala create mode 100644 src/library/scala/collection/immutable/LinearSeq.scala delete mode 100644 src/library/scala/collection/immutable/LinearSequence.scala create mode 100644 src/library/scala/collection/immutable/Seq.scala delete mode 100644 src/library/scala/collection/immutable/Sequence.scala create mode 100644 src/library/scala/collection/interfaces/SeqMethods.scala delete mode 100644 src/library/scala/collection/interfaces/SequenceMethods.scala create mode 100644 src/library/scala/collection/mutable/LinearSeq.scala delete mode 100644 src/library/scala/collection/mutable/LinearSequence.scala create mode 100644 src/library/scala/collection/mutable/Seq.scala delete mode 100644 src/library/scala/collection/mutable/Sequence.scala create mode 100755 test/pending/neg/t1545.scala create mode 100644 test/pending/neg/t1800.scala create mode 100644 test/pending/neg/t1845.scala create mode 100644 test/pending/neg/t2080.scala create mode 100644 test/pending/neg/t2180.scala create mode 100644 test/pending/pos/local-objects.scala create mode 100644 test/pending/pos/t1722.scala create mode 100755 test/pending/pos/t1722/C.scala create mode 100755 test/pending/pos/t1722/Test.scala create mode 100755 test/pending/pos/t1722/Top.scala create mode 100644 test/pending/pos/t1756.scala create mode 100644 test/pending/pos/t1786.scala create mode 100644 test/pending/pos/t1798.scala create mode 100644 test/pending/pos/t1987.scala create mode 100644 test/pending/pos/t1996.scala create mode 100644 test/pending/pos/t2071.scala create mode 100644 test/pending/pos/t2081.scala create mode 100644 test/pending/pos/t2099.scala create mode 100644 test/pending/pos/t2108.scala create mode 100644 test/pending/pos/t2127.scala create mode 100644 test/pending/pos/t2130.scala create mode 100644 test/pending/pos/t2162.scala create mode 100644 test/pending/pos/t2173.scala create mode 100644 test/pending/pos/t2178.scala create mode 100644 test/pending/pos/t2179.scala create mode 100644 test/pending/pos/t2185,.scala create mode 100644 test/pending/pos/t2188.scala create mode 100644 test/pending/pos/t2194.scala create mode 100644 test/pending/pos/t2201.scala create mode 100644 test/pending/pos/ticket2251.scala create mode 100644 test/pending/run/t1939.scala create mode 100644 test/pending/run/t1980.scala create mode 100644 test/pending/run/t2005.scala diff --git a/lib/scala-compiler.jar.desired.sha1 b/lib/scala-compiler.jar.desired.sha1 index 9b42928d0d..18aecb6c18 100644 --- a/lib/scala-compiler.jar.desired.sha1 +++ b/lib/scala-compiler.jar.desired.sha1 @@ -1 +1 @@ -f39fa1359f164294d0bab462c2abbcad629194bb ?scala-compiler.jar +a68c666e5fdd3c2e6f389ecde369f5c6718d1a6e ?scala-compiler.jar diff --git a/lib/scala-library.jar.desired.sha1 b/lib/scala-library.jar.desired.sha1 index 56a65e30c4..7f8055b117 100644 --- a/lib/scala-library.jar.desired.sha1 +++ b/lib/scala-library.jar.desired.sha1 @@ -1 +1 @@ -93135c0a9a529f3cc3ef76bbd6540e870dc562c7 ?scala-library.jar +4ae7d29d34a187321e703462f2d1c62e0b650731 ?scala-library.jar diff --git a/src/compiler/scala/tools/nsc/Global.scala b/src/compiler/scala/tools/nsc/Global.scala index 56b8253fa7..ff795d446e 100644 --- a/src/compiler/scala/tools/nsc/Global.scala +++ b/src/compiler/scala/tools/nsc/Global.scala @@ -822,6 +822,8 @@ class Global(var settings: Settings, var reporter: Reporter) extends SymbolTable advancePhase } + println(narrowCount+" narrowings") + if (settings.Xshowcls.value != "") showDef(newTermName(settings.Xshowcls.value), false) if (settings.Xshowobj.value != "") diff --git a/src/compiler/scala/tools/nsc/doc/DocUtil.scala b/src/compiler/scala/tools/nsc/doc/DocUtil.scala index dbb6f94386..c01d9566f8 100644 --- a/src/compiler/scala/tools/nsc/doc/DocUtil.scala +++ b/src/compiler/scala/tools/nsc/doc/DocUtil.scala @@ -92,7 +92,7 @@ object DocUtil else xs.head ++ sep ++ interleave(xs.tail, sep) def mkXML(begin: NodeSeq, separator: NodeSeq, end: NodeSeq)(f: T => NodeSeq): NodeSeq = - begin ++ interleave(list.toSequence map f, separator) ++ end + begin ++ interleave(list.toSeq map f, separator) ++ end def mkXML(begin: String, separator: String, end: String)(f: T => NodeSeq): NodeSeq = this.mkXML(Text(begin), Text(separator), Text(end))(f) diff --git a/src/compiler/scala/tools/nsc/io/Path.scala b/src/compiler/scala/tools/nsc/io/Path.scala index ed750e5b11..37cc64cf75 100644 --- a/src/compiler/scala/tools/nsc/io/Path.scala +++ b/src/compiler/scala/tools/nsc/io/Path.scala @@ -9,7 +9,7 @@ import java.io.{ FileInputStream, FileOutputStream, BufferedReader, BufferedWriter, InputStreamReader, OutputStreamWriter, BufferedInputStream, BufferedOutputStream, File => JFile } import java.net.{ URI, URL } -import collection.{ Sequence, Traversable } +import collection.{ Seq, Traversable } import PartialFunction._ import scala.util.Random.nextASCIIString diff --git a/src/compiler/scala/tools/nsc/io/ZipArchive.scala b/src/compiler/scala/tools/nsc/io/ZipArchive.scala index 54deec939e..e75f68ca92 100644 --- a/src/compiler/scala/tools/nsc/io/ZipArchive.scala +++ b/src/compiler/scala/tools/nsc/io/ZipArchive.scala @@ -301,6 +301,6 @@ final class URLZipArchive(url: URL) extends AbstractFile with ZipContainer val minusOne = (-1).toByte val buf = new BufferedInputStream(in) val bytes = Iterator continually in.read().toByte takeWhile (_ != minusOne) - new ByteArrayInputStream(bytes.toSequence.toArray) + new ByteArrayInputStream(bytes.toSeq.toArray) } } diff --git a/src/compiler/scala/tools/nsc/symtab/Definitions.scala b/src/compiler/scala/tools/nsc/symtab/Definitions.scala index 71e9725944..9c2df4a6cb 100644 --- a/src/compiler/scala/tools/nsc/symtab/Definitions.scala +++ b/src/compiler/scala/tools/nsc/symtab/Definitions.scala @@ -168,8 +168,8 @@ trait Definitions { def Iterable_next = getMember(IterableClass, nme.next) def Iterable_hasNext = getMember(IterableClass, nme.hasNext) - lazy val SeqClass = getClass2("scala.Seq", "scala.collection.Sequence") - lazy val SeqModule = getModule2("scala.Seq", "scala.collection.Sequence") + lazy val SeqClass = getClass2("scala.Seq", "scala.collection.Seq") + lazy val SeqModule = getModule2("scala.Seq", "scala.collection.Seq") def Seq_length = getMember(SeqClass, nme.length) lazy val RandomAccessSeqMutableClass = getMember( getModule2("scala.RandomAccessSeq", "scala.collection.Vector"), nme.Mutable) diff --git a/src/compiler/scala/tools/nsc/symtab/Scopes.scala b/src/compiler/scala/tools/nsc/symtab/Scopes.scala index 8a09d361cb..281981e344 100644 --- a/src/compiler/scala/tools/nsc/symtab/Scopes.scala +++ b/src/compiler/scala/tools/nsc/symtab/Scopes.scala @@ -50,7 +50,7 @@ trait Scopes { // Martin: This code contains a lot of stuff for the old Eclipse plugin. // Now it's just needless complexity, which should be eleminated. // We should make the elems list doubly-linked, - // and have scopes inherit from LinearSequence, + // and have scopes inherit from LinearSeq, // that way, we need to do way fewer toList than before. /** diff --git a/src/compiler/scala/tools/nsc/symtab/StdNames.scala b/src/compiler/scala/tools/nsc/symtab/StdNames.scala index 867b13b243..19cce08224 100644 --- a/src/compiler/scala/tools/nsc/symtab/StdNames.scala +++ b/src/compiler/scala/tools/nsc/symtab/StdNames.scala @@ -331,7 +331,7 @@ trait StdNames { val tail = newTermName("tail") val toArray = newTermName("toArray") val toList = newTermName("toList") - val toSequence = newTermName("toSequence") + val toSeq = newTermName("toSeq") val toString_ = newTermName("toString") val clone_ = newTermName("clone") val this_ = newTermName("this") diff --git a/src/compiler/scala/tools/nsc/symtab/Types.scala b/src/compiler/scala/tools/nsc/symtab/Types.scala index 9fdb446ad8..47ede4228e 100644 --- a/src/compiler/scala/tools/nsc/symtab/Types.scala +++ b/src/compiler/scala/tools/nsc/symtab/Types.scala @@ -4780,7 +4780,7 @@ A type's typeSymbol should never be inspected directly. val shorthands = Set( "scala.collection.immutable.List", "scala.collection.immutable.Nil", - "scala.collection.Sequence", + "scala.collection.Seq", "scala.collection.Traversable", "scala.collection.Iterable", "scala.collection.mutable.StringBuilder", diff --git a/src/library/scala/Array.scala b/src/library/scala/Array.scala index afd6f7bb9b..249d80653c 100644 --- a/src/library/scala/Array.scala +++ b/src/library/scala/Array.scala @@ -368,10 +368,10 @@ object Array extends FallbackArrayBuilding { b.result } - /** This method is called in a pattern match { case Sequence(...) => }. + /** This method is called in a pattern match { case Seq(...) => }. * * @param x the selector value - * @return sequence wrapped in an option, if this is a Sequence, otherwise none + * @return sequence wrapped in an option, if this is a Seq, otherwise none */ def unapplySeq[T](x: Array[T]): Option[Vector[T]] = if (x == null) None else Some(x.toVector) diff --git a/src/library/scala/collection/IterableLike.scala b/src/library/scala/collection/IterableLike.scala index 7a00d7933e..1a6b9d7aaf 100644 --- a/src/library/scala/collection/IterableLike.scala +++ b/src/library/scala/collection/IterableLike.scala @@ -364,8 +364,6 @@ self => /** None if iterable is empty. */ @deprecated("use `headOption' instead") def firstOption: Option[A] = headOption - @deprecated("use `toSequence' instead") def toSeq: Sequence[A] = toSequence - /** * returns a projection that can be used to call non-strict filter, * map, and flatMap methods that build projections diff --git a/src/library/scala/collection/Iterator.scala b/src/library/scala/collection/Iterator.scala index 5b6d13d152..5c7e2b6c3b 100644 --- a/src/library/scala/collection/Iterator.scala +++ b/src/library/scala/collection/Iterator.scala @@ -752,7 +752,7 @@ trait Iterator[+A] { self => * understand) this method takes the way one might expect, leaving * the original iterator with 'size' fewer elements. */ - private def takeDestructively(size: Int): Sequence[A] = { + private def takeDestructively(size: Int): Seq[A] = { val buf = new ArrayBuffer[A] var i = 0 while (self.hasNext && i < size) { @@ -763,12 +763,12 @@ trait Iterator[+A] { self => } /** A flexible iterator for transforming an Iterator[A] into an - * Iterator[Sequence[A]], with configurable sequence size, step, and + * Iterator[Seq[A]], with configurable sequence size, step, and * strategy for dealing with elements which don't fit evenly. * * Typical uses can be achieved via methods `grouped' and `sliding'. */ - class GroupedIterator[B >: A](self: Iterator[A], size: Int, step: Int) extends Iterator[Sequence[B]] { + class GroupedIterator[B >: A](self: Iterator[A], size: Int, step: Int) extends Iterator[Seq[B]] { require(size >= 1 && step >= 1) private[this] var buffer: ArrayBuffer[B] = ArrayBuffer() // the buffer @@ -1008,7 +1008,7 @@ trait Iterator[+A] { self => * * @return A sequence which enumerates all elements of this iterator. */ - def toSequence: Sequence[A] = { + def toSeq: Seq[A] = { val buffer = new ArrayBuffer[A] this copyToBuffer buffer buffer @@ -1096,8 +1096,8 @@ trait Iterator[+A] { self => * * @return a sequence which enumerates all elements of this iterator. */ - @deprecated("use toSequence instead") - def collect: Sequence[A] = toSequence + @deprecated("use toSeq instead") + def collect: Seq[A] = toSeq /** Returns a counted iterator from this iterator. */ diff --git a/src/library/scala/collection/LinearSeq.scala b/src/library/scala/collection/LinearSeq.scala new file mode 100644 index 0000000000..4793d36aba --- /dev/null +++ b/src/library/scala/collection/LinearSeq.scala @@ -0,0 +1,43 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2009, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + + +package scala.collection + +import generic._ +import mutable.Builder + +/**

+ * Class Linear[A] represents linear sequences of elements. + * For such sequences isEmpty, head and + * tail are guaranteed to be efficient constant time (or near so) + * operations.
+ * It does not add any methods to Seq but overrides several + * methods with optimized implementations. + *

+ * + * @author Martin Odersky + * @author Matthias Zenger + * @version 1.0, 16/07/2003 + * @since 2.8 + */ +trait LinearSeq[+A] extends Seq[A] + with GenericTraversableTemplate[A, LinearSeq] + with LinearSeqLike[A, LinearSeq[A]] { + override def companion: GenericCompanion[LinearSeq] = LinearSeq +} + +/** + * @since 2.8 + */ +object LinearSeq extends SeqFactory[LinearSeq] { + implicit def builderFactory[A]: BuilderFactory[A, LinearSeq[A], Coll] = new VirtualBuilderFactory[A] + def newBuilder[A]: Builder[A, LinearSeq[A]] = immutable.LinearSeq.newBuilder[A] +} diff --git a/src/library/scala/collection/LinearSeqLike.scala b/src/library/scala/collection/LinearSeqLike.scala new file mode 100644 index 0000000000..ed1f5329c5 --- /dev/null +++ b/src/library/scala/collection/LinearSeqLike.scala @@ -0,0 +1,405 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2009, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + + +package scala.collection +import generic._ + +import mutable.ListBuffer +import scala.util.control.Breaks._ + +/** Class Linear[A] represents linear sequences of elements. + * For such sequences `isEmpty`, `head` and `tail` are guaranteed to be + * efficient constant time (or near so) operations. + * It does not add any methods to Seq but overrides + * several methods with optimized implementations. + * + * @author Martin Odersky + * @author Matthias Zenger + * @version 1.0, 16/07/2003 + * @since 2.8 + */ +trait LinearSeqLike[+A, +Repr <: LinearSeqLike[A, Repr]] extends SeqLike[A, Repr] { self: Repr => + + override protected[this] def thisCollection: LinearSeq[A] = this.asInstanceOf[LinearSeq[A]] + override protected[this] def toCollection(repr: Repr): LinearSeq[A] = repr.asInstanceOf[LinearSeq[A]] + + /** Abstract method to be implemented in a subclass */ + def isEmpty: Boolean + + /** Abstract method to be implemented in a subclass */ + def head: A + + /** Abstract method to be implemented in a subclass */ + def tail: Repr + + /** Returns the number of elements in the linear sequence. + */ + def length: Int = { + var these = self + var len = 0 + while (!these.isEmpty) { + len += 1 + these = these.tail + } + len + } + + /** Returns the n-th element of this linear sequence. The first element + * (head of the linear sequence) is at position 0. + * + * @param n index of the element to return + * @return the element at position n in this linear sequence. + * @throws Predef.NoSuchElementException if the linear sequence is too short. + */ + def apply(n: Int): A = drop(n).head + + /** Returns the elements in the sequence as an iterator + */ + override def iterator: Iterator[A] = new Iterator[A] { + var these = self + def hasNext: Boolean = !these.isEmpty + def next: A = + if (hasNext) { + val result = these.head; these = these.tail; result + } else Iterator.empty.next + override def toList: List[A] = these.toList + } + + /** Apply the given function f to each element of this linear sequence + * (while respecting the order of the elements). + * + * @param f the treatment to apply to each element. + */ + override def foreach[B](f: A => B) { + var these = this + while (!these.isEmpty) { + f(these.head) + these = these.tail + } + } + + /** Tests if the predicate p is satisfied by all elements + * in this list. + * + * @param p the test predicate. + * @return true iff all elements of this list satisfy the + * predicate p. + */ + override def forall(p: A => Boolean): Boolean = { + var these = this + while (!these.isEmpty) { + if (!p(these.head)) return false + these = these.tail + } + true + } + + /** Tests the existence in this list of an element that satisfies the + * predicate p. + * + * @param p the test predicate. + * @return true iff there exists an element in this list that + * satisfies the predicate p. + */ + override def exists(p: A => Boolean): Boolean = { + var these = this + while (!these.isEmpty) { + if (p(these.head)) return true + these = these.tail + } + false + } + + /** Count the number of elements in the iterable which satisfy a predicate. + * + * @param p the predicate for which to count + * @return the number of elements satisfying the predicate p. + */ + override def count(p: A => Boolean): Int = { + var these = this + var cnt = 0 + while (!these.isEmpty) { + if (p(these.head)) cnt += 1 + these = these.tail + } + cnt + } + + /** Find and return the first element of the list satisfying a + * predicate, if any. + * + * @param p the predicate + * @return the first element in the list satisfying p, + * or None if none exists. + */ + override def find(p: A => Boolean): Option[A] = { + var these = this + while (!these.isEmpty) { + if (p(these.head)) return Some(these.head) + these = these.tail + } + None + } + + /** Combines the elements of this list together using the binary + * function f, from left to right, and starting with + * the value z. + * + * @return f(... (f(f(z, a0), a1) ...), + * an) if the list is + * [a0, a1, ..., an]. + */ + override def foldLeft[B](z: B)(f: (B, A) => B): B = { + var acc = z + var these = this + while (!these.isEmpty) { + acc = f(acc, these.head) + these = these.tail + } + acc + } + + /** Combines the elements of this list together using the binary + * function f, from right to left, and starting with + * the value z. + * + * @return f(a0, f(a1, f(..., f(an, z)...))) + * if the list is [a0, a1, ..., an]. + */ + override def foldRight[B](z: B)(f: (A, B) => B): B = + if (this.isEmpty) z + else f(head, tail.foldRight(z)(f)) + + /** Combines the elements of this list together using the binary + * operator op, from left to right + * @param op The operator to apply + * @return op(... op(a0,a1), ..., an) + if the list has elements + * a0, a1, ..., an. + * @throws Predef.UnsupportedOperationException if the list is empty. + */ + override def reduceLeft[B >: A](f: (B, A) => B): B = + if (isEmpty) throw new UnsupportedOperationException("empty.reduceLeft") + else tail.foldLeft[B](head)(f) + + /** Combines the elements of this iterable object together using the binary + * operator op, from right to left + * @note Will not terminate for infinite-sized collections. + * @param op The operator to apply + * + * @return a0 op (... op (an-1 op an)...) + * if the iterable object has elements a0, a1, ..., + * an. + * + * @throws Predef.UnsupportedOperationException if the iterator is empty. + */ + override def reduceRight[B >: A](op: (A, B) => B): B = + if (isEmpty) throw new UnsupportedOperationException("Nil.reduceRight") + else if (tail.isEmpty) head + else op(head, tail.reduceRight(op)) + + /** The last element of this linear sequence. + * + * @throws Predef.NoSuchElementException if the linear sequence is empty. + */ + override def last: A = { + if (isEmpty) throw new NoSuchElementException + var these = this + var nx = these.tail + while (!nx.isEmpty) { + these = nx + nx = nx.tail + } + these.head + } + + override def take(n: Int): Repr = { + val b = newBuilder + var i = 0 + var these = repr + while (!these.isEmpty && i < n) { + i += 1 + b += these.head + these = these.tail + } + b.result + } + + override def drop(n: Int): Repr = { + var these: Repr = repr + var count = n + while (!these.isEmpty && count > 0) { + these = these.tail + count -= 1 + } + these + } + + /** Returns the rightmost n elements from this iterable. + * @param n the number of elements to take + */ + override def dropRight(n: Int): Repr = { + val b = newBuilder + var these = this + var lead = this drop n + while (!lead.isEmpty) { + b += these.head + these = these.tail + lead = lead.tail + } + b.result + } + + /** A sub-traversable starting at index `from` + * and extending up to (but not including) index `until`. + * + * @note c.slice(from, to) is equivalent to (but possibly more efficient than) + * c.drop(from).take(to - from) + * + * @param from The index of the first element of the returned subsequence + * @param until The index of the element following the returned subsequence + * @note Might return different results for different runs, unless this traversable is ordered + */ + override def slice(from: Int, until: Int): Repr = { + val b = newBuilder + var i = from + var these = this drop from + while (i < until && !these.isEmpty) { + b += these.head + these = these.tail + i += 1 + } + b.result + } + + /** Returns the longest prefix of this traversable whose elements satisfy + * the predicate p. + * + * @param p the test predicate. + * @note Might return different results for different runs, unless this traversable is ordered + */ + override def takeWhile(p: A => Boolean): Repr = { + val b = newBuilder + var these = this + while (!these.isEmpty && p(these.head)) { + b += these.head + these = these.tail + } + b.result + } + + /** Returns a pair consisting of the longest prefix of the linear sequence whose + * elements all satisfy the given predicate, and the rest of the linear sequence. + * + * @param p the test predicate + */ + override def span(p: A => Boolean): (Repr, Repr) = { + var these: Repr = repr + val b = newBuilder + while (!these.isEmpty && p(these.head)) { + b += these.head + these = these.tail + } + (b.result, these) + } + + /** Returns true iff the other linear sequence contains the + * same elements as this one. + * + * @note will not terminate for two infinite-sized linear sequences. + * @param that the other linear sequence + */ + override def sameElements[B >: A](that: Iterable[B]): Boolean = that match { + case that1: LinearSeq[_] => + var these = this + var those = that1 + while (!these.isEmpty && !those.isEmpty && these.head == those.head) { + these = these.tail + those = those.tail + } + these.isEmpty && those.isEmpty + case _ => + super.sameElements(that) + } + + // Overridden methods from Seq + + /** Result of comparing length with operand len. + * returns x where + * x < 0 iff this.length < len + * x == 0 iff this.length == len + * x > 0 iff this.length > len. + */ + override def lengthCompare(len: Int): Int = { + var i = 0 + var these = self + while (!these.isEmpty && i <= len) { + i += 1 + these = these.tail + } + i - len + } + + /** Is this partial function defined for the index x? + */ + override def isDefinedAt(x: Int): Boolean = x >= 0 && lengthCompare(x) > 0 + + /** Returns length of longest segment starting from a start index `from` + * such that every element of the segment satisfies predicate `p`. + * @note may not terminate for infinite-sized collections. + * @param p the predicate + * @param from the start index + */ + override def segmentLength(p: A => Boolean, from: Int): Int = { + var i = 0 + var these = this drop from + while (!these.isEmpty && p(these.head)) { + i += 1 + these = these.tail + } + i + } + + /** Returns index of the first element starting from a start index + * satisying a predicate, or -1, if none exists. + * + * @note may not terminate for infinite-sized linear sequences. + * @param p the predicate + * @param from the start index + */ + override def indexWhere(p: A => Boolean, from: Int): Int = { + var i = from + var these = this drop from + while (!these.isEmpty && !p(these.head)) { + i += 1 + these = these.tail + } + if (these.isEmpty) -1 else i + } + + /** Returns index of the last element satisying a predicate, or -1, if none exists. + * + * @param p the predicate + * @return the index of the last element satisfying p, + * or -1 if such an element does not exist + */ + override def lastIndexWhere(p: A => Boolean, end: Int): Int = { + var i = 0 + var these = this + var last = -1 + while (!these.isEmpty && i <= end) { + if (p(these.head)) last = i + these = these.tail + i += 1 + } + last + } +} diff --git a/src/library/scala/collection/LinearSequence.scala b/src/library/scala/collection/LinearSequence.scala deleted file mode 100644 index fbd85ed544..0000000000 --- a/src/library/scala/collection/LinearSequence.scala +++ /dev/null @@ -1,43 +0,0 @@ -/* __ *\ -** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003-2009, LAMP/EPFL ** -** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** -** /____/\___/_/ |_/____/_/ | | ** -** |/ ** -\* */ - -// $Id$ - - -package scala.collection - -import generic._ -import mutable.Builder - -/**

- * Class Linear[A] represents linear sequences of elements. - * For such sequences isEmpty, head and - * tail are guaranteed to be efficient constant time (or near so) - * operations.
- * It does not add any methods to Sequence but overrides several - * methods with optimized implementations. - *

- * - * @author Martin Odersky - * @author Matthias Zenger - * @version 1.0, 16/07/2003 - * @since 2.8 - */ -trait LinearSequence[+A] extends Sequence[A] - with GenericTraversableTemplate[A, LinearSequence] - with LinearSequenceLike[A, LinearSequence[A]] { - override def companion: GenericCompanion[LinearSequence] = LinearSequence -} - -/** - * @since 2.8 - */ -object LinearSequence extends SequenceFactory[LinearSequence] { - implicit def builderFactory[A]: BuilderFactory[A, LinearSequence[A], Coll] = new VirtualBuilderFactory[A] - def newBuilder[A]: Builder[A, LinearSequence[A]] = immutable.LinearSequence.newBuilder[A] -} diff --git a/src/library/scala/collection/LinearSequenceLike.scala b/src/library/scala/collection/LinearSequenceLike.scala deleted file mode 100644 index 131ce2624b..0000000000 --- a/src/library/scala/collection/LinearSequenceLike.scala +++ /dev/null @@ -1,405 +0,0 @@ -/* __ *\ -** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003-2009, LAMP/EPFL ** -** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** -** /____/\___/_/ |_/____/_/ | | ** -** |/ ** -\* */ - -// $Id$ - - -package scala.collection -import generic._ - -import mutable.ListBuffer -import scala.util.control.Breaks._ - -/** Class Linear[A] represents linear sequences of elements. - * For such sequences `isEmpty`, `head` and `tail` are guaranteed to be - * efficient constant time (or near so) operations. - * It does not add any methods to Sequence but overrides - * several methods with optimized implementations. - * - * @author Martin Odersky - * @author Matthias Zenger - * @version 1.0, 16/07/2003 - * @since 2.8 - */ -trait LinearSequenceLike[+A, +Repr <: LinearSequenceLike[A, Repr]] extends SequenceLike[A, Repr] { self: Repr => - - override protected[this] def thisCollection: LinearSequence[A] = this.asInstanceOf[LinearSequence[A]] - override protected[this] def toCollection(repr: Repr): LinearSequence[A] = repr.asInstanceOf[LinearSequence[A]] - - /** Abstract method to be implemented in a subclass */ - def isEmpty: Boolean - - /** Abstract method to be implemented in a subclass */ - def head: A - - /** Abstract method to be implemented in a subclass */ - def tail: Repr - - /** Returns the number of elements in the linear sequence. - */ - def length: Int = { - var these = self - var len = 0 - while (!these.isEmpty) { - len += 1 - these = these.tail - } - len - } - - /** Returns the n-th element of this linear sequence. The first element - * (head of the linear sequence) is at position 0. - * - * @param n index of the element to return - * @return the element at position n in this linear sequence. - * @throws Predef.NoSuchElementException if the linear sequence is too short. - */ - def apply(n: Int): A = drop(n).head - - /** Returns the elements in the sequence as an iterator - */ - override def iterator: Iterator[A] = new Iterator[A] { - var these = self - def hasNext: Boolean = !these.isEmpty - def next: A = - if (hasNext) { - val result = these.head; these = these.tail; result - } else Iterator.empty.next - override def toList: List[A] = these.toList - } - - /** Apply the given function f to each element of this linear sequence - * (while respecting the order of the elements). - * - * @param f the treatment to apply to each element. - */ - override def foreach[B](f: A => B) { - var these = this - while (!these.isEmpty) { - f(these.head) - these = these.tail - } - } - - /** Tests if the predicate p is satisfied by all elements - * in this list. - * - * @param p the test predicate. - * @return true iff all elements of this list satisfy the - * predicate p. - */ - override def forall(p: A => Boolean): Boolean = { - var these = this - while (!these.isEmpty) { - if (!p(these.head)) return false - these = these.tail - } - true - } - - /** Tests the existence in this list of an element that satisfies the - * predicate p. - * - * @param p the test predicate. - * @return true iff there exists an element in this list that - * satisfies the predicate p. - */ - override def exists(p: A => Boolean): Boolean = { - var these = this - while (!these.isEmpty) { - if (p(these.head)) return true - these = these.tail - } - false - } - - /** Count the number of elements in the iterable which satisfy a predicate. - * - * @param p the predicate for which to count - * @return the number of elements satisfying the predicate p. - */ - override def count(p: A => Boolean): Int = { - var these = this - var cnt = 0 - while (!these.isEmpty) { - if (p(these.head)) cnt += 1 - these = these.tail - } - cnt - } - - /** Find and return the first element of the list satisfying a - * predicate, if any. - * - * @param p the predicate - * @return the first element in the list satisfying p, - * or None if none exists. - */ - override def find(p: A => Boolean): Option[A] = { - var these = this - while (!these.isEmpty) { - if (p(these.head)) return Some(these.head) - these = these.tail - } - None - } - - /** Combines the elements of this list together using the binary - * function f, from left to right, and starting with - * the value z. - * - * @return f(... (f(f(z, a0), a1) ...), - * an) if the list is - * [a0, a1, ..., an]. - */ - override def foldLeft[B](z: B)(f: (B, A) => B): B = { - var acc = z - var these = this - while (!these.isEmpty) { - acc = f(acc, these.head) - these = these.tail - } - acc - } - - /** Combines the elements of this list together using the binary - * function f, from right to left, and starting with - * the value z. - * - * @return f(a0, f(a1, f(..., f(an, z)...))) - * if the list is [a0, a1, ..., an]. - */ - override def foldRight[B](z: B)(f: (A, B) => B): B = - if (this.isEmpty) z - else f(head, tail.foldRight(z)(f)) - - /** Combines the elements of this list together using the binary - * operator op, from left to right - * @param op The operator to apply - * @return op(... op(a0,a1), ..., an) - if the list has elements - * a0, a1, ..., an. - * @throws Predef.UnsupportedOperationException if the list is empty. - */ - override def reduceLeft[B >: A](f: (B, A) => B): B = - if (isEmpty) throw new UnsupportedOperationException("empty.reduceLeft") - else tail.foldLeft[B](head)(f) - - /** Combines the elements of this iterable object together using the binary - * operator op, from right to left - * @note Will not terminate for infinite-sized collections. - * @param op The operator to apply - * - * @return a0 op (... op (an-1 op an)...) - * if the iterable object has elements a0, a1, ..., - * an. - * - * @throws Predef.UnsupportedOperationException if the iterator is empty. - */ - override def reduceRight[B >: A](op: (A, B) => B): B = - if (isEmpty) throw new UnsupportedOperationException("Nil.reduceRight") - else if (tail.isEmpty) head - else op(head, tail.reduceRight(op)) - - /** The last element of this linear sequence. - * - * @throws Predef.NoSuchElementException if the linear sequence is empty. - */ - override def last: A = { - if (isEmpty) throw new NoSuchElementException - var these = this - var nx = these.tail - while (!nx.isEmpty) { - these = nx - nx = nx.tail - } - these.head - } - - override def take(n: Int): Repr = { - val b = newBuilder - var i = 0 - var these = repr - while (!these.isEmpty && i < n) { - i += 1 - b += these.head - these = these.tail - } - b.result - } - - override def drop(n: Int): Repr = { - var these: Repr = repr - var count = n - while (!these.isEmpty && count > 0) { - these = these.tail - count -= 1 - } - these - } - - /** Returns the rightmost n elements from this iterable. - * @param n the number of elements to take - */ - override def dropRight(n: Int): Repr = { - val b = newBuilder - var these = this - var lead = this drop n - while (!lead.isEmpty) { - b += these.head - these = these.tail - lead = lead.tail - } - b.result - } - - /** A sub-traversable starting at index `from` - * and extending up to (but not including) index `until`. - * - * @note c.slice(from, to) is equivalent to (but possibly more efficient than) - * c.drop(from).take(to - from) - * - * @param from The index of the first element of the returned subsequence - * @param until The index of the element following the returned subsequence - * @note Might return different results for different runs, unless this traversable is ordered - */ - override def slice(from: Int, until: Int): Repr = { - val b = newBuilder - var i = from - var these = this drop from - while (i < until && !these.isEmpty) { - b += these.head - these = these.tail - i += 1 - } - b.result - } - - /** Returns the longest prefix of this traversable whose elements satisfy - * the predicate p. - * - * @param p the test predicate. - * @note Might return different results for different runs, unless this traversable is ordered - */ - override def takeWhile(p: A => Boolean): Repr = { - val b = newBuilder - var these = this - while (!these.isEmpty && p(these.head)) { - b += these.head - these = these.tail - } - b.result - } - - /** Returns a pair consisting of the longest prefix of the linear sequence whose - * elements all satisfy the given predicate, and the rest of the linear sequence. - * - * @param p the test predicate - */ - override def span(p: A => Boolean): (Repr, Repr) = { - var these: Repr = repr - val b = newBuilder - while (!these.isEmpty && p(these.head)) { - b += these.head - these = these.tail - } - (b.result, these) - } - - /** Returns true iff the other linear sequence contains the - * same elements as this one. - * - * @note will not terminate for two infinite-sized linear sequences. - * @param that the other linear sequence - */ - override def sameElements[B >: A](that: Iterable[B]): Boolean = that match { - case that1: LinearSequence[_] => - var these = this - var those = that1 - while (!these.isEmpty && !those.isEmpty && these.head == those.head) { - these = these.tail - those = those.tail - } - these.isEmpty && those.isEmpty - case _ => - super.sameElements(that) - } - - // Overridden methods from Sequence - - /** Result of comparing length with operand len. - * returns x where - * x < 0 iff this.length < len - * x == 0 iff this.length == len - * x > 0 iff this.length > len. - */ - override def lengthCompare(len: Int): Int = { - var i = 0 - var these = self - while (!these.isEmpty && i <= len) { - i += 1 - these = these.tail - } - i - len - } - - /** Is this partial function defined for the index x? - */ - override def isDefinedAt(x: Int): Boolean = x >= 0 && lengthCompare(x) > 0 - - /** Returns length of longest segment starting from a start index `from` - * such that every element of the segment satisfies predicate `p`. - * @note may not terminate for infinite-sized collections. - * @param p the predicate - * @param from the start index - */ - override def segmentLength(p: A => Boolean, from: Int): Int = { - var i = 0 - var these = this drop from - while (!these.isEmpty && p(these.head)) { - i += 1 - these = these.tail - } - i - } - - /** Returns index of the first element starting from a start index - * satisying a predicate, or -1, if none exists. - * - * @note may not terminate for infinite-sized linear sequences. - * @param p the predicate - * @param from the start index - */ - override def indexWhere(p: A => Boolean, from: Int): Int = { - var i = from - var these = this drop from - while (!these.isEmpty && !p(these.head)) { - i += 1 - these = these.tail - } - if (these.isEmpty) -1 else i - } - - /** Returns index of the last element satisying a predicate, or -1, if none exists. - * - * @param p the predicate - * @return the index of the last element satisfying p, - * or -1 if such an element does not exist - */ - override def lastIndexWhere(p: A => Boolean, end: Int): Int = { - var i = 0 - var these = this - var last = -1 - while (!these.isEmpty && i <= end) { - if (p(these.head)) last = i - these = these.tail - i += 1 - } - last - } -} diff --git a/src/library/scala/collection/Seq.scala b/src/library/scala/collection/Seq.scala new file mode 100644 index 0000000000..6e5f084a06 --- /dev/null +++ b/src/library/scala/collection/Seq.scala @@ -0,0 +1,62 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2009, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + + +package scala.collection + +import generic._ +import mutable.Builder + +/**

+ * Class Seq[A] represents sequences of elements + * of type A.
+ * It adds the following methods to class Iterable: + * length, lengthCompare, apply, + * isDefinedAt, segmentLength, + * prefixLength, indexWhere, indexOf, + * lastIndexWhere, lastIndexOf, reverse, + * reverseIterator, startsWith, + * endsWith, indexOfSeq. + *

+ * + * @author Martin Odersky + * @author Matthias Zenger + * @version 1.0, 16/07/2003 + */ +trait Seq[+A] extends PartialFunction[Int, A] + with Iterable[A] + with GenericTraversableTemplate[A, Seq] + with SeqLike[A, Seq[A]] { + override def companion: GenericCompanion[Seq] = Seq +} + +/** Factory object for Seq trait. + * + * @author Martin Odersky + * @version 2.8 + */ +object Seq extends SeqFactory[Seq] { + + private[collection] val hashSeed = "Seq".hashCode + + implicit def builderFactory[A]: BuilderFactory[A, Seq[A], Coll] = new VirtualBuilderFactory[A] + def newBuilder[A]: Builder[A, Seq[A]] = immutable.Seq.newBuilder[A] + + @deprecated("use View instead") + type Projection[A] = SeqView[A, Coll] + + @deprecated("use Seq(value) instead") + def singleton[A](value: A) = Seq(value) + + /** Builds a singleton sequence. */ + @deprecated("use Seq(x) instead.") + def single[A](x: A) = singleton(x) +} + diff --git a/src/library/scala/collection/SeqLike.scala b/src/library/scala/collection/SeqLike.scala new file mode 100644 index 0000000000..50ea0c3a5a --- /dev/null +++ b/src/library/scala/collection/SeqLike.scala @@ -0,0 +1,597 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2009, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + + +package scala.collection +import generic._ +import mutable.{ListBuffer, HashMap, GenericArray} + +// import immutable.{List, Nil, ::} +import generic._ + +/** Contains a KMP implementation, based on the undoubtedly reliable wikipedia entry. + * + * @author paulp + * @since 2.8 + */ +object SeqLike { + + private def KMP[B](S: Seq[B], W: Seq[B]): Option[Int] = { + // trivial cases + if (W.isEmpty) return Some(0) + else if (W drop 1 isEmpty) return (S indexOf W(0)) match { + case -1 => None + case x => Some(x) + } + + val T: Array[Int] = { + val arr = new Array[Int](W.length) + var pos = 2 + var cnd = 0 + arr(0) = -1 + arr(1) = 0 + while (pos < W.length) { + if (W(pos - 1) == W(cnd)) { + arr(pos) = cnd + 1 + pos += 1 + cnd += 1 + } + else if (cnd > 0) { + cnd = arr(cnd) + } + else { + arr(pos) = 0 + pos += 1 + } + } + arr + } + + var m, i = 0 + def mi = m + i + + while (mi < S.length) { + if (W(i) == S(mi)) { + i += 1 + if (i == W.length) + return Some(m) + } + else { + m = mi - T(i) + if (i > 0) + i = T(i) + } + } + None + } + + def indexOf[B]( + source: Seq[B], sourceOffset: Int, sourceCount: Int, + target: Seq[B], targetOffset: Int, targetCount: Int, + fromIndex: Int): Int = + KMP(source.slice(sourceOffset, sourceCount) drop fromIndex, target.slice(targetOffset, targetCount)) match { + case None => -1 + case Some(x) => x + fromIndex + } + + def lastIndexOf[B]( + source: Seq[B], sourceOffset: Int, sourceCount: Int, + target: Seq[B], targetOffset: Int, targetCount: Int, + fromIndex: Int): Int = { + val src = (source.slice(sourceOffset, sourceCount) take fromIndex).reverse + val tgt = target.slice(targetOffset, targetCount).reverse + + KMP(src, tgt) match { + case None => -1 + case Some(x) => (src.length - tgt.length - x) + sourceOffset + } + } +} + +/** Class Seq[A] represents sequences of elements + * of type A. + * It adds the following methods to class Iterable: + * `length`, `lengthCompare`, `apply`, `isDefinedAt`, `segmentLength`, `prefixLength`, + * `indexWhere`, `indexOf`, `lastIndexWhere`, `lastIndexOf`, `reverse`, `reverseIterator`, + * `startsWith`, `endsWith`, `indexOfSeq`, , `zip`, `zipAll`, `zipWithIndex`. + * + * + * @author Martin Odersky + * @author Matthias Zenger + * @version 1.0, 16/07/2003 + * @since 2.8 + */ +trait SeqLike[+A, +Repr] extends IterableLike[A, Repr] { self => + + override protected[this] def thisCollection: Seq[A] = this.asInstanceOf[Seq[A]] + override protected[this] def toCollection(repr: Repr): Seq[A] = repr.asInstanceOf[Seq[A]] + + import Traversable.breaks._ + + /** Returns the length of the sequence. + */ + def length: Int + + /** Returns the elements at position `idx` + */ + def apply(idx: Int): A + + /** Result of comparing length with operand len. + * returns x where + * x < 0 iff this.length < len + * x == 0 iff this.length == len + * x > 0 iff this.length > len. + * + * The method as implemented here does not call length directly; its running time + * is O(length min len) instead of O(length). The method should be overwritten + * if computing length is cheap. + */ + def lengthCompare(len: Int): Int = { + var i = 0 + breakable { + for (_ <- this) { + i += 1 + if (i > len) break + } + } + i - len + } + + /** Should always be length */ + override def size = length + + /** Is this partial function defined for the index x? + */ + def isDefinedAt(x: Int): Boolean = (x >= 0) && (x < length) + + /** Returns length of longest segment starting from a start index `from` + * such that every element of the segment satisfies predicate `p`. + * @note may not terminate for infinite-sized collections. + * @param p the predicate + * @param from the start index + */ + def segmentLength(p: A => Boolean, from: Int): Int = { + var result = 0 + var i = 0 + breakable { + for (x <- this) { + if (i >= from && !p(x)) { result = i - from; break } + else i += 1 + } + } + result + } + + /** Returns length of longest prefix of this seqence + * such that every element of the prefix satisfies predicate `p`. + * @note may not terminate for infinite-sized collections. + * @param p the predicate + */ + def prefixLength(p: A => Boolean) = segmentLength(p, 0) + + /** Returns index of the first element satisfying a predicate, or -1, if none exists. + * + * @note may not terminate for infinite-sized collections. + * @param p the predicate + */ + def indexWhere(p: A => Boolean): Int = indexWhere(p, 0) + + /** Returns index of the first element starting from a start index + * satisying a predicate, or -1, if none exists. + * + * @note may not terminate for infinite-sized collections. + * @param p the predicate + * @param from the start index + */ + def indexWhere(p: A => Boolean, from: Int): Int = { + var result = -1 + var i = from + breakable { + for (x <- this) { + if (i >= from && p(x)) { result = i; break } + else i += 1 + } + } + result + } + + /** Returns index of the first element satisying a predicate, or -1. */ + @deprecated("Use `indexWhere' instead") + def findIndexOf(p: A => Boolean): Int = indexWhere(p) + + /** Returns the index of the first occurence of the specified + * object in this iterable object. + * + * @note may not terminate for infinite-sized collections. + * @param elem element to search for. + * @return the index in this sequence of the first occurence of the + * specified element, or -1 if the sequence does not contain + * this element. + */ + def indexOf[B >: A](elem: B): Int = indexOf(elem, 0) + + /** Returns the index of the first occurence of the specified + * object in this iterable object, starting from a start index, or + * -1, if none exists. + * + * @note may not terminate for infinite-sized collections. + * @param elem element to search for. + */ + def indexOf[B >: A](elem: B, from: Int): Int = indexWhere(elem ==, from) + + /** Returns the index of the last occurence of the specified element + * in this sequence, or -1 if the sequence does not contain this element. + * + * @param elem element to search for. + * @return the index in this sequence of the last occurence of the + * specified element, or -1 if the sequence does not contain + * this element. + */ + def lastIndexOf[B >: A](elem: B): Int = lastIndexWhere(elem ==) + + /** Returns the index of the last + * occurence of the specified element in this sequence + * before or at a given end index, + * or -1 if the sequence does not contain this element. + * + * @param elem element to search for. + * @param end the end index + */ + def lastIndexOf[B >: A](elem: B, end: Int): Int = lastIndexWhere(elem ==, end) + + /** Returns index of the last element satisying a predicate, or -1, if none exists. + * + * @param p the predicate + * @return the index of the last element satisfying p, + * or -1 if such an element does not exist + */ + def lastIndexWhere(p: A => Boolean): Int = lastIndexWhere(p, length - 1) + + /** Returns index of the last element not exceeding a given end index + * and satisying a predicate, or -1 if none exists. + * + * @param end the end index + * @param p the predicate + */ + def lastIndexWhere(p: A => Boolean, end: Int): Int = { + var i = length - 1 + val it = reverseIterator + while (it.hasNext && { val elem = it.next; (i > end || !p(elem)) }) i -= 1 + i + } + + /** A sequence of type C consisting of all elements of + * this sequence in reverse order. + * @note the operation is implemented by building a new sequence + * from this(length - 1), ..., this(0) + * If random access is inefficient for the given sequence implementation, + * this operation should be overridden. + */ + def reverse: Repr = { + var xs: List[A] = List() + for (x <- this) + xs = x :: xs + val b = newBuilder + for (x <- xs) + b += x + b.result + } + + /** The elements of this sequence in reversed order + */ + def reverseIterator: Iterator[A] = toCollection(reverse).iterator + + @deprecated("use `reverseIterator' instead") + def reversedElements = reverseIterator + + /** + * Checks whether the argument sequence is contained at the + * specified index within the receiver object. + * + * If the both the receiver object, this and + * the argument, that are infinite sequences + * this method may not terminate. + * + * @return true if that is contained in + * this, at the specified index, otherwise false + * + * @see String.startsWith + */ + def startsWith[B](that: Seq[B], offset: Int): Boolean = { + val i = this.iterator drop offset + val j = that.iterator + while (j.hasNext && i.hasNext) + if (i.next != j.next) + return false + + !j.hasNext + } + + /** + * Check whether the receiver object starts with the argument sequence. + * + * @return true if that is a prefix of this, + * otherwise false + */ + def startsWith[B](that: Seq[B]): Boolean = startsWith(that, 0) + + /** @return true if this sequence end with that sequence + * @see String.endsWith + */ + def endsWith[B](that: Seq[B]): Boolean = { + val i = this.iterator.drop(length - that.length) + val j = that.iterator + while (i.hasNext && j.hasNext) + if (i.next != j.next) + return false + + !j.hasNext + } + + /** @return -1 if that not contained in this, otherwise the + * first index where that is contained. + */ + def indexOfSeq[B >: A](that: Seq[B]): Int = indexOfSeq(that, 0) + + def indexOfSeq[B >: A](that: Seq[B], fromIndex: Int): Int = + if (this.hasDefiniteSize && that.hasDefiniteSize) + SeqLike.indexOf(thisCollection, 0, length, that, 0, that.length, fromIndex) + else { + var i = fromIndex + var s: Seq[A] = thisCollection drop i + while (!s.isEmpty) { + if (s startsWith that) + return i + + i += 1 + s = s.tail + } + -1 + } + + /** @return -1 if that not contained in this, otherwise the + * last index where that is contained. + * @note may not terminate for infinite-sized collections. + */ + def lastIndexOfSeq[B >: A](that: Seq[B]): Int = lastIndexOfSeq(that, that.length) + + // since there's no way to find the last index in an infinite sequence, + // we just document it may not terminate and assume it will. + def lastIndexOfSeq[B >: A](that: Seq[B], fromIndex: Int): Int = + SeqLike.lastIndexOf(thisCollection, 0, length, that, 0, that.length, fromIndex) + + /** Tests if the given value elem is a member of this + * sequence. + * + * @param elem element whose membership has to be tested. + * @return true iff there is an element of this sequence + * which is equal (w.r.t. ==) to elem. + */ + def contains(elem: Any): Boolean = exists (_ == elem) + + /**

+ * Computes the multiset union of this sequence and the given sequence + * that. For example: + *

+   *    val xs = List(1, 1, 2)
+   *    val ys = List(1, 2, 2, 3)
+   *    println(xs union ys)  // prints "List(1, 1, 2, 1, 2, 2, 3)"
+   *    println(ys union xs)  // prints "List(1, 2, 2, 3, 1, 1, 2)"
+   *  
+ * + * @param that the sequence of elements to add to the sequence. + * @return a sequence containing the elements of this + * sequence and those of the given sequence that. + */ + def union[B >: A, That](that: Seq[B])(implicit bf: BuilderFactory[B, That, Repr]): That = + this ++ that + + /**

+ * Computes the multiset difference between this sequence and the + * given sequence that. If an element appears more + * than once in both sequences, the difference contains m copies + * of that element, where m is the difference between the + * number of times the element appears in this sequence and the number + * of times it appears in that. For example: + *

+   *    val xs = List(1, 1, 2)
+   *    val ys = List(1, 2, 2, 3)
+   *    println(xs diff ys)  // prints "List(1)"
+   *    println(xs -- ys)    // prints "List()"
+   *  
+ * + * @param that the sequence of elements to remove from this sequence. + * @return the sequence of elements contained only in this sequence plus + * m copies of each element present in both sequences, + * where m is defined as above. + */ + def diff[B >: A, That](that: Seq[B]): Repr = { + val occ = occCounts(that) + val b = newBuilder + for (x <- this) + if (occ(x) == 0) b += x + else occ(x) -= 1 + b.result + } + + /**

+ * Computes the multiset intersection between this sequence and the + * given sequence that; the intersection contains m + * copies of an element contained in both sequences, where m is + * the smaller of the number of times the element appears in this + * sequence or in that. For example: + *

+   *    val xs = List(1, 1, 2)
+   *    val ys = List(3, 2, 2, 1)
+   *    println(xs intersect ys)  // prints "List(1, 2)"
+   *    println(ys intersect xs)  // prints "List(2, 1)"
+   *  
+ * + * @param that the sequence to intersect. + * @return the sequence of elements contained both in this sequence and + * in the given sequence that. + */ + def intersect[B >: A, That](that: Seq[B]): Repr = { + val occ = occCounts(that) + val b = newBuilder + for (x <- this) + if (occ(x) > 0) { + b += x + occ(x) -= 1 + } + b.result + } + + private def occCounts[B](seq: Seq[B]): mutable.Map[B, Int] = { + val occ = new mutable.HashMap[B, Int] { override def default(k: B) = 0 } + for (y <- seq) occ(y) += 1 + occ + } + + /** Builds a new sequence from this sequence in which any duplicates (wrt to ==) removed. + * Among duplicate elements, only the first one is retained in the result sequence + */ + def removeDuplicates: Repr = { + val b = newBuilder + var seen = Set[A]() + for (x <- this) { + if (!(seen contains x)) { + b += x + seen = (seen + x) + } + } + b.result + } + + /** A new sequence, consisting of all elements of current sequence + * except that `replaced` elements starting from `from` are replaced + * by `patch`. + */ + def patch[B >: A, That](from: Int, patch: Seq[B], replaced: Int)(implicit bf: BuilderFactory[B, That, Repr]): That = { + val b = bf(repr) + val (prefix, rest) = this.splitAt(from) + b ++= toCollection(prefix) + b ++= patch + b ++= toCollection(rest).view drop replaced + b.result + } + + /** Returns a new sequence of given length containing the elements of this sequence followed by zero + * or more occurrences of given elements. + */ + def padTo[B >: A, That](len: Int, elem: B)(implicit bf: BuilderFactory[B, That, Repr]): That = { + val b = bf(repr) + b.sizeHint(length max len) + var diff = len - length + b ++= thisCollection + while (diff > 0) { + b += elem + diff -=1 + } + b.result + } + + /** Sort the iterable according to the comparison function + * <(e1: a, e2: a) => Boolean, + * which should be true iff e1 is smaller than + * e2. + * The sort is stable. That is elements that are equal wrt `lt` appear in the + * same order in the sorted sequence as in the original. + * + * @param lt the comparison function + * @return an iterable sorted according to the comparison function + * <(e1: a, e2: a) => Boolean. + * @ex
+   *    List("Steve", "Tom", "John", "Bob")
+   *      .sortWith((e1, e2) => (e1 compareTo e2) < 0) =
+   *    List("Bob", "John", "Steve", "Tom")
+ */ + def sortWith(lt: (A, A) => Boolean): Repr = { + val arr = new GenericArray[A](this.length) + var i = 0 + for (x <- this) { + arr(i) = x + i += 1 + } + java.util.Arrays.sort( + arr.array, (Ordering fromLessThan lt).asInstanceOf[Ordering[Object]]) + val b = newBuilder + for (x <- arr) b += x + b.result + } + + /** + * Overridden for efficiency. + * + * @return the sequence itself + */ + override def toSeq: Seq[A] = thisCollection + + /** The range of all indices of this sequence. + */ + def indices: Range = 0 until length + + override def view = new SeqView[A, Repr] { + protected lazy val underlying = self.repr + override def iterator = self.iterator + override def length = self.length + override def apply(idx: Int) = self.apply(idx) + } + + override def view(from: Int, until: Int) = view.slice(from, until) + + override def hashCode() = (Seq.hashSeed /: this)(_ * 41 + _.hashCode) + + override def equals(that: Any): Boolean = that match { + case that: Seq[_] => (that canEqual this) && (this sameElements that) + case _ => false + } + + /** Need to override string, so that it's not the Function1's string that gets mixed in. + */ + override def toString = super[IterableLike].toString + + /** Returns index of the last element satisying a predicate, or -1. */ + @deprecated("use `lastIndexWhere' instead") + def findLastIndexOf(p: A => Boolean): Int = lastIndexWhere(p) + + /** A sub-sequence starting at index from + * and extending up to the length of the current sequence + * + * @param from The index of the first element of the slice + * @throws IndexOutOfBoundsException if from < 0 + */ + @deprecated("use `drop' instead") + def slice(from: Int): Seq[A] = toCollection(slice(from, length)) + + @deprecated("Should be replaced by (s1, s2) forall { case (x, y) => f(x, y) }") + def equalsWith[B](that: Seq[B])(f: (A,B) => Boolean): Boolean = { + val i = this.iterator + val j = that.iterator + while (i.hasNext && j.hasNext) + if (!f(i.next, j.next)) + return false + + !i.hasNext && !j.hasNext + } + + /** Is that a slice in this? */ + @deprecated("Should be replaced by indexOfSeq(that) != -1") + def containsSlice[B](that: Seq[B]): Boolean = indexOfSeq(that) != -1 + + /** + * returns a projection that can be used to call non-strict filter, + * map, and flatMap methods that build projections + * of the collection. + */ + @deprecated("use `view' instead") + override def projection = view +} + diff --git a/src/library/scala/collection/SeqProxy.scala b/src/library/scala/collection/SeqProxy.scala new file mode 100644 index 0000000000..d2e4c0a7b7 --- /dev/null +++ b/src/library/scala/collection/SeqProxy.scala @@ -0,0 +1,21 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2009, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + + +package scala.collection + +/** This trait implements a proxy for sequence objects. It forwards + * all calls to a different sequence object. + * + * @author Martin Odersky + * @version 2.8 + * @since 2.8 + */ +trait SeqProxy[+A] extends Seq[A] with SeqProxyLike[A, Seq[A]] diff --git a/src/library/scala/collection/SeqProxyLike.scala b/src/library/scala/collection/SeqProxyLike.scala new file mode 100644 index 0000000000..4ea57a2e99 --- /dev/null +++ b/src/library/scala/collection/SeqProxyLike.scala @@ -0,0 +1,63 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2009, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + + +package scala.collection + +import generic._ + +// Methods could be printed by cat SeqLike.scala | egrep '^ (override )?def' + + +/** This trait implements a proxy for sequences. It forwards + * all calls to a different sequence. + * + * @author Martin Odersky + * @version 2.8 + * @since 2.8 + */ +trait SeqProxyLike[+A, +This <: SeqLike[A, This] with Seq[A]] extends SeqLike[A, This] with IterableProxyLike[A, This] { + override def length: Int = self.length + override def apply(idx: Int): A = self.apply(idx) + override def lengthCompare(len: Int): Int = self.lengthCompare(len) + override def size = self.size + override def isDefinedAt(x: Int): Boolean = self.isDefinedAt(x) + override def segmentLength(p: A => Boolean, from: Int): Int = self.segmentLength(p, from) + override def prefixLength(p: A => Boolean) = self.prefixLength(p) + override def indexWhere(p: A => Boolean): Int = self.indexWhere(p) + override def indexWhere(p: A => Boolean, from: Int): Int = self.indexWhere(p, from) + override def findIndexOf(p: A => Boolean): Int = self.indexWhere(p) + override def indexOf[B >: A](elem: B): Int = self.indexOf(elem) + override def indexOf[B >: A](elem: B, from: Int): Int = self.indexOf(elem, from) + override def lastIndexOf[B >: A](elem: B): Int = self.lastIndexOf(elem) + override def lastIndexOf[B >: A](elem: B, end: Int): Int = self.lastIndexWhere(elem ==, end) + override def lastIndexWhere(p: A => Boolean): Int = self.lastIndexWhere(p, length - 1) + override def lastIndexWhere(p: A => Boolean, end: Int): Int = self.lastIndexWhere(p) + override def reverse: This = self.reverse + override def reverseIterator: Iterator[A] = self.reverseIterator + override def startsWith[B](that: Seq[B], offset: Int): Boolean = self.startsWith(that, offset) + override def startsWith[B](that: Seq[B]): Boolean = self.startsWith(that) + override def endsWith[B](that: Seq[B]): Boolean = self.endsWith(that) + override def indexOfSeq[B >: A](that: Seq[B]): Int = self.indexOfSeq(that) + override def contains(elem: Any): Boolean = self.contains(elem) + override def union[B >: A, That](that: Seq[B])(implicit bf: BuilderFactory[B, That, This]): That = self.union(that)(bf) + override def diff[B >: A, That](that: Seq[B]): This = self.diff(that) + override def intersect[B >: A, That](that: Seq[B]): This = self.intersect(that) + override def removeDuplicates: This = self.removeDuplicates + override def patch[B >: A, That](from: Int, patch: Seq[B], replaced: Int)(implicit bf: BuilderFactory[B, That, This]): That = self.patch(from, patch, replaced)(bf) + override def padTo[B >: A, That](len: Int, elem: B)(implicit bf: BuilderFactory[B, That, This]): That = self.padTo(len, elem)(bf) + override def indices: Range = self.indices + override def view = self.view + override def view(from: Int, until: Int) = self.view(from, until) + override def findLastIndexOf(p: A => Boolean): Int = self.lastIndexWhere(p) + override def slice(from: Int): Seq[A] = self.drop(from) + override def equalsWith[B](that: Seq[B])(f: (A,B) => Boolean): Boolean = (self zip that) forall { case (x,y) => f(x,y) } + override def containsSlice[B](that: Seq[B]): Boolean = self.indexOfSeq(that) != -1 +} diff --git a/src/library/scala/collection/SeqView.scala b/src/library/scala/collection/SeqView.scala new file mode 100644 index 0000000000..40e1022754 --- /dev/null +++ b/src/library/scala/collection/SeqView.scala @@ -0,0 +1,28 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2009, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + + +package scala.collection + +import generic._ +import TraversableView.NoBuilder + +/** A non-strict projection of an iterable. + * @author Sean McDirmid + * @author Martin Odersky + * @version 2.8 + */ +trait SeqView[+A, +Coll] extends SeqViewLike[A, Coll, SeqView[A, Coll]] + +object SeqView { + type Coll = TraversableView[_, C] forSome {type C <: Traversable[_]} + implicit def builderFactory[A]: BuilderFactory[A, SeqView[A, Seq[_]], Coll] = new BuilderFactory[A, SeqView[A, Seq[_]], Coll] { def apply(from: Coll) = new NoBuilder } +} + diff --git a/src/library/scala/collection/SeqViewLike.scala b/src/library/scala/collection/SeqViewLike.scala new file mode 100644 index 0000000000..471d447a15 --- /dev/null +++ b/src/library/scala/collection/SeqViewLike.scala @@ -0,0 +1,156 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2009, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + + +package scala.collection + +import generic._ +import Seq.fill +import TraversableView.NoBuilder + +/** A non-strict projection of an iterable. + * @author Sean McDirmid + * @author Martin Odersky + * @version 2.8 + */ +trait SeqViewLike[+A, + +Coll, + +This <: SeqView[A, Coll] with SeqViewLike[A, Coll, This]] + extends Seq[A] with SeqLike[A, This] with IterableView[A, Coll] with IterableViewLike[A, Coll, This] +{ self => + + trait Transformed[+B] extends SeqView[B, Coll] with super.Transformed[B] { + override def length: Int + override def apply(idx: Int): B + } + + trait Sliced extends Transformed[A] with super.Sliced { + override def length = ((until min self.length) - from) max 0 + override def apply(idx: Int): A = + if (idx + from < until) self.apply(idx + from) + else throw new IndexOutOfBoundsException(idx.toString) + } + + trait Mapped[B] extends Transformed[B] with super.Mapped[B] { + override def length = self.length + override def apply(idx: Int): B = mapping(self apply idx) + } + + trait FlatMapped[B] extends Transformed[B] with super.FlatMapped[B] { + protected[this] lazy val index = { + val index = new Array[Int](self.length + 1) + index(0) = 0 + for (i <- 0 until self.length) + index(i + 1) = index(i) + mapping(self(i)).size + index + } + protected[this] def findRow(idx: Int, lo: Int, hi: Int): Int = { + val mid = (lo + hi) / 2 + if (idx < index(mid)) findRow(idx, lo, mid - 1) + else if (idx >= index(mid + 1)) findRow(idx, mid + 1, hi) + else mid + } + override def length = index(self.length) + override def apply(idx: Int) = { + val row = findRow(idx, 0, self.length - 1) + mapping(self(row)).toSeq(idx - index(row)) + } + } + + trait Appended[B >: A] extends Transformed[B] with super.Appended[B] { + lazy val restSeq = rest.toSeq + override def length = self.length + restSeq.length + override def apply(idx: Int) = + if (idx < self.length) self(idx) else restSeq(idx - self.length) + } + + trait Filtered extends Transformed[A] with super.Filtered { + protected[this] lazy val index = { + var len = 0 + val arr = new Array[Int](self.length) + for (i <- 0 until self.length) + if (pred(self(i))) { + arr(len) = i + len += 1 + } + arr take len + } + override def length = index.length + override def apply(idx: Int) = self(index(idx)) + } + + trait TakenWhile extends Transformed[A] with super.TakenWhile { + protected[this] lazy val len = self prefixLength pred + override def length = len + override def apply(idx: Int) = + if (idx < len) self(idx) + else throw new IndexOutOfBoundsException(idx.toString) + } + + trait DroppedWhile extends Transformed[A] with super.DroppedWhile { + protected[this] lazy val start = self prefixLength pred + override def length = self.length - start + override def apply(idx: Int) = + if (idx >= 0) self(idx + start) + else throw new IndexOutOfBoundsException(idx.toString) + } + + trait Reversed extends Transformed[A] { + override def iterator: Iterator[A] = self.reverseIterator + override def length: Int = self.length + override def apply(idx: Int): A = self.apply(length - 1 - idx) + override def stringPrefix = self.stringPrefix+"R" + } + + trait Patched[B >: A] extends Transformed[B] { + protected[this] val from: Int + protected[this] val patch: Seq[B] + protected[this] val replaced: Int + private lazy val plen = patch.length + override def iterator: Iterator[B] = self.iterator patch (from, patch.iterator, replaced) + override def length: Int = self.length + plen - replaced + override def apply(idx: Int): B = + if (idx < from) self.apply(idx) + else if (idx < from + plen) patch.apply(idx - from) + else self.apply(idx - plen + replaced) + override def stringPrefix = self.stringPrefix+"P" + } + + /** Boilerplate method, to override in each subclass + * This method could be eliminated if Scala had virtual classes + */ + protected override def newAppended[B >: A](that: Traversable[B]): Transformed[B] = new Appended[B] { val rest = that } + protected override def newMapped[B](f: A => B): Transformed[B] = new Mapped[B] { val mapping = f } + protected override def newFlatMapped[B](f: A => Traversable[B]): Transformed[B] = new FlatMapped[B] { val mapping = f } + protected override def newFiltered(p: A => Boolean): Transformed[A] = new Filtered { val pred = p } + protected override def newSliced(_from: Int, _until: Int): Transformed[A] = new Sliced { val from = _from; val until = _until } + protected override def newDroppedWhile(p: A => Boolean): Transformed[A] = new DroppedWhile { val pred = p } + protected override def newTakenWhile(p: A => Boolean): Transformed[A] = new TakenWhile { val pred = p } + protected def newReversed: Transformed[A] = new Reversed { } + protected def newPatched[B >: A](_from: Int, _patch: Seq[B], _replaced: Int): Transformed[B] = new Patched[B] { + val from = _from; val patch = _patch; val replaced = _replaced + } + + override def reverse: This = newReversed.asInstanceOf[This] + + override def patch[B >: A, That](from: Int, patch: Seq[B], replaced: Int)(implicit bf: BuilderFactory[B, That, This]): That = { + newPatched(from, patch, replaced).asInstanceOf[That] +// was: val b = bf(repr) +// if (b.isInstanceOf[NoBuilder[_]]) newPatched(from, patch, replaced).asInstanceOf[That] +// else super.patch[B, That](from, patch, replaced)(bf) + } + + override def padTo[B >: A, That](len: Int, elem: B)(implicit bf: BuilderFactory[B, That, This]): That = + patch(length, fill(len - length)(elem), 0) + + override def stringPrefix = "SeqView" +} + + diff --git a/src/library/scala/collection/Sequence.scala b/src/library/scala/collection/Sequence.scala deleted file mode 100644 index b9439c909b..0000000000 --- a/src/library/scala/collection/Sequence.scala +++ /dev/null @@ -1,62 +0,0 @@ -/* __ *\ -** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003-2009, LAMP/EPFL ** -** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** -** /____/\___/_/ |_/____/_/ | | ** -** |/ ** -\* */ - -// $Id$ - - -package scala.collection - -import generic._ -import mutable.Builder - -/**

- * Class Sequence[A] represents sequences of elements - * of type A.
- * It adds the following methods to class Iterable: - * length, lengthCompare, apply, - * isDefinedAt, segmentLength, - * prefixLength, indexWhere, indexOf, - * lastIndexWhere, lastIndexOf, reverse, - * reverseIterator, startsWith, - * endsWith, indexOfSeq. - *

- * - * @author Martin Odersky - * @author Matthias Zenger - * @version 1.0, 16/07/2003 - */ -trait Sequence[+A] extends PartialFunction[Int, A] - with Iterable[A] - with GenericTraversableTemplate[A, Sequence] - with SequenceLike[A, Sequence[A]] { - override def companion: GenericCompanion[Sequence] = Sequence -} - -/** Factory object for Sequence trait. - * - * @author Martin Odersky - * @version 2.8 - */ -object Sequence extends SequenceFactory[Sequence] { - - private[collection] val hashSeed = "Sequence".hashCode - - implicit def builderFactory[A]: BuilderFactory[A, Sequence[A], Coll] = new VirtualBuilderFactory[A] - def newBuilder[A]: Builder[A, Sequence[A]] = immutable.Sequence.newBuilder[A] - - @deprecated("use View instead") - type Projection[A] = SequenceView[A, Coll] - - @deprecated("use Sequence(value) instead") - def singleton[A](value: A) = Sequence(value) - - /** Builds a singleton sequence. */ - @deprecated("use Sequence(x) instead.") - def single[A](x: A) = singleton(x) -} - diff --git a/src/library/scala/collection/SequenceLike.scala b/src/library/scala/collection/SequenceLike.scala deleted file mode 100644 index 20f0c20f57..0000000000 --- a/src/library/scala/collection/SequenceLike.scala +++ /dev/null @@ -1,597 +0,0 @@ -/* __ *\ -** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003-2009, LAMP/EPFL ** -** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** -** /____/\___/_/ |_/____/_/ | | ** -** |/ ** -\* */ - -// $Id$ - - -package scala.collection -import generic._ -import mutable.{ListBuffer, HashMap, GenericArray} - -// import immutable.{List, Nil, ::} -import generic._ - -/** Contains a KMP implementation, based on the undoubtedly reliable wikipedia entry. - * - * @author paulp - * @since 2.8 - */ -object SequenceLike { - - private def KMP[B](S: Seq[B], W: Seq[B]): Option[Int] = { - // trivial cases - if (W.isEmpty) return Some(0) - else if (W drop 1 isEmpty) return (S indexOf W(0)) match { - case -1 => None - case x => Some(x) - } - - val T: Array[Int] = { - val arr = new Array[Int](W.length) - var pos = 2 - var cnd = 0 - arr(0) = -1 - arr(1) = 0 - while (pos < W.length) { - if (W(pos - 1) == W(cnd)) { - arr(pos) = cnd + 1 - pos += 1 - cnd += 1 - } - else if (cnd > 0) { - cnd = arr(cnd) - } - else { - arr(pos) = 0 - pos += 1 - } - } - arr - } - - var m, i = 0 - def mi = m + i - - while (mi < S.length) { - if (W(i) == S(mi)) { - i += 1 - if (i == W.length) - return Some(m) - } - else { - m = mi - T(i) - if (i > 0) - i = T(i) - } - } - None - } - - def indexOf[B]( - source: Seq[B], sourceOffset: Int, sourceCount: Int, - target: Seq[B], targetOffset: Int, targetCount: Int, - fromIndex: Int): Int = - KMP(source.slice(sourceOffset, sourceCount) drop fromIndex, target.slice(targetOffset, targetCount)) match { - case None => -1 - case Some(x) => x + fromIndex - } - - def lastIndexOf[B]( - source: Seq[B], sourceOffset: Int, sourceCount: Int, - target: Seq[B], targetOffset: Int, targetCount: Int, - fromIndex: Int): Int = { - val src = (source.slice(sourceOffset, sourceCount) take fromIndex).reverse - val tgt = target.slice(targetOffset, targetCount).reverse - - KMP(src, tgt) match { - case None => -1 - case Some(x) => (src.length - tgt.length - x) + sourceOffset - } - } -} - -/** Class Sequence[A] represents sequences of elements - * of type A. - * It adds the following methods to class Iterable: - * `length`, `lengthCompare`, `apply`, `isDefinedAt`, `segmentLength`, `prefixLength`, - * `indexWhere`, `indexOf`, `lastIndexWhere`, `lastIndexOf`, `reverse`, `reverseIterator`, - * `startsWith`, `endsWith`, `indexOfSeq`, , `zip`, `zipAll`, `zipWithIndex`. - * - * - * @author Martin Odersky - * @author Matthias Zenger - * @version 1.0, 16/07/2003 - * @since 2.8 - */ -trait SequenceLike[+A, +Repr] extends IterableLike[A, Repr] { self => - - override protected[this] def thisCollection: Sequence[A] = this.asInstanceOf[Sequence[A]] - override protected[this] def toCollection(repr: Repr): Sequence[A] = repr.asInstanceOf[Sequence[A]] - - import Traversable.breaks._ - - /** Returns the length of the sequence. - */ - def length: Int - - /** Returns the elements at position `idx` - */ - def apply(idx: Int): A - - /** Result of comparing length with operand len. - * returns x where - * x < 0 iff this.length < len - * x == 0 iff this.length == len - * x > 0 iff this.length > len. - * - * The method as implemented here does not call length directly; its running time - * is O(length min len) instead of O(length). The method should be overwritten - * if computing length is cheap. - */ - def lengthCompare(len: Int): Int = { - var i = 0 - breakable { - for (_ <- this) { - i += 1 - if (i > len) break - } - } - i - len - } - - /** Should always be length */ - override def size = length - - /** Is this partial function defined for the index x? - */ - def isDefinedAt(x: Int): Boolean = (x >= 0) && (x < length) - - /** Returns length of longest segment starting from a start index `from` - * such that every element of the segment satisfies predicate `p`. - * @note may not terminate for infinite-sized collections. - * @param p the predicate - * @param from the start index - */ - def segmentLength(p: A => Boolean, from: Int): Int = { - var result = 0 - var i = 0 - breakable { - for (x <- this) { - if (i >= from && !p(x)) { result = i - from; break } - else i += 1 - } - } - result - } - - /** Returns length of longest prefix of this seqence - * such that every element of the prefix satisfies predicate `p`. - * @note may not terminate for infinite-sized collections. - * @param p the predicate - */ - def prefixLength(p: A => Boolean) = segmentLength(p, 0) - - /** Returns index of the first element satisfying a predicate, or -1, if none exists. - * - * @note may not terminate for infinite-sized collections. - * @param p the predicate - */ - def indexWhere(p: A => Boolean): Int = indexWhere(p, 0) - - /** Returns index of the first element starting from a start index - * satisying a predicate, or -1, if none exists. - * - * @note may not terminate for infinite-sized collections. - * @param p the predicate - * @param from the start index - */ - def indexWhere(p: A => Boolean, from: Int): Int = { - var result = -1 - var i = from - breakable { - for (x <- this) { - if (i >= from && p(x)) { result = i; break } - else i += 1 - } - } - result - } - - /** Returns index of the first element satisying a predicate, or -1. */ - @deprecated("Use `indexWhere' instead") - def findIndexOf(p: A => Boolean): Int = indexWhere(p) - - /** Returns the index of the first occurence of the specified - * object in this iterable object. - * - * @note may not terminate for infinite-sized collections. - * @param elem element to search for. - * @return the index in this sequence of the first occurence of the - * specified element, or -1 if the sequence does not contain - * this element. - */ - def indexOf[B >: A](elem: B): Int = indexOf(elem, 0) - - /** Returns the index of the first occurence of the specified - * object in this iterable object, starting from a start index, or - * -1, if none exists. - * - * @note may not terminate for infinite-sized collections. - * @param elem element to search for. - */ - def indexOf[B >: A](elem: B, from: Int): Int = indexWhere(elem ==, from) - - /** Returns the index of the last occurence of the specified element - * in this sequence, or -1 if the sequence does not contain this element. - * - * @param elem element to search for. - * @return the index in this sequence of the last occurence of the - * specified element, or -1 if the sequence does not contain - * this element. - */ - def lastIndexOf[B >: A](elem: B): Int = lastIndexWhere(elem ==) - - /** Returns the index of the last - * occurence of the specified element in this sequence - * before or at a given end index, - * or -1 if the sequence does not contain this element. - * - * @param elem element to search for. - * @param end the end index - */ - def lastIndexOf[B >: A](elem: B, end: Int): Int = lastIndexWhere(elem ==, end) - - /** Returns index of the last element satisying a predicate, or -1, if none exists. - * - * @param p the predicate - * @return the index of the last element satisfying p, - * or -1 if such an element does not exist - */ - def lastIndexWhere(p: A => Boolean): Int = lastIndexWhere(p, length - 1) - - /** Returns index of the last element not exceeding a given end index - * and satisying a predicate, or -1 if none exists. - * - * @param end the end index - * @param p the predicate - */ - def lastIndexWhere(p: A => Boolean, end: Int): Int = { - var i = length - 1 - val it = reverseIterator - while (it.hasNext && { val elem = it.next; (i > end || !p(elem)) }) i -= 1 - i - } - - /** A sequence of type C consisting of all elements of - * this sequence in reverse order. - * @note the operation is implemented by building a new sequence - * from this(length - 1), ..., this(0) - * If random access is inefficient for the given sequence implementation, - * this operation should be overridden. - */ - def reverse: Repr = { - var xs: List[A] = List() - for (x <- this) - xs = x :: xs - val b = newBuilder - for (x <- xs) - b += x - b.result - } - - /** The elements of this sequence in reversed order - */ - def reverseIterator: Iterator[A] = toCollection(reverse).iterator - - @deprecated("use `reverseIterator' instead") - def reversedElements = reverseIterator - - /** - * Checks whether the argument sequence is contained at the - * specified index within the receiver object. - * - * If the both the receiver object, this and - * the argument, that are infinite sequences - * this method may not terminate. - * - * @return true if that is contained in - * this, at the specified index, otherwise false - * - * @see String.startsWith - */ - def startsWith[B](that: Sequence[B], offset: Int): Boolean = { - val i = this.iterator drop offset - val j = that.iterator - while (j.hasNext && i.hasNext) - if (i.next != j.next) - return false - - !j.hasNext - } - - /** - * Check whether the receiver object starts with the argument sequence. - * - * @return true if that is a prefix of this, - * otherwise false - */ - def startsWith[B](that: Sequence[B]): Boolean = startsWith(that, 0) - - /** @return true if this sequence end with that sequence - * @see String.endsWith - */ - def endsWith[B](that: Sequence[B]): Boolean = { - val i = this.iterator.drop(length - that.length) - val j = that.iterator - while (i.hasNext && j.hasNext) - if (i.next != j.next) - return false - - !j.hasNext - } - - /** @return -1 if that not contained in this, otherwise the - * first index where that is contained. - */ - def indexOfSeq[B >: A](that: Sequence[B]): Int = indexOfSeq(that, 0) - - def indexOfSeq[B >: A](that: Sequence[B], fromIndex: Int): Int = - if (this.hasDefiniteSize && that.hasDefiniteSize) - SequenceLike.indexOf(thisCollection, 0, length, that, 0, that.length, fromIndex) - else { - var i = fromIndex - var s: Sequence[A] = thisCollection drop i - while (!s.isEmpty) { - if (s startsWith that) - return i - - i += 1 - s = s.tail - } - -1 - } - - /** @return -1 if that not contained in this, otherwise the - * last index where that is contained. - * @note may not terminate for infinite-sized collections. - */ - def lastIndexOfSeq[B >: A](that: Sequence[B]): Int = lastIndexOfSeq(that, that.length) - - // since there's no way to find the last index in an infinite sequence, - // we just document it may not terminate and assume it will. - def lastIndexOfSeq[B >: A](that: Sequence[B], fromIndex: Int): Int = - SequenceLike.lastIndexOf(thisCollection, 0, length, that, 0, that.length, fromIndex) - - /** Tests if the given value elem is a member of this - * sequence. - * - * @param elem element whose membership has to be tested. - * @return true iff there is an element of this sequence - * which is equal (w.r.t. ==) to elem. - */ - def contains(elem: Any): Boolean = exists (_ == elem) - - /**

- * Computes the multiset union of this sequence and the given sequence - * that. For example: - *

-   *    val xs = List(1, 1, 2)
-   *    val ys = List(1, 2, 2, 3)
-   *    println(xs union ys)  // prints "List(1, 1, 2, 1, 2, 2, 3)"
-   *    println(ys union xs)  // prints "List(1, 2, 2, 3, 1, 1, 2)"
-   *  
- * - * @param that the sequence of elements to add to the sequence. - * @return a sequence containing the elements of this - * sequence and those of the given sequence that. - */ - def union[B >: A, That](that: Sequence[B])(implicit bf: BuilderFactory[B, That, Repr]): That = - this ++ that - - /**

- * Computes the multiset difference between this sequence and the - * given sequence that. If an element appears more - * than once in both sequences, the difference contains m copies - * of that element, where m is the difference between the - * number of times the element appears in this sequence and the number - * of times it appears in that. For example: - *

-   *    val xs = List(1, 1, 2)
-   *    val ys = List(1, 2, 2, 3)
-   *    println(xs diff ys)  // prints "List(1)"
-   *    println(xs -- ys)    // prints "List()"
-   *  
- * - * @param that the sequence of elements to remove from this sequence. - * @return the sequence of elements contained only in this sequence plus - * m copies of each element present in both sequences, - * where m is defined as above. - */ - def diff[B >: A, That](that: Sequence[B]): Repr = { - val occ = occCounts(that) - val b = newBuilder - for (x <- this) - if (occ(x) == 0) b += x - else occ(x) -= 1 - b.result - } - - /**

- * Computes the multiset intersection between this sequence and the - * given sequence that; the intersection contains m - * copies of an element contained in both sequences, where m is - * the smaller of the number of times the element appears in this - * sequence or in that. For example: - *

-   *    val xs = List(1, 1, 2)
-   *    val ys = List(3, 2, 2, 1)
-   *    println(xs intersect ys)  // prints "List(1, 2)"
-   *    println(ys intersect xs)  // prints "List(2, 1)"
-   *  
- * - * @param that the sequence to intersect. - * @return the sequence of elements contained both in this sequence and - * in the given sequence that. - */ - def intersect[B >: A, That](that: Sequence[B]): Repr = { - val occ = occCounts(that) - val b = newBuilder - for (x <- this) - if (occ(x) > 0) { - b += x - occ(x) -= 1 - } - b.result - } - - private def occCounts[B](seq: Sequence[B]): mutable.Map[B, Int] = { - val occ = new mutable.HashMap[B, Int] { override def default(k: B) = 0 } - for (y <- seq) occ(y) += 1 - occ - } - - /** Builds a new sequence from this sequence in which any duplicates (wrt to ==) removed. - * Among duplicate elements, only the first one is retained in the result sequence - */ - def removeDuplicates: Repr = { - val b = newBuilder - var seen = Set[A]() - for (x <- this) { - if (!(seen contains x)) { - b += x - seen = (seen + x) - } - } - b.result - } - - /** A new sequence, consisting of all elements of current sequence - * except that `replaced` elements starting from `from` are replaced - * by `patch`. - */ - def patch[B >: A, That](from: Int, patch: Sequence[B], replaced: Int)(implicit bf: BuilderFactory[B, That, Repr]): That = { - val b = bf(repr) - val (prefix, rest) = this.splitAt(from) - b ++= toCollection(prefix) - b ++= patch - b ++= toCollection(rest).view drop replaced - b.result - } - - /** Returns a new sequence of given length containing the elements of this sequence followed by zero - * or more occurrences of given elements. - */ - def padTo[B >: A, That](len: Int, elem: B)(implicit bf: BuilderFactory[B, That, Repr]): That = { - val b = bf(repr) - b.sizeHint(length max len) - var diff = len - length - b ++= thisCollection - while (diff > 0) { - b += elem - diff -=1 - } - b.result - } - - /** Sort the iterable according to the comparison function - * <(e1: a, e2: a) => Boolean, - * which should be true iff e1 is smaller than - * e2. - * The sort is stable. That is elements that are equal wrt `lt` appear in the - * same order in the sorted sequence as in the original. - * - * @param lt the comparison function - * @return an iterable sorted according to the comparison function - * <(e1: a, e2: a) => Boolean. - * @ex
-   *    List("Steve", "Tom", "John", "Bob")
-   *      .sortWith((e1, e2) => (e1 compareTo e2) < 0) =
-   *    List("Bob", "John", "Steve", "Tom")
- */ - def sortWith(lt: (A, A) => Boolean): Repr = { - val arr = new GenericArray[A](this.length) - var i = 0 - for (x <- this) { - arr(i) = x - i += 1 - } - java.util.Arrays.sort( - arr.array, (Ordering fromLessThan lt).asInstanceOf[Ordering[Object]]) - val b = newBuilder - for (x <- arr) b += x - b.result - } - - /** - * Overridden for efficiency. - * - * @return the sequence itself - */ - override def toSequence: Sequence[A] = thisCollection - - /** The range of all indices of this sequence. - */ - def indices: Range = 0 until length - - override def view = new SequenceView[A, Repr] { - protected lazy val underlying = self.repr - override def iterator = self.iterator - override def length = self.length - override def apply(idx: Int) = self.apply(idx) - } - - override def view(from: Int, until: Int) = view.slice(from, until) - - override def hashCode() = (Sequence.hashSeed /: this)(_ * 41 + _.hashCode) - - override def equals(that: Any): Boolean = that match { - case that: Sequence[_] => (that canEqual this) && (this sameElements that) - case _ => false - } - - /** Need to override string, so that it's not the Function1's string that gets mixed in. - */ - override def toString = super[IterableLike].toString - - /** Returns index of the last element satisying a predicate, or -1. */ - @deprecated("use `lastIndexWhere' instead") - def findLastIndexOf(p: A => Boolean): Int = lastIndexWhere(p) - - /** A sub-sequence starting at index from - * and extending up to the length of the current sequence - * - * @param from The index of the first element of the slice - * @throws IndexOutOfBoundsException if from < 0 - */ - @deprecated("use `drop' instead") - def slice(from: Int): Sequence[A] = toCollection(slice(from, length)) - - @deprecated("Should be replaced by (s1, s2) forall { case (x, y) => f(x, y) }") - def equalsWith[B](that: Sequence[B])(f: (A,B) => Boolean): Boolean = { - val i = this.iterator - val j = that.iterator - while (i.hasNext && j.hasNext) - if (!f(i.next, j.next)) - return false - - !i.hasNext && !j.hasNext - } - - /** Is that a slice in this? */ - @deprecated("Should be replaced by indexOfSeq(that) != -1") - def containsSlice[B](that: Sequence[B]): Boolean = indexOfSeq(that) != -1 - - /** - * returns a projection that can be used to call non-strict filter, - * map, and flatMap methods that build projections - * of the collection. - */ - @deprecated("use `view' instead") - override def projection = view -} - diff --git a/src/library/scala/collection/SequenceProxy.scala b/src/library/scala/collection/SequenceProxy.scala deleted file mode 100644 index ffa956faf1..0000000000 --- a/src/library/scala/collection/SequenceProxy.scala +++ /dev/null @@ -1,21 +0,0 @@ -/* __ *\ -** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003-2009, LAMP/EPFL ** -** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** -** /____/\___/_/ |_/____/_/ | | ** -** |/ ** -\* */ - -// $Id$ - - -package scala.collection - -/** This trait implements a proxy for sequence objects. It forwards - * all calls to a different sequence object. - * - * @author Martin Odersky - * @version 2.8 - * @since 2.8 - */ -trait SequenceProxy[+A] extends Sequence[A] with SequenceProxyLike[A, Sequence[A]] diff --git a/src/library/scala/collection/SequenceProxyLike.scala b/src/library/scala/collection/SequenceProxyLike.scala deleted file mode 100644 index d2c846fb58..0000000000 --- a/src/library/scala/collection/SequenceProxyLike.scala +++ /dev/null @@ -1,63 +0,0 @@ -/* __ *\ -** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003-2009, LAMP/EPFL ** -** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** -** /____/\___/_/ |_/____/_/ | | ** -** |/ ** -\* */ - -// $Id$ - - -package scala.collection - -import generic._ - -// Methods could be printed by cat SequenceLike.scala | egrep '^ (override )?def' - - -/** This trait implements a proxy for sequences. It forwards - * all calls to a different sequence. - * - * @author Martin Odersky - * @version 2.8 - * @since 2.8 - */ -trait SequenceProxyLike[+A, +This <: SequenceLike[A, This] with Sequence[A]] extends SequenceLike[A, This] with IterableProxyLike[A, This] { - override def length: Int = self.length - override def apply(idx: Int): A = self.apply(idx) - override def lengthCompare(len: Int): Int = self.lengthCompare(len) - override def size = self.size - override def isDefinedAt(x: Int): Boolean = self.isDefinedAt(x) - override def segmentLength(p: A => Boolean, from: Int): Int = self.segmentLength(p, from) - override def prefixLength(p: A => Boolean) = self.prefixLength(p) - override def indexWhere(p: A => Boolean): Int = self.indexWhere(p) - override def indexWhere(p: A => Boolean, from: Int): Int = self.indexWhere(p, from) - override def findIndexOf(p: A => Boolean): Int = self.indexWhere(p) - override def indexOf[B >: A](elem: B): Int = self.indexOf(elem) - override def indexOf[B >: A](elem: B, from: Int): Int = self.indexOf(elem, from) - override def lastIndexOf[B >: A](elem: B): Int = self.lastIndexOf(elem) - override def lastIndexOf[B >: A](elem: B, end: Int): Int = self.lastIndexWhere(elem ==, end) - override def lastIndexWhere(p: A => Boolean): Int = self.lastIndexWhere(p, length - 1) - override def lastIndexWhere(p: A => Boolean, end: Int): Int = self.lastIndexWhere(p) - override def reverse: This = self.reverse - override def reverseIterator: Iterator[A] = self.reverseIterator - override def startsWith[B](that: Sequence[B], offset: Int): Boolean = self.startsWith(that, offset) - override def startsWith[B](that: Sequence[B]): Boolean = self.startsWith(that) - override def endsWith[B](that: Sequence[B]): Boolean = self.endsWith(that) - override def indexOfSeq[B >: A](that: Sequence[B]): Int = self.indexOfSeq(that) - override def contains(elem: Any): Boolean = self.contains(elem) - override def union[B >: A, That](that: Sequence[B])(implicit bf: BuilderFactory[B, That, This]): That = self.union(that)(bf) - override def diff[B >: A, That](that: Sequence[B]): This = self.diff(that) - override def intersect[B >: A, That](that: Sequence[B]): This = self.intersect(that) - override def removeDuplicates: This = self.removeDuplicates - override def patch[B >: A, That](from: Int, patch: Sequence[B], replaced: Int)(implicit bf: BuilderFactory[B, That, This]): That = self.patch(from, patch, replaced)(bf) - override def padTo[B >: A, That](len: Int, elem: B)(implicit bf: BuilderFactory[B, That, This]): That = self.padTo(len, elem)(bf) - override def indices: Range = self.indices - override def view = self.view - override def view(from: Int, until: Int) = self.view(from, until) - override def findLastIndexOf(p: A => Boolean): Int = self.lastIndexWhere(p) - override def slice(from: Int): Sequence[A] = self.drop(from) - override def equalsWith[B](that: Sequence[B])(f: (A,B) => Boolean): Boolean = (self zip that) forall { case (x,y) => f(x,y) } - override def containsSlice[B](that: Sequence[B]): Boolean = self.indexOfSeq(that) != -1 -} diff --git a/src/library/scala/collection/SequenceView.scala b/src/library/scala/collection/SequenceView.scala deleted file mode 100644 index 3a03493932..0000000000 --- a/src/library/scala/collection/SequenceView.scala +++ /dev/null @@ -1,28 +0,0 @@ -/* __ *\ -** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003-2009, LAMP/EPFL ** -** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** -** /____/\___/_/ |_/____/_/ | | ** -** |/ ** -\* */ - -// $Id$ - - -package scala.collection - -import generic._ -import TraversableView.NoBuilder - -/** A non-strict projection of an iterable. - * @author Sean McDirmid - * @author Martin Odersky - * @version 2.8 - */ -trait SequenceView[+A, +Coll] extends SequenceViewLike[A, Coll, SequenceView[A, Coll]] - -object SequenceView { - type Coll = TraversableView[_, C] forSome {type C <: Traversable[_]} - implicit def builderFactory[A]: BuilderFactory[A, SequenceView[A, Sequence[_]], Coll] = new BuilderFactory[A, SequenceView[A, Sequence[_]], Coll] { def apply(from: Coll) = new NoBuilder } -} - diff --git a/src/library/scala/collection/SequenceViewLike.scala b/src/library/scala/collection/SequenceViewLike.scala deleted file mode 100644 index c57985e044..0000000000 --- a/src/library/scala/collection/SequenceViewLike.scala +++ /dev/null @@ -1,156 +0,0 @@ -/* __ *\ -** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003-2009, LAMP/EPFL ** -** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** -** /____/\___/_/ |_/____/_/ | | ** -** |/ ** -\* */ - -// $Id$ - - -package scala.collection - -import generic._ -import Sequence.fill -import TraversableView.NoBuilder - -/** A non-strict projection of an iterable. - * @author Sean McDirmid - * @author Martin Odersky - * @version 2.8 - */ -trait SequenceViewLike[+A, - +Coll, - +This <: SequenceView[A, Coll] with SequenceViewLike[A, Coll, This]] - extends Sequence[A] with SequenceLike[A, This] with IterableView[A, Coll] with IterableViewLike[A, Coll, This] -{ self => - - trait Transformed[+B] extends SequenceView[B, Coll] with super.Transformed[B] { - override def length: Int - override def apply(idx: Int): B - } - - trait Sliced extends Transformed[A] with super.Sliced { - override def length = ((until min self.length) - from) max 0 - override def apply(idx: Int): A = - if (idx + from < until) self.apply(idx + from) - else throw new IndexOutOfBoundsException(idx.toString) - } - - trait Mapped[B] extends Transformed[B] with super.Mapped[B] { - override def length = self.length - override def apply(idx: Int): B = mapping(self apply idx) - } - - trait FlatMapped[B] extends Transformed[B] with super.FlatMapped[B] { - protected[this] lazy val index = { - val index = new Array[Int](self.length + 1) - index(0) = 0 - for (i <- 0 until self.length) - index(i + 1) = index(i) + mapping(self(i)).size - index - } - protected[this] def findRow(idx: Int, lo: Int, hi: Int): Int = { - val mid = (lo + hi) / 2 - if (idx < index(mid)) findRow(idx, lo, mid - 1) - else if (idx >= index(mid + 1)) findRow(idx, mid + 1, hi) - else mid - } - override def length = index(self.length) - override def apply(idx: Int) = { - val row = findRow(idx, 0, self.length - 1) - mapping(self(row)).toSequence(idx - index(row)) - } - } - - trait Appended[B >: A] extends Transformed[B] with super.Appended[B] { - lazy val restSeq = rest.toSequence - override def length = self.length + restSeq.length - override def apply(idx: Int) = - if (idx < self.length) self(idx) else restSeq(idx - self.length) - } - - trait Filtered extends Transformed[A] with super.Filtered { - protected[this] lazy val index = { - var len = 0 - val arr = new Array[Int](self.length) - for (i <- 0 until self.length) - if (pred(self(i))) { - arr(len) = i - len += 1 - } - arr take len - } - override def length = index.length - override def apply(idx: Int) = self(index(idx)) - } - - trait TakenWhile extends Transformed[A] with super.TakenWhile { - protected[this] lazy val len = self prefixLength pred - override def length = len - override def apply(idx: Int) = - if (idx < len) self(idx) - else throw new IndexOutOfBoundsException(idx.toString) - } - - trait DroppedWhile extends Transformed[A] with super.DroppedWhile { - protected[this] lazy val start = self prefixLength pred - override def length = self.length - start - override def apply(idx: Int) = - if (idx >= 0) self(idx + start) - else throw new IndexOutOfBoundsException(idx.toString) - } - - trait Reversed extends Transformed[A] { - override def iterator: Iterator[A] = self.reverseIterator - override def length: Int = self.length - override def apply(idx: Int): A = self.apply(length - 1 - idx) - override def stringPrefix = self.stringPrefix+"R" - } - - trait Patched[B >: A] extends Transformed[B] { - protected[this] val from: Int - protected[this] val patch: Sequence[B] - protected[this] val replaced: Int - private lazy val plen = patch.length - override def iterator: Iterator[B] = self.iterator patch (from, patch.iterator, replaced) - override def length: Int = self.length + plen - replaced - override def apply(idx: Int): B = - if (idx < from) self.apply(idx) - else if (idx < from + plen) patch.apply(idx - from) - else self.apply(idx - plen + replaced) - override def stringPrefix = self.stringPrefix+"P" - } - - /** Boilerplate method, to override in each subclass - * This method could be eliminated if Scala had virtual classes - */ - protected override def newAppended[B >: A](that: Traversable[B]): Transformed[B] = new Appended[B] { val rest = that } - protected override def newMapped[B](f: A => B): Transformed[B] = new Mapped[B] { val mapping = f } - protected override def newFlatMapped[B](f: A => Traversable[B]): Transformed[B] = new FlatMapped[B] { val mapping = f } - protected override def newFiltered(p: A => Boolean): Transformed[A] = new Filtered { val pred = p } - protected override def newSliced(_from: Int, _until: Int): Transformed[A] = new Sliced { val from = _from; val until = _until } - protected override def newDroppedWhile(p: A => Boolean): Transformed[A] = new DroppedWhile { val pred = p } - protected override def newTakenWhile(p: A => Boolean): Transformed[A] = new TakenWhile { val pred = p } - protected def newReversed: Transformed[A] = new Reversed { } - protected def newPatched[B >: A](_from: Int, _patch: Sequence[B], _replaced: Int): Transformed[B] = new Patched[B] { - val from = _from; val patch = _patch; val replaced = _replaced - } - - override def reverse: This = newReversed.asInstanceOf[This] - - override def patch[B >: A, That](from: Int, patch: Sequence[B], replaced: Int)(implicit bf: BuilderFactory[B, That, This]): That = { - newPatched(from, patch, replaced).asInstanceOf[That] -// was: val b = bf(repr) -// if (b.isInstanceOf[NoBuilder[_]]) newPatched(from, patch, replaced).asInstanceOf[That] -// else super.patch[B, That](from, patch, replaced)(bf) - } - - override def padTo[B >: A, That](len: Int, elem: B)(implicit bf: BuilderFactory[B, That, This]): That = - patch(length, fill(len - length)(elem), 0) - - override def stringPrefix = "SequenceView" -} - - diff --git a/src/library/scala/collection/Traversable.scala b/src/library/scala/collection/Traversable.scala index 432acfac62..a5d4c6b18a 100644 --- a/src/library/scala/collection/Traversable.scala +++ b/src/library/scala/collection/Traversable.scala @@ -79,7 +79,7 @@ trait Traversable[+A] extends TraversableLike[A, Traversable[A]] override def toArray[B >: A : ClassManifest]: Array[B] override def toList: List[A] override def toIterable: Iterable[A] - override def toSequence: Sequence[A] + override def toSeq: Seq[A] override def toStream: Stream[A] // override def sortWith(lt : (A,A) => Boolean): Traversable[A] override def mkString(start: String, sep: String, end: String): String diff --git a/src/library/scala/collection/TraversableLike.scala b/src/library/scala/collection/TraversableLike.scala index b87dff042d..9f4bdbac7f 100644 --- a/src/library/scala/collection/TraversableLike.scala +++ b/src/library/scala/collection/TraversableLike.scala @@ -718,7 +718,7 @@ self => /** Returns a sequence with all elements in this traversable object. * @note Will not terminate for infinite-sized collections. */ - def toSequence: Sequence[A] = toList + def toSeq: Seq[A] = toList /** Returns a vector with all elements in this traversable object. * @note Will not terminate for infinite-sized collections. diff --git a/src/library/scala/collection/TraversableProxyLike.scala b/src/library/scala/collection/TraversableProxyLike.scala index ee50105bae..34ebc667c6 100644 --- a/src/library/scala/collection/TraversableProxyLike.scala +++ b/src/library/scala/collection/TraversableProxyLike.scala @@ -72,7 +72,7 @@ trait TraversableProxyLike[+A, +This <: TraversableLike[A, This] with Traversabl override def toArray[B >: A: ClassManifest]: Array[B] = self.toArray override def toList: List[A] = self.toList override def toIterable: Iterable[A] = self.toIterable - override def toSequence: Sequence[A] = self.toSequence + override def toSeq: Seq[A] = self.toSeq override def toStream: Stream[A] = self.toStream override def toSet[B >: A]: immutable.Set[B] = self.toSet override def mkString(start: String, sep: String, end: String): String = self.mkString(start, sep, end) diff --git a/src/library/scala/collection/Vector.scala b/src/library/scala/collection/Vector.scala index 93ca11427c..cf802ca095 100644 --- a/src/library/scala/collection/Vector.scala +++ b/src/library/scala/collection/Vector.scala @@ -27,13 +27,13 @@ import mutable.{Builder, ArrayBuffer} * @version 2.8 * @since 2.8 */ -trait Vector[+A] extends Sequence[A] +trait Vector[+A] extends Seq[A] with GenericTraversableTemplate[A, Vector] with VectorLike[A, Vector[A]] { override def companion: GenericCompanion[Vector] = Vector } -object Vector extends SequenceFactory[Vector] { +object Vector extends SeqFactory[Vector] { implicit def builderFactory[A]: BuilderFactory[A, Vector[A], Coll] = new VirtualBuilderFactory[A] def newBuilder[A]: Builder[A, Vector[A]] = mutable.Vector.newBuilder[A] } diff --git a/src/library/scala/collection/VectorLike.scala b/src/library/scala/collection/VectorLike.scala index 901d99972e..d4b2ca1d4c 100644 --- a/src/library/scala/collection/VectorLike.scala +++ b/src/library/scala/collection/VectorLike.scala @@ -15,7 +15,7 @@ import generic._ import mutable.ArrayBuffer /** Sequences that support O(1) element access and O(1) length computation. - * This class does not add any methods to Sequence but overrides several + * This class does not add any methods to Seq but overrides several * methods with optimized implementations. * * @author Sean McDirmid @@ -23,7 +23,7 @@ import mutable.ArrayBuffer * @version 2.8 * @since 2.8 */ -trait VectorLike[+A, +Repr] extends SequenceLike[A, Repr] { self => +trait VectorLike[+A, +Repr] extends SeqLike[A, Repr] { self => override protected[this] def thisCollection: Vector[A] = this.asInstanceOf[Vector[A]] override protected[this] def toCollection(repr: Repr): Vector[A] = repr.asInstanceOf[Vector[A]] @@ -177,7 +177,7 @@ trait VectorLike[+A, +Repr] extends SequenceLike[A, Repr] { self => } - // Overridden methods from Sequence + // Overridden methods from Seq override def lengthCompare(len: Int): Int = length - len @@ -223,7 +223,7 @@ trait VectorLike[+A, +Repr] extends SequenceLike[A, Repr] { self => } else Iterator.empty.next } - override def startsWith[B](that: Sequence[B], offset: Int): Boolean = that match { + override def startsWith[B](that: Seq[B], offset: Int): Boolean = that match { case that: Vector[_] => var i = offset var j = 0 @@ -247,7 +247,7 @@ trait VectorLike[+A, +Repr] extends SequenceLike[A, Repr] { self => !thatElems.hasNext } - override def endsWith[B](that: Sequence[B]): Boolean = that match { + override def endsWith[B](that: Seq[B]): Boolean = that match { case that: Vector[_] => var i = length - 1 var j = that.length - 1 diff --git a/src/library/scala/collection/VectorViewLike.scala b/src/library/scala/collection/VectorViewLike.scala index 3a1527f13a..dfaa0f4752 100644 --- a/src/library/scala/collection/VectorViewLike.scala +++ b/src/library/scala/collection/VectorViewLike.scala @@ -6,7 +6,7 @@ ** |/ ** \* */ -// $Id: Sequence.scala 16092 2008-09-12 10:37:06Z nielsen $ +// $Id: Seq.scala 16092 2008-09-12 10:37:06Z nielsen $ package scala.collection @@ -24,7 +24,7 @@ import TraversableView.NoBuilder trait VectorViewLike[+A, +Coll, +This <: VectorView[A, Coll] with VectorViewLike[A, Coll, This]] - extends Vector[A] with VectorLike[A, This] with SequenceView[A, Coll] with SequenceViewLike[A, Coll, This] + extends Vector[A] with VectorLike[A, This] with SeqView[A, Coll] with SeqViewLike[A, Coll, This] { self => trait Transformed[+B] extends VectorView[B, Coll] with super.Transformed[B] @@ -100,7 +100,7 @@ trait VectorViewLike[+A, protected override def newDroppedWhile(p: A => Boolean): Transformed[A] = new DroppedWhile { val pred = p } protected override def newTakenWhile(p: A => Boolean): Transformed[A] = new TakenWhile { val pred = p } protected override def newReversed: Transformed[A] = new Reversed { } - protected override def newPatched[B >: A](_from: Int, _patch: Sequence[B], _replaced: Int): Transformed[B] = new Patched[B] { + protected override def newPatched[B >: A](_from: Int, _patch: Seq[B], _replaced: Int): Transformed[B] = new Patched[B] { val from = _from; val patch = _patch; val replaced = _replaced } protected override def newZipped[B](that: Iterable[B]): Transformed[(A, B)] = new Zipped[B] { diff --git a/src/library/scala/collection/generic/IterableForwarder.scala b/src/library/scala/collection/generic/IterableForwarder.scala index 09903ae26a..600104938d 100644 --- a/src/library/scala/collection/generic/IterableForwarder.scala +++ b/src/library/scala/collection/generic/IterableForwarder.scala @@ -39,7 +39,7 @@ trait IterableForwarder[+A] extends Iterable[A] with TraversableForwarder[A] { protected def underlying: Iterable[A] // Iterable delegates - // Iterable methods could be printed by cat IterableTemplate.scala | sed -n '/trait Iterable/,$ p' | egrep '^ (override )?def' + // Iterable methods could be printed by cat IterableLike.scala | sed -n '/trait Iterable/,$ p' | egrep '^ (override )?def' override def iterator = underlying.iterator override def sameElements[B >: A](that: Iterable[B]): Boolean = underlying.sameElements(that) diff --git a/src/library/scala/collection/generic/SeqFactory.scala b/src/library/scala/collection/generic/SeqFactory.scala new file mode 100644 index 0000000000..fabffa0151 --- /dev/null +++ b/src/library/scala/collection/generic/SeqFactory.scala @@ -0,0 +1,27 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2009, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + + +package scala.collection +package generic + +/** A template for companion objects of Seq and subclasses thereof. + * + * @since 2.8 + */ +abstract class SeqFactory[CC[X] <: Seq[X] with GenericTraversableTemplate[X, CC]] extends TraversableFactory[CC] { + + /** This method is called in a pattern match { case Seq(...) => }. + * + * @param x the selector value + * @return sequence wrapped in an option, if this is a Seq, otherwise none + */ + def unapplySeq[A](x: CC[A]): Some[CC[A]] = Some(x) +} diff --git a/src/library/scala/collection/generic/SeqForwarder.scala b/src/library/scala/collection/generic/SeqForwarder.scala new file mode 100644 index 0000000000..0472b901a5 --- /dev/null +++ b/src/library/scala/collection/generic/SeqForwarder.scala @@ -0,0 +1,52 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2009, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + + +package scala.collection.generic +import scala.collection._ + +/** This class implements a forwarder for sequences. It forwards + * all calls to a different sequence object except for + * + * - toString, hashCode, equals, stringPrefix + * - newBuilder, view, toSeq + * - all calls creating a new sequence of the same kind + * + * The above methods are forwarded by subclass SeqProxy + * + * @author Martin Odersky + * @version 2.8 + * @since 2.8 + */ +trait SeqForwarder[+A] extends Seq[A] with IterableForwarder[A] { + + protected override def underlying: Seq[A] + + // PartialFunction delegates + + override def apply(i: Int): A = underlying.apply(i) + override def isDefinedAt(x: Int): Boolean = underlying.isDefinedAt(x) + + // Seq delegates + // Seq methods could be printed by cat SeqLike.scala | sed -n '/trait Seq/,$ p' | egrep '^ (override )?def' + + override def length: Int = underlying.length + override def lengthCompare(l: Int) = underlying lengthCompare l + override def segmentLength(p: A => Boolean, from: Int): Int = underlying.segmentLength(p, from) + override def prefixLength(p: A => Boolean) = underlying.prefixLength(p) + override def indexWhere(p: A => Boolean, from: Int): Int = underlying.indexWhere(p, from) + override def indexOf[B >: A](elem: B, from: Int): Int = underlying.indexOf(elem, from) + override def reverseIterator: Iterator[A] = underlying.reverseIterator + override def startsWith[B](that: Seq[B], offset: Int): Boolean = underlying.startsWith(that, offset) + override def endsWith[B](that: Seq[B]): Boolean = underlying.endsWith(that) + override def indexOfSeq[B >: A](that: Seq[B]): Int = underlying.indexOfSeq(that) + override def contains(elem: Any): Boolean = underlying.contains(elem) + override def indices: Range = underlying.indices +} diff --git a/src/library/scala/collection/generic/SequenceFactory.scala b/src/library/scala/collection/generic/SequenceFactory.scala deleted file mode 100644 index 3c2d36b1fd..0000000000 --- a/src/library/scala/collection/generic/SequenceFactory.scala +++ /dev/null @@ -1,27 +0,0 @@ -/* __ *\ -** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003-2009, LAMP/EPFL ** -** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** -** /____/\___/_/ |_/____/_/ | | ** -** |/ ** -\* */ - -// $Id$ - - -package scala.collection -package generic - -/** A template for companion objects of Sequence and subclasses thereof. - * - * @since 2.8 - */ -abstract class SequenceFactory[CC[X] <: Sequence[X] with GenericTraversableTemplate[X, CC]] extends TraversableFactory[CC] { - - /** This method is called in a pattern match { case Sequence(...) => }. - * - * @param x the selector value - * @return sequence wrapped in an option, if this is a Sequence, otherwise none - */ - def unapplySeq[A](x: CC[A]): Some[CC[A]] = Some(x) -} diff --git a/src/library/scala/collection/generic/SequenceForwarder.scala b/src/library/scala/collection/generic/SequenceForwarder.scala deleted file mode 100644 index f21a02605c..0000000000 --- a/src/library/scala/collection/generic/SequenceForwarder.scala +++ /dev/null @@ -1,52 +0,0 @@ -/* __ *\ -** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003-2009, LAMP/EPFL ** -** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** -** /____/\___/_/ |_/____/_/ | | ** -** |/ ** -\* */ - -// $Id$ - - -package scala.collection.generic -import scala.collection._ - -/** This class implements a forwarder for sequences. It forwards - * all calls to a different sequence object except for - * - * - toString, hashCode, equals, stringPrefix - * - newBuilder, view, toSequence - * - all calls creating a new sequence of the same kind - * - * The above methods are forwarded by subclass SequenceProxy - * - * @author Martin Odersky - * @version 2.8 - * @since 2.8 - */ -trait SequenceForwarder[+A] extends Sequence[A] with IterableForwarder[A] { - - protected override def underlying: Sequence[A] - - // PartialFunction delegates - - override def apply(i: Int): A = underlying.apply(i) - override def isDefinedAt(x: Int): Boolean = underlying.isDefinedAt(x) - - // Sequence delegates - // Sequence methods could be printed by cat SequenceTemplate.scala | sed -n '/trait Seq/,$ p' | egrep '^ (override )?def' - - override def length: Int = underlying.length - override def lengthCompare(l: Int) = underlying lengthCompare l - override def segmentLength(p: A => Boolean, from: Int): Int = underlying.segmentLength(p, from) - override def prefixLength(p: A => Boolean) = underlying.prefixLength(p) - override def indexWhere(p: A => Boolean, from: Int): Int = underlying.indexWhere(p, from) - override def indexOf[B >: A](elem: B, from: Int): Int = underlying.indexOf(elem, from) - override def reverseIterator: Iterator[A] = underlying.reverseIterator - override def startsWith[B](that: Sequence[B], offset: Int): Boolean = underlying.startsWith(that, offset) - override def endsWith[B](that: Sequence[B]): Boolean = underlying.endsWith(that) - override def indexOfSeq[B >: A](that: Sequence[B]): Int = underlying.indexOfSeq(that) - override def contains(elem: Any): Boolean = underlying.contains(elem) - override def indices: Range = underlying.indices -} diff --git a/src/library/scala/collection/generic/TraversableForwarder.scala b/src/library/scala/collection/generic/TraversableForwarder.scala index 50652e9343..5e24f2b367 100644 --- a/src/library/scala/collection/generic/TraversableForwarder.scala +++ b/src/library/scala/collection/generic/TraversableForwarder.scala @@ -42,7 +42,7 @@ trait TraversableForwarder[+A] extends Traversable[A] { protected def underlying: Traversable[A] // Iterable delegates - // Iterable methods could be printed by cat IterableTemplate.scala | sed -n '/trait Iterable/,$ p' | egrep '^ (override )?def' + // Iterable methods could be printed by cat TarversableLike.scala | sed -n '/trait Iterable/,$ p' | egrep '^ (override )?def' override def isEmpty = underlying.isEmpty override def nonEmpty = underlying.nonEmpty @@ -62,7 +62,7 @@ trait TraversableForwarder[+A] extends Traversable[A] { override def copyToArray[B >: A](xs: Array[B], start: Int, len: Int) = underlying.copyToArray(xs, start, len) override def toArray[B >: A : ClassManifest]: Array[B] = underlying.toArray override def toList: List[A] = underlying.toList - override def toSequence: Sequence[A] = underlying.toSequence + override def toSeq: Seq[A] = underlying.toSeq override def toStream: Stream[A] = underlying.toStream override def mkString(start: String, sep: String, end: String): String = underlying.mkString(start, sep, end) override def addString(b: StringBuilder, start: String, sep: String, end: String): StringBuilder = underlying.addString(b, start, sep, end) diff --git a/src/library/scala/collection/immutable/LinearSeq.scala b/src/library/scala/collection/immutable/LinearSeq.scala new file mode 100644 index 0000000000..09ef071ca4 --- /dev/null +++ b/src/library/scala/collection/immutable/LinearSeq.scala @@ -0,0 +1,36 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2009, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + + +package scala.collection +package immutable + +import generic._ +import mutable.Builder + +/** A subtrait of collection.Seq which represents sequences + * that cannot be mutated. + * + * @since 2.8 + */ +trait LinearSeq[+A] extends Seq[A] + with scala.collection.LinearSeq[A] + with GenericTraversableTemplate[A, LinearSeq] + with LinearSeqLike[A, LinearSeq[A]] { + override def companion: GenericCompanion[LinearSeq] = LinearSeq +} + +/** + * @since 2.8 + */ +object LinearSeq extends SeqFactory[LinearSeq] { + implicit def builderFactory[A]: BuilderFactory[A, LinearSeq[A], Coll] = new VirtualBuilderFactory[A] + def newBuilder[A]: Builder[A, LinearSeq[A]] = new mutable.ListBuffer +} diff --git a/src/library/scala/collection/immutable/LinearSequence.scala b/src/library/scala/collection/immutable/LinearSequence.scala deleted file mode 100644 index acfe7e826e..0000000000 --- a/src/library/scala/collection/immutable/LinearSequence.scala +++ /dev/null @@ -1,36 +0,0 @@ -/* __ *\ -** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003-2009, LAMP/EPFL ** -** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** -** /____/\___/_/ |_/____/_/ | | ** -** |/ ** -\* */ - -// $Id$ - - -package scala.collection -package immutable - -import generic._ -import mutable.Builder - -/** A subtrait of collection.Sequence which represents sequences - * that cannot be mutated. - * - * @since 2.8 - */ -trait LinearSequence[+A] extends Sequence[A] - with scala.collection.LinearSequence[A] - with GenericTraversableTemplate[A, LinearSequence] - with LinearSequenceLike[A, LinearSequence[A]] { - override def companion: GenericCompanion[LinearSequence] = LinearSequence -} - -/** - * @since 2.8 - */ -object LinearSequence extends SequenceFactory[LinearSequence] { - implicit def builderFactory[A]: BuilderFactory[A, LinearSequence[A], Coll] = new VirtualBuilderFactory[A] - def newBuilder[A]: Builder[A, LinearSequence[A]] = new mutable.ListBuffer -} diff --git a/src/library/scala/collection/immutable/List.scala b/src/library/scala/collection/immutable/List.scala index cea5753e18..b7a2908536 100644 --- a/src/library/scala/collection/immutable/List.scala +++ b/src/library/scala/collection/immutable/List.scala @@ -26,13 +26,13 @@ import annotation.tailrec * @version 2.8 * @since 2.8 */ -sealed abstract class List[+A] extends LinearSequence[A] +sealed abstract class List[+A] extends LinearSeq[A] with Product with GenericTraversableTemplate[A, List] - with LinearSequenceLike[A, List[A]] { + with LinearSeqLike[A, List[A]] { override def companion: GenericCompanion[List] = List - import scala.collection.{Iterable, Traversable, Sequence, Vector} + import scala.collection.{Iterable, Traversable, Seq, Vector} /** Returns true if the list does not contain any elements. * @return true, iff the list is empty. @@ -450,7 +450,7 @@ case object Nil extends List[Nothing] { throw new NoSuchElementException("tail of empty list") // Removal of equals method here might lead to an infinite recusion similar to IntMap.equals. override def equals(that: Any) = that match { - case that1: Sequence[_] => that1.isEmpty + case that1: Seq[_] => that1.isEmpty case _ => false } } @@ -498,9 +498,9 @@ final case class ::[B](private var hd: B, private[scala] var tl: List[B]) extend * @version 2.8 * @since 2.8 */ -object List extends SequenceFactory[List] { +object List extends SeqFactory[List] { - import scala.collection.{Iterable, Sequence, Vector} + import scala.collection.{Iterable, Seq, Vector} implicit def builderFactory[A]: BuilderFactory[A, List[A], Coll] = new VirtualBuilderFactory[A] def newBuilder[A]: Builder[A, List[A]] = new ListBuffer[A] diff --git a/src/library/scala/collection/immutable/Queue.scala b/src/library/scala/collection/immutable/Queue.scala index 80c7285be8..2d5b05778b 100644 --- a/src/library/scala/collection/immutable/Queue.scala +++ b/src/library/scala/collection/immutable/Queue.scala @@ -30,7 +30,7 @@ object Queue { @SerialVersionUID(-7622936493364270175L) class Queue[+A] protected( protected val in: List[A], - protected val out: List[A]) extends Sequence[A] + protected val out: List[A]) extends Seq[A] { /** Returns the n-th element of this queue. * The first element is at position 0. diff --git a/src/library/scala/collection/immutable/Seq.scala b/src/library/scala/collection/immutable/Seq.scala new file mode 100644 index 0000000000..6091971ffd --- /dev/null +++ b/src/library/scala/collection/immutable/Seq.scala @@ -0,0 +1,36 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2009, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + + +package scala.collection +package immutable + +import generic._ +import mutable.Builder + +/** A subtrait of collection.Seq which represents sequences + * that cannot be mutated. + * + * @since 2.8 + */ +trait Seq[+A] extends Iterable[A] + with scala.collection.Seq[A] + with GenericTraversableTemplate[A, Seq] + with SeqLike[A, Seq[A]] { + override def companion: GenericCompanion[Seq] = Seq +} + +/** + * @since 2.8 + */ +object Seq extends SeqFactory[Seq] { + implicit def builderFactory[A]: BuilderFactory[A, Seq[A], Coll] = new VirtualBuilderFactory[A] + def newBuilder[A]: Builder[A, Seq[A]] = new mutable.ListBuffer +} diff --git a/src/library/scala/collection/immutable/Sequence.scala b/src/library/scala/collection/immutable/Sequence.scala deleted file mode 100644 index 7127b3fc9d..0000000000 --- a/src/library/scala/collection/immutable/Sequence.scala +++ /dev/null @@ -1,36 +0,0 @@ -/* __ *\ -** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003-2009, LAMP/EPFL ** -** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** -** /____/\___/_/ |_/____/_/ | | ** -** |/ ** -\* */ - -// $Id$ - - -package scala.collection -package immutable - -import generic._ -import mutable.Builder - -/** A subtrait of collection.Sequence which represents sequences - * that cannot be mutated. - * - * @since 2.8 - */ -trait Sequence[+A] extends Iterable[A] - with scala.collection.Sequence[A] - with GenericTraversableTemplate[A, Sequence] - with SequenceLike[A, Sequence[A]] { - override def companion: GenericCompanion[Sequence] = Sequence -} - -/** - * @since 2.8 - */ -object Sequence extends SequenceFactory[Sequence] { - implicit def builderFactory[A]: BuilderFactory[A, Sequence[A], Coll] = new VirtualBuilderFactory[A] - def newBuilder[A]: Builder[A, Sequence[A]] = new mutable.ListBuffer -} diff --git a/src/library/scala/collection/immutable/Stack.scala b/src/library/scala/collection/immutable/Stack.scala index a7b3e8a614..2813bc6656 100644 --- a/src/library/scala/collection/immutable/Stack.scala +++ b/src/library/scala/collection/immutable/Stack.scala @@ -33,7 +33,7 @@ object Stack { * @since 1 */ @serializable @SerialVersionUID(1976480595012942526L) -class Stack[+A] protected (protected val elems: List[A]) extends Sequence[A] { +class Stack[+A] protected (protected val elems: List[A]) extends Seq[A] { def this() = this(Nil) diff --git a/src/library/scala/collection/immutable/Stream.scala b/src/library/scala/collection/immutable/Stream.scala index 01530294e0..bcf8076a1c 100644 --- a/src/library/scala/collection/immutable/Stream.scala +++ b/src/library/scala/collection/immutable/Stream.scala @@ -38,13 +38,13 @@ import scala.annotation.tailrec * @version 1.1 08/08/03 * @since 2.8 */ -abstract class Stream[+A] extends LinearSequence[A] +abstract class Stream[+A] extends LinearSeq[A] with GenericTraversableTemplate[A, Stream] - with LinearSequenceLike[A, Stream[A]] { + with LinearSeqLike[A, Stream[A]] { self => override def companion: GenericCompanion[Stream] = Stream - import scala.collection.{Traversable, Iterable, Sequence, Vector} + import scala.collection.{Traversable, Iterable, Seq, Vector} /** is this stream empty? */ def isEmpty: Boolean @@ -317,7 +317,7 @@ self => these } - // there's nothing we can do about dropRight, so we just keep the definition in LinearSequence + // there's nothing we can do about dropRight, so we just keep the definition in LinearSeq /** Returns the longest prefix of this stream whose elements satisfy * the predicate p. @@ -398,7 +398,7 @@ self => * @version 1.1 08/08/03 * @since 2.8 */ -object Stream extends SequenceFactory[Stream] { +object Stream extends SeqFactory[Stream] { /** The factory for streams. * @note Methods such as map/flatMap will not invoke the Builder factory, @@ -415,7 +415,7 @@ object Stream extends SequenceFactory[Stream] { /** Creates a new builder for a stream */ def newBuilder[A]: Builder[A, Stream[A]] = new StreamBuilder[A] - import scala.collection.{Iterable, Sequence, Vector} + import scala.collection.{Iterable, Seq, Vector} /** A builder for streams * @note: This builder is lazy only in the sense that it does not go downs the spine diff --git a/src/library/scala/collection/immutable/StringLike.scala b/src/library/scala/collection/immutable/StringLike.scala index 3d4a0b8fb9..c1aa03a16c 100644 --- a/src/library/scala/collection/immutable/StringLike.scala +++ b/src/library/scala/collection/immutable/StringLike.scala @@ -235,7 +235,7 @@ self => * @throws java.lang.IllegalArgumentException */ def format(args : Any*) : String = - java.lang.String.format(toString, args.asInstanceOf[Seq[AnyRef]]: _*) + java.lang.String.format(toString, args.asInstanceOf[scala.collection.Seq[AnyRef]]: _*) /**

* Like format(args*) but takes an initial Locale parameter @@ -252,6 +252,6 @@ self => * @throws java.lang.IllegalArgumentException */ def format(l: java.util.Locale, args: Any*): String = - java.lang.String.format(l, toString, args.asInstanceOf[Seq[AnyRef]]: _*) + java.lang.String.format(l, toString, args.asInstanceOf[scala.collection.Seq[AnyRef]]: _*) } diff --git a/src/library/scala/collection/immutable/Vector.scala b/src/library/scala/collection/immutable/Vector.scala index 3bc8955194..7e76549f7e 100644 --- a/src/library/scala/collection/immutable/Vector.scala +++ b/src/library/scala/collection/immutable/Vector.scala @@ -20,7 +20,7 @@ import mutable.{Builder, ArrayBuffer} * * @since 2.8 */ -trait Vector[+A] extends Sequence[A] +trait Vector[+A] extends Seq[A] with scala.collection.Vector[A] with GenericTraversableTemplate[A, Vector] with VectorLike[A, Vector[A]] { @@ -30,7 +30,7 @@ trait Vector[+A] extends Sequence[A] /** * @since 2.8 */ -object Vector extends SequenceFactory[Vector] { +object Vector extends SeqFactory[Vector] { // todo: insert better vector implementation here @serializable @SerialVersionUID(7129304555082767876L) class Impl[A](buf: ArrayBuffer[A]) extends Vector[A] { diff --git a/src/library/scala/collection/interfaces/SeqMethods.scala b/src/library/scala/collection/interfaces/SeqMethods.scala new file mode 100644 index 0000000000..a595d101fa --- /dev/null +++ b/src/library/scala/collection/interfaces/SeqMethods.scala @@ -0,0 +1,58 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2009, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +package scala.collection +package interfaces + +import generic._ +import mutable.Buffer +import scala.reflect.ClassManifest + +/** + * @since 2.8 + */ +trait SeqMethods[+A, +This <: SeqLike[A, This] with Seq[A]] extends IterableMethods[A, This] +{ + // abstract + def apply(idx: Int): A + def length: Int + + def contains(elem: Any): Boolean + def diff[B >: A, That](that: Seq[B]): This + def endsWith[B](that: Seq[B]): Boolean + def indexOfSeq[B >: A](that: Seq[B]): Int + def indexOfSeq[B >: A](that: Seq[B], fromIndex: Int): Int + def indexOf[B >: A](elem: B): Int + def indexOf[B >: A](elem: B, from: Int): Int + def indexWhere(p: A => Boolean): Int + def indexWhere(p: A => Boolean, from: Int): Int + def indices: Range + def intersect[B >: A, That](that: Seq[B]): This + def isDefinedAt(x: Int): Boolean + def lastIndexOfSeq[B >: A](that: Seq[B]): Int + def lastIndexOfSeq[B >: A](that: Seq[B], fromIndex: Int): Int + def lastIndexOf[B >: A](elem: B): Int + def lastIndexOf[B >: A](elem: B, end: Int): Int + def lastIndexWhere(p: A => Boolean): Int + def lastIndexWhere(p: A => Boolean, end: Int): Int + def lengthCompare(len: Int): Int + def padTo[B >: A, That](len: Int, elem: B)(implicit bf: BuilderFactory[B, That, This]): That + def patch[B >: A, That](from: Int, patch: Seq[B], replaced: Int)(implicit bf: BuilderFactory[B, That, This]): That + def prefixLength(p: A => Boolean): Int + def removeDuplicates: This + def reverse: This + def reverseIterator: Iterator[A] + def segmentLength(p: A => Boolean, from: Int): Int + def slice(from: Int): Seq[A] + def startsWith[B](that: Seq[B]): Boolean + def startsWith[B](that: Seq[B], offset: Int): Boolean + def union[B >: A, That](that: Seq[B])(implicit bf: BuilderFactory[B, That, This]): That + + override def view: SeqView[A, This] + override def view(from: Int, until: Int): SeqView[A, This] +} diff --git a/src/library/scala/collection/interfaces/SequenceMethods.scala b/src/library/scala/collection/interfaces/SequenceMethods.scala deleted file mode 100644 index ee91d33648..0000000000 --- a/src/library/scala/collection/interfaces/SequenceMethods.scala +++ /dev/null @@ -1,58 +0,0 @@ -/* __ *\ -** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003-2009, LAMP/EPFL ** -** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** -** /____/\___/_/ |_/____/_/ | | ** -** |/ ** -\* */ - -package scala.collection -package interfaces - -import generic._ -import mutable.Buffer -import scala.reflect.ClassManifest - -/** - * @since 2.8 - */ -trait SequenceMethods[+A, +This <: SequenceLike[A, This] with Sequence[A]] extends IterableMethods[A, This] -{ - // abstract - def apply(idx: Int): A - def length: Int - - def contains(elem: Any): Boolean - def diff[B >: A, That](that: Sequence[B]): This - def endsWith[B](that: Sequence[B]): Boolean - def indexOfSeq[B >: A](that: Sequence[B]): Int - def indexOfSeq[B >: A](that: Sequence[B], fromIndex: Int): Int - def indexOf[B >: A](elem: B): Int - def indexOf[B >: A](elem: B, from: Int): Int - def indexWhere(p: A => Boolean): Int - def indexWhere(p: A => Boolean, from: Int): Int - def indices: Range - def intersect[B >: A, That](that: Sequence[B]): This - def isDefinedAt(x: Int): Boolean - def lastIndexOfSeq[B >: A](that: Sequence[B]): Int - def lastIndexOfSeq[B >: A](that: Sequence[B], fromIndex: Int): Int - def lastIndexOf[B >: A](elem: B): Int - def lastIndexOf[B >: A](elem: B, end: Int): Int - def lastIndexWhere(p: A => Boolean): Int - def lastIndexWhere(p: A => Boolean, end: Int): Int - def lengthCompare(len: Int): Int - def padTo[B >: A, That](len: Int, elem: B)(implicit bf: BuilderFactory[B, That, This]): That - def patch[B >: A, That](from: Int, patch: Sequence[B], replaced: Int)(implicit bf: BuilderFactory[B, That, This]): That - def prefixLength(p: A => Boolean): Int - def removeDuplicates: This - def reverse: This - def reverseIterator: Iterator[A] - def segmentLength(p: A => Boolean, from: Int): Int - def slice(from: Int): Sequence[A] - def startsWith[B](that: Sequence[B]): Boolean - def startsWith[B](that: Sequence[B], offset: Int): Boolean - def union[B >: A, That](that: Sequence[B])(implicit bf: BuilderFactory[B, That, This]): That - - override def view: SequenceView[A, This] - override def view(from: Int, until: Int): SequenceView[A, This] -} diff --git a/src/library/scala/collection/interfaces/TraversableMethods.scala b/src/library/scala/collection/interfaces/TraversableMethods.scala index 4e8cdf6ec7..ffedb10476 100644 --- a/src/library/scala/collection/interfaces/TraversableMethods.scala +++ b/src/library/scala/collection/interfaces/TraversableMethods.scala @@ -37,7 +37,7 @@ trait TraversableMethods[+A, +This <: TraversableLike[A, This] with Traversable[ def toArray[B >: A : ClassManifest]: Array[B] def toIterable: Iterable[A] def toList: List[A] - def toSequence: Sequence[A] + def toSeq: Seq[A] def toSet[B >: A]: immutable.Set[B] def toStream: Stream[A] def toVector[B >: A]: mutable.Vector[B] diff --git a/src/library/scala/collection/mutable/ArrayBuffer.scala b/src/library/scala/collection/mutable/ArrayBuffer.scala index f0a52d9f26..6c2f092505 100644 --- a/src/library/scala/collection/mutable/ArrayBuffer.scala +++ b/src/library/scala/collection/mutable/ArrayBuffer.scala @@ -167,7 +167,7 @@ class ArrayBuffer[A](override protected val initialSize: Int) * @version 2.8 * @since 2.8 */ -object ArrayBuffer extends SequenceFactory[ArrayBuffer] { +object ArrayBuffer extends SeqFactory[ArrayBuffer] { implicit def builderFactory[A]: BuilderFactory[A, ArrayBuffer[A], Coll] = new VirtualBuilderFactory[A] def newBuilder[A]: Builder[A, ArrayBuffer[A]] = new ArrayBuffer[A] diff --git a/src/library/scala/collection/mutable/ArrayStack.scala b/src/library/scala/collection/mutable/ArrayStack.scala index 5e2f5e2b1a..0059d5ff6b 100644 --- a/src/library/scala/collection/mutable/ArrayStack.scala +++ b/src/library/scala/collection/mutable/ArrayStack.scala @@ -37,7 +37,7 @@ private object Utils { */ @cloneable class ArrayStack[T] private(private var table : Array[AnyRef], - private var index : Int) extends scala.collection.Sequence[T] with Cloneable[ArrayStack[T]] { + private var index : Int) extends scala.collection.Seq[T] with Cloneable[ArrayStack[T]] { def this() = this(new Array[AnyRef](1), 0) /** Retrieve n'th element from stack, where top of stack has index 0 */ diff --git a/src/library/scala/collection/mutable/Buffer.scala b/src/library/scala/collection/mutable/Buffer.scala index a11ef77fef..59da5bbd83 100644 --- a/src/library/scala/collection/mutable/Buffer.scala +++ b/src/library/scala/collection/mutable/Buffer.scala @@ -25,7 +25,7 @@ import generic._ * @since 1 */ @cloneable -trait Buffer[A] extends Sequence[A] +trait Buffer[A] extends Seq[A] with GenericTraversableTemplate[A, Buffer] with BufferLike[A, Buffer[A]] { override def companion: GenericCompanion[Buffer] = Buffer @@ -33,7 +33,7 @@ trait Buffer[A] extends Sequence[A] /** Factory object for Buffer trait. */ -object Buffer extends SequenceFactory[Buffer] { +object Buffer extends SeqFactory[Buffer] { implicit def builderFactory[A]: BuilderFactory[A, Buffer[A], Coll] = new VirtualBuilderFactory[A] def newBuilder[A]: Builder[A, Buffer[A]] = new ArrayBuffer } diff --git a/src/library/scala/collection/mutable/BufferLike.scala b/src/library/scala/collection/mutable/BufferLike.scala index 428b5b7aa9..a1506ce6ed 100644 --- a/src/library/scala/collection/mutable/BufferLike.scala +++ b/src/library/scala/collection/mutable/BufferLike.scala @@ -33,7 +33,7 @@ trait BufferLike[A, +This <: BufferLike[A, This] with Buffer[A]] with Addable[A, This] with Subtractable[A, This] with Cloneable[This] - with SequenceLike[A, This] + with SeqLike[A, This] { self => import scala.collection.{Iterable, Traversable} @@ -329,7 +329,7 @@ trait BufferLike[A, +This <: BufferLike[A, This] with Buffer[A]] repr } - def readOnly: scala.collection.Sequence[A] = toSequence + def readOnly: scala.collection.Seq[A] = toSeq } diff --git a/src/library/scala/collection/mutable/DoubleLinkedList.scala b/src/library/scala/collection/mutable/DoubleLinkedList.scala index 3534e4727b..1c5d7211c0 100644 --- a/src/library/scala/collection/mutable/DoubleLinkedList.scala +++ b/src/library/scala/collection/mutable/DoubleLinkedList.scala @@ -23,7 +23,7 @@ import generic._ * @since 1 */ @serializable @SerialVersionUID(419155950203746706L) -class DoubleLinkedList[A](_elem: A, _next: DoubleLinkedList[A]) extends LinearSequence[A] +class DoubleLinkedList[A](_elem: A, _next: DoubleLinkedList[A]) extends LinearSeq[A] with GenericTraversableTemplate[A, DoubleLinkedList] with DoubleLinkedListLike[A, DoubleLinkedList[A]] { elem = _elem @@ -31,7 +31,7 @@ class DoubleLinkedList[A](_elem: A, _next: DoubleLinkedList[A]) extends LinearSe override def companion: GenericCompanion[DoubleLinkedList] = DoubleLinkedList } -object DoubleLinkedList extends SequenceFactory[DoubleLinkedList] { +object DoubleLinkedList extends SeqFactory[DoubleLinkedList] { implicit def builderFactory[A]: BuilderFactory[A, DoubleLinkedList[A], Coll] = //new BuilderFactory[A, DoubleLinkedList[A], Coll] { def apply(from: Coll) = from.traversableBuilder[A] } new VirtualBuilderFactory[A] def newBuilder[A]: Builder[A, DoubleLinkedList[A]] = diff --git a/src/library/scala/collection/mutable/DoubleLinkedListLike.scala b/src/library/scala/collection/mutable/DoubleLinkedListLike.scala index 2297d40209..65b0621b6b 100644 --- a/src/library/scala/collection/mutable/DoubleLinkedListLike.scala +++ b/src/library/scala/collection/mutable/DoubleLinkedListLike.scala @@ -21,7 +21,7 @@ package mutable * @version 1.0, 08/07/2003 * @since 2.8 */ -trait DoubleLinkedListLike[A, This >: Null <: LinearSequence[A] with DoubleLinkedListLike[A, This]] extends LinkedListLike[A, This] { self => +trait DoubleLinkedListLike[A, This >: Null <: LinearSeq[A] with DoubleLinkedListLike[A, This]] extends LinkedListLike[A, This] { self => var prev: This = _ diff --git a/src/library/scala/collection/mutable/GenericArray.scala b/src/library/scala/collection/mutable/GenericArray.scala index 106eedc766..9194d7f39c 100644 --- a/src/library/scala/collection/mutable/GenericArray.scala +++ b/src/library/scala/collection/mutable/GenericArray.scala @@ -54,7 +54,7 @@ extends Vector[A] /** Copy all elements to a buffer * @param The buffer to which elements are copied override def copyToBuffer[B >: A](dest: Buffer[B]) { - dest ++= (array: Sequence[AnyRef]).asInstanceOf[Sequence[B]] + dest ++= (array: Seq[AnyRef]).asInstanceOf[Seq[B]] } */ @@ -67,7 +67,7 @@ extends Vector[A] } } -object GenericArray extends SequenceFactory[GenericArray] { +object GenericArray extends SeqFactory[GenericArray] { implicit def builderFactory[A]: BuilderFactory[A, GenericArray[A], Coll] = new VirtualBuilderFactory[A] def newBuilder[A]: Builder[A, GenericArray[A]] = new ArrayBuffer[A] mapResult { buf => diff --git a/src/library/scala/collection/mutable/LinearSeq.scala b/src/library/scala/collection/mutable/LinearSeq.scala new file mode 100644 index 0000000000..285dc7721d --- /dev/null +++ b/src/library/scala/collection/mutable/LinearSeq.scala @@ -0,0 +1,32 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2009, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + + +package scala.collection +package mutable + +import generic._ + +/** A subtrait of collection.Seq which represents sequences + * that cannot be mutated. + * + * @since 2.8 + */ +trait LinearSeq[A] extends Seq[A] + with scala.collection.LinearSeq[A] + with GenericTraversableTemplate[A, LinearSeq] + with LinearSeqLike[A, LinearSeq[A]] { + override def companion: GenericCompanion[LinearSeq] = LinearSeq +} + +object LinearSeq extends SeqFactory[LinearSeq] { + implicit def builderFactory[A]: BuilderFactory[A, LinearSeq[A], Coll] = new VirtualBuilderFactory[A] + def newBuilder[A]: Builder[A, LinearSeq[A]] = new MutableList[A] +} diff --git a/src/library/scala/collection/mutable/LinearSequence.scala b/src/library/scala/collection/mutable/LinearSequence.scala deleted file mode 100644 index 75da5021c3..0000000000 --- a/src/library/scala/collection/mutable/LinearSequence.scala +++ /dev/null @@ -1,32 +0,0 @@ -/* __ *\ -** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003-2009, LAMP/EPFL ** -** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** -** /____/\___/_/ |_/____/_/ | | ** -** |/ ** -\* */ - -// $Id$ - - -package scala.collection -package mutable - -import generic._ - -/** A subtrait of collection.Sequence which represents sequences - * that cannot be mutated. - * - * @since 2.8 - */ -trait LinearSequence[A] extends Sequence[A] - with scala.collection.LinearSequence[A] - with GenericTraversableTemplate[A, LinearSequence] - with LinearSequenceLike[A, LinearSequence[A]] { - override def companion: GenericCompanion[LinearSequence] = LinearSequence -} - -object LinearSequence extends SequenceFactory[LinearSequence] { - implicit def builderFactory[A]: BuilderFactory[A, LinearSequence[A], Coll] = new VirtualBuilderFactory[A] - def newBuilder[A]: Builder[A, LinearSequence[A]] = new MutableList[A] -} diff --git a/src/library/scala/collection/mutable/LinkedList.scala b/src/library/scala/collection/mutable/LinkedList.scala index cc171c07de..a881902a67 100644 --- a/src/library/scala/collection/mutable/LinkedList.scala +++ b/src/library/scala/collection/mutable/LinkedList.scala @@ -23,7 +23,7 @@ import generic._ * @since 1 */ @serializable -class LinkedList[A](_elem: A, _next: LinkedList[A]) extends LinearSequence[A] +class LinkedList[A](_elem: A, _next: LinkedList[A]) extends LinearSeq[A] with GenericTraversableTemplate[A, LinkedList] with LinkedListLike[A, LinkedList[A]] { elem = _elem @@ -31,7 +31,7 @@ class LinkedList[A](_elem: A, _next: LinkedList[A]) extends LinearSequence[A] override def companion: GenericCompanion[LinkedList] = LinkedList } -object LinkedList extends SequenceFactory[LinkedList] { +object LinkedList extends SeqFactory[LinkedList] { implicit def builderFactory[A]: BuilderFactory[A, LinkedList[A], Coll] = new VirtualBuilderFactory[A] def newBuilder[A]: Builder[A, LinkedList[A]] = diff --git a/src/library/scala/collection/mutable/LinkedListLike.scala b/src/library/scala/collection/mutable/LinkedListLike.scala index 9ce8284772..e26735ed23 100644 --- a/src/library/scala/collection/mutable/LinkedListLike.scala +++ b/src/library/scala/collection/mutable/LinkedListLike.scala @@ -24,7 +24,7 @@ import annotation.tailrec * @version 2.8 * @since 2.8 */ -trait LinkedListLike[A, This >: Null <: Sequence[A] with LinkedListLike[A, This]] extends SequenceLike[A, This] { self => +trait LinkedListLike[A, This >: Null <: Seq[A] with LinkedListLike[A, This]] extends SeqLike[A, This] { self => var elem: A = _ var next: This = _ diff --git a/src/library/scala/collection/mutable/ListBuffer.scala b/src/library/scala/collection/mutable/ListBuffer.scala index 7d40719b02..fa8d777977 100644 --- a/src/library/scala/collection/mutable/ListBuffer.scala +++ b/src/library/scala/collection/mutable/ListBuffer.scala @@ -28,7 +28,7 @@ final class ListBuffer[A] with GenericTraversableTemplate[A, ListBuffer] with BufferLike[A, ListBuffer[A]] with Builder[A, List[A]] - with SequenceForwarder[A] + with SeqForwarder[A] { override def companion: GenericCompanion[ListBuffer] = ListBuffer @@ -39,7 +39,7 @@ final class ListBuffer[A] private var exported: Boolean = false private var len = 0 - protected def underlying: immutable.Sequence[A] = start + protected def underlying: immutable.Seq[A] = start /** The current length of the buffer */ @@ -328,7 +328,7 @@ final class ListBuffer[A] * @author Martin Odersky * @version 2.8 */ -object ListBuffer extends SequenceFactory[ListBuffer] { +object ListBuffer extends SeqFactory[ListBuffer] { implicit def builderFactory[A]: BuilderFactory[A, ListBuffer[A], Coll] = new VirtualBuilderFactory[A] def newBuilder[A]: Builder[A, ListBuffer[A]] = diff --git a/src/library/scala/collection/mutable/MutableList.scala b/src/library/scala/collection/mutable/MutableList.scala index dcf163bcb2..d85dde71da 100644 --- a/src/library/scala/collection/mutable/MutableList.scala +++ b/src/library/scala/collection/mutable/MutableList.scala @@ -27,8 +27,8 @@ import generic._ * @since 1 */ @serializable @SerialVersionUID(5938451523372603072L) -class MutableList[A] extends LinearSequence[A] - with LinearSequenceLike[A, MutableList[A]] +class MutableList[A] extends LinearSeq[A] + with LinearSeqLike[A, MutableList[A]] with Builder[A, MutableList[A]] { override protected[this] def newBuilder = new MutableList[A] diff --git a/src/library/scala/collection/mutable/Queue.scala b/src/library/scala/collection/mutable/Queue.scala index 1c9e98b1f0..6316b2d5c6 100644 --- a/src/library/scala/collection/mutable/Queue.scala +++ b/src/library/scala/collection/mutable/Queue.scala @@ -80,9 +80,9 @@ class Queue[A] extends MutableList[A] with Cloneable[Queue[A]] { * @return a sequence of all elements in the queue for which * p yields true. */ - def dequeueAll(p: A => Boolean): Sequence[A] = { + def dequeueAll(p: A => Boolean): Seq[A] = { if (first0 eq null) - Sequence.empty + Seq.empty else { val res = new ArrayBuffer[A] while ((first0 ne null) && p(first0.elem)) { diff --git a/src/library/scala/collection/mutable/ResizableArray.scala b/src/library/scala/collection/mutable/ResizableArray.scala index 2f45881307..25a41b278e 100644 --- a/src/library/scala/collection/mutable/ResizableArray.scala +++ b/src/library/scala/collection/mutable/ResizableArray.scala @@ -63,7 +63,7 @@ trait ResizableArray[A] extends Vector[A] /** Copy all elements to a buffer * @param The buffer to which elements are copied override def copyToBuffer[B >: A](dest: Buffer[B]) { - dest ++= (array: Sequence[AnyRef]).asInstanceOf[Sequence[B]] + dest ++= (array: Seq[AnyRef]).asInstanceOf[Seq[B]] } */ @@ -114,7 +114,7 @@ trait ResizableArray[A] extends Vector[A] } } -object ResizableArray extends SequenceFactory[ResizableArray] { +object ResizableArray extends SeqFactory[ResizableArray] { implicit def builderFactory[A]: BuilderFactory[A, ResizableArray[A], Coll] = new VirtualBuilderFactory[A] def newBuilder[A]: Builder[A, ResizableArray[A]] = new ArrayBuffer[A] } diff --git a/src/library/scala/collection/mutable/Seq.scala b/src/library/scala/collection/mutable/Seq.scala new file mode 100644 index 0000000000..e6a9acf84a --- /dev/null +++ b/src/library/scala/collection/mutable/Seq.scala @@ -0,0 +1,41 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2009, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id$ + + +package scala.collection +package mutable + +import generic._ + +/** A subtrait of collection.Seq which represents sequences + * that can be mutated. + * The class adds an update method to collection.Seq. + * + * @since 2.8 + */ +trait Seq[A] extends Iterable[A] + with scala.collection.Seq[A] + with GenericTraversableTemplate[A, Seq] + with SeqLike[A, Seq[A]] { + override def companion: GenericCompanion[Seq] = Seq + + def update(idx: Int, elem: A) +} + +/** A factory object for the trait Seq. + * + * @author Martin Odersky + * @version 2.8 + * @since 2.8 + */ +object Seq extends SeqFactory[Seq] { + implicit def builderFactory[A]: BuilderFactory[A, Seq[A], Coll] = new VirtualBuilderFactory[A] + def newBuilder[A]: Builder[A, Seq[A]] = new ArrayBuffer +} diff --git a/src/library/scala/collection/mutable/Sequence.scala b/src/library/scala/collection/mutable/Sequence.scala deleted file mode 100644 index 961c144338..0000000000 --- a/src/library/scala/collection/mutable/Sequence.scala +++ /dev/null @@ -1,41 +0,0 @@ -/* __ *\ -** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003-2009, LAMP/EPFL ** -** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** -** /____/\___/_/ |_/____/_/ | | ** -** |/ ** -\* */ - -// $Id$ - - -package scala.collection -package mutable - -import generic._ - -/** A subtrait of collection.Sequence which represents sequences - * that can be mutated. - * The class adds an update method to collection.Sequence. - * - * @since 2.8 - */ -trait Sequence[A] extends Iterable[A] - with scala.collection.Sequence[A] - with GenericTraversableTemplate[A, Sequence] - with SequenceLike[A, Sequence[A]] { - override def companion: GenericCompanion[Sequence] = Sequence - - def update(idx: Int, elem: A) -} - -/** A factory object for the trait Sequence. - * - * @author Martin Odersky - * @version 2.8 - * @since 2.8 - */ -object Sequence extends SequenceFactory[Sequence] { - implicit def builderFactory[A]: BuilderFactory[A, Sequence[A], Coll] = new VirtualBuilderFactory[A] - def newBuilder[A]: Builder[A, Sequence[A]] = new ArrayBuffer -} diff --git a/src/library/scala/collection/mutable/Stack.scala b/src/library/scala/collection/mutable/Stack.scala index 8790a81767..ee66c2a2a3 100644 --- a/src/library/scala/collection/mutable/Stack.scala +++ b/src/library/scala/collection/mutable/Stack.scala @@ -23,7 +23,7 @@ import generic._ * @since 1 */ @serializable @cloneable -class Stack[A] private (var elems: List[A]) extends scala.collection.Sequence[A] with Cloneable[Stack[A]] { +class Stack[A] private (var elems: List[A]) extends scala.collection.Seq[A] with Cloneable[Stack[A]] { def this() = this(Nil) diff --git a/src/library/scala/collection/mutable/StringBuilder.scala b/src/library/scala/collection/mutable/StringBuilder.scala index 6bebc430a7..2f062b4b94 100644 --- a/src/library/scala/collection/mutable/StringBuilder.scala +++ b/src/library/scala/collection/mutable/StringBuilder.scala @@ -35,6 +35,8 @@ final class StringBuilder(initCapacity: Int, private val initValue: String) require(initCapacity >= 0) + import scala.collection.Seq + /** The value is used for character storage. */ private var array = new Array[Char](initCapacity + initValue.length) diff --git a/src/library/scala/collection/mutable/Vector.scala b/src/library/scala/collection/mutable/Vector.scala index cb81aab03b..42fb62fccd 100644 --- a/src/library/scala/collection/mutable/Vector.scala +++ b/src/library/scala/collection/mutable/Vector.scala @@ -17,14 +17,14 @@ import generic._ /** A subtrait of collection.Vector which represents sequences * that can be mutated. */ -trait Vector[A] extends Sequence[A] +trait Vector[A] extends Seq[A] with scala.collection.Vector[A] with GenericTraversableTemplate[A, Vector] with VectorLike[A, Vector[A]] { override def companion: GenericCompanion[Vector] = Vector } -object Vector extends SequenceFactory[Vector] { +object Vector extends SeqFactory[Vector] { implicit def builderFactory[A]: BuilderFactory[A, Vector[A], Coll] = new VirtualBuilderFactory[A] def newBuilder[A]: Builder[A, Vector[A]] = new ArrayBuffer } diff --git a/src/library/scala/package.scala b/src/library/scala/package.scala index c894e1e9ad..a4a14ccdf5 100644 --- a/src/library/scala/package.scala +++ b/src/library/scala/package.scala @@ -16,8 +16,10 @@ package object scala { @deprecated("use Iterable instead") type Collection[+A] = Iterable[A] @deprecated("use Iterable instead") val Collection = Iterable - type Seq[+A] = scala.collection.Sequence[A] - val Seq = scala.collection.Sequence + type Seq[+A] = scala.collection.Seq[A] + val Seq = scala.collection.Seq + + @deprecated("use Seq instead") type Sequence[+A] = scala.collection.Seq[A] type RandomAccessSeq[+A] = scala.collection.Vector[A] val RandomAccessSeq = scala.collection.Vector diff --git a/src/library/scala/runtime/BoxedArray.scala b/src/library/scala/runtime/BoxedArray.scala index dd0f5ddf94..7293cff319 100644 --- a/src/library/scala/runtime/BoxedArray.scala +++ b/src/library/scala/runtime/BoxedArray.scala @@ -14,7 +14,7 @@ package scala.runtime import Predef._ import scala.reflect.ClassManifest import collection.mutable._ -import collection.Sequence +import collection.Seq /** *

A class representing Array[T]

diff --git a/src/library/scala/runtime/ScalaRunTime.scala b/src/library/scala/runtime/ScalaRunTime.scala index 158e83b582..89096e0e6f 100644 --- a/src/library/scala/runtime/ScalaRunTime.scala +++ b/src/library/scala/runtime/ScalaRunTime.scala @@ -12,7 +12,7 @@ package scala.runtime import scala.reflect.ClassManifest -import scala.collection.Sequence +import scala.collection.Seq import scala.collection.mutable._ /* The object ScalaRunTime provides ... @@ -55,7 +55,7 @@ object ScalaRunTime { dest } - def toArray[T](xs: scala.collection.Sequence[T]) = { + def toArray[T](xs: scala.collection.Seq[T]) = { val arr = new Array[AnyRef](xs.length) var i = 0 for (x <- xs) arr(i) = x.asInstanceOf[AnyRef] diff --git a/src/library/scala/util/Random.scala b/src/library/scala/util/Random.scala index ac01b9172c..0cf2490c81 100644 --- a/src/library/scala/util/Random.scala +++ b/src/library/scala/util/Random.scala @@ -107,13 +107,13 @@ class Random(val self: java.util.Random) { */ object Random extends Random { - import collection.Sequence + import collection.Seq /** Returns a new sequence in random order. * @param seq the sequence to shuffle * @return the shuffled sequence */ - def shuffle[T: ClassManifest](seq: Sequence[T]): Sequence[T] = { + def shuffle[T: ClassManifest](seq: Seq[T]): Seq[T] = { // It would be better if this preserved the shape of its container, but I have // again been defeated by the lack of higher-kinded type inference. I can // only make it work that way if it's called like @@ -132,11 +132,11 @@ object Random extends Random swap(n - 1, k) } - buf.toSequence + buf.toSeq } /** I was consumed by weeping when I discovered how easy this - * is to implement in SequenceTemplate rather than trying to + * is to implement in SeqLike rather than trying to * accomplish the inference from the outside. For reference * here is the shape-preserving implementation. */ diff --git a/src/library/scala/util/control/Exception.scala b/src/library/scala/util/control/Exception.scala index 70c34015ac..bf53eed251 100644 --- a/src/library/scala/util/control/Exception.scala +++ b/src/library/scala/util/control/Exception.scala @@ -201,7 +201,7 @@ object Exception } /** Private **/ - private def wouldMatch(x: Throwable, classes: collection.Sequence[Class[_]]): Boolean = + private def wouldMatch(x: Throwable, classes: collection.Seq[Class[_]]): Boolean = classes exists (_ isAssignableFrom x.getClass) private def pfFromExceptions(exceptions: Class[_]*) = diff --git a/src/library/scala/xml/NodeSeq.scala b/src/library/scala/xml/NodeSeq.scala index 8f2e3a994d..bd9999ab3c 100644 --- a/src/library/scala/xml/NodeSeq.scala +++ b/src/library/scala/xml/NodeSeq.scala @@ -12,7 +12,7 @@ package scala.xml import collection.immutable -import collection.SequenceLike +import collection.SeqLike import collection.mutable.{Builder, ListBuffer} import collection.generic.BuilderFactory @@ -38,7 +38,7 @@ object NodeSeq { * @author Burak Emir * @version 1.0 */ -abstract class NodeSeq extends immutable.Sequence[Node] with SequenceLike[Node, NodeSeq] { +abstract class NodeSeq extends immutable.Seq[Node] with SeqLike[Node, NodeSeq] { import NodeSeq.seqToNodeSeq // import view magic for NodeSeq wrappers /** Creates a list buffer as builder for this class */ diff --git a/src/library/scala/xml/transform/BasicTransformer.scala b/src/library/scala/xml/transform/BasicTransformer.scala index f3a4d10a33..301f2e23a5 100644 --- a/src/library/scala/xml/transform/BasicTransformer.scala +++ b/src/library/scala/xml/transform/BasicTransformer.scala @@ -31,7 +31,7 @@ abstract class BasicTransformer extends Function1[Node,Node] * to NodeBuffer. */ def transform(it: Iterator[Node], nb: NodeBuffer): Seq[Node] = - it.foldLeft(nb)(_ ++= transform(_)).toSequence + it.foldLeft(nb)(_ ++= transform(_)).toSeq /** Call transform(Node) to each node in ns, yield ns if nothing changes, * otherwise a new sequence of concatenated results. diff --git a/src/swing/scala/swing/ListView.scala b/src/swing/scala/swing/ListView.scala index 9dbd14fcbe..49b69396fe 100644 --- a/src/swing/scala/swing/ListView.scala +++ b/src/swing/scala/swing/ListView.scala @@ -203,8 +203,8 @@ class ListView[A] extends Component { /** * The currently selected items. */ - object items extends scala.collection.SequenceProxy[A] { - def self = peer.getSelectedValues.toSequence.map(_.asInstanceOf[A]) + object items extends scala.collection.SeqProxy[A] { + def self = peer.getSelectedValues.toSeq.map(_.asInstanceOf[A]) def leadIndex: Int = peer.getSelectionModel.getLeadSelectionIndex def anchorIndex: Int = peer.getSelectionModel.getAnchorSelectionIndex } diff --git a/src/swing/scala/swing/Table.scala b/src/swing/scala/swing/Table.scala index 729d0c344c..d31325863d 100644 --- a/src/swing/scala/swing/Table.scala +++ b/src/swing/scala/swing/Table.scala @@ -199,7 +199,7 @@ class Table extends Component with Scrollable.Wrapper { } def cells: Set[(Int, Int)] = - new SelectionSet[(Int, Int)]((for(r <- selection.rows; c <- selection.columns) yield (r,c)).toSequence) { outer => + new SelectionSet[(Int, Int)]((for(r <- selection.rows; c <- selection.columns) yield (r,c)).toSeq) { outer => def -=(n: (Int, Int)) = { peer.removeRowSelectionInterval(n._1,n._1) peer.removeColumnSelectionInterval(n._2,n._2) diff --git a/test/files/neg/bug910.check b/test/files/neg/bug910.check index 4ec73094cd..fe4ad4fca4 100644 --- a/test/files/neg/bug910.check +++ b/test/files/neg/bug910.check @@ -1,6 +1,6 @@ bug910.scala:4: error: type mismatch; - found : Sequence[Char] - required: Seq[int] + found : Seq[Char] + required: scala.Seq[int] val y: Seq[int] = rest ^ one error found diff --git a/test/files/neg/t0259.check b/test/files/neg/t0259.check index a0b322d985..24e35e6176 100644 --- a/test/files/neg/t0259.check +++ b/test/files/neg/t0259.check @@ -1,7 +1,7 @@ t0259.scala:4: error: double definition: constructor TestCase3:(groups: String*)test.TestCase3 and constructor TestCase3:(groups: (String, Int)*)test.TestCase3 at line 3 -have same type after erasure: (groups: Sequence)test.TestCase3 +have same type after erasure: (groups: Seq)test.TestCase3 def this( groups: String*) = this() ^ one error found diff --git a/test/files/pos/implicits.scala b/test/files/pos/implicits.scala index a8d362d0a7..aeb6591507 100644 --- a/test/files/pos/implicits.scala +++ b/test/files/pos/implicits.scala @@ -32,7 +32,7 @@ object Test1625 { } object Test2188 { - implicit def toJavaList[A: ClassManifest](t:collection.Sequence[A]):java.util.List[A] = java.util.Arrays.asList(t.toArray:_*) + implicit def toJavaList[A: ClassManifest](t:collection.Seq[A]):java.util.List[A] = java.util.Arrays.asList(t.toArray:_*) val x: java.util.List[String] = List("foo") } diff --git a/test/files/pos/spec-List.scala b/test/files/pos/spec-List.scala index 7cd6a03f6c..a68e2774cb 100644 --- a/test/files/pos/spec-List.scala +++ b/test/files/pos/spec-List.scala @@ -23,13 +23,13 @@ import annotation.tailrec * @author Martin Odersky and others * @version 2.8 */ -sealed abstract class List[@specialized +A] extends LinearSequence[A] +sealed abstract class List[@specialized +A] extends LinearSeq[A] with Product with GenericTraversableTemplate[A, List] - with LinearSequenceLike[A, List[A]] { + with LinearSeqLike[A, List[A]] { override def companion: GenericCompanion[List] = List - import scala.collection.{Iterable, Traversable, Sequence, Vector} + import scala.collection.{Iterable, Traversable, Seq, Vector} /** Returns true if the list does not contain any elements. * @return true, iff the list is empty. @@ -446,7 +446,7 @@ case object Nil extends List[Nothing] { throw new NoSuchElementException("tail of empty list") // Removal of equals method here might lead to an infinite recusion similar to IntMap.equals. override def equals(that: Any) = that match { - case that1: Sequence[_] => that1.isEmpty + case that1: Seq[_] => that1.isEmpty case _ => false } } @@ -492,9 +492,9 @@ final case class ::[@specialized B](private var hd: B, private[scala] var tl: Li * @author Martin Odersky * @version 2.8 */ -object List extends SequenceFactory[List] { +object List extends SeqFactory[List] { - import collection.{Iterable, Sequence, Vector} + import collection.{Iterable, Seq, Vector} implicit def builderFactory[A]: BuilderFactory[A, List[A], Coll] = new VirtualBuilderFactory[A] def newBuilder[A]: Builder[A, List[A]] = new ListBuffer[A] diff --git a/test/files/run/colltest1.scala b/test/files/run/colltest1.scala index ea8ff8e305..efdce00277 100644 --- a/test/files/run/colltest1.scala +++ b/test/files/run/colltest1.scala @@ -55,10 +55,10 @@ object Test extends Application { secondFive copyToBuffer buf assert(buf.result == ten, buf.result) assert(ten.toArray.size == 10) - assert(ten.toArray.toSequence == ten, ten.toArray.toSequence) + assert(ten.toArray.toSeq == ten, ten.toArray.toSeq) assert(ten.toIterable == ten) assert(ten.toList == ten) - assert(ten.toSequence == ten) + assert(ten.toSeq == ten) assert(ten.toStream == ten) assert(ten.toString endsWith "(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)") assert(ten.mkString("[", "; ", "]") endsWith "[1; 2; 3; 4; 5; 6; 7; 8; 9; 10]") @@ -73,7 +73,7 @@ object Test extends Application { assert(six sameElements (1 to 6)) } - def sequenceTest(empty: Sequence[Int]) { + def sequenceTest(empty: Seq[Int]) { orderedIterableTest(empty) val ten = empty ++ (1 to 10) println(ten) @@ -125,7 +125,7 @@ object Test extends Application { assert((ten ++ ten).removeDuplicates == ten) assert(ten.patch(3, List(4, 5, 6, 7), 4) == ten) assert(ten.patch(0, List(1, 2, 3), 9) == List(1, 2, 3, 10)) - assert(empty.padTo(10, 7) == Array.fill(10)(7).toSequence) + assert(empty.padTo(10, 7) == Array.fill(10)(7).toSeq) assert((ten zip ten.indices) == ten.zipWithIndex) assert(ten.sortWith(_ < _) == ten) assert(ten.sortWith(_ > _) == ten.reverse) @@ -209,12 +209,12 @@ object Test extends Application { orderedIterableTest(Iterable()) orderedIterableTest(mutable.Iterable()) orderedIterableTest(immutable.Iterable()) - sequenceTest(Sequence()) - sequenceTest(mutable.Sequence()) - sequenceTest(immutable.Sequence()) - sequenceTest(LinearSequence()) -// sequenceTest(mutable.LinearSequence()) - sequenceTest(immutable.LinearSequence()) + sequenceTest(Seq()) + sequenceTest(mutable.Seq()) + sequenceTest(immutable.Seq()) + sequenceTest(LinearSeq()) +// sequenceTest(mutable.LinearSeq()) + sequenceTest(immutable.LinearSeq()) sequenceTest(mutable.Vector()) rangeTest(1 to 10) diff --git a/test/files/run/sequenceComparisons.scala b/test/files/run/sequenceComparisons.scala index cc1cf3610c..cf1f64d67d 100644 --- a/test/files/run/sequenceComparisons.scala +++ b/test/files/run/sequenceComparisons.scala @@ -1,27 +1,27 @@ import scala.collection.{ mutable, immutable } -import collection.{ Sequence, Traversable } +import collection.{ Seq, Traversable } object Test { // TODO: // - // SequenceProxy - // SequenceForwarder + // SeqProxy + // SeqForwarder // the commented out ones in seqMakers - val seqMakers = List[List[Int] => Sequence[Int]]( + val seqMakers = List[List[Int] => Seq[Int]]( // scala.Array(_: _*), mutable.ArrayBuffer(_: _*), // mutable.ArrayStack(_: _*), mutable.Buffer(_: _*), - mutable.LinearSequence(_: _*), + mutable.LinearSeq(_: _*), // null on Nil // mutable.LinkedList(_: _*), mutable.ListBuffer(_: _*), // mutable.PriorityQueue(_: _*), // immutable.Queue(_: _*), // mutable.Queue(_: _*), - immutable.Sequence(_: _*), - mutable.Sequence(_: _*), + immutable.Seq(_: _*), + mutable.Seq(_: _*), // immutable.Stack(_: _*), // mutable.Stack(_: _*), immutable.Vector(_: _*), @@ -31,12 +31,12 @@ object Test { ) abstract class Data[T] { - val seq: Sequence[T] + val seq: Seq[T] private def seqList = seq.toList // _1 is inputs which must be true, _2 which must be false type Inputs = (List[List[T]], List[List[T]]) case class Method( - f: (Sequence[T], Sequence[T]) => Boolean, + f: (Seq[T], Seq[T]) => Boolean, inputs: Inputs, descr: String ) { @@ -53,7 +53,7 @@ object Test { lazy val endsWith = Method(_ endsWith _, endsWithInputs, "%s endsWith %s") val indexOfSeqInputs: Inputs - private def subseqTest(s1: Sequence[T], s2: Sequence[T]) = (s1 indexOfSeq s2) != -1 + private def subseqTest(s1: Seq[T], s2: Seq[T]) = (s1 indexOfSeq s2) != -1 lazy val indexOfSeq = Method(subseqTest _, indexOfSeqInputs, "(%s indexOfSeq %s) != -1") val sameElementsInputs: Inputs diff --git a/test/pending/neg/t1545.scala b/test/pending/neg/t1545.scala new file mode 100755 index 0000000000..d7c0245725 --- /dev/null +++ b/test/pending/neg/t1545.scala @@ -0,0 +1,16 @@ +object Main extends Application { + + case class Foo (field : Option[String]) + + val x : PartialFunction[Foo,Int] = + { + c => c.field match { + case Some (s) => 42 + case None => 99 + } + } + + println (x (Foo (None))) // prints 99 + println (x (Foo (Some ("foo")))) // prints 42 + +} diff --git a/test/pending/neg/t1800.scala b/test/pending/neg/t1800.scala new file mode 100644 index 0000000000..eebbbad9c7 --- /dev/null +++ b/test/pending/neg/t1800.scala @@ -0,0 +1,28 @@ +object ObjectHolder { + private[ObjectHolder] class PrivateObject + def getPrivateObject = new PrivateObject +} + +object Test { + def main(args: Array[String]) { + // compiler error: class PrivateObject cannot be accessed + // in object test.ObjectHolder + val a: ObjectHolder.PrivateObject = ObjectHolder.getPrivateObject + + // works fine + val b = ObjectHolder.getPrivateObject + println(b.getClass) + } +} +/* +When declaring objects as private[package/object] or protected[package/object] it is possible to leak out references to these objects into the public api (can be desirable, this in itself is not a problem). + +When users of the api receive such private object via a function call, they can create a variable to reference the private object using inferred typing: + +val b = getPrivateObject() + +However they cannot create such variable using declared typing: + +val a: PrivateObject? = getPrivateObject() + +The line above will generate a compiler error: "class PrivateObject? cannot be accessed". Which makes sense, because PrivateObject? was declared private. But in this case inferred typing should not work either, otherwise the behaviors of inferred typing and declared typing become inconsistent. */ diff --git a/test/pending/neg/t1845.scala b/test/pending/neg/t1845.scala new file mode 100644 index 0000000000..cfb28aa03c --- /dev/null +++ b/test/pending/neg/t1845.scala @@ -0,0 +1,12 @@ +// Compiling the attached code makes scalac give a NPE. + +import scala.util.parsing.combinator.syntactical.TokenParsers +import scala.util.parsing.combinator.lexical.StdLexical +import scala.util.parsing.syntax.StdTokens + +class MyTokenParsers extends TokenParsers { + import lexical._ + type Tokens = StdTokens + type Elem = lexical.Token + val lexical = new StdLexical +} diff --git a/test/pending/neg/t2080.scala b/test/pending/neg/t2080.scala new file mode 100644 index 0000000000..0880a40faa --- /dev/null +++ b/test/pending/neg/t2080.scala @@ -0,0 +1,17 @@ +trait A { + type T + def f(x : T) : T +} + +trait B extends A { + trait T { } + override def f(x : T) : T = x +} + +object C extends B { + override trait T { + def g { } + } + override def f(x : T) : T = { x.g; x } +} +//It compiles without errors, but T in B and T in C are completely unrelated types. diff --git a/test/pending/neg/t2180.scala b/test/pending/neg/t2180.scala new file mode 100644 index 0000000000..a8055bf77d --- /dev/null +++ b/test/pending/neg/t2180.scala @@ -0,0 +1,31 @@ + + +Given the following code (which is broken): + +class Mxml { + + private def processChildren( children:Seq[Any] ):List[Mxml] = { + + children.toList.flatMap ( e => { + + e match { + + case s:scala.collection.Traversable[_] => s case a => List(a) + + } + + }) + + } + +} + +I get the following error: + +Mxml.scala:5: error: could not find implicit value for parameter bf:scala.collection.generic.BuilderFactory[Any,List[Mxml],Sequence[Any]]. + + children.flatMap ( e => { + +I spent 4 hours failing before I figured out the problem. The return type was wrong. It should have been List[Any]. + +I have seen similar errors with map. My solution in the past has been to change it to a foldLeft because I have never been able to determine how to fix the problem until now. diff --git a/test/pending/pos/local-objects.scala b/test/pending/pos/local-objects.scala new file mode 100644 index 0000000000..ed7c50ead9 --- /dev/null +++ b/test/pending/pos/local-objects.scala @@ -0,0 +1,9 @@ + + +object Main { + val x = { + object Boo + Boo + } +} + diff --git a/test/pending/pos/t1722.scala b/test/pending/pos/t1722.scala new file mode 100644 index 0000000000..d059bf22f8 --- /dev/null +++ b/test/pending/pos/t1722.scala @@ -0,0 +1,10 @@ +sealed trait Top +trait C { + private object P extends Top +} +/* +$ scala -e 'new AnyRef with C' +error: error while loading Top, class file '/private/tmp/bobobo/./Top.class' is broken +(error reading Scala signature of /private/tmp/bobobo/./Top.class: malformed Scala signature of Top at 185; reference value P of trait C refers to nonexisting symbol.) +one error found +*/ diff --git a/test/pending/pos/t1722/C.scala b/test/pending/pos/t1722/C.scala new file mode 100755 index 0000000000..2695296292 --- /dev/null +++ b/test/pending/pos/t1722/C.scala @@ -0,0 +1,9 @@ +trait C { + private object P extends Top +} +/* +$ scala -e 'new AnyRef with C' +error: error while loading Top, class file '/private/tmp/bobobo/./Top.class' is broken +(error reading Scala signature of /private/tmp/bobobo/./Top.class: malformed Scala signature of Top at 185; reference value P of trait C refers to nonexisting symbol.) +one error found +*/ diff --git a/test/pending/pos/t1722/Test.scala b/test/pending/pos/t1722/Test.scala new file mode 100755 index 0000000000..4cba7ab01b --- /dev/null +++ b/test/pending/pos/t1722/Test.scala @@ -0,0 +1,4 @@ +package t1722 +object Test { + val x = new AnyRef with C +} diff --git a/test/pending/pos/t1722/Top.scala b/test/pending/pos/t1722/Top.scala new file mode 100755 index 0000000000..4ac52412aa --- /dev/null +++ b/test/pending/pos/t1722/Top.scala @@ -0,0 +1,13 @@ +package t1722 + +sealed trait Top +trait C { + private object P extends Top +} +/* +$ scala -e 'new AnyRef with C' +error: error while loading Top, class file '/private/tmp/bobobo/./Top.class' is broken +(error reading Scala signature of /private/tmp/bobobo/./Top.class: malformed Scala signature of Top at 185; reference value P of trait C refers to nonexisting symbol.) +one error found +Martin: I think this has to do with children property. +*/ diff --git a/test/pending/pos/t1756.scala b/test/pending/pos/t1756.scala new file mode 100644 index 0000000000..c22d5509e1 --- /dev/null +++ b/test/pending/pos/t1756.scala @@ -0,0 +1,30 @@ +trait Ring[T <: Ring[T]] { + def +(that: T): T + def *(that: T): T +} + +class A extends Ring[A] { + def +(that: A) = new A + def *(that: A) = new A +} + +class Poly[C <: Ring[C]](val c: C) extends Ring[Poly[C]] { + def +(that: Poly[C]) = new Poly(this.c+that.c) + def *(that: Poly[C]) = new Poly(this.c*that.c) +} + +implicit def coef2poly[C <: Ring[C]](c: C) = new Poly(c) + +val a = new A +val x = new Poly(new A) + +x+a // works +a+x // works + +val y = new Poly(new Poly(new A)) + +x+y*x // works +x*y+x // works +y*x+x // works + +x+x*y diff --git a/test/pending/pos/t1786.scala b/test/pending/pos/t1786.scala new file mode 100644 index 0000000000..86aaee0121 --- /dev/null +++ b/test/pending/pos/t1786.scala @@ -0,0 +1,13 @@ +class SomeClass(val intValue:Int) +class MyClass[T <: SomeClass](val myValue:T) + +def myMethod(i:MyClass[_]) { + i.myValue.intValue/2 // << error i is of type Any +} + +def myMethod(i:MyClass[_ <: SomeClass]) { + i.myValue.intValue/2 // << works +} +/* +The below code shows a compiler flaw in that the wildcard "_" as value for a bounded type parameter either breaks the boundry - as it result in Any - or doesnt (as id hoped it to be) evaluates to the boundy. +*/ diff --git a/test/pending/pos/t1798.scala b/test/pending/pos/t1798.scala new file mode 100644 index 0000000000..1624e3025e --- /dev/null +++ b/test/pending/pos/t1798.scala @@ -0,0 +1,10 @@ +object Foo { private def bar(): Int = 55 } +class Foo(x: Int) { def this() = this(Foo.bar()) } + +/* + * scalac28 a.scala +a.scala:2: error: method bar cannot be accessed in object Foo +class Foo(x: Int) { def this() = this(Foo.bar()) } + ^ +one error found +*/ diff --git a/test/pending/pos/t1987.scala b/test/pending/pos/t1987.scala new file mode 100644 index 0000000000..ccab133716 --- /dev/null +++ b/test/pending/pos/t1987.scala @@ -0,0 +1,8 @@ +package object overloading { + def bar(f: (Int) => Unit): Unit = () + def bar(f: (Int, Int) => Unit): Unit = () +} + +class PackageObjectOverloadingTest { + overloading.bar( (i: Int) => () ) // doesn't compile. +} diff --git a/test/pending/pos/t1996.scala b/test/pending/pos/t1996.scala new file mode 100644 index 0000000000..2730128196 --- /dev/null +++ b/test/pending/pos/t1996.scala @@ -0,0 +1,19 @@ +object forbug { + val l1 = List(List(ValDef(new A)), List(ValDef(new A))) + for ((e1s, e2s) <- l1.zip(l1); + (e1, e2) <- e1s.zip(e2s)) { + e1.a.doSome(20) +// () + } +} + + +class A { + def doSome(a: Int): this.type = { + println(a) + this + } +} + +case class ValDef(a: A) + diff --git a/test/pending/pos/t2071.scala b/test/pending/pos/t2071.scala new file mode 100644 index 0000000000..a96edb4cfb --- /dev/null +++ b/test/pending/pos/t2071.scala @@ -0,0 +1,14 @@ +trait Iterable[+S] +trait Box[U] + +trait A { + type T <: Iterable[S] forSome { type S <: Box[U]; type U } +} + +trait B extends A { + type T <: Iterable[S] forSome { type S <: Box[U]; type U } +} +But according to SLS, 3.5.1 Type Equivalence: Two existential types (§3.2.10) are equivalent if they have the same number of quantifiers, and, after renaming one list of type quantifiers by another, the quantified types as well as lower and upper bounds of corresponding quantifiers are equivalent. + +So, every existential type must be equivalent to (and conform to) itself. +Attachments diff --git a/test/pending/pos/t2081.scala b/test/pending/pos/t2081.scala new file mode 100644 index 0000000000..72ebd0557b --- /dev/null +++ b/test/pending/pos/t2081.scala @@ -0,0 +1,7 @@ +class RichInt(n: Int) { + def days = 1000*60*60*24*n +} + +implicit def RichInt(n: Int): RichInt = new RichInt(n) + +10.days diff --git a/test/pending/pos/t2099.scala b/test/pending/pos/t2099.scala new file mode 100644 index 0000000000..ddeee58a4b --- /dev/null +++ b/test/pending/pos/t2099.scala @@ -0,0 +1,27 @@ + + +I have a trait: + +trait Vis[+T] + +and an object/class pair: + +object VisImpl? { def apply() = new VisImpl? } class VisImpl? extends Vis[Missing] + +Where Missing is some class of mine. In a separate project (where Vis and VisImpl? are on the classpath but Missing is not), if I do: + +object Test extends Application { + + val v = VisImpl?() println(v) + +} + +This causes a Scala compiler error (using 2.7.5 compiler). The error is: + +"Caused by java.lang.RuntimeException?: malformed Scala signature of VisImpl? at 3634; reference value data of package mypack refers to nonexisting symbol" + +Where mypack is the root package of the Missing class. This is not a helpful error as all my classes share the same root package and the problem is not in the VisImpl? declaration in any case. + +I would expect to see an error of the form: + +" Type parameter not found 'Missing': VisImpl? extends Vis[Missing] at Test: #4: val v = VisImpl?() " diff --git a/test/pending/pos/t2108.scala b/test/pending/pos/t2108.scala new file mode 100644 index 0000000000..cd73b42627 --- /dev/null +++ b/test/pending/pos/t2108.scala @@ -0,0 +1,2 @@ +val a: Vector[_ <: Vector[Any]] = Array(Array("", 0)) +val x = a(0) // java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to scala.collection.mutable.Vector diff --git a/test/pending/pos/t2127.scala b/test/pending/pos/t2127.scala new file mode 100644 index 0000000000..a196a95c58 --- /dev/null +++ b/test/pending/pos/t2127.scala @@ -0,0 +1,29 @@ + + +As discussed here: http://www.nabble.com/Companion-object-constructor-visibility-td24342096.html + +//Simplified example: + + class Foo private (val value : Int) + + abstract class Bar(val ctor : (Int) => Foo) + + object Foo extends Bar(new Foo(_)) //<--- ILLEGAL ACCESS + +//however the following is legal: + + class Foo private (val value : Int) + + abstract class Bar{ + + var ctor : (Int) => Foo + + } + + object Foo extends Bar{ + + ctor = new Foo(_) //<--- Legal access + + } + +The constructor invocation of Bar is done within the scope of object Foo's constructor, and therefor the private constructor of Foo should be visible and accessible. diff --git a/test/pending/pos/t2130.scala b/test/pending/pos/t2130.scala new file mode 100644 index 0000000000..8e978b125b --- /dev/null +++ b/test/pending/pos/t2130.scala @@ -0,0 +1,44 @@ +Assertion failed while re-compiling a package object containing a case class +Reported by: rjm Owned by: odersky +Priority: normal Component: Compiler +Keywords: Cc: erik.engbrecht@… +Fixed in version: +Description + +While playing with the latest 2.8 snapshot (r18215-b20090706020217 to be precise) I noticed that a package object containing a case class gets an assertion failure when re-compiling (i.e., the class files already exist from a previous run, even a previous build of the exact same source). A minimal test case: + +package object foo { + + case class X() + +} + +And the error: + +Exception in thread "main" java.lang.AssertionError?: assertion failed: List(object package$X, object package$X) + + at scala.Predef$.assert(Predef.scala:97) at scala.tools.nsc.symtab.Symbols$Symbol.suchThat(Symbols.scala:988) at scala.tools.nsc.symtab.Symbols$Symbol.linkedModuleOfClass(Symbols.scala:1159) at scala.tools.nsc.backend.jvm.GenJVM$BytecodeGenerator?.genClass(GenJVM.scala:203) at scala.tools.nsc.backend.jvm.GenJVM$JvmPhase?$$anonfun$run$2.apply(GenJVM.scala:50) at scala.tools.nsc.backend.jvm.GenJVM$JvmPhase?$$anonfun$run$2.apply(GenJVM.scala:50) at scala.collection.Iterator$class.foreach(Iterator.scala:500) at scala.collection.generic.MapTemplate?$$anon$4.foreach(MapTemplate?.scala:156) at scala.tools.nsc.backend.jvm.GenJVM$JvmPhase?.run(GenJVM.scala:50) at scala.tools.nsc.Global$Run.compileSources(Global.scala:781) at scala.tools.nsc.Global$Run.compile(Global.scala:855) at scala.tools.nsc.Main$.process(Main.scala:75) at scala.tools.nsc.Main$.main(Main.scala:89) at scala.tools.nsc.Main.main(Main.scala) + +A normal class does not cause the failure; nor does a case object. +Attachments +Change History +Changed 4 weeks ago by eengbrec + + * cc erik.engbrecht@… added + +Changed 3 weeks ago by rompf + + * owner changed from scala_reviewer to scala_meeting + +Changed 3 weeks ago by rompf + + * owner changed from scala_meeting to odersky + +Add/Change #2130 (Assertion failed while re-compiling a package object containing a case class) +Comment (you may use WikiFormatting here): + +Change Properties +Summary: +Reporter: +Description: While playing with the latest 2.8 snapshot (r18215-b20090706020217 to be precise) I noticed that a package object containing a case class gets an assertion failure when re-compiling (i.e., the class files already exist from a previous run, even a previous build of the exact same source). A minimal test case: package object foo { case class X() } And the error: Exception in thread "main" java.lang.AssertionError: assertion failed: List(object package$X, object package$X) at scala.Predef$.assert(Predef.scala:97) at scala.tools.nsc.symtab.Symbols$Symbol.suchThat(Symbols.scala:988) at scala.tools.nsc.symtab.Symbols$Symbol.linkedModuleOfClass(Symbols.scala:1159) at scala.tools.nsc.backend.jvm.GenJVM$BytecodeGenerator.genClass(GenJVM.scala:203) at scala.tools.nsc.backend.jvm.GenJVM$JvmPhase$$anonfun$run$2.apply(GenJVM.scala:50) at scala.tools.nsc.backend.jvm.GenJVM$JvmPhase$$anonfun$run$2.apply(GenJVM.scala:50) at scala.collection.Iterator$class.foreach(Iterator.scala:500) at scala.collection.generic.MapTemplate$$anon$4.foreach(MapTemplate.scala:156) at scala.tools.nsc.backend.jvm.GenJVM$JvmPhase.run(GenJVM.scala:50) at scala.tools.nsc.Global$Run.compileSources(Global.scala:781) at scala.tools.nsc.Global$Run.compile(Global.scala:855) at scala.tools.nsc.Main$.process(Main.scala:75) at scala.tools.nsc.Main$.main(Main.scala:89) at scala.tools.nsc.Main.main(Main.scala) A normal class does not cause the failure; nor does a case object. +Type: diff --git a/test/pending/pos/t2162.scala b/test/pending/pos/t2162.scala new file mode 100644 index 0000000000..f90d48101d --- /dev/null +++ b/test/pending/pos/t2162.scala @@ -0,0 +1,19 @@ +class Foo(x: Int) +class Bar extends Foo(1) + +trait A { + def foo[T <: Foo] +} +class B extends A { + def foo[Bar] { println("B.foo[Bar]") } +} +object test { + val x = new B + val y = new A { + def foo[Bar] { println("A.foo[Bar]") } + } + def main(args: Array[String]) { + x.foo // ok + y.foo // java.lang.AssertionError: assertion failed (Erasure.scala:441 in r18338)) + } +} diff --git a/test/pending/pos/t2173.scala b/test/pending/pos/t2173.scala new file mode 100644 index 0000000000..9a9a2edce4 --- /dev/null +++ b/test/pending/pos/t2173.scala @@ -0,0 +1,17 @@ + + +This (somewhat convoluted) code fails to compile + +class A[+U>:Null] { + + type R[+X>:Null] = X type O[+X] = A[R[X]] + +} + +with the following error: + +type arguments [A.this.R[X]] do not conform to class A's type parameter bounds [+U >: Null] + +However, because type R[+X>:Null] is identical to X, it should carry X bounds and R[X] lower bound should be known to be X's lower bound, i.e. Null. + +The same problem occurs with upper bounds. diff --git a/test/pending/pos/t2178.scala b/test/pending/pos/t2178.scala new file mode 100644 index 0000000000..9ee3165a72 --- /dev/null +++ b/test/pending/pos/t2178.scala @@ -0,0 +1,10 @@ +scala> Array(Array(1)).last.last +java.lang.ClassCastException: [I + at .(:5) + at .() + at RequestResult$.(:4) + at RequestResult$.() + at RequestResult$result() + at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) + at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) + at sun.reflect.DelegatingMethodAccessorImpl... diff --git a/test/pending/pos/t2179.scala b/test/pending/pos/t2179.scala new file mode 100644 index 0000000000..fd37d8f760 --- /dev/null +++ b/test/pending/pos/t2179.scala @@ -0,0 +1,6 @@ +on trunk r18368, this delightful little poison pill: + +(Nil:List[List[Double]]).reduceLeft((_: Any, _: Any) => Nil.indices.map(_ => 0d)) + +sends the compiler into an apparently infinite loop. a sample thread dump shows: + diff --git a/test/pending/pos/t2185,.scala b/test/pending/pos/t2185,.scala new file mode 100644 index 0000000000..98e544e5ad --- /dev/null +++ b/test/pending/pos/t2185,.scala @@ -0,0 +1,3 @@ +scala> def foo { Nil.map(identity) } +:4: error: could not find implicit value for parameter bf:scala.collection.generic.BuilderFactory[Nothing,Unit,List[Nothing]]. + def foo { Nil.map(identity) } diff --git a/test/pending/pos/t2188.scala b/test/pending/pos/t2188.scala new file mode 100644 index 0000000000..e9f6e4fb2c --- /dev/null +++ b/test/pending/pos/t2188.scala @@ -0,0 +1,9 @@ +scala> implicit def toJavaList[A](t:collection.Sequence[A]):java.util.List[A] = + | java.util.Arrays.asList(t.toArray:_*) +toJavaList: [A](t: Sequence[A])java.util.List[A] + +scala> val x: java.util.List[String] = List("foo") +:7: error: type mismatch; + found : List[Any] + required: java.util.List[String] + val x: java.util.List[String] = List("foo") diff --git a/test/pending/pos/t2194.scala b/test/pending/pos/t2194.scala new file mode 100644 index 0000000000..6197ca0e90 --- /dev/null +++ b/test/pending/pos/t2194.scala @@ -0,0 +1,15 @@ +scala> class C +defined class C + +scala> def f = { object o extends C; o} +f: ewo.type forSome { val o: o; type o <: C with ScalaObject } + +scala> val x = f +:6: error: type mismatch; + found : o.type(in object $iw) where type o.type(in object $iw) <: o with Singleton + required: o.type(in value x) forSome { type o.type(in value x) <: o with Singleton; type o <: C with ScalaObject } + val x = f + ^ + +scala> val x : C = f +x: C = o$2$@111985e diff --git a/test/pending/pos/t2201.scala b/test/pending/pos/t2201.scala new file mode 100644 index 0000000000..21af170cf1 --- /dev/null +++ b/test/pending/pos/t2201.scala @@ -0,0 +1,8 @@ +class Test +object Test { implicit def view(x : Test) = 0 } + +object Call { + def call(implicit view : Test => Int) = view(null) + call + call +} diff --git a/test/pending/pos/ticket2251.scala b/test/pending/pos/ticket2251.scala new file mode 100644 index 0000000000..7b6efb0ea0 --- /dev/null +++ b/test/pending/pos/ticket2251.scala @@ -0,0 +1,25 @@ + +// Martin: I am not sure this is a solvable problem right now. I'll leave it in pending. +// derived from pos/bug1001 +class A +trait B[T <: B[T]] extends A +class C extends B[C] +class D extends B[D] + +class Data { + // force computing lub of C and D (printLubs enabled:) + +/* +lub of List(D, C) at depth 2 + lub of List(D, C) at depth 1 + lub of List(D, C) at depth 0 + lub of List(D, C) is A + lub of List(D, C) is B[_1] forSome { type _1 >: D with C <: A } +lub of List(D, C) is B[_2] forSome { type _2 >: D with C{} <: B[_1] forSome { type _1 >: D with C{} <: A } } +*/ +// --> result = WRONG + + // should be: B[X] forSome {type X <: B[X]} -- can this be done automatically? for now, just detect f-bounded polymorphism and fall back to more coarse approximation + + val data: List[A] = List(new C, new D) +} diff --git a/test/pending/run/t1939.scala b/test/pending/run/t1939.scala new file mode 100644 index 0000000000..4860ca8169 --- /dev/null +++ b/test/pending/run/t1939.scala @@ -0,0 +1,42 @@ + + +class Module {} + +abstract class T { + type moduleType <: Module + def module: moduleType +} + +final class T1(val module: Module) extends T { + type moduleType = Module +} + +final class T2(_module: Module) extends T { + type moduleType = Module + + def module = _module +} + +object Main extends Application { + + type mType = Module + + type tType = T { type moduleType <: mType } + // type tType = T { type moduleType <: Module } // runs successfully + // type tType = T // runs successfully + + def f(ts: List[tType]): Unit = { + + for (t <- ts; m = t.module) {} + ts.map(_.module).foreach { _ => () } + // ts.map(t => (t : T).module).foreach { _ => () } // runs successfully + } + + f(new T1(new Module) :: new T2(new Module) :: Nil) +} + +/* + * java.lang.AbstractMethodError + at scala.List.foreach(List.scala:849) + at Main$.f +*/ diff --git a/test/pending/run/t1980.scala b/test/pending/run/t1980.scala new file mode 100644 index 0000000000..7c5737f95e --- /dev/null +++ b/test/pending/run/t1980.scala @@ -0,0 +1,13 @@ +scala> def foo() = { println("foo") ; 5 } +foo: ()Int + +scala> class C { def m1(f: => Int) = () ; def m2_:(f: => Int) = () } +defined class C + +scala> val c = new C +c: C = C@96d484 + +scala> c m1 foo() + +scala> foo() m2_: c +foo diff --git a/test/pending/run/t2005.scala b/test/pending/run/t2005.scala new file mode 100644 index 0000000000..8e4243a768 --- /dev/null +++ b/test/pending/run/t2005.scala @@ -0,0 +1,24 @@ +Object Bug { + def main(args: Array[String]) { + val a = new Array[Array[Int]](2,2) + test(a) + } + def test[A](t: Array[Array[A]]) { + val tmp = t(0) + t(1) = tmp + } +} +java.lang.ArrayStoreException: scala.runtime.BoxedIntArray + at scala.runtime.BoxedObjectArray.update(BoxedObjectArray.scala:26) + at Bug$.test(Bug.scala:12) + at Bug$.main(Bug.scala:7) + at Bug.main(Bug.scala) + at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) + at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) + at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) + at java.lang.reflect.Method.invoke(Method.java:585) + at scala.tools.nsc.ObjectRunner$$anonfun$run$1.apply(ObjectRunner.scala:75) + at scala.tools.nsc.ObjectRunner$.withContextClassLoader(ObjectRunner.scala:49) + at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:74) + at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:154) + at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala) -- cgit v1.2.3