add matrix

This commit is contained in:
InsanusMokrassar 2021-02-11 14:43:07 +06:00
parent 3dde486126
commit a3cabd7a9e
9 changed files with 69 additions and 0 deletions

View File

@ -2,6 +2,9 @@
## 0.4.25
* `Matrix`:
* Subproject has been created
## 0.4.24
* `Versions`:

7
matrix/build.gradle Normal file
View File

@ -0,0 +1,7 @@
plugins {
id "org.jetbrains.kotlin.multiplatform"
id "org.jetbrains.kotlin.plugin.serialization"
id "com.android.library"
}
apply from: "$mppProjectWithSerializationPresetPath"

View File

@ -0,0 +1,3 @@
package dev.inmo.micro_utils.matrix
typealias Matrix<T> = List<Row<T>>

View File

@ -0,0 +1,10 @@
package dev.inmo.micro_utils.matrix
class MatrixBuilder<T> {
private val mutMatrix: MutableList<List<T>> = ArrayList()
val matrix: Matrix<T>
get() = mutMatrix
fun add(t: List<T>) = mutMatrix.add(t)
operator fun plus(t: List<T>) = add(t)
}

View File

@ -0,0 +1,3 @@
package dev.inmo.micro_utils.matrix
typealias Row<T> = List<T>

View File

@ -0,0 +1,10 @@
package dev.inmo.micro_utils.matrix
class RowBuilder<T> {
private val mutRow: MutableList<T> = ArrayList()
val row: Row<T>
get() = mutRow
fun add(t: T) = mutRow.add(t)
operator fun T.unaryPlus() = add(this)
}

View File

@ -0,0 +1,31 @@
package dev.inmo.micro_utils.matrix
fun <T> matrix(block: MatrixBuilder<T>.() -> Unit): Matrix<T> {
return MatrixBuilder<T>().also(block).matrix
}
fun <T> flatMatrix(block: RowBuilder<T>.() -> Unit): Matrix<T> {
return MatrixBuilder<T>().apply {
row(block)
}.matrix
}
fun <T> flatMatrix(vararg elements: T): Matrix<T> {
return MatrixBuilder<T>().apply {
row { elements.forEach { +it } }
}.matrix
}
fun <T> row(block: RowBuilder<T>.() -> Unit): List<T> {
return RowBuilder<T>().also(block).row
}
fun <T> MatrixBuilder<T>.row(block: RowBuilder<T>.() -> Unit) {
add(RowBuilder<T>().also(block).row)
}
fun <T> MatrixBuilder<T>.row(vararg elements: T) {
add(elements.toList())
}
operator fun <T> RowBuilder<T>.plus(t: T) = add(t)

View File

@ -0,0 +1 @@
<manifest package="dev.inmo.micro_utils.matrix"/>

View File

@ -2,6 +2,7 @@ rootProject.name='micro_utils'
String[] includes = [
":common",
":matrix",
":crypto",
":selector:common",
":pagination:common",