summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTiark Rompf <tiark.rompf@epfl.ch>2009-10-30 07:51:19 +0000
committerTiark Rompf <tiark.rompf@epfl.ch>2009-10-30 07:51:19 +0000
commit6ae7f2cbc183d57ba7d00fe790889f359c7b3a11 (patch)
tree66700f3cdb7fd3c29ee28e6b58c77c947cb52a98
parentc5157c830cc515c7bd0b5b23ca2cecde77609c8d (diff)
downloadscala-6ae7f2cbc183d57ba7d00fe790889f359c7b3a11.tar.gz
scala-6ae7f2cbc183d57ba7d00fe790889f359c7b3a11.tar.bz2
scala-6ae7f2cbc183d57ba7d00fe790889f359c7b3a11.zip
fixed 2544, reimplemented SeqLike.indexWhere, s...
fixed 2544, reimplemented SeqLike.indexWhere, segmentLength and lengthCompare in terms of iterators instead of foreach and breaks
-rw-r--r--src/library/scala/collection/SeqLike.scala41
-rw-r--r--test/files/run/t2544.check10
-rw-r--r--test/files/run/t2544.scala19
3 files changed, 44 insertions, 26 deletions
diff --git a/src/library/scala/collection/SeqLike.scala b/src/library/scala/collection/SeqLike.scala
index ff5a8207c1..3c8eaec9a2 100644
--- a/src/library/scala/collection/SeqLike.scala
+++ b/src/library/scala/collection/SeqLike.scala
@@ -115,8 +115,6 @@ 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
@@ -135,13 +133,12 @@ trait SeqLike[+A, +Repr] extends IterableLike[A, Repr] { self =>
* is O(length min len) instead of O(length). The method should be overwritten
* if computing length is cheap.
*/
- def lengthCompare(len: Int): Int = { //TR: should use iterator?
+ def lengthCompare(len: Int): Int = {
var i = 0
- breakable {
- for (_ <- this) {
- i += 1
- if (i > len) break
- }
+ val it = iterator
+ while (it.hasNext && i <= len) {
+ it.next()
+ i += 1
}
i - len
}
@@ -159,16 +156,12 @@ trait SeqLike[+A, +Repr] extends IterableLike[A, Repr] { self =>
* @param p the predicate
* @param from the start index
*/
- def segmentLength(p: A => Boolean, from: Int): Int = { //TR: should use iterator?
- var result = 0
+ def segmentLength(p: A => Boolean, from: Int): Int = {
var i = 0
- breakable {
- for (x <- this) {
- if (i >= from && !p(x)) { result = i - from; break }
- else i += 1
- }
- }
- result
+ var it = iterator.drop(from)
+ while (it.hasNext && p(it.next()))
+ i += 1
+ i
}
/** Returns length of longest prefix of this seqence
@@ -192,16 +185,12 @@ trait SeqLike[+A, +Repr] extends IterableLike[A, Repr] { self =>
* @param p the predicate
* @param from the start index
*/
- def indexWhere(p: A => Boolean, from: Int): Int = { //TR: should use iterator?
- var result = -1
+ def indexWhere(p: A => Boolean, from: Int): Int = {
var i = from
- breakable {
- for (x <- this) {
- if (i >= from && p(x)) { result = i; break }
- else i += 1
- }
- }
- result
+ var it = iterator.drop(from)
+ while (it.hasNext && !p(it.next()))
+ i += 1
+ if (it.hasNext) i else -1
}
/** Returns index of the first element satisying a predicate, or -1. */
diff --git a/test/files/run/t2544.check b/test/files/run/t2544.check
new file mode 100644
index 0000000000..716b146ac4
--- /dev/null
+++ b/test/files/run/t2544.check
@@ -0,0 +1,10 @@
+2
+2
+3
+3
+-2
+-2
+1
+1
+0
+0
diff --git a/test/files/run/t2544.scala b/test/files/run/t2544.scala
new file mode 100644
index 0000000000..7e7cfeb357
--- /dev/null
+++ b/test/files/run/t2544.scala
@@ -0,0 +1,19 @@
+object Test {
+ object Foo extends Seq[Int] {
+ def apply(i: Int) = i
+ def length = 4
+ def iterator = Iterator(0,1,2,3,4)
+ }
+ def main(args: Array[String]) = {
+ println(Foo indexWhere(_ >= 2,1))
+ println(Foo.toList indexWhere(_ >= 2,1))
+ println(Foo segmentLength(_ <= 3,1))
+ println(Foo.toList segmentLength(_ <= 3,1))
+ println(Foo lengthCompare 7)
+ println(Foo.toList lengthCompare 7)
+ println(Foo lengthCompare 2)
+ println(Foo.toList lengthCompare 2)
+ println(Foo lengthCompare 5)
+ println(Foo.toList lengthCompare 5)
+ }
+} \ No newline at end of file