v0.16: matrix stuff
This commit is contained in:
@@ -12,7 +12,7 @@ repositories {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation("me.alex_s168:blitz:0.15")
|
implementation("me.alex_s168:blitz:0.16")
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@@ -5,7 +5,7 @@ plugins {
|
|||||||
}
|
}
|
||||||
|
|
||||||
group = "me.alex_s168"
|
group = "me.alex_s168"
|
||||||
version = "0.15"
|
version = "0.16"
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
package blitz.collections
|
package blitz.collections
|
||||||
|
|
||||||
|
import blitz.str.MutMultiLineString
|
||||||
import kotlin.math.min
|
import kotlin.math.min
|
||||||
|
|
||||||
class Matrix<T>(
|
class Matrix<T>(
|
||||||
@@ -27,9 +28,23 @@ class Matrix<T>(
|
|||||||
rows[pos.second][pos.first] = value
|
rows[pos.second][pos.first] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun row(row: Int): IndexableSequence<T> =
|
||||||
|
rows[row].asSequence().asIndexable()
|
||||||
|
|
||||||
fun column(col: Int): IndexableSequence<T> =
|
fun column(col: Int): IndexableSequence<T> =
|
||||||
generateSequenceWithIndex(height) { row -> this[col, row] }
|
generateSequenceWithIndex(height) { row -> this[col, row] }
|
||||||
|
|
||||||
|
fun elements(): Sequence<T> =
|
||||||
|
rows.asSequence().flatten()
|
||||||
|
|
||||||
|
fun elementsWithIndexes(): Sequence<Pair<T, Pair<Int, Int>>> =
|
||||||
|
rows.asSequence()
|
||||||
|
.flatMapIndexed { row, ts ->
|
||||||
|
ts.mapIndexed { col, t ->
|
||||||
|
t to (col to row)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun fill(fn: (x: Int, y: Int) -> T) {
|
fun fill(fn: (x: Int, y: Int) -> T) {
|
||||||
repeat(height) { row ->
|
repeat(height) { row ->
|
||||||
repeat(width) { col ->
|
repeat(width) { col ->
|
||||||
@@ -95,19 +110,23 @@ class Matrix<T>(
|
|||||||
|
|
||||||
fun toString(alignRight: Boolean): String {
|
fun toString(alignRight: Boolean): String {
|
||||||
val str = stringMat()
|
val str = stringMat()
|
||||||
|
|
||||||
val widths = str.columnWidths()
|
val widths = str.columnWidths()
|
||||||
val out = StringBuilder()
|
val heights = str.rowHeights()
|
||||||
|
|
||||||
val padfn = if (alignRight) String::padStart else String::padEnd
|
val out = MutMultiLineString(' ')
|
||||||
|
|
||||||
|
var y = 0
|
||||||
str.rows.forEachIndexed { row, elems ->
|
str.rows.forEachIndexed { row, elems ->
|
||||||
if (row > 0)
|
var x = 0
|
||||||
out.append('\n')
|
elems.forEachIndexed { col, elem ->
|
||||||
elems.forEachIndexed { col, x ->
|
if (alignRight)
|
||||||
if (col > 0)
|
out[y, x + widths[col] - elem.lines().maxOf { it.length }] = MutMultiLineString.from(elem, ' ')
|
||||||
out.append(' ')
|
else
|
||||||
out.append(padfn(x, widths[col], ' '))
|
out[y, x] = elem
|
||||||
|
x += widths[col] + 1
|
||||||
}
|
}
|
||||||
|
y += heights[row] + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
return out.toString()
|
return out.toString()
|
||||||
@@ -124,7 +143,10 @@ class Matrix<T>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun Matrix<String>.columnWidths(): IntArray =
|
fun Matrix<String>.columnWidths(): IntArray =
|
||||||
IntArray(width) { col -> column(col).maxOf { it.length } }
|
IntArray(width) { col -> column(col).maxOf { it.lines().maxOf { it.length } } }
|
||||||
|
|
||||||
|
fun Matrix<String>.rowHeights(): IntArray =
|
||||||
|
IntArray(height) { row -> row(row).maxOf { it.lines().size } }
|
||||||
|
|
||||||
fun Matrix<String>.lengths(): Matrix<Int> =
|
fun Matrix<String>.lengths(): Matrix<Int> =
|
||||||
Matrix(width, height) { x, y -> this[x, y].length }
|
Matrix(width, height) { x, y -> this[x, y].length }
|
||||||
|
Reference in New Issue
Block a user