diff --git a/src/library/scala/Iterator.scala b/src/library/scala/Iterator.scala index de7b5b2be..44e271665 100644 --- a/src/library/scala/Iterator.scala +++ b/src/library/scala/Iterator.scala @@ -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 [start;end). */ - 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 * en+1 = en + step @@ -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 [start;end). */ - 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 * en+1 = step(en) * where e0 = start @@ -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 [start;end). */ + @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(an) if this iterator yields the * elements a0, ..., an. */ + @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 true iff there is an element of this iterator which * is equal (w.r.t. ==) to elem. */ - 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] { * a0, a1, ..., an. * @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 sz 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 toString()) * are separated by the string sep. diff --git a/test/files/run/iterators.check b/test/files/run/iterators.check index b55b63e9d..5129510e6 100644 --- a/test/files/run/iterators.check +++ b/test/files/run/iterators.check @@ -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 diff --git a/test/files/run/iterators.scala b/test/files/run/iterators.scala index 797241805..026c48f51 100644 --- a/test/files/run/iterators.scala +++ b/test/files/run/iterators.scala @@ -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() } }