From 536b81e27b1ea2ad87bef6b7138863cf149d98cf Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Tue, 20 Feb 2024 21:24:20 +0700 Subject: [PATCH] Array2D.fill --- gradle.properties | 2 +- .../dbotthepony/kommons/arrays/Boolean2DArray.kt | 15 +++++++++++++++ .../ru/dbotthepony/kommons/arrays/Byte2DArray.kt | 12 ++++++++++++ .../ru/dbotthepony/kommons/arrays/Char2DArray.kt | 12 ++++++++++++ .../dbotthepony/kommons/arrays/Double2DArray.kt | 12 ++++++++++++ .../ru/dbotthepony/kommons/arrays/Float2DArray.kt | 12 ++++++++++++ .../ru/dbotthepony/kommons/arrays/Int2DArray.kt | 12 ++++++++++++ .../ru/dbotthepony/kommons/arrays/Long2DArray.kt | 12 ++++++++++++ .../dbotthepony/kommons/arrays/Object2DArray.kt | 4 ++++ .../ru/dbotthepony/kommons/arrays/Short2DArray.kt | 12 ++++++++++++ 10 files changed, 104 insertions(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index 8f3010f..34ff5a3 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,7 +4,7 @@ kotlin.code.style=official specifyKotlinAsDependency=false projectGroup=ru.dbotthepony.kommons -projectVersion=2.7.1 +projectVersion=2.7.2 guavaDepVersion=33.0.0 gsonDepVersion=2.8.9 diff --git a/src/main/kotlin/ru/dbotthepony/kommons/arrays/Boolean2DArray.kt b/src/main/kotlin/ru/dbotthepony/kommons/arrays/Boolean2DArray.kt index 6b677c7..a0346d5 100644 --- a/src/main/kotlin/ru/dbotthepony/kommons/arrays/Boolean2DArray.kt +++ b/src/main/kotlin/ru/dbotthepony/kommons/arrays/Boolean2DArray.kt @@ -18,6 +18,14 @@ abstract class Boolean2DArray : Array2D() { open fun contentEquals(other: Boolean2DArray): Boolean = matrixContentEquals(this, other) { a, b, c, d -> a[c, d] == b[c, d] } + open fun fill(value: Boolean) { + for (x in 0 until rows) { + for (y in 0 until columns) { + this[x, y] = value + } + } + } + override fun equals(other: Any?): Boolean { return matrixEquals(this, other) { a, b, c, d -> a[c, d] == b[c, d] } } @@ -54,6 +62,13 @@ abstract class Boolean2DArray : Array2D() { mem[column + row * columns] = value } + override fun fill(value: Boolean) { + if (value) + mem.set(0, columns * rows) + else + mem.clear(0, columns * rows) + } + override fun equals(other: Any?): Boolean { if (other is Impl) return mem == other.mem return super.equals(other) diff --git a/src/main/kotlin/ru/dbotthepony/kommons/arrays/Byte2DArray.kt b/src/main/kotlin/ru/dbotthepony/kommons/arrays/Byte2DArray.kt index 8216895..8aecdf5 100644 --- a/src/main/kotlin/ru/dbotthepony/kommons/arrays/Byte2DArray.kt +++ b/src/main/kotlin/ru/dbotthepony/kommons/arrays/Byte2DArray.kt @@ -17,6 +17,14 @@ abstract class Byte2DArray : Array2D() { open fun contentEquals(other: Byte2DArray): Boolean = matrixContentEquals(this, other) { a, b, c, d -> a[c, d] == b[c, d] } + open fun fill(value: Byte) { + for (x in 0 until rows) { + for (y in 0 until columns) { + this[x, y] = value + } + } + } + override fun equals(other: Any?): Boolean { return matrixEquals(this, other) { a, b, c, d -> a[c, d] == b[c, d] } } @@ -55,6 +63,10 @@ abstract class Byte2DArray : Array2D() { } } + override fun fill(value: Byte) { + mem.fill(value) + } + override fun equals(other: Any?): Boolean { if (other is Impl) return sizeEquals(other) && mem.contentEquals(other.mem) return super.equals(other) diff --git a/src/main/kotlin/ru/dbotthepony/kommons/arrays/Char2DArray.kt b/src/main/kotlin/ru/dbotthepony/kommons/arrays/Char2DArray.kt index cbdff2f..f22151b 100644 --- a/src/main/kotlin/ru/dbotthepony/kommons/arrays/Char2DArray.kt +++ b/src/main/kotlin/ru/dbotthepony/kommons/arrays/Char2DArray.kt @@ -20,6 +20,14 @@ abstract class Char2DArray : Array2D() { fun load(from: Char2DArray) { matrixLoad(from, this, Char2DArray::get, Char2DArray::set) } fun storeTransposed(from: Char2DArray) { matrixLoadTransposed(from, this, Char2DArray::get, Char2DArray::set) } + open fun fill(value: Char) { + for (x in 0 until rows) { + for (y in 0 until columns) { + this[x, y] = value + } + } + } + open fun contentEquals(other: Char2DArray): Boolean = matrixContentEquals(this, other) { a, b, c, d -> a[c, d] == b[c, d] } override fun equals(other: Any?): Boolean { @@ -60,6 +68,10 @@ abstract class Char2DArray : Array2D() { } } + override fun fill(value: Char) { + mem.fill(value) + } + override fun equals(other: Any?): Boolean { if (other is Impl) return sizeEquals(other) && mem.contentEquals(other.mem) return super.equals(other) diff --git a/src/main/kotlin/ru/dbotthepony/kommons/arrays/Double2DArray.kt b/src/main/kotlin/ru/dbotthepony/kommons/arrays/Double2DArray.kt index 6416ff3..6643993 100644 --- a/src/main/kotlin/ru/dbotthepony/kommons/arrays/Double2DArray.kt +++ b/src/main/kotlin/ru/dbotthepony/kommons/arrays/Double2DArray.kt @@ -20,6 +20,14 @@ abstract class Double2DArray : Array2D() { fun load(from: Double2DArray) { matrixLoad(from, this, Double2DArray::get, Double2DArray::set) } fun storeTransposed(from: Double2DArray) { matrixLoadTransposed(from, this, Double2DArray::get, Double2DArray::set) } + open fun fill(value: Double) { + for (x in 0 until rows) { + for (y in 0 until columns) { + this[x, y] = value + } + } + } + open fun contentEquals(other: Double2DArray): Boolean = matrixContentEquals(this, other) { a, b, c, d -> a[c, d] == b[c, d] } open fun add(other: Double2DArray): Double2DArray = matrixPlainOperator(this, other, Double2DArray::get, Double::plus, Double2DArray::set) @@ -107,6 +115,10 @@ abstract class Double2DArray : Array2D() { } } + override fun fill(value: Double) { + mem.fill(value) + } + override fun equals(other: Any?): Boolean { if (other is Impl) return sizeEquals(other) && mem.contentEquals(other.mem) return super.equals(other) diff --git a/src/main/kotlin/ru/dbotthepony/kommons/arrays/Float2DArray.kt b/src/main/kotlin/ru/dbotthepony/kommons/arrays/Float2DArray.kt index 6866965..701a4c4 100644 --- a/src/main/kotlin/ru/dbotthepony/kommons/arrays/Float2DArray.kt +++ b/src/main/kotlin/ru/dbotthepony/kommons/arrays/Float2DArray.kt @@ -20,6 +20,14 @@ abstract class Float2DArray : Array2D() { fun load(from: Float2DArray) { matrixLoad(from, this, Float2DArray::get, Float2DArray::set) } fun storeTransposed(from: Float2DArray) { matrixLoadTransposed(from, this, Float2DArray::get, Float2DArray::set) } + open fun fill(value: Float) { + for (x in 0 until rows) { + for (y in 0 until columns) { + this[x, y] = value + } + } + } + open fun contentEquals(other: Float2DArray): Boolean = matrixContentEquals(this, other) { a, b, c, d -> a[c, d] == b[c, d] } open fun add(other: Float2DArray): Float2DArray = matrixPlainOperator(this, other, Float2DArray::get, Float::plus, Float2DArray::set) @@ -108,6 +116,10 @@ abstract class Float2DArray : Array2D() { } } + override fun fill(value: Float) { + mem.fill(value) + } + override fun equals(other: Any?): Boolean { if (other is Impl) return sizeEquals(other) && mem.contentEquals(other.mem) return super.equals(other) diff --git a/src/main/kotlin/ru/dbotthepony/kommons/arrays/Int2DArray.kt b/src/main/kotlin/ru/dbotthepony/kommons/arrays/Int2DArray.kt index a48331f..27f7a5e 100644 --- a/src/main/kotlin/ru/dbotthepony/kommons/arrays/Int2DArray.kt +++ b/src/main/kotlin/ru/dbotthepony/kommons/arrays/Int2DArray.kt @@ -20,6 +20,14 @@ abstract class Int2DArray : Array2D() { fun load(from: Int2DArray) { matrixLoad(from, this, Int2DArray::get, Int2DArray::set) } fun storeTransposed(from: Int2DArray) { matrixLoadTransposed(from, this, Int2DArray::get, Int2DArray::set) } + open fun fill(value: Int) { + for (x in 0 until rows) { + for (y in 0 until columns) { + this[x, y] = value + } + } + } + open fun contentEquals(other: Int2DArray): Boolean = matrixContentEquals(this, other) { a, b, c, d -> a[c, d] == b[c, d] } open fun add(other: Int2DArray): Int2DArray = matrixPlainOperator(this, other, Int2DArray::get, Int::plus, Int2DArray::set) @@ -107,6 +115,10 @@ abstract class Int2DArray : Array2D() { } } + override fun fill(value: Int) { + mem.fill(value) + } + override fun equals(other: Any?): Boolean { if (other is Impl) return sizeEquals(other) && mem.contentEquals(other.mem) return super.equals(other) diff --git a/src/main/kotlin/ru/dbotthepony/kommons/arrays/Long2DArray.kt b/src/main/kotlin/ru/dbotthepony/kommons/arrays/Long2DArray.kt index 7beb175..fccb61c 100644 --- a/src/main/kotlin/ru/dbotthepony/kommons/arrays/Long2DArray.kt +++ b/src/main/kotlin/ru/dbotthepony/kommons/arrays/Long2DArray.kt @@ -20,6 +20,14 @@ abstract class Long2DArray : Array2D() { fun load(from: Long2DArray) { matrixLoad(from, this, Long2DArray::get, Long2DArray::set) } fun storeTransposed(from: Long2DArray) { matrixLoadTransposed(from, this, Long2DArray::get, Long2DArray::set) } + open fun fill(value: Long) { + for (x in 0 until rows) { + for (y in 0 until columns) { + this[x, y] = value + } + } + } + open fun contentEquals(other: Long2DArray): Boolean = matrixContentEquals(this, other) { a, b, c, d -> a[c, d] == b[c, d] } open fun add(other: Long2DArray): Long2DArray = matrixPlainOperator(this, other, Long2DArray::get, Long::plus, Long2DArray::set) @@ -107,6 +115,10 @@ abstract class Long2DArray : Array2D() { } } + override fun fill(value: Long) { + mem.fill(value) + } + override fun equals(other: Any?): Boolean { if (other is Impl) return sizeEquals(other) && mem.contentEquals(other.mem) return super.equals(other) diff --git a/src/main/kotlin/ru/dbotthepony/kommons/arrays/Object2DArray.kt b/src/main/kotlin/ru/dbotthepony/kommons/arrays/Object2DArray.kt index 15b5602..b53374f 100644 --- a/src/main/kotlin/ru/dbotthepony/kommons/arrays/Object2DArray.kt +++ b/src/main/kotlin/ru/dbotthepony/kommons/arrays/Object2DArray.kt @@ -64,6 +64,10 @@ class Object2DArray private constructor( return matrixToString(this) { a, b, c -> a[b, c].toString() } } + fun fill(value: T) { + mem.fill(value) + } + fun store(into: Object2DArray) { into.load(this) } diff --git a/src/main/kotlin/ru/dbotthepony/kommons/arrays/Short2DArray.kt b/src/main/kotlin/ru/dbotthepony/kommons/arrays/Short2DArray.kt index d2b57f7..298838e 100644 --- a/src/main/kotlin/ru/dbotthepony/kommons/arrays/Short2DArray.kt +++ b/src/main/kotlin/ru/dbotthepony/kommons/arrays/Short2DArray.kt @@ -20,6 +20,14 @@ abstract class Short2DArray : Array2D() { fun load(from: Short2DArray) { matrixLoad(from, this, Short2DArray::get, Short2DArray::set) } fun storeTransposed(from: Short2DArray) { matrixLoadTransposed(from, this, Short2DArray::get, Short2DArray::set) } + open fun fill(value: Short) { + for (x in 0 until rows) { + for (y in 0 until columns) { + this[x, y] = value + } + } + } + open fun contentEquals(other: Short2DArray): Boolean = matrixContentEquals(this, other) { a, b, c, d -> a[c, d] == b[c, d] } override fun equals(other: Any?): Boolean { @@ -60,6 +68,10 @@ abstract class Short2DArray : Array2D() { } } + override fun fill(value: Short) { + mem.fill(value) + } + override fun equals(other: Any?): Boolean { if (other is Impl) return sizeEquals(other) && mem.contentEquals(other.mem) return super.equals(other)