From 8db5ca9171447b6b1c3c034926057378fffd7b71 Mon Sep 17 00:00:00 2001 From: alex-s168 <63254202+alex-s168@users.noreply.github.com> Date: Fri, 29 Mar 2024 20:02:20 +0100 Subject: [PATCH] matrices --- build.gradle.kts | 2 +- .../kotlin/blitz/collections/FindCommon.kt | 15 ++++ src/main/kotlin/blitz/collections/Matrix.kt | 70 +++++++++++++++++++ 3 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/blitz/collections/FindCommon.kt create mode 100644 src/main/kotlin/blitz/collections/Matrix.kt diff --git a/build.gradle.kts b/build.gradle.kts index 07fa9b9..d39c237 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -5,7 +5,7 @@ plugins { } group = "me.alex_s168" -version = "0.10" +version = "0.11" repositories { mavenCentral() diff --git a/src/main/kotlin/blitz/collections/FindCommon.kt b/src/main/kotlin/blitz/collections/FindCommon.kt new file mode 100644 index 0000000..49550e4 --- /dev/null +++ b/src/main/kotlin/blitz/collections/FindCommon.kt @@ -0,0 +1,15 @@ +package blitz.collections + +/** Returns the element that is at all positions in the list (if there is one) */ +fun Iterable.findCommon(): T? = + firstOrNull()?.let { first -> + if (all { it == first }) first + else null + } + +/** Returns the element that is at all positions in the list (if there is one) */ +fun Sequence.findCommon(): T? = + firstOrNull()?.let { first -> + if (all { it == first }) first + else null + } \ No newline at end of file diff --git a/src/main/kotlin/blitz/collections/Matrix.kt b/src/main/kotlin/blitz/collections/Matrix.kt new file mode 100644 index 0000000..4648fa8 --- /dev/null +++ b/src/main/kotlin/blitz/collections/Matrix.kt @@ -0,0 +1,70 @@ +package blitz.collections + +import kotlin.math.min + +class Matrix( + val width: Int, + val height: Int, + private val init: (x: Int, y: Int) -> T +) { + val rows = MutableList(height) { y -> + MutableList(width) { x -> + init(x, y) + } + } + + operator fun get(x: Int, y: Int): T = + rows[y][x] + + operator fun set(x: Int, y: Int, value: T) { + rows[y][x] = value + } + + fun transposeCopy(): Matrix = + Matrix(width, height) { x, y -> this[y, x] } + + fun perfectSquareCopy(): Matrix = + min(width, height).let { wh -> + Matrix(wh, wh) { x, y -> this[x, y] } + } + + fun copy(): Matrix = + Matrix(width, height) { x, y -> this[x, y] } + + fun diagonalTopLeftToBottomRight(to: MutableList = mutableListOf()): MutableList { + repeat(min(width, height)) { pos -> + to.add(this[pos, pos]) + } + return to + } + + fun diagonalTopRightToBottomLeft(to: MutableList = mutableListOf()): MutableList { + val wh = min(width, height) + for(pos in wh - 1 downTo 0) { + to.add(this[pos, wh - 1 - pos]) + } + return to + } + + fun diagonals(to: MutableList> = mutableListOf()): MutableList> { + to.add(diagonalTopLeftToBottomRight()) + to.add(diagonalTopRightToBottomLeft()) + return to + } + + // TODO: make better + override fun toString(): String = + "--\n" + + rows.joinToString(separator = "\n") { + it.joinToString(separator = ", ", prefix = "| ", postfix = " |") + } + "\n--" +} + +fun main() { + val mat = Matrix(3, 3) { x, y -> x * (3-y) } + println(mat) + + println(mat.transposeCopy()) + + println(mat.diagonals()) +} \ No newline at end of file