summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSean McDirmid <sean.mcdirmid@gmail.com>2007-12-02 12:55:28 +0000
committerSean McDirmid <sean.mcdirmid@gmail.com>2007-12-02 12:55:28 +0000
commit5d4605b6935ee2c77b15b222201a0aa3643960d8 (patch)
treecf096a01564d96d3cd9f53f9224cfd0c4bd06f81
parent7f8c296130d40769da5d49e17bcaadaaeadac5a4 (diff)
downloadscala-5d4605b6935ee2c77b15b222201a0aa3643960d8.tar.gz
scala-5d4605b6935ee2c77b15b222201a0aa3643960d8.tar.bz2
scala-5d4605b6935ee2c77b15b222201a0aa3643960d8.zip
Fixed bug in to with custom by
-rw-r--r--src/library/scala/Range.scala32
1 files changed, 20 insertions, 12 deletions
diff --git a/src/library/scala/Range.scala b/src/library/scala/Range.scala
index 9a3ab8f597..83dccd32cf 100644
--- a/src/library/scala/Range.scala
+++ b/src/library/scala/Range.scala
@@ -33,18 +33,19 @@ class Range(val start: Int, val end: Int, val step: Int) extends RandomAccessSeq
/** create a new range with the start and end values of this range and a new <code>step</code> */
def by(step : Int) = new Range(start, end, step)
- def length : Int = {
- if (this.step == 0) throw new Predef.UnsupportedOperationException
- if (start < end && this.step < 0) return 0
- if (start > end && this.step > 0) return 0
-
- val base = if (start < end) end - start
- else start - end
- assert(base >= 0)
- val step = if (this.step < 0) -this.step else this.step
- assert(step >= 0)
- base / step + (if (base % step != 0) 1 else 0)
+ lazy val length : Int = {
+ if (start < end && this.step < 0) 0
+ else if (start > end && this.step > 0) 0
+ else {
+ val base = if (start < end) end - start
+ else start - end
+ assert(base >= 0)
+ val step = if (this.step < 0) -this.step else this.step
+ assert(step >= 0)
+ base / step + last(base, step)
+ }
}
+ protected def last(base : Int, step : Int) = (if (base % step != 0) 1 else 0)
def apply(idx : Int) = {
if (idx < 0 || idx >= length) throw new Predef.IndexOutOfBoundsException
start + (step * idx)
@@ -52,5 +53,12 @@ class Range(val start: Int, val end: Int, val step: Int) extends RandomAccessSeq
/** a <code>Seq.contains</code>, not a <code>Iterator.contains</code>! */
def contains(x : Int): Boolean =
x >= start && x < end && (((x - start) % step) == 0)
-
+ def inclusive = new Range.Inclusive(start,end,step)
+}
+object Range {
+ class Inclusive(start : Int, end : Int, by : Int) extends Range(start,end,by) {
+ override protected def last(base : Int, step : Int) = 1
+ override def by(step : Int) = new Inclusive(start, end, step)
+ }
}
+