diff options
author | Simon Ochsenreither <simon@ochsenreither.de> | 2012-03-27 20:08:30 +0200 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2012-04-06 11:57:41 -0700 |
commit | e593d8b9ed6d8d98c698d0a7dc47c3341d59c357 (patch) | |
tree | fedd5df5a4592d50c46944dc12a8e3496df78c84 /src | |
parent | 41c0b0b7b9bd5089e35e1bf32fbcb471a9c78641 (diff) | |
download | scala-e593d8b9ed6d8d98c698d0a7dc47c3341d59c357.tar.gz scala-e593d8b9ed6d8d98c698d0a7dc47c3341d59c357.tar.bz2 scala-e593d8b9ed6d8d98c698d0a7dc47c3341d59c357.zip |
Make NumericRange# O(1) instead of O(n).
It makes me a bit nervous that NumericRange[Int] will
get different wrong values in overflow situations compared
to Range due to the missing toLong though.
It could probably need some investigation if reordering the
operations can rule out wrong values, e. g. only fail when
the fold also fails.
Apart from that, it might make sense to just throw an exception
if an overflow happens instead of silent overflow.
Diffstat (limited to 'src')
-rw-r--r-- | src/library/scala/collection/immutable/NumericRange.scala | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/src/library/scala/collection/immutable/NumericRange.scala b/src/library/scala/collection/immutable/NumericRange.scala index 65bd9ab6f2..0966fa035f 100644 --- a/src/library/scala/collection/immutable/NumericRange.scala +++ b/src/library/scala/collection/immutable/NumericRange.scala @@ -172,6 +172,13 @@ extends AbstractSeq[T] with IndexedSeq[T] with Serializable { try containsTyped(x.asInstanceOf[T]) catch { case _: ClassCastException => false } + final override def sum[B >: T](implicit num: Numeric[B]): B = { + import num.Ops + if (isEmpty) this.num fromInt 0 + else if (numRangeElements == 1) head + else ((this.num fromInt numRangeElements) * (head + last) / (this.num fromInt 2)) + } + override lazy val hashCode = super.hashCode() override def equals(other: Any) = other match { case x: NumericRange[_] => |