From 58e939e0cf727fd3cc8a61223004db401c85107d Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Wed, 16 Jan 2019 15:51:28 +0300 Subject: [PATCH] Revert "Added internal mapping functionality to buffers" --- .../kmath/structures/BoxingNDField.kt | 8 +++ .../scientifik/kmath/structures/Buffers.kt | 60 ------------------- .../kmath/structures/NDStructure.kt | 2 +- 3 files changed, 9 insertions(+), 61 deletions(-) diff --git a/kmath-core/src/commonMain/kotlin/scientifik/kmath/structures/BoxingNDField.kt b/kmath-core/src/commonMain/kotlin/scientifik/kmath/structures/BoxingNDField.kt index e06119766..c1e1bcc38 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/structures/BoxingNDField.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/structures/BoxingNDField.kt @@ -32,6 +32,10 @@ class BoxingNDField>( return BufferedNDFieldElement( this, buildBuffer(arg.strides.linearSize) { offset -> elementContext.transform(arg.buffer[offset]) }) + +// val buffer = arg.buffer.transform { _, value -> elementContext.transform(value) } +// return BufferedNDFieldElement(this, buffer) + } override fun mapIndexed( @@ -47,6 +51,10 @@ class BoxingNDField>( arg.buffer[offset] ) }) + +// val buffer = +// arg.buffer.transform { offset, value -> elementContext.transform(arg.strides.index(offset), value) } +// return BufferedNDFieldElement(this, buffer) } override fun combine( diff --git a/kmath-core/src/commonMain/kotlin/scientifik/kmath/structures/Buffers.kt b/kmath-core/src/commonMain/kotlin/scientifik/kmath/structures/Buffers.kt index 14fcfc2ff..1cd2dc502 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/structures/Buffers.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/structures/Buffers.kt @@ -31,11 +31,6 @@ interface Buffer { fun contentEquals(other: Buffer<*>): Boolean = asSequence().mapIndexed { index, value -> value == other[index] }.all { it } - /** - * Map the contents of the buffer to new buffer of the same type - */ - fun transform(transformation: (index: Int, value: T) -> T): Buffer - companion object { /** @@ -78,8 +73,6 @@ interface MutableBuffer : Buffer { */ fun copy(): MutableBuffer - fun transformInPlace(transformation: (index: Int, value: T) -> T) - companion object { /** * Create a boxing mutable buffer of given type @@ -112,9 +105,6 @@ inline class ListBuffer(private val list: List) : Buffer { override fun get(index: Int): T = list[index] override fun iterator(): Iterator = list.iterator() - - override fun transform(transformation: (index: Int, value: T) -> T): ListBuffer = - list.mapIndexed(transformation).asBuffer() } fun List.asBuffer() = ListBuffer(this) @@ -131,15 +121,7 @@ inline class MutableListBuffer(private val list: MutableList) : MutableBuf } override fun iterator(): Iterator = list.iterator() - - override fun transform(transformation: (index: Int, value: T) -> T): ListBuffer = - list.mapIndexed(transformation).asBuffer() - override fun copy(): MutableBuffer = MutableListBuffer(ArrayList(list)) - - override fun transformInPlace(transformation: (index: Int, value: T) -> T) = list.forEachIndexed { index, value -> - list[index] = transformation(index, value) - } } fun MutableList.asBuffer() = MutableListBuffer(this) @@ -158,14 +140,6 @@ class ArrayBuffer(private val array: Array) : MutableBuffer { override fun iterator(): Iterator = array.iterator() override fun copy(): MutableBuffer = ArrayBuffer(array.copyOf()) - - override fun transform(transformation: (index: Int, value: T) -> T): ArrayBuffer = - Array(size) { index -> transformation(index, get(index)) }.asBuffer() - - - override fun transformInPlace(transformation: (index: Int, value: T) -> T) = array.forEachIndexed { index, value -> - array[index] = transformation(index, value) - } } fun Array.asBuffer() = ArrayBuffer(this) @@ -183,13 +157,6 @@ inline class DoubleBuffer(private val array: DoubleArray) : MutableBuffer = DoubleBuffer(array.copyOf()) - override fun transform(transformation: (index: Int, value: Double) -> Double): DoubleBuffer = - DoubleArray(size) { index -> transformation(index, get(index)) }.asBuffer() - - override fun transformInPlace(transformation: (index: Int, value: Double) -> Double) = - array.forEachIndexed { index, value -> - array[index] = transformation(index, value) - } } fun DoubleArray.asBuffer() = DoubleBuffer(this) @@ -207,13 +174,6 @@ inline class ShortBuffer(private val array: ShortArray) : MutableBuffer { override fun copy(): MutableBuffer = ShortBuffer(array.copyOf()) - override fun transform(transformation: (index: Int, value: Short) -> Short): ShortBuffer = - ShortArray(size) { index -> transformation(index, get(index)) }.asBuffer() - - override fun transformInPlace(transformation: (index: Int, value: Short) -> Short) = - array.forEachIndexed { index, value -> - array[index] = transformation(index, value) - } } fun ShortArray.asBuffer() = ShortBuffer(this) @@ -231,13 +191,6 @@ inline class IntBuffer(private val array: IntArray) : MutableBuffer { override fun copy(): MutableBuffer = IntBuffer(array.copyOf()) - override fun transform(transformation: (index: Int, value: Int) -> Int): IntBuffer = - IntArray(size) { index -> transformation(index, get(index)) }.asBuffer() - - override fun transformInPlace(transformation: (index: Int, value: Int) -> Int) = - array.forEachIndexed { index, value -> - array[index] = transformation(index, value) - } } fun IntArray.asBuffer() = IntBuffer(this) @@ -255,13 +208,6 @@ inline class LongBuffer(private val array: LongArray) : MutableBuffer { override fun copy(): MutableBuffer = LongBuffer(array.copyOf()) - override fun transform(transformation: (index: Int, value: Long) -> Long): LongBuffer = - LongArray(size) { index -> transformation(index, get(index)) }.asBuffer() - - override fun transformInPlace(transformation: (index: Int, value: Long) -> Long) = - array.forEachIndexed { index, value -> - array[index] = transformation(index, value) - } } fun LongArray.asBuffer() = LongBuffer(this) @@ -272,8 +218,6 @@ inline class ReadOnlyBuffer(private val buffer: MutableBuffer) : Buffer override fun get(index: Int): T = buffer.get(index) override fun iterator(): Iterator = buffer.iterator() - - override fun transform(transformation: (index: Int, value: T) -> T): Buffer = buffer.transform(transformation) } /** @@ -292,10 +236,6 @@ class VirtualBuffer(override val size: Int, private val generator: (Int) -> T super.contentEquals(other) } } - - // TODO this composition could become very compex very fast, replace it by boxed generator? - override fun transform(transformation: (index: Int, value: T) -> T): Buffer = - VirtualBuffer(size) { index -> transformation(index, generator(index)) } } /** diff --git a/kmath-core/src/commonMain/kotlin/scientifik/kmath/structures/NDStructure.kt b/kmath-core/src/commonMain/kotlin/scientifik/kmath/structures/NDStructure.kt index d9601a5d4..6c545d1af 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/structures/NDStructure.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/structures/NDStructure.kt @@ -200,7 +200,7 @@ fun ndStructure(strides: Strides, bufferFactory: BufferFactory = Buffer.C * Inline create NDStructure with non-boxing buffer implementation if it is possible */ inline fun inlineNDStructure(strides: Strides, crossinline initializer: (IntArray) -> T) = - BufferNDStructure(strides, Buffer.Companion.auto(strides.linearSize) { i -> initializer(strides.index(i)) }) + BufferNDStructure(strides, Buffer.auto(strides.linearSize) { i -> initializer(strides.index(i)) }) fun ndStructure(shape: IntArray, bufferFactory: BufferFactory = Buffer.Companion::boxing, initializer: (IntArray) -> T) = ndStructure(DefaultStrides(shape), bufferFactory, initializer)