Adding some extensions

This commit is contained in:
2026-05-12 12:37:53 +02:00
parent 49b6dffdba
commit 512ba38209
2 changed files with 46 additions and 0 deletions
@@ -0,0 +1,24 @@
package org.duckdns.davygora.matrix.impl
import org.duckdns.davygora.matrix.Matrix
import org.duckdns.davygora.matrix.MutableMatrix
import org.duckdns.davygora.matrix.storage.toMatrixStorage
import org.duckdns.davygora.matrix.util.toListChecked
inline fun <reified T> mutableMatrixOf(
xSize: Int,
ySize: Int,
vararg matrix: T,
) = ArrayMatrix(xSize, ySize, matrix.asList().toMatrixStorage()) as MutableMatrix<T>
inline fun <reified T> matrixOf(
xSize: Int,
ySize: Int,
vararg matrix: T,
) = mutableMatrixOf(xSize, ySize, *matrix) as Matrix<T>
inline fun <reified T> Iterable<Any>.toMatrix() = ArrayMatrix.create(this.map { it.toListChecked<T>() })
inline fun <reified T> Array<Any>.toMatrix() = this.asList().toMatrix<T>()
inline fun <reified T> Sequence<Any>.toMatrix() = this.toList().toMatrix<T>()
@@ -0,0 +1,22 @@
package org.duckdns.davygora.matrix.util
@Suppress("UNCHECKED_CAST")
inline fun <reified T> Any.toListChecked(): List<T> =
when (this) {
is List<*> -> this
is Collection<*> -> this.toList()
is Iterable<*> -> this.toList()
is Sequence<*> -> this.toList()
is Array<*> -> this.asList()
is BooleanArray -> this.asList()
is ByteArray -> this.asList()
is CharArray -> this.asList()
is DoubleArray -> this.asList()
is FloatArray -> this.asList()
is IntArray -> this.asList()
is LongArray -> this.asList()
is ShortArray -> this.asList()
else -> error("Unsupported type: ${this::class}")
}.also { list ->
require(list.all { it is T })
} as List<T>