From d56254196585115d735ca54385202679de13acd1 Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Fri, 12 Mar 2021 19:23:01 +0700 Subject: [PATCH] Improve conversion functions for Buffer --- kmath-core/api/kmath-core.api | 11 ++++---- .../space/kscience/kmath/structures/Buffer.kt | 26 ++++++++++++++++--- .../kscience/kmath/structures/FloatBuffer.kt | 8 +++--- .../kscience/kmath/structures/IntBuffer.kt | 8 +++--- .../kscience/kmath/structures/LongBuffer.kt | 8 +++--- .../kscience/kmath/structures/RealBuffer.kt | 8 +++--- .../kscience/kmath/structures/ShortBuffer.kt | 8 +++--- 7 files changed, 54 insertions(+), 23 deletions(-) diff --git a/kmath-core/api/kmath-core.api b/kmath-core/api/kmath-core.api index 8b30e4c05..d19c2c640 100644 --- a/kmath-core/api/kmath-core.api +++ b/kmath-core/api/kmath-core.api @@ -2314,6 +2314,7 @@ public final class space/kscience/kmath/structures/BufferKt { public static final fun asSequence (Lspace/kscience/kmath/structures/Buffer;)Lkotlin/sequences/Sequence; public static final fun getIndices (Lspace/kscience/kmath/structures/Buffer;)Lkotlin/ranges/IntRange; public static final fun toList (Lspace/kscience/kmath/structures/Buffer;)Ljava/util/List; + public static final fun toMutableList (Lspace/kscience/kmath/structures/Buffer;)Ljava/util/List; } public abstract interface class space/kscience/kmath/structures/FlaggedBuffer : space/kscience/kmath/structures/Buffer { @@ -2376,7 +2377,7 @@ public final class space/kscience/kmath/structures/FloatBufferKt { public static final fun FloatBuffer (ILkotlin/jvm/functions/Function1;)[F public static final fun FloatBuffer ([F)[F public static final fun asBuffer ([F)[F - public static final fun getArray (Lspace/kscience/kmath/structures/MutableBuffer;)[F + public static final fun toFloatArray (Lspace/kscience/kmath/structures/Buffer;)[F } public final class space/kscience/kmath/structures/IntBuffer : space/kscience/kmath/structures/MutableBuffer { @@ -2412,7 +2413,7 @@ public final class space/kscience/kmath/structures/IntBufferKt { public static final fun IntBuffer (ILkotlin/jvm/functions/Function1;)[I public static final fun IntBuffer ([I)[I public static final fun asBuffer ([I)[I - public static final fun getArray (Lspace/kscience/kmath/structures/MutableBuffer;)[I + public static final fun toIntArray (Lspace/kscience/kmath/structures/Buffer;)[I } public final class space/kscience/kmath/structures/ListBuffer : space/kscience/kmath/structures/Buffer { @@ -2470,7 +2471,7 @@ public final class space/kscience/kmath/structures/LongBufferKt { public static final fun LongBuffer (ILkotlin/jvm/functions/Function1;)[J public static final fun LongBuffer ([J)[J public static final fun asBuffer ([J)[J - public static final fun getArray (Lspace/kscience/kmath/structures/MutableBuffer;)[J + public static final fun toLongArray (Lspace/kscience/kmath/structures/Buffer;)[J } public class space/kscience/kmath/structures/MemoryBuffer : space/kscience/kmath/structures/Buffer { @@ -2749,7 +2750,7 @@ public final class space/kscience/kmath/structures/RealBufferKt { public static final fun RealBuffer ([D)[D public static final fun asBuffer ([D)[D public static final fun contentEquals-2uVC2J0 ([D[D)Z - public static final fun getArray (Lspace/kscience/kmath/structures/MutableBuffer;)[D + public static final fun toDoubleArray (Lspace/kscience/kmath/structures/Buffer;)[D } public final class space/kscience/kmath/structures/ShortBuffer : space/kscience/kmath/structures/MutableBuffer { @@ -2785,7 +2786,7 @@ public final class space/kscience/kmath/structures/ShortBufferKt { public static final fun ShortBuffer (ILkotlin/jvm/functions/Function1;)[S public static final fun ShortBuffer ([S)[S public static final fun asBuffer ([S)[S - public static final fun getArray (Lspace/kscience/kmath/structures/MutableBuffer;)[S + public static final fun toShortArray (Lspace/kscience/kmath/structures/Buffer;)[S } public final class space/kscience/kmath/structures/ValueFlag : java/lang/Enum { diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Buffer.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Buffer.kt index 2fccd2eeb..2bde18fce 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Buffer.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Buffer.kt @@ -100,9 +100,29 @@ public fun Buffer.asSequence(): Sequence = Sequence(::iterator) public fun Buffer.asIterable(): Iterable = Iterable(::iterator) /** - * Converts this [Buffer] to a new [List] + * Returns a new [List] containing all elements of this buffer. */ -public fun Buffer.toList(): List = asSequence().toList() +public fun Buffer.toList(): List = when (this) { + is ArrayBuffer -> array.toList() + is ListBuffer -> list.toList() + is MutableListBuffer -> list.toList() + else -> asSequence().toList() +} + +/** + * Returns a new [MutableList] filled with all elements of this buffer. + */ +public fun Buffer.toMutableList(): MutableList = when (this) { + is ArrayBuffer -> array.toMutableList() + is ListBuffer -> list.toMutableList() + is MutableListBuffer -> list.toMutableList() + else -> asSequence().toMutableList() +} + +/** + * Returns a new [Array] containing all elements of this buffer. + */ +public inline fun Buffer.toTypedArray(): Array = asSequence().toList().toTypedArray() /** * Returns an [IntRange] of the valid indices for this [Buffer]. @@ -222,7 +242,7 @@ public inline class MutableListBuffer(public val list: MutableList) : Muta * @param T the type of elements contained in the buffer. * @property array The underlying array. */ -public class ArrayBuffer(private val array: Array) : MutableBuffer { +public class ArrayBuffer(internal val array: Array) : MutableBuffer { // Can't inline because array is invariant override val size: Int get() = array.size diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/FloatBuffer.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/FloatBuffer.kt index 3839be8e1..9fc7d55f3 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/FloatBuffer.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/FloatBuffer.kt @@ -36,10 +36,12 @@ public inline fun FloatBuffer(size: Int, init: (Int) -> Float): FloatBuffer = Fl public fun FloatBuffer(vararg floats: Float): FloatBuffer = FloatBuffer(floats) /** - * Returns a [FloatArray] containing all of the elements of this [MutableBuffer]. + * Returns a new [FloatArray] containing all of the elements of this [Buffer]. */ -public val MutableBuffer.array: FloatArray - get() = (if (this is FloatBuffer) array else FloatArray(size) { get(it) }) +public fun Buffer.toFloatArray(): FloatArray = when(this) { + is FloatBuffer -> array.copyOf() + else -> FloatArray(size, ::get) +} /** * Returns [FloatBuffer] over this array. diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/IntBuffer.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/IntBuffer.kt index df3289a3e..d3d0f79a5 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/IntBuffer.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/IntBuffer.kt @@ -35,10 +35,12 @@ public inline fun IntBuffer(size: Int, init: (Int) -> Int): IntBuffer = IntBuffe public fun IntBuffer(vararg ints: Int): IntBuffer = IntBuffer(ints) /** - * Returns a [IntArray] containing all of the elements of this [MutableBuffer]. + * Returns a new [IntArray] containing all of the elements of this [Buffer]. */ -public val MutableBuffer.array: IntArray - get() = (if (this is IntBuffer) array else IntArray(size) { get(it) }) +public fun Buffer.toIntArray(): IntArray = when(this) { + is IntBuffer -> array.copyOf() + else -> IntArray(size, ::get) +} /** * Returns [IntBuffer] over this array. diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/LongBuffer.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/LongBuffer.kt index d07d44799..fec358421 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/LongBuffer.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/LongBuffer.kt @@ -35,10 +35,12 @@ public inline fun LongBuffer(size: Int, init: (Int) -> Long): LongBuffer = LongB public fun LongBuffer(vararg longs: Long): LongBuffer = LongBuffer(longs) /** - * Returns a [IntArray] containing all of the elements of this [MutableBuffer]. + * Returns a new [LongArray] containing all of the elements of this [Buffer]. */ -public val MutableBuffer.array: LongArray - get() = (if (this is LongBuffer) array else LongArray(size) { get(it) }) +public fun Buffer.toLongArray(): LongArray = when(this) { + is LongBuffer -> array.copyOf() + else -> LongArray(size, ::get) +} /** * Returns [LongBuffer] over this array. diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/RealBuffer.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/RealBuffer.kt index e53ed85c7..01b533138 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/RealBuffer.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/RealBuffer.kt @@ -40,10 +40,12 @@ public fun RealBuffer(vararg doubles: Double): RealBuffer = RealBuffer(doubles) public fun RealBuffer.contentEquals(vararg doubles: Double): Boolean = array.contentEquals(doubles) /** - * Returns a [DoubleArray] containing all of the elements of this [MutableBuffer]. + * Returns a new [DoubleArray] containing all of the elements of this [Buffer]. */ -public val MutableBuffer.array: DoubleArray - get() = (if (this is RealBuffer) array else DoubleArray(size) { get(it) }) +public fun Buffer.toDoubleArray(): DoubleArray = when(this) { + is RealBuffer -> array.copyOf() + else -> DoubleArray(size, ::get) +} /** * Returns [RealBuffer] over this array. diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/ShortBuffer.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/ShortBuffer.kt index afc472b72..77f128403 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/ShortBuffer.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/ShortBuffer.kt @@ -33,10 +33,12 @@ public inline fun ShortBuffer(size: Int, init: (Int) -> Short): ShortBuffer = Sh public fun ShortBuffer(vararg shorts: Short): ShortBuffer = ShortBuffer(shorts) /** - * Returns a [ShortArray] containing all of the elements of this [MutableBuffer]. + * Returns a new [ShortArray] containing all of the elements of this [Buffer]. */ -public val MutableBuffer.array: ShortArray - get() = (if (this is ShortBuffer) array else ShortArray(size) { get(it) }) +public fun Buffer.toShortArray(): ShortArray = when(this) { + is ShortBuffer -> array.copyOf() + else -> ShortArray(size, ::get) +} /** * Returns [ShortBuffer] over this array.