diff options
author | Sean McDirmid <sean.mcdirmid@gmail.com> | 2007-05-22 13:18:17 +0000 |
---|---|---|
committer | Sean McDirmid <sean.mcdirmid@gmail.com> | 2007-05-22 13:18:17 +0000 |
commit | c4df28255a9af5352ec081cdb88c805c05f567bc (patch) | |
tree | b3358f6c982a4a26bb4d9603269709f9320bd603 | |
parent | d13cbc73c321c7f677015920a378eabf825505f2 (diff) | |
download | scala-c4df28255a9af5352ec081cdb88c805c05f567bc.tar.gz scala-c4df28255a9af5352ec081cdb88c805c05f567bc.tar.bz2 scala-c4df28255a9af5352ec081cdb88c805c05f567bc.zip |
Reverted Range, documented unusual behavior.
-rw-r--r-- | src/library/scala/Iterator.scala | 19 | ||||
-rw-r--r-- | src/library/scala/Range.scala | 36 | ||||
-rw-r--r-- | src/library/scala/runtime/RichInt.scala | 21 |
3 files changed, 40 insertions, 36 deletions
diff --git a/src/library/scala/Iterator.scala b/src/library/scala/Iterator.scala index 5486b93518..f019357982 100644 --- a/src/library/scala/Iterator.scala +++ b/src/library/scala/Iterator.scala @@ -104,31 +104,32 @@ object Iterator { def fromCaseClass(n: Product) = fromProduct(n) /** Create an iterator with elements - * <code>e<sub>n+1</sub> = e<sub>n</sub> +/- 1</code> + * <code>e<sub>n+1</sub> = e<sub>n</sub> + 1</code> * where <code>e<sub>0</sub> = start</code> - * and <code>e<sub>i</sub> < end</code>. + * and <code>e<sub>i</sub> < end</code>. However, + * if start > end, then it will return an empty trange. * - * @deprecated use <code>Int.until</code> instead. * @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>. */ - @deprecated def range(start: Int, end: Int): Range = - if (start < end) range(start, end, 1) - else range(start, 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> = start</code> - * and <code>e<sub>i</sub> < end</code>. + * and <code>e<sub>i</sub> < end</code>. Will return an empty range + * for nonsensical range/step arguments. * - * @deprecated use <code>Int.until</code> instead. * @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>. */ - @deprecated def range(start: Int, end: Int, step: Int): Range = start.until(end, step) + def range(start: Int, end: Int, step: Int): Range = + if (start >= end && step > 0) new Range(0, 0, 1) + else if (start <= end && step < 0) new Range(0, 0, 1) + else new Range(start, end, step) /** Create an iterator with elements * <code>e<sub>n+1</sub> = step(e<sub>n</sub>)</code> diff --git a/src/library/scala/Range.scala b/src/library/scala/Range.scala index 6c85231915..2fdef9a085 100644 --- a/src/library/scala/Range.scala +++ b/src/library/scala/Range.scala @@ -16,6 +16,7 @@ import Predef._ /** <p> * The <code>Range</code> class represents integer values in range * <code>[start;end)</code> with non-zero step value <code>step</code>. + * Sort of acts like a sequence also (supports length and contains). * For example: * </p><pre> * <b>val</b> r1 = Iterator.range(0, 10) @@ -26,11 +27,26 @@ import Predef._ * @author Stephane Micheloud * @version 1.0, 01/05/2007 */ -class Range(val start: Int, val end: Int, val step: Int) extends RandomAccessSeq.Projection[Int] { +class Range(val start: Int, val end: Int, val step: Int) extends BufferedIterator[Int] { assert(step != 0) assert(if (step > 0) end >= start else end <= start) + private var jdx = 0 - override def length = { + override def peekList(sz : Int) : Seq[Int] = return new RandomAccessSeq.Projection[Int] { + def length = Range.this.length - jdx + def apply(idx : Int) = Range.this.apply(jdx + idx) + } + override def hasNext = jdx < length + override def next = { + val ret = apply(jdx) + jdx = jdx + 1 + ret + } + def seqOfRange = new RandomAccessSeq.Projection[Int] { + def length = Range.this.length + def apply(idx : Int) = Range.this.apply(idx) + } + def length = { val base = if (start < end) end - start else start - end assert(base >= 0) @@ -38,18 +54,14 @@ class Range(val start: Int, val end: Int, val step: Int) extends RandomAccessSeq assert(step >= 0) base / step + (if (base % step != 0) 1 else 0) } - - override def apply(idx : Int) = { + def apply(idx : Int) = { if (idx < 0 || idx >= length) throw new Predef.IndexOutOfBoundsException start + (step * idx) } - override protected def stringPrefix = "Range" - - def contains(x : Int): Boolean = { + /** a <code>Seq.contains</code>, not a <code>Iterator.contains</code>! */ + def contains(x : Int): Boolean = x >= start && x < end && (((x - start) % step) == 0) - } - override def contains(elem: Any): Boolean = elem match { - case elem : Int => contains(elem) - case _ => false - } + + /** a <code>Iterator.contains</code>, not a <code>Seq.contains</code>! */ + override def contains(elem: Any): Boolean = super.contains(elem) } diff --git a/src/library/scala/runtime/RichInt.scala b/src/library/scala/runtime/RichInt.scala index a3e39a4346..31e3778a96 100644 --- a/src/library/scala/runtime/RichInt.scala +++ b/src/library/scala/runtime/RichInt.scala @@ -18,21 +18,12 @@ final class RichInt(start: Int) extends Proxy with Ordered[Int] { // Ordered[Int] def compare (that: Int): Int = if (start < that) -1 else if (start > that) 1 else 0 - /** Creates an iteration of integers from <code>this</code> until <code>end</code>, - incrementing or decrementing by 1 as appropriate. */ - def until(end: Int): Range = until(end, +1) - def until(end : Int, step : Int) : Range = { - if (start <= end && step > 0) new Range(start, end, step) - else if (start >= end && step < 0) new Range(start, end, step) - else if (start >= end && step > 0) new Range(start, end, -step) - else throw new Predef.IllegalArgumentException("" + start + " until " + end + " with step " + step) - } - - def to(end: Int): Range = { - if (start == end) until(start) - else if (start < end) until(end + 1) - else until(end - 1) - } + /** See <code>Iterator.range</code>. */ + def until(end: Int): Range = Iterator.range(start, end) + /** See <code>Iterator.range</code>. */ + def until(end : Int, step : Int) : Range = Iterator.range(start, end, step) + /** like <code>until</code>, but includes the last index */ + def to(end : Int) = until(end + 1) def min(that: Int): Int = if (start < that) start else that def max(that: Int): Int = if (start > that) start else that |