diff options
author | michelou <michelou@epfl.ch> | 2007-05-01 15:59:31 +0000 |
---|---|---|
committer | michelou <michelou@epfl.ch> | 2007-05-01 15:59:31 +0000 |
commit | dbce4463e8d0fb7fef571139008c6011215b1e1a (patch) | |
tree | c29e3e91da9a4096c2d625eb307991a8b642f7d8 /src/library | |
parent | d6369095cfc306b6fb40da037735247aca3c9813 (diff) | |
download | scala-dbce4463e8d0fb7fef571139008c6011215b1e1a.tar.gz scala-dbce4463e8d0fb7fef571139008c6011215b1e1a.tar.bz2 scala-dbce4463e8d0fb7fef571139008c6011215b1e1a.zip |
added scala.Range and test cases
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/scala/BufferedIterator.scala | 4 | ||||
-rw-r--r-- | src/library/scala/Iterator.scala | 87 | ||||
-rw-r--r-- | src/library/scala/Range.scala | 39 | ||||
-rw-r--r-- | src/library/scala/runtime/RichInt.scala | 2 |
4 files changed, 78 insertions, 54 deletions
diff --git a/src/library/scala/BufferedIterator.scala b/src/library/scala/BufferedIterator.scala index 67d5016193..38d81eb513 100644 --- a/src/library/scala/BufferedIterator.scala +++ b/src/library/scala/BufferedIterator.scala @@ -1,7 +1,7 @@ /* __ *\ ** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2003-2006, LAMP/EPFL ** -** __\ \/ /__/ __ |/ /__/ __ | ** +** / __/ __// _ | / / / _ | (c) 2003-2007, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** ** /____/\___/_/ |_/____/_/ | | ** ** |/ ** \* */ diff --git a/src/library/scala/Iterator.scala b/src/library/scala/Iterator.scala index e813c6ade2..b4936d1a76 100644 --- a/src/library/scala/Iterator.scala +++ b/src/library/scala/Iterator.scala @@ -14,7 +14,6 @@ package scala import Predef._ import collection.mutable.{Buffer, ListBuffer} -import compat.StringBuilder /** The <code>Iterator</code> object provides various functions for * creating specialized iterators. @@ -102,54 +101,42 @@ object Iterator { /** Create an iterator with elements * <code>e<sub>n+1</sub> = e<sub>n</sub> + 1</code> - * where <code>e<sub>0</sub> = lo</code> + * where <code>e<sub>0</sub> = start</code> * and <code>e<sub>i</sub> < end</code>. * - * @param lo the start value of the iterator - * @param end the end value of the iterator - * @return the iterator with values in range <code>[lo;end)</code>. + * @param start the start value of the iterator + * @param end the end value of the iterator + * @return the iterator with values in range <code>[start;end)</code>. */ - def range(lo: Int, end: Int): Iterator[Int] = - range(lo, end, 1) + def range(start: Int, end: Int): Range = range(start, end, 1) /** Create an iterator with elements * <code>e<sub>n+1</sub> = e<sub>n</sub> + step</code> - * where <code>e<sub>0</sub> = lo</code> + * where <code>e<sub>0</sub> = start</code> * and <code>e<sub>i</sub> < end</code>. * - * @param lo the start value of the iterator - * @param end the end value of the iterator - * @param step the increment value of the iterator (must be positive or negative) - * @return the iterator with values in range <code>[lo;end)</code>. + * @param start the start value of the iterator + * @param end the end value of the 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(lo: Int, end: Int, step: Int): Iterator[Int] = { - assert(step != 0) - new BufferedIterator[Int] { - private var i = lo - def hasNext: Boolean = if (step > 0) i < end else i > end - def next(): Int = - if (hasNext) { val j = i; i += step; j } - else throw new NoSuchElementException("next on empty iterator") - def head: Int = - if (hasNext) i - else throw new NoSuchElementException("head on empty iterator") - } - } + def range(start: Int, end: Int, step: Int): Range = + new Range(start, end, step) /** Create an iterator with elements * <code>e<sub>n+1</sub> = step(e<sub>n</sub>)</code> - * where <code>e<sub>0</sub> = lo</code> + * where <code>e<sub>0</sub> = start</code> * and <code>e<sub>i</sub> < end</code>. * - * @param lo the start value of the iterator - * @param end the end value of the iterator - * @param step the increment function of the iterator - * @return the iterator with values in range <code>[lo;end)</code>. + * @param start the start value of the iterator + * @param end the end value of the iterator + * @param step the increment function of the iterator + * @return the iterator with values in range <code>[start;end)</code>. */ - def range(lo: Int, end: Int, step: Int => Int): Iterator[Int] = + def range(start: Int, end: Int, step: Int => Int): Iterator[Int] = new BufferedIterator[Int] { - private var i = lo - def hasNext: Boolean = i < end + private var i = start + def hasNext: Boolean = i < end def next(): Int = if (i < end) { val j = i; i = step(i); j } else throw new NoSuchElementException("next on empty iterator") @@ -160,25 +147,25 @@ object Iterator { /** Create an iterator with elements * <code>e<sub>n+1</sub> = e<sub>n</sub> + 1</code> - * where <code>e<sub>0</sub> = lo</code>. + * where <code>e<sub>0</sub> = start</code>. * - * @param lo the start value of the iterator - * @return the iterator starting at value <code>lo</code>. + * @param start the start value of the iterator + * @return the iterator starting at value <code>start</code>. */ - def from(lo: Int): Iterator[Int] = - from(lo, 1) + def from(start: Int): Iterator[Int] = + from(start, 1) /** Create an iterator with elements * <code>e<sub>n+1</sub> = e<sub>n</sub> + step</code> - * where <code>e<sub>0</sub> = lo</code>. + * where <code>e<sub>0</sub> = start</code>. * - * @param lo the start value of the iterator - * @param step the increment value of the iterator - * @return the iterator starting at value <code>lo</code>. + * @param start the start value of the iterator + * @param step the increment value of the iterator + * @return the iterator starting at value <code>start</code>. */ - def from(lo: Int, step: Int): Iterator[Int] = + def from(start: Int, step: Int): Iterator[Int] = new BufferedIterator[Int] { - private var i = lo + private var i = start def hasNext: Boolean = true def next(): Int = { val j = i; i += step; j } def head: Int = i @@ -186,15 +173,15 @@ object Iterator { /** Create an iterator with elements * <code>e<sub>n+1</sub> = step(e<sub>n</sub>)</code> - * where <code>e<sub>0</sub> = lo</code>. + * where <code>e<sub>0</sub> = start</code>. * - * @param lo the start value of the iterator - * @param step the increment function of the iterator - * @return the iterator starting at value <code>lo</code>. + * @param start the start value of the iterator + * @param step the increment function of the iterator + * @return the iterator starting at value <code>start</code>. */ - def from(lo: Int, step: Int => Int): Iterator[Int] = + def from(start: Int, step: Int => Int): Iterator[Int] = new BufferedIterator[Int] { - private var i = lo + private var i = start def hasNext: Boolean = true def next(): Int = { val j = i; i = step(i); j } def head: Int = i diff --git a/src/library/scala/Range.scala b/src/library/scala/Range.scala new file mode 100644 index 0000000000..8e3fcce9ca --- /dev/null +++ b/src/library/scala/Range.scala @@ -0,0 +1,39 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2006-2007, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id: $ + + +package scala + +/** The <code>Range</code> class represents integer values in range + * <code>[start;end)</code> with . + * + * @author Stephane Micheloud + * @version 1.0, 01/05/2007 + */ +class Range(val start: Int, val end: Int, val step: Int) extends BufferedIterator[Int] { + assert(step != 0) + private var i = start + + def hasNext: Boolean = if (step > 0) i < end else i > end + + def next(): Int = + if (hasNext) { val j = i; i += step; j } + else throw new NoSuchElementException("next on empty iterator") + + def head: Int = + if (hasNext) i + else throw new NoSuchElementException("head on empty iterator") + + def length: Int = + 1 + (Math.abs(start - end) - 1) / Math.abs(step) + + def contains(x: Int): Boolean = + Iterator.range(0, length) exists { x == start + _ * step } +} diff --git a/src/library/scala/runtime/RichInt.scala b/src/library/scala/runtime/RichInt.scala index 60264226c3..dfa47dbedc 100644 --- a/src/library/scala/runtime/RichInt.scala +++ b/src/library/scala/runtime/RichInt.scala @@ -18,8 +18,6 @@ final class RichInt(x: Int) extends Proxy with Ordered[Int] { // Ordered[Int] def compare (y: Int): Int = if (x < y) -1 else if (x > y) 1 else 0 - def upto(y: Int): List[Int] = List.range(x, y + 1) - def until(y: Int): Iterator[Int] = Iterator.range(x, y) def to(y: Int): Iterator[Int] = Iterator.range(x, y + 1) |