Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Quafadas committed Nov 7, 2023
1 parent f5a5942 commit eab7965
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 54 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ jobs:

- name: Make target directories
if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main')
run: mkdir -p core/native/target core/js/target core/jvm/target project/target
run: mkdir -p shim/js/target shim/jvm/target core/native/target core/js/target core/jvm/target shim/native/target project/target

- name: Compress target directories
if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main')
run: tar cf targets.tar core/native/target core/js/target core/jvm/target project/target
run: tar cf targets.tar shim/js/target shim/jvm/target core/native/target core/js/target core/jvm/target shim/native/target project/target

- name: Upload target directories
if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main')
Expand Down
33 changes: 17 additions & 16 deletions core/js/src/main/scala/vecxt/array.extensions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,35 @@
*/

package vecxt
import vecxt.BoundsCheck
import vecxt.Limits.Limit
import vecxt.Retentions.Retention

import scala.util.chaining.*
import vecxt.BoundsCheck
import scala.scalajs.js.typedarray.Float64Array
import scala.scalajs.js
import scala.scalajs.js.annotation.JSBracketAccess

import scala.scalajs.js.typedarray.Float64Array
import scala.util.chaining.*

extension (v: Float64Array)
inline def sort(): Unit = v.asInstanceOf[TypedArrayFacade].sort()
inline def reverse(): Unit = v.asInstanceOf[TypedArrayFacade].reverse()
inline def slice(): Float64Array = v.asInstanceOf[TypedArrayFacade].slice()
end extension

@js.native
trait TypedArrayFacade extends js.Object :
trait TypedArrayFacade extends js.Object:
def sort(): Unit = js.native
def reverse(): Unit = js.native // mutable https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/reverse
def reverse(): Unit =
js.native // mutable https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/reverse
def slice(): Float64Array = js.native
end TypedArrayFacade

extension [A](v: js.Array[A])
inline def fill(a: A): Unit = v.asInstanceOf[JsArrayFacade].fill(a)
extension [A](v: js.Array[A]) inline def fill(a: A): Unit = v.asInstanceOf[JsArrayFacade].fill(a)

@js.native
trait JsArrayFacade extends js.Object :
trait JsArrayFacade extends js.Object:
def fill[A](a: A): Unit = js.native

end JsArrayFacade

extension (vec: js.Array[Boolean])
inline def countTrue: Int =
Expand Down Expand Up @@ -76,7 +77,7 @@ end extension

extension (vec: Float64Array)

inline def idxBoolean(index: js.Array[Boolean])(using inline boundsCheck : BoundsCheck) =
inline def idxBoolean(index: js.Array[Boolean])(using inline boundsCheck: BoundsCheck) =
dimCheck(vec, index)
val trues = index.countTrue
val newVec = Float64Array(trues)
Expand Down Expand Up @@ -128,7 +129,7 @@ extension (vec: Float64Array)
end while
end cumsum

inline def -(vec2: Float64Array)(using inline boundsCheck : BoundsCheck) =
inline def -(vec2: Float64Array)(using inline boundsCheck: BoundsCheck) =
dimCheck(vec, vec2)
val out = Float64Array(vec.length)
var i = 0
Expand All @@ -139,7 +140,7 @@ extension (vec: Float64Array)
out
end -

inline def -=(vec2: Float64Array)(using inline boundsCheck : BoundsCheck): Unit =
inline def -=(vec2: Float64Array)(using inline boundsCheck: BoundsCheck): Unit =
dimCheck(vec, vec2)
var i = 0
while i < vec.length do
Expand All @@ -148,7 +149,7 @@ extension (vec: Float64Array)
end while
end -=

inline def +(vec2: Float64Array)(using inline boundsCheck : BoundsCheck) =
inline def +(vec2: Float64Array)(using inline boundsCheck: BoundsCheck) =
dimCheck(vec, vec2)
val out = new Array[Double](vec.length)
var i = 0
Expand All @@ -159,7 +160,7 @@ extension (vec: Float64Array)
out
end +

inline def +=(vec2: Float64Array)(using inline boundsCheck : BoundsCheck): Unit =
inline def +=(vec2: Float64Array)(using inline boundsCheck: BoundsCheck): Unit =
dimCheck(vec, vec2)
var i = 0
while i < vec.length do
Expand All @@ -168,7 +169,7 @@ extension (vec: Float64Array)
end while
end +=

inline def *=(d: Double): Float64Array=
inline def *=(d: Double): Float64Array =
var i = 0
while i < vec.length do
vec(i) = vec(i) * d
Expand Down
2 changes: 1 addition & 1 deletion core/js/src/main/scala/vecxt/dimCheck.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package vecxt

import scala.scalajs.js.typedarray.Float64Array
import scala.scalajs.js
import scala.scalajs.js.typedarray.Float64Array

object dimCheck:
inline def apply[A](a: Float64Array, b: js.Array[A])(using inline doCheck: BoundsCheck) =
Expand Down
23 changes: 20 additions & 3 deletions core/js/src/main/scala/vecxt/main.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
import scala.scalajs.js.typedarray.Float64Array
/*
* Copyright 2023 quafadas
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import scala.util.chaining.*
import vecxt.*

import scala.scalajs.js.typedarray.Float64Array
import scala.util.chaining.*

@main def checkBytecode =
val a = Float64Array(3).tap(_.fill(1.0))
val a1 = Float64Array(3).tap(_.fill(2.0))
Expand All @@ -11,4 +27,5 @@ import vecxt.*

import vecxt.BoundsCheck.yes

a - a1
a - a1
end checkBytecode
5 changes: 2 additions & 3 deletions core/jvm-native/src/main/scala/vecxt/array.extensions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@
*/

package vecxt
import vecxt.BoundsCheck
import vecxt.Limits.Limit
import vecxt.Retentions.Retention

import scala.util.chaining.*
import vecxt.BoundsCheck


extension (vec: Array[Boolean])
inline def countTrue: Int =
Expand Down Expand Up @@ -105,7 +104,7 @@ extension (vec: Array[Double])
end while
end cumsum

inline def -(vec2: Array[Double])(using inline boundsCheck : BoundsCheck) =
inline def -(vec2: Array[Double])(using inline boundsCheck: BoundsCheck) =
dimCheck(vec, vec2)
val out = new Array[Double](vec.length)
var i = 0
Expand Down
13 changes: 7 additions & 6 deletions core/jvm-native/src/main/scala/vecxt/dimCheck.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
package vecxt

object dimCheck:
inline def apply[A, B](a: Array[A], b : Array[B]) (using inline doCheck: BoundsCheck)=
inline if doCheck then
if a.length != b.length then throw VectorDimensionMismatch(a.length, b.length)
inline def apply[A, B](a: Array[A], b: Array[B])(using inline doCheck: BoundsCheck) =
inline if doCheck then if a.length != b.length then throw VectorDimensionMismatch(a.length, b.length)
end dimCheck

case class VectorDimensionMismatch(givenDimension:Int, requiredDimension:Int) extends Exception(
s"Expected Vector dimensions to match. First dimension was : $requiredDimension, second was : $givenDimension ."
)
case class VectorDimensionMismatch(givenDimension: Int, requiredDimension: Int)
extends Exception(
s"Expected Vector dimensions to match. First dimension was : $requiredDimension, second was : $givenDimension ."
)
22 changes: 19 additions & 3 deletions core/jvm-native/src/main/scala/vecxt/main.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
/*
* Copyright 2023 quafadas
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import vecxt.*

@main def checkBytecode =
val a = Array[Double](1,2,3)
val a1 = Array[Double](1,2,3)
val a = Array[Double](1, 2, 3)
val a1 = Array[Double](1, 2, 3)
// val b = Array[Boolean](true, false, true)
// val c = Array[Boolean](false, true, true)

import vecxt.BoundsCheck.yes
a - a1
a - a1
end checkBytecode
20 changes: 18 additions & 2 deletions core/shared/src/main/scala/vecxt/bounds.check.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2023 quafadas
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package vecxt

// opaque type BoundsCheck = Boolean
Expand All @@ -11,6 +27,6 @@ package vecxt
type BoundsCheck = Boolean

object BoundsCheck:
inline given yes : BoundsCheck = true
inline given no : BoundsCheck = false
inline given yes: BoundsCheck = true
inline given no: BoundsCheck = false
end BoundsCheck
16 changes: 16 additions & 0 deletions core/shared/src/main/scala/vecxt/loss.calc.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2023 quafadas
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package vecxt

enum LossCalc:
Expand Down
15 changes: 7 additions & 8 deletions tests/js/src/test/scala/arrayExtensions.test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@

package vecxt

import scala.util.chaining.*
import scala.scalajs.js.typedarray.Float64Array
import scala.scalajs.js
import scala.scalajs.js.typedarray.Float64Array
import scala.util.chaining.*

class ArrayExtensionSuite extends munit.FunSuite:
import vecxt.BoundsCheck.yes

def v_fill = Float64Array(5).tap(a =>
a.update(0, 0.0)
a.update(1, 1.0)
a.update(2, 2.0)
a.update(3, 3.0)
a.update(4, 4.0)
a.update(1, 1.0)
a.update(2, 2.0)
a.update(3, 3.0)
a.update(4, 4.0)
)

def v_3 = Float64Array(3).tap(a =>
Expand Down Expand Up @@ -162,7 +162,6 @@ class ArrayExtensionSuite extends munit.FunSuite:
assertEquals(afterIndex.length, 2)
assertEqualsDouble(afterIndex.head, 1, 0.0001)
assertEqualsDouble(afterIndex.last, 3.0, 0.0001)
}
}

end ArrayExtensionSuite

4 changes: 1 addition & 3 deletions tests/js/src/test/scala/boundsCheck.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@

package vecxt

import scala.util.chaining.*
import scala.scalajs.js.typedarray.Float64Array

import scala.util.chaining.*

class BoundsCheckSuite extends munit.FunSuite:


lazy val v_fill = Float64Array(5).tap(a =>
a(0) = 1.0
a(1) = 2.0
Expand Down
7 changes: 4 additions & 3 deletions tests/js/src/test/scala/rpt.test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,20 @@

package vecxt

import Retentions.*
import Limits.*
import scala.scalajs.js.typedarray.Float64Array
import scala.util.chaining.*

import Retentions.*
import Limits.*

extension [A <: AnyRef](o: A) def some = Some(o)

class ReinsurancePricingSuite extends munit.FunSuite:

def v_8_11_16 = Float64Array(3).tap(a =>
a.update(0, 8)
a.update(1, 11)
a.update(2,16)
a.update(2, 16)
)

test("reinsurance function - ret and limit") {
Expand Down
6 changes: 2 additions & 4 deletions tests/jvm-native/src/test/scala/boundsCheck.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,16 @@ package vecxt

import scala.util.chaining.*


class BoundsCheckSuite extends munit.FunSuite:


lazy val v_fill = Array.tabulate(5)(i => i.toDouble)

test("Bounds check") {
intercept[vecxt.VectorDimensionMismatch](v_fill.-(Array[Double](1,2,3))(using vecxt.BoundsCheck.yes))
intercept[vecxt.VectorDimensionMismatch](v_fill.-(Array[Double](1, 2, 3))(using vecxt.BoundsCheck.yes))
}

test("no bound check") {
intercept[java.lang.ArrayIndexOutOfBoundsException](v_fill.-(Array[Double](1,2,3))(using vecxt.BoundsCheck.no))
intercept[java.lang.ArrayIndexOutOfBoundsException](v_fill.-(Array[Double](1, 2, 3))(using vecxt.BoundsCheck.no))
}

end BoundsCheckSuite

0 comments on commit eab7965

Please sign in to comment.