From 9a571a072dd3e0f55a215008a857e47385495869 Mon Sep 17 00:00:00 2001 From: alex-s168 <63254202+alex-s168@users.noreply.github.com> Date: Tue, 2 Apr 2024 20:25:45 +0200 Subject: [PATCH] matrix stuff --- src/main/kotlin/blitz/collections/Contents.kt | 12 ++++ src/main/kotlin/blitz/collections/Matrix.kt | 71 +++++++++++++++++-- .../blitz/collections/SequenceCreators.kt | 21 ++++++ src/main/kotlin/blitz/parse/JSON.kt | 17 ++++- 4 files changed, 114 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/blitz/collections/Contents.kt b/src/main/kotlin/blitz/collections/Contents.kt index 2151f22..97c1d37 100644 --- a/src/main/kotlin/blitz/collections/Contents.kt +++ b/src/main/kotlin/blitz/collections/Contents.kt @@ -48,4 +48,16 @@ val Sequence.contents get() = Contents(this.asIterable()) val Array.contents get() = + Contents(this.asIterable()) + +val IntArray.contents get() = + Contents(this.asIterable()) + +val ByteArray.contents get() = + Contents(this.asIterable()) + +val DoubleArray.contents get() = + Contents(this.asIterable()) + +val FloatArray.contents get() = Contents(this.asIterable()) \ No newline at end of file diff --git a/src/main/kotlin/blitz/collections/Matrix.kt b/src/main/kotlin/blitz/collections/Matrix.kt index e856fd9..6afb32d 100644 --- a/src/main/kotlin/blitz/collections/Matrix.kt +++ b/src/main/kotlin/blitz/collections/Matrix.kt @@ -20,6 +20,21 @@ class Matrix( rows[y][x] = value } + fun column(col: Int): IndexableSequence = + generateSequenceWithIndex(height) { row -> this[col, row] } + + fun fill(fn: (x: Int, y: Int) -> T) { + repeat(height) { row -> + repeat(width) { col -> + this[col, row] = fn(col, row) + } + } + } + + fun transpose(dest: Matrix) { + dest.fill { x, y -> this[y, x] } + } + fun transposeCopy(): Matrix = Matrix(width, height) { x, y -> this[y, x] } @@ -28,6 +43,10 @@ class Matrix( Matrix(wh, wh) { x, y -> this[x, y] } } + fun copyTo(dest: Matrix) { + dest.fill { x, y -> this[x, y] } + } + fun copy(): Matrix = Matrix(width, height) { x, y -> this[x, y] } @@ -52,10 +71,50 @@ class Matrix( return to } - // TODO: make better + fun stringMat(): Matrix = + Matrix(width, height) { x, y -> this[x, y].toString() } + + fun stringMatTo(dest: Matrix) { + dest.fill { x, y -> this[x, y].toString() } + } + + fun window(w: Int, h: Int): Matrix> = + Matrix(width / w, height / h) { x, y -> + Matrix(w, h) { sx, sy -> this[x * w + sx, y * w + sy] } + } + + fun toString(alignRight: Boolean): String { + val str = stringMat() + val widths = str.columnWidths() + val out = StringBuilder() + + val padfn = if (alignRight) String::padStart else String::padEnd + + str.rows.forEachIndexed { row, elems -> + if (row > 0) + out.append('\n') + elems.forEachIndexed { col, x -> + if (col > 0) + out.append(' ') + out.append(padfn(x, widths[col], ' ')) + } + } + + return out.toString() + } + override fun toString(): String = - "--\n" + - rows.joinToString(separator = "\n") { - it.joinToString(separator = ", ", prefix = "| ", postfix = " |") - } + "\n--" -} \ No newline at end of file + toString(true) + + override fun equals(other: Any?): Boolean = + rows == other + + override fun hashCode(): Int = + rows.hashCode() +} + +fun Matrix.columnWidths(): IntArray = + IntArray(width) { col -> column(col).maxOf { it.length } } + +fun Matrix.lengths(): Matrix = + Matrix(width, height) { x, y -> this[x, y].length } \ No newline at end of file diff --git a/src/main/kotlin/blitz/collections/SequenceCreators.kt b/src/main/kotlin/blitz/collections/SequenceCreators.kt index ff330b2..3e4749c 100644 --- a/src/main/kotlin/blitz/collections/SequenceCreators.kt +++ b/src/main/kotlin/blitz/collections/SequenceCreators.kt @@ -74,4 +74,25 @@ fun selfInitializingSequence(block: Provider>): Sequence = override fun iterator(): Iterator = Iter() + } + +fun generateSequenceWithIndex(len: Int, fn: (index: Int) -> T): IndexableSequence = + object : IndexableSequence { + override fun get(index: Int): T { + if (index >= len) error("Index $index out of bounds!") + return fn(index) + } + + override fun iterator(): Iterator = + object : Iterator { + private var index = 0 + + override fun hasNext(): Boolean = + index < len + + override fun next(): T { + if (index >= len) error("No next") + return fn(index ++) + } + } } \ No newline at end of file diff --git a/src/main/kotlin/blitz/parse/JSON.kt b/src/main/kotlin/blitz/parse/JSON.kt index ee2d055..169caef 100644 --- a/src/main/kotlin/blitz/parse/JSON.kt +++ b/src/main/kotlin/blitz/parse/JSON.kt @@ -4,15 +4,18 @@ import blitz.parse.comb.* object JSON { lateinit var jsonElement: Parser + val jsonNum = parser { it.map(NumParse.float)?.mapSecond { n -> Number(n) } } + val jsonString = parser { it.require("\"") ?.untilRequire("\"") { str -> Str(str) } } + val jsonArray = parser { it.require("[") ?.array(",") { elem -> @@ -23,6 +26,10 @@ object JSON { ?.require("]") ?.mapSecond { x -> Array(x) } } + + val jsonBool = parser { it.require("true")?.to(Bool(true)) } or + parser { it.require("false")?.to(Bool(false)) } + val jsonObj = parser { it.require("{") ?.array(",") { elem -> @@ -40,7 +47,7 @@ object JSON { } init { - jsonElement = (jsonArray or jsonNum or jsonString or jsonObj).trim() + jsonElement = (jsonArray or jsonNum or jsonString or jsonObj or jsonBool).trim() } interface Element { @@ -48,11 +55,13 @@ object JSON { val num get() = (this as Number).value val str get() = (this as Str).value val obj get() = (this as Obj).value + val bool get() = (this as Bool).value fun isArr() = this is Array fun isNum() = this is Number fun isStr() = this is Str fun isObj() = this is Obj + fun isBool() = this is Bool } data class Array(val value: List): Element { @@ -75,6 +84,12 @@ object JSON { value.map { (k, v) -> "\"$k\": $v" }.joinToString(separator = ", ", prefix = "{", postfix = "}") } + + data class Bool(val value: Boolean): Element { + override fun toString(): String = + value.toString() + } + fun parse(string: String): Element? = jsonElement(Parsable(string))?.second } \ No newline at end of file