Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement copy for DataViewMemory #106

Merged
merged 2 commits into from
Jun 16, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions kmath-memory/src/jsMain/kotlin/scientifik/memory/DataViewMemory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,16 @@ class DataViewMemory(val view: DataView) : Memory {
override fun view(offset: Int, length: Int): Memory {
require(offset >= 0) { "offset shouldn't be negative: $offset" }
require(length >= 0) { "length shouldn't be negative: $length" }
if (offset + length > size) {

if (offset + length > size)
throw IndexOutOfBoundsException("offset + length > size: $offset + $length > $size")
}

return DataViewMemory(DataView(view.buffer, view.byteOffset + offset, length))
}

override fun copy(): Memory = DataViewMemory(DataView(view.buffer.slice(0)))

override fun copy(): Memory {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}

private val reader = object : MemoryReader {
private val reader: MemoryReader = object : MemoryReader {
override val memory: Memory get() = this@DataViewMemory

override fun readDouble(offset: Int): Double = view.getFloat64(offset, false)
Expand All @@ -45,7 +43,7 @@ class DataViewMemory(val view: DataView) : Memory {

override fun reader(): MemoryReader = reader

private val writer = object : MemoryWriter {
private val writer: MemoryWriter = object : MemoryWriter {
override val memory: Memory get() = this@DataViewMemory

override fun writeDouble(offset: Int, value: Double) {
Expand Down Expand Up @@ -76,7 +74,6 @@ class DataViewMemory(val view: DataView) : Memory {
override fun release() {
//does nothing on JS
}

}

override fun writer(): MemoryWriter = writer
Expand All @@ -94,4 +91,4 @@ actual fun Memory.Companion.allocate(length: Int): Memory {
actual fun Memory.Companion.wrap(array: ByteArray): Memory {
@Suppress("CAST_NEVER_SUCCEEDS") val int8Array = array as Int8Array
return DataViewMemory(DataView(int8Array.buffer, int8Array.byteOffset, int8Array.length))
}
}