diff options
author | Iulian Dragos <jaguarul@gmail.com> | 2010-08-23 08:52:03 +0000 |
---|---|---|
committer | Iulian Dragos <jaguarul@gmail.com> | 2010-08-23 08:52:03 +0000 |
commit | b64d1956019b61244981562a088c9d2b633e61fc (patch) | |
tree | d826d4f2c2a18a3e0343a3fa270120a49c43d5dd /src | |
parent | 0cab741d082842175808d988d6a48b9a9ffeedaf (diff) | |
download | scala-b64d1956019b61244981562a088c9d2b633e61fc.tar.gz scala-b64d1956019b61244981562a088c9d2b633e61fc.tar.bz2 scala-b64d1956019b61244981562a088c9d2b633e61fc.zip |
Made ranges a bit more friendly to the optimizer.
Diffstat (limited to 'src')
-rw-r--r-- | src/library/scala/collection/immutable/Range.scala | 24 | ||||
-rw-r--r-- | src/library/scala/runtime/RichInt.scala | 4 | ||||
-rw-r--r-- | src/library/scala/runtime/RichLong.scala | 2 |
3 files changed, 9 insertions, 21 deletions
diff --git a/src/library/scala/collection/immutable/Range.scala b/src/library/scala/collection/immutable/Range.scala index 68b50fd09f..ba31ace05c 100644 --- a/src/library/scala/collection/immutable/Range.scala +++ b/src/library/scala/collection/immutable/Range.scala @@ -51,7 +51,7 @@ class Range(val start: Int, val end: Int, val step: Int) extends IndexedSeq[Int] def isInclusive = false - override def foreach[@specialized(Unit) U](f: Int => U) { + @inline final override def foreach[@specialized(Unit) U](f: Int => U) { if (fullLength > 0) { val last = this.last var i = start @@ -73,7 +73,7 @@ class Range(val start: Int, val end: Int, val step: Int) extends IndexedSeq[Int] def length: Int = fullLength.toInt - protected def fullLength: Long = if (end > start == step > 0 && start != end) + def fullLength: Long = if (end > start == step > 0 && start != end) ((last.toLong - start.toLong) / step.toLong + 1) else 0 @@ -249,7 +249,7 @@ object Range { end else ((end.toLong - start.toLong) / step.toLong * step.toLong + start.toLong).toInt - protected override def fullLength: Long = if (end > start == step > 0 || start == end) + override def fullLength: Long = if (end > start == step > 0 || start == end) ((last.toLong - start.toLong) / step.toLong + 1) else 0 @@ -262,7 +262,7 @@ object Range { /** Make an range from `start` to `end` inclusive with step value 1. */ - def apply(start: Int, end: Int): Range with ByOne = new Range(start, end, 1) with ByOne + def apply(start: Int, end: Int): Range = new Range(start, end, 1) /** Make an inclusive range from start to end with given step value. * @note step != 0 @@ -271,21 +271,7 @@ object Range { /** Make an inclusive range from start to end with step value 1. */ - def inclusive(start: Int, end: Int): Range.Inclusive with ByOne = new Inclusive(start, end, 1) with ByOne - - trait ByOne extends Range { - override final def foreach[@specialized(Unit) U](f: Int => U) { - if (length > 0) { - val last = this.last - var i = start - while (i != last) { - f(i) - i += 1 - } - f(i) - } - } - } + def inclusive(start: Int, end: Int): Range.Inclusive = new Inclusive(start, end, 1) // BigInt and Long are straightforward generic ranges. object BigInt { diff --git a/src/library/scala/runtime/RichInt.scala b/src/library/scala/runtime/RichInt.scala index 30fc010d17..c5ed5df07c 100644 --- a/src/library/scala/runtime/RichInt.scala +++ b/src/library/scala/runtime/RichInt.scala @@ -21,11 +21,11 @@ final class RichInt(val 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 - def until(end: Int): Range with Range.ByOne = Range(start, end) + def until(end: Int): Range = Range(start, end) def until(end: Int, step: Int): Range = Range(start, end, step) /** like <code>until</code>, but includes the last index */ - def to(end: Int): Range.Inclusive with Range.ByOne = Range.inclusive(start, end) + def to(end: Int): Range.Inclusive = Range.inclusive(start, end) def to(end: Int, step: Int): Range.Inclusive = Range.inclusive(start, end, step) def min(that: Int): Int = if (start < that) start else that diff --git a/src/library/scala/runtime/RichLong.scala b/src/library/scala/runtime/RichLong.scala index bf573abc62..82e2c77ef7 100644 --- a/src/library/scala/runtime/RichLong.scala +++ b/src/library/scala/runtime/RichLong.scala @@ -29,6 +29,8 @@ final class RichLong(x: Long) extends Proxy with Ordered[Long] { */ def until(end: Long, step: Long = 1L): NumericRange.Exclusive[Long] = Range.Long(x, end, step) + def until(end: Long): NumericRange.Exclusive[Long] = Range.Long(x, end, 1l) + /** Like until, but inclusive of the end value. */ def to(end: Long, step: Long = 1L): NumericRange.Inclusive[Long] = Range.Long.inclusive(x, end, step) |