diff options
author | Rex Kerr <ichoran@gmail.com> | 2014-01-30 12:28:14 -0800 |
---|---|---|
committer | Rex Kerr <ichoran@gmail.com> | 2014-02-09 08:29:13 -0800 |
commit | 95f21ca8a095767202e1c4d620a865c1647d7e6c (patch) | |
tree | c33429ea26538b29817949ceac9a9ee09bb0c252 /src | |
parent | 08e51dfec50842253afb87cc5ae3c7400dc18ced (diff) | |
download | scala-95f21ca8a095767202e1c4d620a865c1647d7e6c.tar.gz scala-95f21ca8a095767202e1c4d620a865c1647d7e6c.tar.bz2 scala-95f21ca8a095767202e1c4d620a865c1647d7e6c.zip |
SI-6736 Range.contains is wrong
Removed once-used private method that was calculating ranges in error and corrected the contains method (plus improved performance).
Diffstat (limited to 'src')
-rw-r--r-- | src/library/scala/collection/immutable/Range.scala | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/src/library/scala/collection/immutable/Range.scala b/src/library/scala/collection/immutable/Range.scala index 786b18cd21..ba695dfbdc 100644 --- a/src/library/scala/collection/immutable/Range.scala +++ b/src/library/scala/collection/immutable/Range.scala @@ -203,12 +203,6 @@ extends scala.collection.AbstractSeq[Int] } counted } - // Tests whether a number is within the endpoints, without testing - // whether it is a member of the sequence (i.e. when step > 1.) - private def isWithinBoundaries(elem: Int) = !isEmpty && ( - (step > 0 && start <= elem && elem <= last ) || - (step < 0 && last <= elem && elem <= start) - ) // Methods like apply throw exceptions on invalid n, but methods like take/drop // are forgiving: therefore the checks are with the methods. private def locationAfterN(n: Int) = start + (step * n) @@ -256,7 +250,17 @@ extends scala.collection.AbstractSeq[Int] if (isInclusive) this else new Range.Inclusive(start, end, step) - final def contains(x: Int) = isWithinBoundaries(x) && ((x - start) % step == 0) + final def contains(x: Int) = { + if (x==end && !isInclusive) false + else if (step > 0) { + if (x < start || x > end) false + else (step == 1) || (((x - start) % step) == 0) + } + else { + if (x < end || x > start) false + else (step == -1) || (((x - start) % step) == 0) + } + } final override def sum[B >: Int](implicit num: Numeric[B]): Int = { if (num eq scala.math.Numeric.IntIsIntegral) { |