summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIulian Dragos <jaguarul@gmail.com>2008-05-09 10:40:48 +0000
committerIulian Dragos <jaguarul@gmail.com>2008-05-09 10:40:48 +0000
commitd864fda9a04a4eb42458581d3a32614d76d0eccf (patch)
treea90854916099ab8d5d995cb32b8868c39c2fa6c4
parent914e09a4a3e632e34414b2e0749d96e1dd58e6b8 (diff)
downloadscala-d864fda9a04a4eb42458581d3a32614d76d0eccf.tar.gz
scala-d864fda9a04a4eb42458581d3a32614d76d0eccf.tar.bz2
scala-d864fda9a04a4eb42458581d3a32614d76d0eccf.zip
Fixed #855.
-rw-r--r--src/library/scala/Range.scala20
1 files changed, 17 insertions, 3 deletions
diff --git a/src/library/scala/Range.scala b/src/library/scala/Range.scala
index 61ca53b84c..f25e58fbf4 100644
--- a/src/library/scala/Range.scala
+++ b/src/library/scala/Range.scala
@@ -57,11 +57,18 @@ 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 =
+ def contains(x: Int): Boolean = {
+ inInterval(x) && (((x - start) % step) == 0)
+ }
+
+ /** Is the argument inside the interval defined by `start' and `end'?
+ * Returns true if `x' is inside [start, end).
+ */
+ protected def inInterval(x: Int): Boolean =
if (step > 0)
- x >= start && x < end && (((x - start) % step) == 0)
+ (x >= start && x < end)
else
- x <= start && x > end && (((x - end) % step) == 0)
+ (x <= start && x > end)
def inclusive = new Range.Inclusive(start,end,step)
}
@@ -71,5 +78,12 @@ object Range {
override def apply(idx: Int): Int = super.apply(idx)
override protected def last(base: Int, step: Int): Int = 1
override def by(step: Int): Range = new Inclusive(start, end, step)
+
+ /** Returns true if x is inside the interval [start, end]. */
+ override protected def inInterval(x: Int) =
+ if (step > 0)
+ (x >= start && x <= end)
+ else
+ (x <= start && x >= end)
}
}