Skip to content

Commit

Permalink
Merge pull request #982 from cquiroz/scalafmt
Browse files Browse the repository at this point in the history
scalafmt
  • Loading branch information
larsrh authored Feb 3, 2021
2 parents 1e6fa56 + f7b7e4a commit 3942ef9
Show file tree
Hide file tree
Showing 280 changed files with 6,403 additions and 5,645 deletions.
13 changes: 11 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,14 @@ jobs:
- name: Check that workflows are up to date
run: sbt ++${{ matrix.scala }} githubWorkflowCheck

- name: Build project
run: sbt ++${{ matrix.scala }} test
- name: Check formatting
run: sbt ++${{ matrix.scala }} scalafmtCheckAll scalafmtSbtCheck

- name: Compile
run: 'sbt ++${{ matrix.scala }} test:compile'

- name: Run tests
run: sbt ++${{ matrix.scala }} test

- name: Build docs
run: sbt ++${{ matrix.scala }} doc
13 changes: 13 additions & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version=2.7.5
align.openParenCallSite = true
align.openParenDefnSite = true
maxColumn = 120
continuationIndent.defnSite = 2
assumeStandardLibraryStripMargin = true
danglingParentheses = true
rewrite.rules = [AvoidInfix, SortImports, RedundantParens, SortModifiers]
docstrings = JavaDoc
newlines.afterCurlyLambda = preserve
docstrings.style = Asterisk
docstrings.oneline = unfold
project.excludeFilters = [ "core/src/main/scala-3.x" ]
12 changes: 6 additions & 6 deletions benchmark/src/main/scala/spire/benchmark/AddBenchmarks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ import spire.math._
@OutputTimeUnit(TimeUnit.MICROSECONDS)
class AddBenchmarks {

def addGeneric[@sp(Int, Long, Float, Double) A:Ring](data:Array[A]):A = {
def addGeneric[@sp(Int, Long, Float, Double) A: Ring](data: Array[A]): A = {
var total = Ring[A].zero
var i = 0
val len = data.length
while (i < len) { total = Ring[A].plus(total, data(i)); i += 1 }
total
}

def addFastComplex(data: Array[Long]):Long = {
var total = FastComplex(0.0F, 0.0F)
def addFastComplex(data: Array[Long]): Long = {
var total = FastComplex(0.0f, 0.0f)
var i = 0
val len = data.length
while (i < len) { total = FastComplex.add(total, data(i)); i += 1 }
Expand Down Expand Up @@ -56,7 +56,7 @@ class AddBenchmarks {
@Benchmark
def addFloatDirect(state: FloatState): Float = {
val data = state.values
var total = 0.0F
var total = 0.0f
var i = 0
val len = data.length
while (i < len) { total += data(i); i += 1 }
Expand Down Expand Up @@ -93,7 +93,7 @@ class AddBenchmarks {
@Benchmark
def addFastComplexDirect(state: FastComplexState): Long = {
val data = state.values
var total = FastComplex(0.0F, 0.0F)
var total = FastComplex(0.0f, 0.0f)
var i = 0
val len = data.length
while (i < len) { total = FastComplex.add(total, data(i)); i += 1 }
Expand All @@ -109,7 +109,7 @@ class AddBenchmarks {
while (i < len) { total += data(i); i += 1 }
total
}

@Benchmark
def addComplexDoubleStateGeneric(state: ComplexDoubleState): Complex[Double] = addGeneric(state.values)

Expand Down
18 changes: 11 additions & 7 deletions benchmark/src/main/scala/spire/benchmark/Arrays.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,27 @@ object ArrayOrder {

object Arrays {

/** Sugar for building arrays using a per-cell init function. */
def init[A:ClassTag](size: Int)(f: => A): Array[A] = {
/**
* Sugar for building arrays using a per-cell init function.
*/
def init[A: ClassTag](size: Int)(f: => A): Array[A] = {
val data = Array.ofDim[A](size)
for (i <- 0 until size) data(i) = f
data
}

/** Sugar for building arrays using a per-cell init function. */
def mkarray[A:ClassTag:Order](size: Int, layout: ArrayOrder)(f: => A): Array[A] = {
/**
* Sugar for building arrays using a per-cell init function.
*/
def mkarray[A: ClassTag: Order](size: Int, layout: ArrayOrder)(f: => A): Array[A] = {
val data = init(size)(f)
val ct = implicitly[ClassTag[A]]
val order = Order[A]
layout match {
case ArrayOrder.Random =>
case ArrayOrder.Sorted => spire.math.Sorting.sort(data)(order, ct)
case ArrayOrder.Random =>
case ArrayOrder.Sorted => spire.math.Sorting.sort(data)(order, ct)
case ArrayOrder.Reversed => spire.math.Sorting.sort(data)(Order.reverse(order), ct)
case _ => sys.error(s"unknown layout: $layout")
case _ => sys.error(s"unknown layout: $layout")
}
data
}
Expand Down
7 changes: 2 additions & 5 deletions benchmark/src/main/scala/spire/benchmark/BigIntRational.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ final class BigIntRational private (val n: BigInt, val d: BigInt) {
}
}


def -(r: BigIntRational): BigIntRational = {
val dgcd: BigInt = d.gcd(r.d)
if (dgcd == 1) {
Expand All @@ -51,14 +50,12 @@ final class BigIntRational private (val n: BigInt, val d: BigInt) {
}
}


def *(r: BigIntRational): BigIntRational = {
val a = n.gcd(r.d)
val b = d.gcd(r.n)
new BigIntRational((n / a) * (r.n / b), (d / b) * (r.d / a))
}


def /(r: BigIntRational): BigIntRational = {
val a = n.gcd(r.n)
val b = d.gcd(r.d)
Expand All @@ -74,9 +71,9 @@ final class BigIntRational private (val n: BigInt, val d: BigInt) {
def pow(exp: Int): BigIntRational = if (exp == 0) {
BigIntRational.one
} else if (exp < 0) {
new BigIntRational(d pow exp.abs, n pow exp.abs)
new BigIntRational(d.pow(exp.abs), n.pow(exp.abs))
} else {
new BigIntRational(n pow exp, d pow exp)
new BigIntRational(n.pow(exp), d.pow(exp))
}

def compare(r: BigIntRational): Int = {
Expand Down
5 changes: 2 additions & 3 deletions benchmark/src/main/scala/spire/benchmark/CForBenchmark.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import java.util.concurrent.TimeUnit

import org.openjdk.jmh.annotations._


import scala.util.Random
import Random._

Expand Down Expand Up @@ -348,8 +347,8 @@ class CForBenchmarks {
def doCForIntArrayMultiply: Array[Long] = {
val arr2 = arr.clone
val len = size
cfor(0)(_ < len, _ + 1) {
i => {
cfor(0)(_ < len, _ + 1) { i =>
{
val value = arr2(i)
arr2(i) = value * 2
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ class CheckedBenchmarks extends MyBenchmark {
sum
}
}
*/
*/
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ class ComplexAddBenchmarks {
def timeAddFloatComplexesBoxed(reps:Int) = run(reps)(addFloatComplexesBoxed(fcs))
def timeAddFloatComplexesUnboxed(reps:Int) = run(reps)(addFloatComplexesUnboxed(longs))
}
*/
*/
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class FibBenchmarks {
}

def scalaGenFib[@sp(Int, Long) A](n: Int)(implicit r: Rig[A]): A = {
@tailrec def loop(n :Int, a: A, b: A): A =
@tailrec def loop(n: Int, a: A, b: A): A =
if (n == 0) a else loop(n - 1, b, a + b)
loop(n, r.zero, r.one)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class JuliaBenchmarks {
var z = c
var n = 1
while (n < 80) {
if (z.abs > 2.0F) return n - 1
if (z.abs > 2.0f) return n - 1
z = z * z + c
n += 1
}
Expand All @@ -38,7 +38,7 @@ class JuliaBenchmarks {
var z = c
var n = 1
while (n < 80) {
if (FastComplex.abs(z) > 2.0F) return n - 1
if (FastComplex.abs(z) > 2.0f) return n - 1
z = FastComplex.add(FastComplex.multiply(z, z), c)
n += 1
}
Expand Down
7 changes: 2 additions & 5 deletions benchmark/src/main/scala/spire/benchmark/LongRational.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ final class LongRational private (val n: Long, val d: Long) {
}
}


def -(r: LongRational): LongRational = {
val dgcd: Long = gcd(d, r.d)
if (dgcd == 1) {
Expand All @@ -57,14 +56,12 @@ final class LongRational private (val n: Long, val d: Long) {
}
}


def *(r: LongRational): LongRational = {
val a = gcd(n, r.d)
val b = gcd(d, r.n)
new LongRational((n / a) * (r.n / b), (d / b) * (r.d / a))
}


def /(r: LongRational): LongRational = {
val a = gcd(n, r.n)
val b = gcd(d, r.d)
Expand All @@ -80,9 +77,9 @@ final class LongRational private (val n: Long, val d: Long) {
def pow(exp: Int): LongRational = if (exp == 0) {
LongRational.One
} else if (exp < 0) {
new LongRational(d pow java.lang.Math.abs(exp), n pow java.lang.Math.abs(exp))
new LongRational(d.pow(java.lang.Math.abs(exp)), n.pow(java.lang.Math.abs(exp)))
} else {
new LongRational(n pow exp, d pow exp)
new LongRational(n.pow(exp), d.pow(exp))
}

def compare(r: LongRational): Int = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ class MapSemigroupBenchmarks extends MyBenchmark with BenchmarkData {
//}
/**
* Original code from Algebird, though without the isNonZero stuff. That is
* not, in general, deciable.
*/
* Original code from Algebird, though without the isNonZero stuff. That is
* not, in general, deciable.
*/
def algebirdAdd[K, V](x: Map[K, V], y: Map[K, V])(implicit
semigroup: Semigroup[V], eq: Eq[V]): Map[K, V] = {
val (big, small, bigOnLeft) = if(x.size > y.size) { (x,y,true) } else { (y,x,false) }
Expand Down Expand Up @@ -123,4 +123,4 @@ class MapSemigroupBenchmarks extends MyBenchmark with BenchmarkData {
}
}
}
*/
*/
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ class NaturalBenchmarks extends MyBenchmark {
def timeBigIntMin(reps: Int) = run(reps)(bigints.qmin)
def timeSafeLongMin(reps: Int) = run(reps)(safes.qmin)
}
*/
*/
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,4 @@ class PolynomialBenchmarks extends MyBenchmark {
def timeQuotModSpireDoublePolysDense(reps: Int) = run(reps)(quotModSpireDoublePolynomials(spireDenseDoublePolys))
}
*/
*/
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ class PowBenchmarks extends MyBenchmark {
t
}
}
*/
*/
Original file line number Diff line number Diff line change
Expand Up @@ -650,4 +650,4 @@ class RandomBenchmarks extends MyBenchmark with BenchmarkData {
cfor(0)(_ < nextLen, _ + 1)(_ => rng.fillBytes(bytes))
}
}
*/
*/
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ import Arrays.init
@State(Scope.Thread)
class RatComparisonBenchmarks {
@Param(Array("100"))
var size:Int = 0
var size: Int = 0

var spires:Array[Rational] = null
var aps:Array[Aprational] = null
var jscis:Array[number.Rational] = null
var spires: Array[Rational] = null
var aps: Array[Aprational] = null
var jscis: Array[number.Rational] = null

var nums:Array[Int] = null
var denoms:Array[Int] = null
var nums: Array[Int] = null
var denoms: Array[Int] = null

@Setup
def setup(): Unit = {
Expand All @@ -52,12 +52,12 @@ class RatComparisonBenchmarks {
if (false) assert(verify())
}

def spireToAp(r:Rational): Aprational = new Aprational(
def spireToAp(r: Rational): Aprational = new Aprational(
new Apint(r.numerator.toBigInt.bigInteger),
new Apint(r.denominator.toBigInt.bigInteger)
)

def spireToJsci(r:Rational): number.Rational = number.Rational.valueOf(
def spireToJsci(r: Rational): number.Rational = number.Rational.valueOf(
number.LargeInteger.valueOf(r.numerator.toBigInt.bigInteger),
number.LargeInteger.valueOf(r.denominator.toBigInt.bigInteger)
)
Expand Down
19 changes: 11 additions & 8 deletions benchmark/src/main/scala/spire/benchmark/RationalBenchmarks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ class RationalMultiplyDivideBenchmark {
"bi_bi_bi" -> ((Rational(12345) + Long.MaxValue, Rational(67890) + Long.MaxValue)),
"lf_lf_lf" -> ((Rational(12345, 67891), Rational(67890, 12347))),
"lf_lf_bf" -> ((Rational(Long.MaxValue, Int.MaxValue - 1), Rational(Long.MaxValue, Int.MaxValue - 3))),
"bf_bf_bf" -> ((Rational(Long.MaxValue) + Rational(1, 3), Rational(Long.MaxValue) + Rational(1, 5))))
"bf_bf_bf" -> ((Rational(Long.MaxValue) + Rational(1, 3), Rational(Long.MaxValue) + Rational(1, 5)))
)
check(pairs, _ * _)
check(pairs, _ / _.inverse)

Expand Down Expand Up @@ -85,9 +86,10 @@ class RationalAddSubtractBenchmark {
val pairs = Map(
"li_li_li" -> ((Rational(12345), Rational(67890))),
"bi_bi_bi" -> ((Rational(12345) + Long.MaxValue, Rational(67890) + Long.MaxValue)),
"lf_lf_lf" -> ((Rational(12345,67891), Rational(67890,12347))),
"lf_lf_bf" -> ((Rational(Long.MaxValue,Int.MaxValue - 1), Rational(Long.MaxValue,Int.MaxValue - 3))),
"bf_bf_bf" -> ((Rational(Long.MaxValue) + Rational(1,3), Rational(Long.MaxValue) + Rational(1,5))))
"lf_lf_lf" -> ((Rational(12345, 67891), Rational(67890, 12347))),
"lf_lf_bf" -> ((Rational(Long.MaxValue, Int.MaxValue - 1), Rational(Long.MaxValue, Int.MaxValue - 3))),
"bf_bf_bf" -> ((Rational(Long.MaxValue) + Rational(1, 3), Rational(Long.MaxValue) + Rational(1, 5)))
)
check(pairs, _ + _)
check(pairs, _ - -_)

Expand Down Expand Up @@ -122,9 +124,10 @@ class RationalAddSubtractBenchmark {
class RationalCompareBenchmark {
val pairs = Map(
"li_li" -> ((Rational(12345), Rational(67890))),
"lf_lf" -> ((Rational(12345,67891), Rational(67890,12347))),
"lf_lf_intermediateBig" -> ((Rational(Long.MaxValue,Int.MaxValue - 1), Rational(Long.MaxValue,Int.MaxValue - 3))),
"bf_bf" -> ((Rational(Long.MaxValue) + Rational(1,3), Rational(Long.MaxValue) + Rational(1,5))))
"lf_lf" -> ((Rational(12345, 67891), Rational(67890, 12347))),
"lf_lf_intermediateBig" -> ((Rational(Long.MaxValue, Int.MaxValue - 1), Rational(Long.MaxValue, Int.MaxValue - 3))),
"bf_bf" -> ((Rational(Long.MaxValue) + Rational(1, 3), Rational(Long.MaxValue) + Rational(1, 5)))
)
check(pairs)

@Param(Array("li_li", "lf_lf", "lf_lf_intermediateBig", "bf_bf"))
Expand All @@ -143,6 +146,6 @@ class RationalCompareBenchmark {

@Benchmark
def compare(x: Blackhole): Unit = {
x.consume(a compare b)
x.consume(a.compare(b))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,4 @@ class RexBenchmarks extends MyBenchmark with BenchmarkData {
ai(k)
}
}
*/
*/
Loading

0 comments on commit 3942ef9

Please sign in to comment.