git-svn-id: http://lampsvn.epfl.ch/svn-repos/scala/scala/trunk@14042 5e8d7ff9-d8ef-0310-90f0-a4852d11357a
This commit is contained in:
michelou 2008-02-18 13:48:37 +00:00
parent a80bcf5afc
commit 5df9475b4b
3 changed files with 56 additions and 33 deletions

View File

@ -53,20 +53,21 @@ object Iterator {
/**
* @param xs the array of elements
* @param start the start index
* @param length the end index
* @param length the length
* @see also: RandomAccessSeq.elements and slice
*/
@throws(classOf[NoSuchElementException])
def fromArray[a](xs: Array[a], start: Int, length: Int): Iterator[a] =
new BufferedIterator.Advanced[a] {
private var i = start
val end = if ((start + length) < xs.length) start else xs.length
val end = if (start + length < xs.length) start + length else xs.length
override def hasNext: Boolean = i < end
def next: a =
if (hasNext) { val x = xs(i) ; i += 1 ; x }
else throw new NoSuchElementException("next on empty iterator")
override protected def defaultPeek : a = throw new NoSuchElementException("no lookahead")
override def peekList(sz : Int) : Seq[a] =
override def peekList(sz: Int): Seq[a] =
xs.slice(i, if (i + sz > xs.length) xs.length else i + sz)
}
@ -83,7 +84,7 @@ object Iterator {
def next = { val c = str charAt i; i += 1; c }
override protected def defaultPeek : Char = throw new NoSuchElementException
override def peekList(sz : Int) : Seq[Char] =
override def peekList(sz: Int): Seq[Char] =
str.substring(i, if (i + sz > str.length) str.length else i + sz)
}
@ -114,7 +115,7 @@ object Iterator {
* @param end the end value of the iterator
* @return the iterator with values in range <code>[start;end)</code>.
*/
def range(start: Int, end: Int): Iterator[Int] = range(start, end, 1)
def range(start: Int, end: Int): Iterator[Int] = range(start, end, 1)
/** Create an iterator with elements
* <code>e<sub>n+1</sub> = e<sub>n</sub> + step</code>
@ -127,14 +128,15 @@ object Iterator {
* @param step the increment value of the iterator (must be positive or negative)
* @return the iterator with values in range <code>[start;end)</code>.
*/
def range(start: Int, end: Int, step: Int) = new Iterator[Int] {
private var i = start
def hasNext: Boolean = (step <= 0 || i < end) && (step >= 0 || i > end)
def next(): Int =
if (hasNext) { val j = i; i = i + step; j }
else throw new NoSuchElementException("next on empty iterator")
}
@throws(classOf[NoSuchElementException])
def range(start: Int, end: Int, step: Int) = new Iterator[Int] {
private var i = start
def hasNext: Boolean = (step <= 0 || i < end) && (step >= 0 || i > end)
def next(): Int =
if (hasNext) { val j = i; i += step; j }
else throw new NoSuchElementException("next on empty iterator")
}
/** Create an iterator with elements
* <code>e<sub>n+1</sub> = step(e<sub>n</sub>)</code>
* where <code>e<sub>0</sub> = start</code>
@ -146,6 +148,7 @@ object Iterator {
* @param step the increment function of the iterator, must be monotonically increasing or decreasing
* @return the iterator with values in range <code>[start;end)</code>.
*/
@throws(classOf[NoSuchElementException])
def range(start: Int, end: Int, step: Int => Int) = new Iterator[Int] {
private val up = step(start) > start
private val down = step(start) < start
@ -215,6 +218,7 @@ trait Iterator[+A] {
* @param n the number of elements to take
* @return the new iterator
*/
@throws(classOf[NoSuchElementException])
def take(n: Int) = new Iterator[A] {
var remaining = n
def hasNext = remaining > 0 && Iterator.this.hasNext
@ -271,6 +275,7 @@ trait Iterator[+A] {
* f(a<sub>n</sub>)</code> if this iterator yields the
* elements <code>a<sub>0</sub>, ..., a<sub>n</sub></code>.
*/
@throws(classOf[NoSuchElementException])
def flatMap[B](f: A => Iterator[B]): Iterator[B] = new Iterator[B] {
private var cur: Iterator[B] = Iterator.empty
def hasNext: Boolean =
@ -289,7 +294,7 @@ trait Iterator[+A] {
protected class PredicatedIterator(p : A => Boolean) extends BufferedIterator.Default[A] {
protected def doEnd : Boolean = false
protected override def fill(sz : Int) : Seq[A] = {
protected override def fill(sz: Int): Seq[A] = {
while (true) {
if (!Iterator.this.hasNext) return Nil
val ret = Iterator.this.next
@ -415,7 +420,7 @@ trait Iterator[+A] {
* @return <code>true</code> iff there is an element of this iterator which
* is equal (w.r.t. <code>==</code>) to <code>elem</code>.
*/
def contains(elem: Any): Boolean = exists { x => x == elem }
def contains(elem: Any): Boolean = exists { _ == elem }
/** Find and return the first element of the iterable object satisfying a
* predicate, if any.
@ -491,6 +496,7 @@ trait Iterator[+A] {
* <code>a<sub>0</sub>, a<sub>1</sub>, ..., a<sub>n</sub></code>.
* @throws Predef.UnsupportedOperationException if the iterator is empty.
*/
@throws(classOf[UnsupportedOperationException])
def reduceLeft[B >: A](op: (B, B) => B): B = {
if (hasNext) foldLeft[B](next)(op)
else throw new UnsupportedOperationException("empty.reduceLeft")
@ -506,6 +512,7 @@ trait Iterator[+A] {
* @throws Predef.UnsupportedOperationException if the iterator is empty.
*/
@throws(classOf[UnsupportedOperationException])
def reduceRight[B >: A](op: (B, B) => B): B = {
if (!hasNext) throw new UnsupportedOperationException("empty.reduceRight")
val x = next
@ -573,7 +580,7 @@ trait Iterator[+A] {
* @param start the starting index.
* @pre the array must be large enough to hold all elements.
*/
def copyToArray[B >: A](xs: Array[B], start: Int): Unit = {
def copyToArray[B >: A](xs: Array[B], start: Int) {
var i = start
while (hasNext) {
xs(i) = next
@ -589,25 +596,28 @@ trait Iterator[+A] {
* @param sz the maximum number of elements to be read.
* @pre the array must be large enough to hold <code>sz</code> elements.
*/
def readInto[B >: A](xs: Array[B], start : Int, sz : Int) : Unit = {
def readInto[B >: A](xs: Array[B], start: Int, sz: Int) {
var i = start
while (hasNext && i - start < sz) {
xs(i) = next
i += 1
}
}
}
def readInto[B >: A](xs: Array[B], start: Int) {
readInto(xs, start, xs.length - start)
}
def readInto[B >: A](xs: Array[B]) {
readInto(xs, 0, xs.length)
}
def readInto[B >: A](xs : Array[B], start : Int) : Unit = readInto(xs, start, xs.length - start)
def readInto[B >: A](xs : Array[B]) : Unit = readInto(xs, 0, xs.length)
/** Copy all elements to a buffer
* @param The buffer to which elements are copied
* @return The buffer to which elements are copied
*/
def copyToBuffer[B >: A](dest: Buffer[B]): Unit =
def copyToBuffer[B >: A](dest: Buffer[B]) {
while (hasNext) dest += next
}
/** Transform this iterator into a list of all elements.
*
* @return a list which enumerates all elements of this iterator.
@ -633,10 +643,10 @@ trait Iterator[+A] {
* @return a string representation of this iterable object.
*/
def mkString(start: String, sep: String, end: String): String = {
val buf = new StringBuilder()
val buf = new StringBuilder
addString(buf, start, sep, end).toString
}
/** Returns a string representation of this iterable object. The string
* representations of elements (w.r.t. the method <code>toString()</code>)
* are separated by the string <code>sep</code>.

View File

@ -6,4 +6,5 @@ test check_take was successful
test check_drop was successful
test check_foreach was successful
test check_forall was successful
test check_fromArray failed: expected 0, found 14

View File

@ -12,6 +12,7 @@ object Test {
val it2 = Iterator.from(0, -1)
it1.next + it2.next
}
def check_range: Int = {
val xs1 = Iterator.range(0, 10, 2) toList;
val xs2 = Iterator.range(0, 10, -2) toList;
@ -73,20 +74,30 @@ object Test {
0
}
def check_success[A](name: String, closure: => A, expected: A): Unit = {
Console.print("test " + name)
def check_fromArray: Int = { // ticket #429
val a = List(1, 2, 3, 4).toArray
var xs0 = Iterator.fromArray(a).toList;
var xs1 = Iterator.fromArray(a, 0, 1).toList;
var xs2 = Iterator.fromArray(a, 0, 2).toList;
var xs3 = Iterator.fromArray(a, 0, 3).toList;
var xs4 = Iterator.fromArray(a, 0, 4).toList;
xs0.length + xs1.length + xs2.length + xs3.length + xs4.length
}
def check_success[A](name: String, closure: => A, expected: A) {
print("test " + name)
try {
val actual: A = closure
if (actual == expected)
Console.print(" was successful")
print(" was successful")
else
Console.print(" failed: expected "+ expected +", found "+ actual)
print(" failed: expected "+ expected +", found "+ actual)
}
catch {
case exception: Throwable =>
Console.print(" raised exception " + exception)
print(" raised exception " + exception)
}
Console.println
println()
}
def main(args: Array[String]) {
@ -98,7 +109,8 @@ object Test {
check_success("check_drop", check_drop, 12)
check_success("check_foreach", check_foreach, 190)
check_success("check_forall", check_forall, 0)
Console.println
check_success("check_fromArray",check_fromArray, 0)
println()
}
}