diff options
author | Burak Emir <emir@epfl.ch> | 2007-09-01 10:24:31 +0000 |
---|---|---|
committer | Burak Emir <emir@epfl.ch> | 2007-09-01 10:24:31 +0000 |
commit | 623a1e50fb1999e0eea2f2c229d54d01eaae80d8 (patch) | |
tree | 2a7a89e57e1efb205c5231f3fc3045a947001e7d /src/library | |
parent | 8d61f90ec53171781d639b9ca7c6c026641d929e (diff) | |
download | scala-623a1e50fb1999e0eea2f2c229d54d01eaae80d8.tar.gz scala-623a1e50fb1999e0eea2f2c229d54d01eaae80d8.tar.bz2 scala-623a1e50fb1999e0eea2f2c229d54d01eaae80d8.zip |
enhancement #20
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/scala/List.scala | 39 | ||||
-rw-r--r-- | src/library/scala/Seq.scala | 5 | ||||
-rw-r--r-- | src/library/scala/Stream.scala | 9 |
3 files changed, 26 insertions, 27 deletions
diff --git a/src/library/scala/List.scala b/src/library/scala/List.scala index 9fd43f5140..8cc9e4e419 100644 --- a/src/library/scala/List.scala +++ b/src/library/scala/List.scala @@ -401,6 +401,15 @@ sealed abstract class List[+A] extends Seq[A] { */ def head: A + + /** returns length - l, without calling length + */ + override def lengthCompare(l: Int) = { + if (isEmpty) 0 - l + else if (l <= 0) 1 + else tail.lengthCompare(l - 1) + } + /** Returns this list without its first element. * * @return this list without its first element. @@ -1210,32 +1219,8 @@ case object Nil extends List[Nothing] { * @version 1.0, 15/07/2003 */ @SerialVersionUID(0L - 8476791151983527571L) -final case class ::[B](private[scala] var hd: B, private[scala] var tl: List[B]) extends List[B] { - /* XXX: making hd a class-private var crashes the compiler. */ - def head : B = hd - def tail : List[B] = tl +final case class ::[B](hd: B, private[scala] var tl: List[B]) extends List[B] { + def head = hd + def tail = tl override def isEmpty: Boolean = false - - import java.io._ - - private def writeObject(out : ObjectOutputStream) = { - val i = elements - while (i.hasNext) out.writeObject(i.next) - out.writeObject(ListSerializeEnd) - } - private def readObject(in : ObjectInputStream) : Unit = { - hd = in.readObject.asInstanceOf[B] - assert(hd != ListSerializeEnd) - var current : ::[B] = this - while (true) in.readObject match { - case ListSerializeEnd => current.tl = Nil; return - case a : Any => - val list : ::[B] = new ::(a.asInstanceOf[B], Nil) - current.tl = list - current = list - } - } } -/** Only used for list serialization */ -@SerialVersionUID(0L - 8476791151975527571L) -private[scala] case object ListSerializeEnd
\ No newline at end of file diff --git a/src/library/scala/Seq.scala b/src/library/scala/Seq.scala index b78c3f9123..dd59ddfa81 100644 --- a/src/library/scala/Seq.scala +++ b/src/library/scala/Seq.scala @@ -156,6 +156,11 @@ trait Seq[+A] extends AnyRef with PartialFunction[Int, A] with Collection[A] { */ def length: Int + /** Returns length - l. This method is used by matching streams against right-ignoring (...,_*) patterns. + * Lazy sequences should override this method if length forces evaluation of the stream. + */ + def lengthCompare(l: Int): Int = length - l + /** should always be <code>length</code> */ def size = length diff --git a/src/library/scala/Stream.scala b/src/library/scala/Stream.scala index 14bfd258d6..51c96b3f0c 100644 --- a/src/library/scala/Stream.scala +++ b/src/library/scala/Stream.scala @@ -196,6 +196,15 @@ trait Stream[+A] extends Seq.Projection[A] { /** The length of this stream */ override def length: Int = if (isEmpty) 0 else tail.length + 1 + + /** returns length - l without calling length + */ + override def lengthCompare(l: Int) = { + if (isEmpty) 0 - l + else if (l <= 0) 1 + else tail.lengthCompare(l - 1) + } + override def hasDefiniteSize = false |