Basic Matrix and Matrix Storage implementation #2
@@ -0,0 +1,5 @@
|
|||||||
|
package org.duckdns.davygora.matrix.exception
|
||||||
|
|
||||||
|
class MatrixNotRectangularException(
|
||||||
|
xSizes: Collection<Int>,
|
||||||
|
) : MatrixException("Submitted matrix is not rectangular, different row sizes are $xSizes")
|
||||||
@@ -1,12 +1,12 @@
|
|||||||
package org.duckdns.davygora.matrix.impl
|
package org.duckdns.davygora.matrix.impl
|
||||||
|
|
||||||
import org.duckdns.davygora.matrix.AbstractMutableMatrix
|
import org.duckdns.davygora.matrix.AbstractMutableMatrix
|
||||||
import org.duckdns.davygora.matrix.Matrix
|
|
||||||
import org.duckdns.davygora.matrix.exception.MatrixCoordinateOutOfRangeException
|
import org.duckdns.davygora.matrix.exception.MatrixCoordinateOutOfRangeException
|
||||||
import org.duckdns.davygora.matrix.exception.MatrixInvalidSizeException
|
import org.duckdns.davygora.matrix.exception.MatrixInvalidSizeException
|
||||||
import org.duckdns.davygora.matrix.exception.MatrixNotRectangularException
|
import org.duckdns.davygora.matrix.exception.MatrixNotRectangularException
|
||||||
import org.duckdns.davygora.matrix.exception.MatrixSizeMismatchException
|
import org.duckdns.davygora.matrix.exception.MatrixSizeMismatchException
|
||||||
import org.duckdns.davygora.matrix.storage.MatrixStorage
|
import org.duckdns.davygora.matrix.storage.MatrixStorage
|
||||||
|
import org.duckdns.davygora.matrix.storage.toMatrixStorage
|
||||||
import org.duckdns.davygora.memoize.memoize
|
import org.duckdns.davygora.memoize.memoize
|
||||||
|
|
||||||
class ArrayMatrix<T>(
|
class ArrayMatrix<T>(
|
||||||
@@ -54,4 +54,24 @@ class ArrayMatrix<T>(
|
|||||||
} else {
|
} else {
|
||||||
throw MatrixCoordinateOutOfRangeException(coordName, coord, maxCoord)
|
throw MatrixCoordinateOutOfRangeException(coordName, coord, maxCoord)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
inline fun <reified T> create(values: List<List<T>>): ArrayMatrix<T> {
|
||||||
|
val ySize = values.size
|
||||||
|
val xSize =
|
||||||
|
when {
|
||||||
|
ySize > 0 -> {
|
||||||
|
values.map { row -> row.size }.toSet().let { xSizes ->
|
||||||
|
if (xSizes.size == 1) xSizes.first() else throw MatrixNotRectangularException(xSizes)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ArrayMatrix(xSize, ySize, values.flatten().toMatrixStorage())
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,3 +17,5 @@ value class BooleanStorage(
|
|||||||
|
|
||||||
override fun toString(): String = data.contentToString()
|
override fun toString(): String = data.contentToString()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun BooleanArray.toMatrixStorage() = BooleanStorage(this)
|
||||||
|
|||||||
@@ -17,3 +17,5 @@ value class ByteStorage(
|
|||||||
|
|
||||||
override fun toString(): String = data.contentToString()
|
override fun toString(): String = data.contentToString()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun ByteArray.toMatrixStorage() = ByteStorage(this)
|
||||||
|
|||||||
@@ -17,3 +17,5 @@ value class CharStorage(
|
|||||||
|
|
||||||
override fun toString(): String = data.contentToString()
|
override fun toString(): String = data.contentToString()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun CharArray.toMatrixStorage() = CharStorage(this)
|
||||||
|
|||||||
@@ -17,3 +17,5 @@ value class DoubleStorage(
|
|||||||
|
|
||||||
override fun toString(): String = data.contentToString()
|
override fun toString(): String = data.contentToString()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun DoubleArray.toMatrixStorage() = DoubleStorage(this)
|
||||||
|
|||||||
@@ -17,3 +17,5 @@ value class FloatStorage(
|
|||||||
|
|
||||||
override fun toString(): String = data.contentToString()
|
override fun toString(): String = data.contentToString()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun FloatArray.toMatrixStorage() = FloatStorage(this)
|
||||||
|
|||||||
@@ -17,3 +17,5 @@ value class IntStorage(
|
|||||||
|
|
||||||
override fun toString(): String = data.contentToString()
|
override fun toString(): String = data.contentToString()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun IntArray.toMatrixStorage() = IntStorage(this)
|
||||||
|
|||||||
@@ -17,3 +17,5 @@ value class LongStorage(
|
|||||||
|
|
||||||
override fun toString(): String = data.contentToString()
|
override fun toString(): String = data.contentToString()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun LongArray.toMatrixStorage() = LongStorage(this)
|
||||||
|
|||||||
@@ -10,3 +10,17 @@ sealed interface MatrixStorage<T> {
|
|||||||
value: T,
|
value: T,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Suppress("UNCHECKED_CAST")
|
||||||
|
inline fun <reified T> List<T>.toMatrixStorage() =
|
||||||
|
when (T::class) {
|
||||||
|
Boolean::class -> (this as List<Boolean>).toBooleanArray().toMatrixStorage()
|
||||||
|
Byte::class -> (this as List<Byte>).toByteArray().toMatrixStorage()
|
||||||
|
Char::class -> (this as List<Char>).toCharArray().toMatrixStorage()
|
||||||
|
Double::class -> (this as List<Double>).toDoubleArray().toMatrixStorage()
|
||||||
|
Float::class -> (this as List<Float>).toFloatArray().toMatrixStorage()
|
||||||
|
Int::class -> (this as List<Int>).toIntArray().toMatrixStorage()
|
||||||
|
Long::class -> (this as List<Long>).toLongArray().toMatrixStorage()
|
||||||
|
Short::class -> (this as List<Short>).toShortArray().toMatrixStorage()
|
||||||
|
else -> this.toTypedArray().toMatrixStorage()
|
||||||
|
} as MatrixStorage<T>
|
||||||
|
|||||||
@@ -17,3 +17,5 @@ value class ObjectStorage<T>(
|
|||||||
|
|
||||||
override fun toString(): String = data.contentToString()
|
override fun toString(): String = data.contentToString()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun <T> Array<T>.toMatrixStorage() = ObjectStorage(this)
|
||||||
|
|||||||
@@ -17,3 +17,5 @@ value class ShortStorage(
|
|||||||
|
|
||||||
override fun toString(): String = data.contentToString()
|
override fun toString(): String = data.contentToString()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun ShortArray.toMatrixStorage() = ShortStorage(this)
|
||||||
|
|||||||
Reference in New Issue
Block a user