Skip to content

Commit

Permalink
Well... that works in the here and now
Browse files Browse the repository at this point in the history
  • Loading branch information
Quafadas committed Nov 7, 2023
1 parent a68edca commit f5a5942
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
16 changes: 10 additions & 6 deletions core/js/src/main/scala/vecxt/array.extensions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ end extension

extension (vec: Float64Array)

def idxBoolean(index: js.Array[Boolean]) =
inline def idxBoolean(index: js.Array[Boolean])(using inline boundsCheck : BoundsCheck) =
dimCheck(vec, index)
val trues = index.countTrue
val newVec = Float64Array(trues)
var j = 0
Expand Down Expand Up @@ -138,15 +139,17 @@ extension (vec: Float64Array)
out
end -

inline def -=(vec2: Float64Array): Unit =
inline def -=(vec2: Float64Array)(using inline boundsCheck : BoundsCheck): Unit =
dimCheck(vec, vec2)
var i = 0
while i < vec.length do
vec(i) = vec(i) - vec2(i)
i = i + 1
end while
end -=

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

inline def +=(vec2: Float64Array): Unit =
inline def +=(vec2: Float64Array)(using inline boundsCheck : BoundsCheck): Unit =
dimCheck(vec, vec2)
var i = 0
while i < vec.length do
vec(i) = vec(i) + vec2(i)
i = i + 1
end while
end +=

inline def *=(d: Double) =
inline def *=(d: Double): Float64Array=
var i = 0
while i < vec.length do
vec(i) = vec(i) * d
Expand All @@ -173,7 +177,7 @@ extension (vec: Float64Array)
vec
end *=

inline def *(d: Double) =
inline def *(d: Double): Float64Array =
val out = Float64Array(vec.length)
var i = 0
while i < vec.length do
Expand Down
16 changes: 10 additions & 6 deletions core/js/src/main/scala/vecxt/dimCheck.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@
package vecxt

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

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

case class VectorDimensionMismatch(givenDimension:Int, requiredDimension:Int) extends Exception(
s"Expected Vector dimensions to match. First dimension was : $requiredDimension, second was : $givenDimension ."
)
inline def apply(a: Float64Array, b: Float64Array)(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 ."
)

0 comments on commit f5a5942

Please sign in to comment.