-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added many NArrayOps extensions and some tests.
Broke NArrayBuilder into two traits and implemented a special version for js.Array[T] called NativeArrayBuilder. Added copyAs and copyOf, but they don't seem to work. Added NArrayView[T]. Added test helper method: assertArray2NArrayEquality
- Loading branch information
c
committed
Dec 18, 2024
1 parent
55b3c00
commit 4acd5b2
Showing
10 changed files
with
1,154 additions
and
149 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
62 changes: 62 additions & 0 deletions
62
narr/js/src/main/scala/narr/native/NativeArrayBuilder.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright 2023 dragonfly.ai | ||
* | ||
* 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 narr.native | ||
|
||
import narr.{NArray, NArrayBuilder, NativeArray} | ||
|
||
import scala.reflect.ClassTag | ||
|
||
|
||
// in JavaScript, just use a js.Array. | ||
case class NativeArrayBuilder[T](initCapacity:Int = NArrayBuilder.DefaultInitialSize)(using ClassTag[T]) extends NArrayBuilder[T] { | ||
//given clz:Class[T] = implicitly[ClassTag[T]].runtimeClass.asInstanceOf[Class[T]] | ||
override inline def makeNArray(len: Int): NArray[T] = new scala.scalajs.js.Array[T]().asInstanceOf[NArray[T]] | ||
|
||
override inline def copyInto(src: NArray[T], dest: NArray[T], dstPos: Int): Unit = NArray.copyNativeArray( | ||
src.asInstanceOf[NativeArray[T]], dest.asInstanceOf[NativeArray[T]], dstPos | ||
) | ||
override inline def copyInto(src: NArray[T], srcPos: Int, dest: NArray[T], dstPos: Int, length: Int): Unit = { | ||
NArray.copyNativeArray(src.asInstanceOf[NativeArray[T]], srcPos, dest.asInstanceOf[NativeArray[T]], dstPos, length) | ||
} | ||
|
||
private var elements: scala.scalajs.js.Array[T] = new scala.scalajs.js.Array[T](initCapacity).asInstanceOf[NativeArray[T]] | ||
|
||
private var length:Int = 0 | ||
|
||
override def size: Int = length | ||
|
||
override def addOne(e: T): this.type = { | ||
elements(length) = e | ||
length = length + 1 | ||
this | ||
} | ||
|
||
override def addAll(es: NArray[T]): this.type = { | ||
//copyInto(es, elements.asInstanceOf[NArray[T]], length) // slow copy | ||
elements = result.asInstanceOf[NArr[T]].concat(es.asInstanceOf[NArr[T]]).asInstanceOf[scala.scalajs.js.Array[T]] | ||
length = length + es.asInstanceOf[NArr[T]].length | ||
this | ||
} | ||
|
||
override def result: NArray[T] = { | ||
elements.slice(0, length).asInstanceOf[NArray[T]] | ||
} | ||
|
||
override def apply(idx: Int): T = if (idx > -1 && idx < length) elements(idx) else throw ArrayIndexOutOfBoundsException( | ||
s"No index: $idx for array builder of length: $length." | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
narr/jvm-native/src/main/scala/narr/native/NativeArrayBuilder.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright 2023 dragonfly.ai | ||
* | ||
* 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 narr.native | ||
|
||
import narr.{NArray, NArrayBuilder, NativeArray, TypedArrayBuilder} | ||
|
||
import scala.reflect.ClassTag | ||
|
||
|
||
// in JavaScript, just use a js.Array. | ||
case class NativeArrayBuilder[T](override val initCapacity:Int = NArrayBuilder.DefaultInitialSize)(using ClassTag[T]) extends TypedArrayBuilder[T] { | ||
//given clz:Class[T] = implicitly[ClassTag[T]].runtimeClass.asInstanceOf[Class[T]] | ||
override inline def makeNArray(len: Int): NArray[T] = NArray.ofSize[T](len) | ||
|
||
override inline def make2DNArray(len: Int): NArray[NArray[T]] = { | ||
narr.native.makeNativeArrayOfSize[NativeArray[T]](len).asInstanceOf[NArray[NArray[T]]] | ||
} | ||
|
||
override inline def copyInto(src: NArray[T], dest: NArray[T], dstPos: Int): Unit = NArray.copyNativeArray( | ||
src.asInstanceOf[NativeArray[T]], dest.asInstanceOf[NativeArray[T]], dstPos | ||
) | ||
override inline def copyInto(src: NArray[T], srcPos: Int, dest: NArray[T], dstPos: Int, length: Int): Unit = { | ||
NArray.copyNativeArray(src.asInstanceOf[NativeArray[T]], srcPos, dest.asInstanceOf[NativeArray[T]], dstPos, length) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.