diff options
author | Paul Phillips <paulp@improving.org> | 2012-12-28 16:52:47 -0800 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2012-12-28 16:57:33 -0800 |
commit | 13643815fe41b17e931be9d0a8906200b0147a23 (patch) | |
tree | a9646c5c5b945049e248629ffb61099009b2d6ef /src/library | |
parent | 24a033b2aa4174fe8e9c3c02372b6508ef404601 (diff) | |
download | scala-13643815fe41b17e931be9d0a8906200b0147a23.tar.gz scala-13643815fe41b17e931be9d0a8906200b0147a23.tar.bz2 scala-13643815fe41b17e931be9d0a8906200b0147a23.zip |
LinearSeq lengthCompare without an iterator.
Had to fix up an iffy test: not only was it testing undefined
behavior, it demanded just the right numbers be printed in a
context where all negative or positive numbers are equivalent.
It's the ol' "get them coming and going" trick.
Diffstat (limited to 'src/library')
-rwxr-xr-x | src/library/scala/collection/LinearSeqOptimized.scala | 22 |
1 files changed, 10 insertions, 12 deletions
diff --git a/src/library/scala/collection/LinearSeqOptimized.scala b/src/library/scala/collection/LinearSeqOptimized.scala index 48b0b5469e..81cccea519 100755 --- a/src/library/scala/collection/LinearSeqOptimized.scala +++ b/src/library/scala/collection/LinearSeqOptimized.scala @@ -12,6 +12,7 @@ import generic._ import mutable.ListBuffer import immutable.List import scala.util.control.Breaks._ +import scala.annotation.tailrec /** A template trait for linear sequences of type `LinearSeq[A]` which optimizes * the implementation of several methods under the assumption of fast linear access. @@ -248,19 +249,16 @@ trait LinearSeqOptimized[+A, +Repr <: LinearSeqOptimized[A, Repr]] extends Linea override /*SeqLike*/ def lengthCompare(len: Int): Int = { - // TODO: Remove this method when incrementing a major revision - // This method is the same as in SeqLike, no need to redefine it - if (len < 0) 1 - else { - var i = 0 - val it = iterator - while (it.hasNext) { - if (i == len) return if (it.hasNext) 1 else 0 - it.next() - i += 1 - } - i - len + @tailrec def loop(i: Int, xs: Repr): Int = { + if (i == len) + if (xs.isEmpty) 0 else 1 + else if (xs.isEmpty) + -1 + else + loop(i + 1, xs.tail) } + if (len < 0) 1 + else loop(0, this) } override /*SeqLike*/ |