diff options
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/scala/AnyVal.scala | 2 | ||||
-rw-r--r-- | src/library/scala/App.scala | 12 | ||||
-rw-r--r-- | src/library/scala/collection/GenSeqLike.scala | 2 | ||||
-rw-r--r-- | src/library/scala/collection/immutable/NumericRange.scala | 33 | ||||
-rw-r--r-- | src/library/scala/collection/immutable/Range.scala | 21 | ||||
-rw-r--r-- | src/library/scala/math/Numeric.scala | 4 | ||||
-rw-r--r-- | src/library/scala/runtime/AbstractPartialFunction.scala | 12 | ||||
-rw-r--r-- | src/library/scala/runtime/MethodCache.scala | 8 | ||||
-rw-r--r-- | src/library/scala/util/Sorting.scala | 4 |
9 files changed, 66 insertions, 32 deletions
diff --git a/src/library/scala/AnyVal.scala b/src/library/scala/AnyVal.scala index 9def6cb054..ff62948413 100644 --- a/src/library/scala/AnyVal.scala +++ b/src/library/scala/AnyVal.scala @@ -33,7 +33,7 @@ package scala * * User-defined value classes which avoid object allocation... * - * - must have a single, public `val` parameter that is the underlying runtime representation. + * - must have a single `val` parameter that is the underlying runtime representation. * - can define `def`s, but no `val`s, `var`s, or nested `traits`s, `class`es or `object`s. * - typically extend no other trait apart from `AnyVal`. * - cannot be used in type tests or pattern matching. diff --git a/src/library/scala/App.scala b/src/library/scala/App.scala index 90a8977e81..ef39ee2134 100644 --- a/src/library/scala/App.scala +++ b/src/library/scala/App.scala @@ -28,9 +28,8 @@ import scala.collection.mutable.ListBuffer * functionality, which means that fields of the object will not have been initialized * before the main method has been executed.''''' * - * It should also be noted that the `main` method will not normally need to be overridden: - * the purpose is to turn the whole class body into the “main method”. You should only - * chose to override it if you know what you are doing. + * It should also be noted that the `main` method should not be overridden: + * the whole class body becomes the “main method”. * * @author Martin Odersky * @version 2.1, 15/02/2011 @@ -61,11 +60,12 @@ trait App extends DelayedInit { } /** The main method. - * This stores all argument so that they can be retrieved with `args` - * and the executes all initialization code segments in the order they were - * passed to `delayedInit` + * This stores all arguments so that they can be retrieved with `args` + * and then executes all initialization code segments in the order in which + * they were passed to `delayedInit`. * @param args the arguments passed to the main method */ + @deprecatedOverriding("main should not be overridden", "2.11.0") def main(args: Array[String]) = { this._args = args for (proc <- initCode) proc() diff --git a/src/library/scala/collection/GenSeqLike.scala b/src/library/scala/collection/GenSeqLike.scala index 63295fec10..c3bad60072 100644 --- a/src/library/scala/collection/GenSeqLike.scala +++ b/src/library/scala/collection/GenSeqLike.scala @@ -190,7 +190,7 @@ trait GenSeqLike[+A, +Repr] extends Any with GenIterableLike[A, Repr] with Equal */ def lastIndexWhere(p: A => Boolean, end: Int): Int - /** Returns new $coll wih elements in reversed order. + /** Returns new $coll with elements in reversed order. * * $willNotTerminateInf * diff --git a/src/library/scala/collection/immutable/NumericRange.scala b/src/library/scala/collection/immutable/NumericRange.scala index 486c2b6c8f..249d76584d 100644 --- a/src/library/scala/collection/immutable/NumericRange.scala +++ b/src/library/scala/collection/immutable/NumericRange.scala @@ -175,9 +175,36 @@ extends AbstractSeq[T] with IndexedSeq[T] with Serializable { catch { case _: ClassCastException => false } final override def sum[B >: T](implicit num: Numeric[B]): B = { - if (isEmpty) this.num fromInt 0 - else if (numRangeElements == 1) head - else ((this.num fromInt numRangeElements) * (head + last) / (this.num fromInt 2)) + // arithmetic series formula can be used for regular addition + if ((num eq scala.math.Numeric.IntIsIntegral)|| + (num eq scala.math.Numeric.BigIntIsIntegral)|| + (num eq scala.math.Numeric.ShortIsIntegral)|| + (num eq scala.math.Numeric.ByteIsIntegral)|| + (num eq scala.math.Numeric.CharIsIntegral)|| + (num eq scala.math.Numeric.LongIsIntegral)|| + (num eq scala.math.Numeric.FloatAsIfIntegral)|| + (num eq scala.math.Numeric.BigDecimalIsFractional)|| + (num eq scala.math.Numeric.DoubleAsIfIntegral)) { + val numAsIntegral = num.asInstanceOf[Integral[B]] + import numAsIntegral._ + if (isEmpty) num fromInt 0 + else if (numRangeElements == 1) head + else ((num fromInt numRangeElements) * (head + last) / (num fromInt 2)) + } else { + // user provided custom Numeric, we cannot rely on arithmetic series formula + if (isEmpty) num.zero + else { + var acc = num.zero + var i = head + var idx = 0 + while(idx < length) { + acc = num.plus(acc, i) + i = i + step + idx = idx + 1 + } + acc + } + } } override lazy val hashCode = super.hashCode() diff --git a/src/library/scala/collection/immutable/Range.scala b/src/library/scala/collection/immutable/Range.scala index 00f398a4b0..786b18cd21 100644 --- a/src/library/scala/collection/immutable/Range.scala +++ b/src/library/scala/collection/immutable/Range.scala @@ -259,9 +259,24 @@ extends scala.collection.AbstractSeq[Int] final def contains(x: Int) = isWithinBoundaries(x) && ((x - start) % step == 0) final override def sum[B >: Int](implicit num: Numeric[B]): Int = { - if (isEmpty) 0 - else if (numRangeElements == 1) head - else (numRangeElements.toLong * (head + last) / 2).toInt + if (num eq scala.math.Numeric.IntIsIntegral) { + // this is normal integer range with usual addition. arithmetic series formula can be used + if (isEmpty) 0 + else if (numRangeElements == 1) head + else (numRangeElements.toLong * (head + last) / 2).toInt + } else { + // user provided custom Numeric, we cannot rely on arithmetic series formula + if (isEmpty) num.toInt(num.zero) + else { + var acc = num.zero + var i = head + while(i != terminalElement) { + acc = num.plus(acc, i) + i = i + step + } + num.toInt(acc) + } + } } override def toIterable = this diff --git a/src/library/scala/math/Numeric.scala b/src/library/scala/math/Numeric.scala index e6644c0dfc..eafbf96993 100644 --- a/src/library/scala/math/Numeric.scala +++ b/src/library/scala/math/Numeric.scala @@ -127,6 +127,8 @@ object Numeric { def toLong(x: Float): Long = x.toLong def toFloat(x: Float): Float = x def toDouble(x: Float): Double = x.toDouble + // logic in Numeric base trait mishandles abs(-0.0f) + override def abs(x: Float): Float = math.abs(x) } trait FloatIsFractional extends FloatIsConflicted with Fractional[Float] { def div(x: Float, y: Float): Float = x / y @@ -149,6 +151,8 @@ object Numeric { def toLong(x: Double): Long = x.toLong def toFloat(x: Double): Float = x.toFloat def toDouble(x: Double): Double = x + // logic in Numeric base trait mishandles abs(-0.0) + override def abs(x: Double): Double = math.abs(x) } trait DoubleIsFractional extends DoubleIsConflicted with Fractional[Double] { def div(x: Double, y: Double): Double = x / y diff --git a/src/library/scala/runtime/AbstractPartialFunction.scala b/src/library/scala/runtime/AbstractPartialFunction.scala index 7129f22f60..986cd0390f 100644 --- a/src/library/scala/runtime/AbstractPartialFunction.scala +++ b/src/library/scala/runtime/AbstractPartialFunction.scala @@ -35,15 +35,3 @@ abstract class AbstractPartialFunction[@specialized(scala.Int, scala.Long, scala // let's not make it final so as not to confuse anyone /*final*/ def apply(x: T1): R = applyOrElse(x, PartialFunction.empty) } - -// Manual stand-ins for formerly specialized variations. -// Not comprehensive, only sufficent to run scala-check built scala 2.11.0-M5 -// TODO Scala 2.10.0.M6 Remove this once scalacheck is published against M6. -private[runtime] abstract class AbstractPartialFunction$mcIL$sp extends scala.runtime.AbstractPartialFunction[Any, Int] { - override def apply(x: Any): Int = apply$mcIL$sp(x) - def apply$mcIL$sp(x: Any): Int = applyOrElse(x, PartialFunction.empty) -} -private[runtime] abstract class AbstractPartialFunction$mcFL$sp extends scala.runtime.AbstractPartialFunction[Any, Float] { - override def apply(x: Any): Float = apply$mcIL$sp(x) - def apply$mcIL$sp(x: Any): Float = applyOrElse(x, PartialFunction.empty) -} diff --git a/src/library/scala/runtime/MethodCache.scala b/src/library/scala/runtime/MethodCache.scala index bbf80593db..2d5f832e1f 100644 --- a/src/library/scala/runtime/MethodCache.scala +++ b/src/library/scala/runtime/MethodCache.scala @@ -22,7 +22,7 @@ import scala.annotation.tailrec * generated per call point, and will uniquely relate to the method called * at that point, making the method name and argument types irrelevant. */ /* TODO: if performance is acceptable, PolyMethodCache should be made generic on the method type */ -sealed abstract class MethodCache { +private[scala] sealed abstract class MethodCache { /** Searches for a cached method in the `MethodCache` chain that * is compatible with receiver class `forReceiver`. If none is cached, * `null` is returned. If `null` is returned, find's caller should look- @@ -32,7 +32,7 @@ sealed abstract class MethodCache { def add(forReceiver: JClass[_], forMethod: JMethod): MethodCache } -final class EmptyMethodCache extends MethodCache { +private[scala] final class EmptyMethodCache extends MethodCache { def find(forReceiver: JClass[_]): JMethod = null @@ -41,7 +41,7 @@ final class EmptyMethodCache extends MethodCache { } -final class MegaMethodCache( +private[scala] final class MegaMethodCache( private[this] val forName: String, private[this] val forParameterTypes: Array[JClass[_]] ) extends MethodCache { @@ -53,7 +53,7 @@ final class MegaMethodCache( } -final class PolyMethodCache( +private[scala] final class PolyMethodCache( private[this] val next: MethodCache, private[this] val receiver: JClass[_], private[this] val method: JMethod, diff --git a/src/library/scala/util/Sorting.scala b/src/library/scala/util/Sorting.scala index 276e157f55..2e021ad9d9 100644 --- a/src/library/scala/util/Sorting.scala +++ b/src/library/scala/util/Sorting.scala @@ -141,14 +141,14 @@ object Sorting { var done = false while (!done) { while (b <= c && x(b) <= v) { - if (x(b) == v) { + if (x(b) equiv v) { swap(a, b) a += 1 } b += 1 } while (c >= b && x(c) >= v) { - if (x(c) == v) { + if (x(c) equiv v) { swap(c, d) d -= 1 } |