From 91207c8c9aa398bec0cf500481e65e9a558021d3 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Tue, 12 Feb 2019 12:28:35 +0300 Subject: [PATCH] Complex buffer optimization --- .../scientifik/kmath/structures/ComplexBufferSpec.kt | 10 ++++++++-- .../scientifik/kmath/structures/ObjectBuffer.kt | 11 ++++++----- .../kmath/structures/ComplexBufferSpecTest.kt | 6 +----- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/kmath-core/src/jvmMain/kotlin/scientifik/kmath/structures/ComplexBufferSpec.kt b/kmath-core/src/jvmMain/kotlin/scientifik/kmath/structures/ComplexBufferSpec.kt index f8c93e754..34f21cf75 100644 --- a/kmath-core/src/jvmMain/kotlin/scientifik/kmath/structures/ComplexBufferSpec.kt +++ b/kmath-core/src/jvmMain/kotlin/scientifik/kmath/structures/ComplexBufferSpec.kt @@ -26,10 +26,16 @@ object ComplexBufferSpec : FixedSizeBufferSpec { /** * Create a read-only/mutable buffer which ignores boxing */ -fun Buffer.Companion.complex(size: Int, initializer: ((Int) -> Complex)? = null): Buffer = +fun Buffer.Companion.complex(size: Int): Buffer = + ObjectBuffer.create(ComplexBufferSpec, size) + +inline fun Buffer.Companion.complex(size: Int, crossinline initializer: (Int) -> Complex): Buffer = ObjectBuffer.create(ComplexBufferSpec, size, initializer) -fun MutableBuffer.Companion.complex(size: Int, initializer: ((Int) -> Complex)? = null) = +fun MutableBuffer.Companion.complex(size: Int) = + ObjectBuffer.create(ComplexBufferSpec, size) + +inline fun MutableBuffer.Companion.complex(size: Int, crossinline initializer: (Int) -> Complex) = ObjectBuffer.create(ComplexBufferSpec, size, initializer) fun NDField.Companion.complex(shape: IntArray) = ComplexNDField(shape) diff --git a/kmath-core/src/jvmMain/kotlin/scientifik/kmath/structures/ObjectBuffer.kt b/kmath-core/src/jvmMain/kotlin/scientifik/kmath/structures/ObjectBuffer.kt index 422ed64ad..e95c54b2c 100644 --- a/kmath-core/src/jvmMain/kotlin/scientifik/kmath/structures/ObjectBuffer.kt +++ b/kmath-core/src/jvmMain/kotlin/scientifik/kmath/structures/ObjectBuffer.kt @@ -26,12 +26,13 @@ class ObjectBuffer(private val buffer: ByteBuffer, private val spec: Fi } companion object { - fun create(spec: FixedSizeBufferSpec, size: Int, initializer: ((Int) -> T)? = null) = + fun create(spec: FixedSizeBufferSpec, size: Int) = + ObjectBuffer(ByteBuffer.allocate(size * spec.unitSize), spec) + + inline fun create(spec: FixedSizeBufferSpec, size: Int, crossinline initializer: (Int) -> T) = ObjectBuffer(ByteBuffer.allocate(size * spec.unitSize), spec).also { buffer -> - if (initializer != null) { - (0 until size).forEach { - buffer[it] = initializer(it) - } + (0 until size).forEach { + buffer[it] = initializer(it) } } } diff --git a/kmath-core/src/jvmTest/kotlin/scientifik/kmath/structures/ComplexBufferSpecTest.kt b/kmath-core/src/jvmTest/kotlin/scientifik/kmath/structures/ComplexBufferSpecTest.kt index 1a3aea10f..a07f2167e 100644 --- a/kmath-core/src/jvmTest/kotlin/scientifik/kmath/structures/ComplexBufferSpecTest.kt +++ b/kmath-core/src/jvmTest/kotlin/scientifik/kmath/structures/ComplexBufferSpecTest.kt @@ -7,11 +7,7 @@ import kotlin.test.assertEquals class ComplexBufferSpecTest { @Test fun testComplexBuffer() { - val buffer = MutableBuffer.complex(20) - (0 until 20).forEach { - buffer[it] = Complex(it.toDouble(), -it.toDouble()) - } - + val buffer = MutableBuffer.complex(20){Complex(it.toDouble(), -it.toDouble())} assertEquals(Complex(5.0, -5.0), buffer[5]) } } \ No newline at end of file