Skip to content

Commit

Permalink
start on int arrays.
Browse files Browse the repository at this point in the history
  • Loading branch information
Quafadas committed Dec 2, 2024
1 parent 8f8e7c8 commit 7d90ae5
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
24 changes: 24 additions & 0 deletions vecxt/jvm/src/arrays.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ import dev.ludovic.netlib.blas.JavaBLAS.getInstance as blas
import jdk.incubator.vector.ByteVector
import jdk.incubator.vector.DoubleVector
import jdk.incubator.vector.VectorOperators
import jdk.incubator.vector.IntVector

object arrays:

final val spi = IntVector.SPECIES_PREFERRED
final val spd = DoubleVector.SPECIES_PREFERRED
final val spb = ByteVector.SPECIES_PREFERRED

final val spdl = spd.length()
final val spbl = spb.length()
final val spil = spi.length()

extension (vec: Array[Boolean])
inline def countTrue: Int =
Expand Down Expand Up @@ -89,6 +92,27 @@ object arrays:
end ||
end extension

extension (vec: Array[Int])
inline def -(vec2: Array[Int]): Array[Int] =
val newVec = Array.ofDim[Int](vec.length)
var i = 0

while i < spb.loopBound(vec.length) do
IntVector
.fromArray(spi, vec, i)
.sub(IntVector.fromArray(spi, vec2, i))
.intoArray(newVec, i)
i += spil
end while

while i < vec.length do
newVec(i) = vec(i) - vec2(i)
i += 1
end while
newVec

end extension

extension (vec: Array[Double])

inline def apply(index: Array[Boolean])(using inline boundsCheck: BoundsCheck) =
Expand Down
34 changes: 34 additions & 0 deletions vecxt/test/src/intarray.test.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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

import narr.*
import scala.util.chaining.*
import vecxt.all.*

class IntArrayExtensionSuite extends munit.FunSuite:
import BoundsCheck.DoBoundsCheck.yes

lazy val v_fill = NArray[Double](0, 1, 2, 3, 4)

test("-") {
val v1 = NArray.tabulate[Int](21)(i => i)
val v2 = v1.drop(1) :+ (v1.last + 1)

assertVecEquals((v1 - v2), NArray.fill[Int](v1.length)(-1))
}
end IntArrayExtensionSuite

0 comments on commit 7d90ae5

Please sign in to comment.