scala/scala — pull request #16 by soc (2011-12-02 14:46:17+00:00)
Title: Makes Range#sum an O(1) operation instead of an O(n) one.

Partially fixes SI-4658. NumericRange stays slow, thanks to the brilliant idea that Numeric doesn't need a division operation.

Some notes:

- I did use `if` instead of `match` because I was not sure if there is a difference in performance especially with the work on the new pattern matcher.
- I used `(len.toLong * (head + last) / 2)` instead of `(len.toLong * (head.toLong + last) / 2)` because I couldn't find a situation in which working with a Long here avoided an overflow error in the result. (There are no inputs where `head + last` would overflow but the result wouldn't.) The first one is more or less "overflow error compatible" to the slow O(n) way, while the second one produces different overflow errors. The only case where it avoided an overflow error was in Ranges with a single element, but the code before catches this, thanks to notnoop spotting it (https://github.com/scala/legacy-svn-scala/pull/83).