summaryrefslogtreecommitdiff
path: root/src
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 /src
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
Diffstat (limited to 'src')
-rw-r--r--src/library/scala/collection/SeqLike.scala41
1 files changed, 15 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. */