From 6cc27fb6e05a04cddad4a934518ca502d0f1c352 Mon Sep 17 00:00:00 2001
From: DBotThePony <dbotthepony@yandex.ru>
Date: Sat, 6 Apr 2024 02:40:00 +0700
Subject: [PATCH] try catch is much more expensive than manual index check

---
 gradle.properties                                  |  2 +-
 .../ru/dbotthepony/kommons/arrays/Byte2DArray.kt   | 14 ++++++--------
 .../ru/dbotthepony/kommons/arrays/Char2DArray.kt   | 14 ++++++--------
 .../ru/dbotthepony/kommons/arrays/Double2DArray.kt | 14 ++++++--------
 .../ru/dbotthepony/kommons/arrays/Float2DArray.kt  | 14 ++++++--------
 .../ru/dbotthepony/kommons/arrays/Int2DArray.kt    | 14 ++++++--------
 .../ru/dbotthepony/kommons/arrays/Long2DArray.kt   | 14 ++++++--------
 .../ru/dbotthepony/kommons/arrays/Object2DArray.kt | 14 ++++++--------
 .../ru/dbotthepony/kommons/arrays/Short2DArray.kt  | 14 ++++++--------
 9 files changed, 49 insertions(+), 65 deletions(-)

diff --git a/gradle.properties b/gradle.properties
index 1d57092..e7b8ccd 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -4,7 +4,7 @@ kotlin.code.style=official
 specifyKotlinAsDependency=false
 
 projectGroup=ru.dbotthepony.kommons
-projectVersion=2.12.1
+projectVersion=2.12.3
 
 guavaDepVersion=33.0.0
 gsonDepVersion=2.8.9
diff --git a/src/main/kotlin/ru/dbotthepony/kommons/arrays/Byte2DArray.kt b/src/main/kotlin/ru/dbotthepony/kommons/arrays/Byte2DArray.kt
index 8aecdf5..de48e0c 100644
--- a/src/main/kotlin/ru/dbotthepony/kommons/arrays/Byte2DArray.kt
+++ b/src/main/kotlin/ru/dbotthepony/kommons/arrays/Byte2DArray.kt
@@ -48,19 +48,17 @@ abstract class Byte2DArray : Array2D() {
 		private val mem = ByteArray(columns * rows)
 
 		override fun get(column: Int, row: Int): Byte {
-			try {
-				return mem[column + row * columns]
-			} catch (err: IndexOutOfBoundsException) {
+			if (column !in 0 until columns || row !in 0 until rows)
 				throw IndexOutOfBoundsException("Matrix index out of bounds: $column x $row, while this array has dimensions of $columns x $rows")
-			}
+
+			return mem[column + row * columns]
 		}
 
 		override fun set(column: Int, row: Int, value: Byte) {
-			try {
-				mem[column + row * columns] = value
-			} catch (err: IndexOutOfBoundsException) {
+			if (column !in 0 until columns || row !in 0 until rows)
 				throw IndexOutOfBoundsException("Matrix index out of bounds: $column x $row, while this array has dimensions of $columns x $rows")
-			}
+
+			mem[column + row * columns] = value
 		}
 
 		override fun fill(value: Byte) {
diff --git a/src/main/kotlin/ru/dbotthepony/kommons/arrays/Char2DArray.kt b/src/main/kotlin/ru/dbotthepony/kommons/arrays/Char2DArray.kt
index f22151b..8937f06 100644
--- a/src/main/kotlin/ru/dbotthepony/kommons/arrays/Char2DArray.kt
+++ b/src/main/kotlin/ru/dbotthepony/kommons/arrays/Char2DArray.kt
@@ -53,19 +53,17 @@ abstract class Char2DArray : Array2D() {
 		private val mem = CharArray(columns * rows)
 
 		override fun get(column: Int, row: Int): Char {
-			try {
-				return mem[column + row * columns]
-			} catch (err: IndexOutOfBoundsException) {
+			if (column !in 0 until columns || row !in 0 until rows)
 				throw IndexOutOfBoundsException("Matrix index out of bounds: $column x $row, while this array has dimensions of $columns x $rows")
-			}
+
+			return mem[column + row * columns]
 		}
 
 		override fun set(column: Int, row: Int, value: Char) {
-			try {
-				mem[column + row * columns] = value
-			} catch (err: IndexOutOfBoundsException) {
+			if (column !in 0 until columns || row !in 0 until rows)
 				throw IndexOutOfBoundsException("Matrix index out of bounds: $column x $row, while this array has dimensions of $columns x $rows")
-			}
+
+			mem[column + row * columns] = value
 		}
 
 		override fun fill(value: Char) {
diff --git a/src/main/kotlin/ru/dbotthepony/kommons/arrays/Double2DArray.kt b/src/main/kotlin/ru/dbotthepony/kommons/arrays/Double2DArray.kt
index 6643993..536eb4c 100644
--- a/src/main/kotlin/ru/dbotthepony/kommons/arrays/Double2DArray.kt
+++ b/src/main/kotlin/ru/dbotthepony/kommons/arrays/Double2DArray.kt
@@ -100,19 +100,17 @@ abstract class Double2DArray : Array2D() {
 		private val mem = DoubleArray(columns * rows)
 
 		override fun get(column: Int, row: Int): Double {
-			try {
-				return mem[column + row * columns]
-			} catch (err: IndexOutOfBoundsException) {
+			if (column !in 0 until columns || row !in 0 until rows)
 				throw IndexOutOfBoundsException("Matrix index out of bounds: $column x $row, while this array has dimensions of $columns x $rows")
-			}
+
+			return mem[column + row * columns]
 		}
 
 		override fun set(column: Int, row: Int, value: Double) {
-			try {
-				mem[column + row * columns] = value
-			} catch (err: IndexOutOfBoundsException) {
+			if (column !in 0 until columns || row !in 0 until rows)
 				throw IndexOutOfBoundsException("Matrix index out of bounds: $column x $row, while this array has dimensions of $columns x $rows")
-			}
+
+			mem[column + row * columns] = value
 		}
 
 		override fun fill(value: Double) {
diff --git a/src/main/kotlin/ru/dbotthepony/kommons/arrays/Float2DArray.kt b/src/main/kotlin/ru/dbotthepony/kommons/arrays/Float2DArray.kt
index 701a4c4..7a95cbc 100644
--- a/src/main/kotlin/ru/dbotthepony/kommons/arrays/Float2DArray.kt
+++ b/src/main/kotlin/ru/dbotthepony/kommons/arrays/Float2DArray.kt
@@ -101,19 +101,17 @@ abstract class Float2DArray : Array2D() {
 		private val mem = FloatArray(columns * rows)
 
 		override fun get(column: Int, row: Int): Float {
-			try {
-				return mem[column + row * columns]
-			} catch (err: IndexOutOfBoundsException) {
+			if (column !in 0 until columns || row !in 0 until rows)
 				throw IndexOutOfBoundsException("Matrix index out of bounds: $column x $row, while this array has dimensions of $columns x $rows")
-			}
+
+			return mem[column + row * columns]
 		}
 
 		override fun set(column: Int, row: Int, value: Float) {
-			try {
-				mem[column + row * columns] = value
-			} catch (err: IndexOutOfBoundsException) {
+			if (column !in 0 until columns || row !in 0 until rows)
 				throw IndexOutOfBoundsException("Matrix index out of bounds: $column x $row, while this array has dimensions of $columns x $rows")
-			}
+
+			mem[column + row * columns] = value
 		}
 
 		override fun fill(value: Float) {
diff --git a/src/main/kotlin/ru/dbotthepony/kommons/arrays/Int2DArray.kt b/src/main/kotlin/ru/dbotthepony/kommons/arrays/Int2DArray.kt
index 27f7a5e..1d076d1 100644
--- a/src/main/kotlin/ru/dbotthepony/kommons/arrays/Int2DArray.kt
+++ b/src/main/kotlin/ru/dbotthepony/kommons/arrays/Int2DArray.kt
@@ -100,19 +100,17 @@ abstract class Int2DArray : Array2D() {
 		private val mem = IntArray(columns * rows)
 
 		override fun get(column: Int, row: Int): Int {
-			try {
-				return mem[column + row * columns]
-			} catch (err: IndexOutOfBoundsException) {
+			if (column !in 0 until columns || row !in 0 until rows)
 				throw IndexOutOfBoundsException("Matrix index out of bounds: $column x $row, while this array has dimensions of $columns x $rows")
-			}
+
+			return mem[column + row * columns]
 		}
 
 		override fun set(column: Int, row: Int, value: Int) {
-			try {
-				mem[column + row * columns] = value
-			} catch (err: IndexOutOfBoundsException) {
+			if (column !in 0 until columns || row !in 0 until rows)
 				throw IndexOutOfBoundsException("Matrix index out of bounds: $column x $row, while this array has dimensions of $columns x $rows")
-			}
+
+			mem[column + row * columns] = value
 		}
 
 		override fun fill(value: Int) {
diff --git a/src/main/kotlin/ru/dbotthepony/kommons/arrays/Long2DArray.kt b/src/main/kotlin/ru/dbotthepony/kommons/arrays/Long2DArray.kt
index fccb61c..6b289f3 100644
--- a/src/main/kotlin/ru/dbotthepony/kommons/arrays/Long2DArray.kt
+++ b/src/main/kotlin/ru/dbotthepony/kommons/arrays/Long2DArray.kt
@@ -100,19 +100,17 @@ abstract class Long2DArray : Array2D() {
 		private val mem = LongArray(columns * rows)
 
 		override fun get(column: Int, row: Int): Long {
-			try {
-				return mem[column + row * columns]
-			} catch (err: IndexOutOfBoundsException) {
+			if (column !in 0 until columns || row !in 0 until rows)
 				throw IndexOutOfBoundsException("Matrix index out of bounds: $column x $row, while this array has dimensions of $columns x $rows")
-			}
+
+			return mem[column + row * columns]
 		}
 
 		override fun set(column: Int, row: Int, value: Long) {
-			try {
-				mem[column + row * columns] = value
-			} catch (err: IndexOutOfBoundsException) {
+			if (column !in 0 until columns || row !in 0 until rows)
 				throw IndexOutOfBoundsException("Matrix index out of bounds: $column x $row, while this array has dimensions of $columns x $rows")
-			}
+
+			mem[column + row * columns] = value
 		}
 
 		override fun fill(value: Long) {
diff --git a/src/main/kotlin/ru/dbotthepony/kommons/arrays/Object2DArray.kt b/src/main/kotlin/ru/dbotthepony/kommons/arrays/Object2DArray.kt
index b53374f..6078411 100644
--- a/src/main/kotlin/ru/dbotthepony/kommons/arrays/Object2DArray.kt
+++ b/src/main/kotlin/ru/dbotthepony/kommons/arrays/Object2DArray.kt
@@ -83,19 +83,17 @@ class Object2DArray<T> private constructor(
 	}
 
 	operator fun get(column: Int, row: Int): T {
-		try {
-			return mem[column * rows + row] as T
-		} catch (err: IndexOutOfBoundsException) {
+		if (column !in 0 until columns || row !in 0 until rows)
 			throw IndexOutOfBoundsException("Matrix index out of bounds: $column x $row, while this array has dimensions of $columns x $rows")
-		}
+
+		return mem[column * rows + row] as T
 	}
 
 	operator fun set(column: Int, row: Int, value: T) {
-		try {
-			mem[column * rows + row] = value
-		} catch (err: IndexOutOfBoundsException) {
+		if (column !in 0 until columns || row !in 0 until rows)
 			throw IndexOutOfBoundsException("Matrix index out of bounds: $column x $row, while this array has dimensions of $columns x $rows")
-		}
+
+		mem[column * rows + row] = value
 	}
 
 	val transposed get(): Object2DArray<T> {
diff --git a/src/main/kotlin/ru/dbotthepony/kommons/arrays/Short2DArray.kt b/src/main/kotlin/ru/dbotthepony/kommons/arrays/Short2DArray.kt
index 298838e..5fcbd79 100644
--- a/src/main/kotlin/ru/dbotthepony/kommons/arrays/Short2DArray.kt
+++ b/src/main/kotlin/ru/dbotthepony/kommons/arrays/Short2DArray.kt
@@ -53,19 +53,17 @@ abstract class Short2DArray : Array2D() {
 		private val mem = ShortArray(columns * rows)
 
 		override fun get(column: Int, row: Int): Short {
-			try {
-				return mem[column + row * columns]
-			} catch (err: IndexOutOfBoundsException) {
+			if (column !in 0 until columns || row !in 0 until rows)
 				throw IndexOutOfBoundsException("Matrix index out of bounds: $column x $row, while this array has dimensions of $columns x $rows")
-			}
+
+			return mem[column + row * columns]
 		}
 
 		override fun set(column: Int, row: Int, value: Short) {
-			try {
-				mem[column + row * columns] = value
-			} catch (err: IndexOutOfBoundsException) {
+			if (column !in 0 until columns || row !in 0 until rows)
 				throw IndexOutOfBoundsException("Matrix index out of bounds: $column x $row, while this array has dimensions of $columns x $rows")
-			}
+
+			mem[column + row * columns] = value
 		}
 
 		override fun fill(value: Short) {