Improve conversion functions for Buffer

This commit is contained in:
Iaroslav Postovalov 2021-03-12 19:23:01 +07:00
parent f449bdd58f
commit d562541965
No known key found for this signature in database
GPG Key ID: 46E15E4A31B3BCD7
7 changed files with 54 additions and 23 deletions

View File

@ -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 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 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 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 { 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 (ILkotlin/jvm/functions/Function1;)[F
public static final fun FloatBuffer ([F)[F public static final fun FloatBuffer ([F)[F
public static final fun asBuffer ([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 { 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 (ILkotlin/jvm/functions/Function1;)[I
public static final fun IntBuffer ([I)[I public static final fun IntBuffer ([I)[I
public static final fun asBuffer ([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 { 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 (ILkotlin/jvm/functions/Function1;)[J
public static final fun LongBuffer ([J)[J public static final fun LongBuffer ([J)[J
public static final fun asBuffer ([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 { 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 RealBuffer ([D)[D
public static final fun asBuffer ([D)[D public static final fun asBuffer ([D)[D
public static final fun contentEquals-2uVC2J0 ([D[D)Z 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 { 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 (ILkotlin/jvm/functions/Function1;)[S
public static final fun ShortBuffer ([S)[S public static final fun ShortBuffer ([S)[S
public static final fun asBuffer ([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 { public final class space/kscience/kmath/structures/ValueFlag : java/lang/Enum {

View File

@ -100,9 +100,29 @@ public fun <T> Buffer<T>.asSequence(): Sequence<T> = Sequence(::iterator)
public fun <T> Buffer<T>.asIterable(): Iterable<T> = Iterable(::iterator) public fun <T> Buffer<T>.asIterable(): Iterable<T> = Iterable(::iterator)
/** /**
* Converts this [Buffer] to a new [List] * Returns a new [List] containing all elements of this buffer.
*/ */
public fun <T> Buffer<T>.toList(): List<T> = asSequence().toList() public fun <T> Buffer<T>.toList(): List<T> = when (this) {
is ArrayBuffer<T> -> array.toList()
is ListBuffer<T> -> list.toList()
is MutableListBuffer<T> -> list.toList()
else -> asSequence().toList()
}
/**
* Returns a new [MutableList] filled with all elements of this buffer.
*/
public fun <T> Buffer<T>.toMutableList(): MutableList<T> = when (this) {
is ArrayBuffer<T> -> array.toMutableList()
is ListBuffer<T> -> list.toMutableList()
is MutableListBuffer<T> -> list.toMutableList()
else -> asSequence().toMutableList()
}
/**
* Returns a new [Array] containing all elements of this buffer.
*/
public inline fun <reified T> Buffer<T>.toTypedArray(): Array<T> = asSequence().toList().toTypedArray()
/** /**
* Returns an [IntRange] of the valid indices for this [Buffer]. * Returns an [IntRange] of the valid indices for this [Buffer].
@ -222,7 +242,7 @@ public inline class MutableListBuffer<T>(public val list: MutableList<T>) : Muta
* @param T the type of elements contained in the buffer. * @param T the type of elements contained in the buffer.
* @property array The underlying array. * @property array The underlying array.
*/ */
public class ArrayBuffer<T>(private val array: Array<T>) : MutableBuffer<T> { public class ArrayBuffer<T>(internal val array: Array<T>) : MutableBuffer<T> {
// Can't inline because array is invariant // Can't inline because array is invariant
override val size: Int override val size: Int
get() = array.size get() = array.size

View File

@ -36,10 +36,12 @@ public inline fun FloatBuffer(size: Int, init: (Int) -> Float): FloatBuffer = Fl
public fun FloatBuffer(vararg floats: Float): FloatBuffer = FloatBuffer(floats) 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<out Float>.array: FloatArray public fun Buffer<Float>.toFloatArray(): FloatArray = when(this) {
get() = (if (this is FloatBuffer) array else FloatArray(size) { get(it) }) is FloatBuffer -> array.copyOf()
else -> FloatArray(size, ::get)
}
/** /**
* Returns [FloatBuffer] over this array. * Returns [FloatBuffer] over this array.

View File

@ -35,10 +35,12 @@ public inline fun IntBuffer(size: Int, init: (Int) -> Int): IntBuffer = IntBuffe
public fun IntBuffer(vararg ints: Int): IntBuffer = IntBuffer(ints) 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<out Int>.array: IntArray public fun Buffer<Int>.toIntArray(): IntArray = when(this) {
get() = (if (this is IntBuffer) array else IntArray(size) { get(it) }) is IntBuffer -> array.copyOf()
else -> IntArray(size, ::get)
}
/** /**
* Returns [IntBuffer] over this array. * Returns [IntBuffer] over this array.

View File

@ -35,10 +35,12 @@ public inline fun LongBuffer(size: Int, init: (Int) -> Long): LongBuffer = LongB
public fun LongBuffer(vararg longs: Long): LongBuffer = LongBuffer(longs) 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<out Long>.array: LongArray public fun Buffer<Long>.toLongArray(): LongArray = when(this) {
get() = (if (this is LongBuffer) array else LongArray(size) { get(it) }) is LongBuffer -> array.copyOf()
else -> LongArray(size, ::get)
}
/** /**
* Returns [LongBuffer] over this array. * Returns [LongBuffer] over this array.

View File

@ -40,10 +40,12 @@ public fun RealBuffer(vararg doubles: Double): RealBuffer = RealBuffer(doubles)
public fun RealBuffer.contentEquals(vararg doubles: Double): Boolean = array.contentEquals(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<out Double>.array: DoubleArray public fun Buffer<Double>.toDoubleArray(): DoubleArray = when(this) {
get() = (if (this is RealBuffer) array else DoubleArray(size) { get(it) }) is RealBuffer -> array.copyOf()
else -> DoubleArray(size, ::get)
}
/** /**
* Returns [RealBuffer] over this array. * Returns [RealBuffer] over this array.

View File

@ -33,10 +33,12 @@ public inline fun ShortBuffer(size: Int, init: (Int) -> Short): ShortBuffer = Sh
public fun ShortBuffer(vararg shorts: Short): ShortBuffer = ShortBuffer(shorts) 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<out Short>.array: ShortArray public fun Buffer<Short>.toShortArray(): ShortArray = when(this) {
get() = (if (this is ShortBuffer) array else ShortArray(size) { get(it) }) is ShortBuffer -> array.copyOf()
else -> ShortArray(size, ::get)
}
/** /**
* Returns [ShortBuffer] over this array. * Returns [ShortBuffer] over this array.