From 4754d04e50e5877175052797c6fab5189a7c747a Mon Sep 17 00:00:00 2001 From: Simon Parten Date: Mon, 6 Nov 2023 15:15:16 +0100 Subject: [PATCH] . --- .../vecxt/{main.scala => dimCheck.scala} | 11 ++- .../src/main/scala/vecxt/limit.rpt.scala | 3 - .../shared/src/main/scala/vecxt/package.scala | 68 -------------- core/shared/src/main/scala/vecxt/vector.scala | 88 ------------------- 4 files changed, 9 insertions(+), 161 deletions(-) rename core/shared/src/main/scala/vecxt/{main.scala => dimCheck.scala} (60%) delete mode 100644 core/shared/src/main/scala/vecxt/package.scala delete mode 100644 core/shared/src/main/scala/vecxt/vector.scala diff --git a/core/shared/src/main/scala/vecxt/main.scala b/core/shared/src/main/scala/vecxt/dimCheck.scala similarity index 60% rename from core/shared/src/main/scala/vecxt/main.scala rename to core/shared/src/main/scala/vecxt/dimCheck.scala index dbfbc7d..d883bc9 100644 --- a/core/shared/src/main/scala/vecxt/main.scala +++ b/core/shared/src/main/scala/vecxt/dimCheck.scala @@ -14,5 +14,12 @@ * limitations under the License. */ -@main -def hi = println("Hello world!") +package vecxt + +object dimCheck: + inline def apply[A, B](a: Array[A], b : Array[B]) = + if a.length != b.length then throw UnsupportedVectorDimension(a.length, b.length) + +case class UnsupportedVectorDimension(givenDimension:Int, requiredDimension:Int) extends Exception( + s"Expected Vector dimensions to match. First dimension was : $requiredDimension, second was : $givenDimension ." +) diff --git a/core/shared/src/main/scala/vecxt/limit.rpt.scala b/core/shared/src/main/scala/vecxt/limit.rpt.scala index dad60e6..e204a8d 100644 --- a/core/shared/src/main/scala/vecxt/limit.rpt.scala +++ b/core/shared/src/main/scala/vecxt/limit.rpt.scala @@ -16,9 +16,6 @@ package vexct -/* -These opaque types prevent boxing? - */ import vexct.Retentions.* object Limits: diff --git a/core/shared/src/main/scala/vecxt/package.scala b/core/shared/src/main/scala/vecxt/package.scala deleted file mode 100644 index be821fb..0000000 --- a/core/shared/src/main/scala/vecxt/package.scala +++ /dev/null @@ -1,68 +0,0 @@ -/* - * 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.NArray - -package object idx: - - opaque type Index[N <: Int] = NArray[Boolean] - - object Index: - def none(num: Int): Index[num.type] = NArray.fill(num)(false) - def trues(num: Int): Index[num.type] = NArray.fill(num)(true) - def none[N <: Int](using ValueOf[N]): Index[N] = - NArray.fill[Boolean](valueOf[N])(false) - def all[N <: Int](using ValueOf[N]): Index[N] = - NArray.fill[Boolean](valueOf[N])(true) - end Index - - extension [N <: Int](thisVector: Index[N]) - inline def apply(idx: Int): Boolean = thisVector(idx) - inline def at(idx: Int): Boolean = thisVector(idx) - inline def update(idx: Int, value: Boolean): Unit = thisVector(idx) = value - inline def changeAt(idx: Int, value: Boolean): Unit = thisVector(idx) = value - inline def dimension: Int = thisVector.length - inline def countTrue: Int = - var sum = 0 - for i <- 0 until dimension do if thisVector(i) then sum = sum + 1 - sum - end countTrue - - inline def &&(thatIdx: Index[N]): Index[N] = - val result: Index[N] = new NArray[Boolean](dimension) - for i <- 0 until dimension do result(i) = thisVector(i) && thatIdx(i) - result - end && - - inline def ||(thatIdx: Index[N]): Index[N] = - val result: Index[N] = new NArray[Boolean](dimension) - for i <- 0 until dimension do result(i) = thisVector(i) || thatIdx(i) - result - end || - - def copy: Index[N] = - val copyOfThisVector: Index[N] = new NArray[Boolean](dimension) - var i = 0 - while i < dimension do - copyOfThisVector(i) = thisVector(i) - i = i + 1 - end while - copyOfThisVector - end copy - end extension -end idx diff --git a/core/shared/src/main/scala/vecxt/vector.scala b/core/shared/src/main/scala/vecxt/vector.scala deleted file mode 100644 index 2742c4c..0000000 --- a/core/shared/src/main/scala/vecxt/vector.scala +++ /dev/null @@ -1,88 +0,0 @@ -/* - * 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 ai.dragonfly.math.vector.Vec -import ai.dragonfly.math.vector.Vec.* -import vecxt.idx.* - -import scala.util.NotGiven - -package object vector: - - object dynamic: - - extension [N <: Int](thisVector: Vec[N])(using ValueOf[N]) - transparent inline def index[M <: Int](index: Index[M])(using - ValueOf[M] - ) = - dimCheck.dimensionCheck(valueOf[M], valueOf[N]) - val newLength = index.countTrue - type D = newLength.type - val newVec = Vec.zeros[D] - var j = 0 - for i <- 0 until valueOf[M] do - if index.at(i) then - newVec(j) = thisVector(i) - j += 1 - end for - newVec.asInstanceOf[Vec[Int]] - end extension - - extension [N <: Int](thisVector: Vec[N]) - - def `+!`[M <: Int](thatVector: Vec[M])(using NotGiven[M =:= N]): Vec[N] = - dimCheck.dimensionCheck(thisVector.dimension, thatVector.dimension) - import ai.dragonfly.math.vector.Vec.+ - thisVector + thatVector.asInstanceOf[Vec[N]] - end `+!` - end extension - end dynamic - - object logical: - extension [N <: Int](thisVector: Vec[N]) - - inline def <(num: Double): Index[N] = - logicalIdx((a, b) => a < b, num) - - inline def <=(num: Double): Index[N] = - logicalIdx((a, b) => a <= b, num) - - inline def >(num: Double): Index[N] = - logicalIdx((a, b) => a > b, num) - - inline def >=(num: Double): Index[N] = - logicalIdx((a, b) => a >= b, num) - - inline def logicalIdx( - inline op: (Double, Double) => Boolean, - inline num: Double - ): Index[N] = - val n = thisVector.dimension - val idx = Index.none(n) - var i = 0 - while i < n do - if op(thisVector(i), num) then idx.changeAt(i, true) - i = i + 1 - end while - idx.asInstanceOf[Index[N]] - end logicalIdx - - end extension - - end logical -end vector