From e8b630715a59a81e701a8081ffbc43e54c630de6 Mon Sep 17 00:00:00 2001 From: sergeych Date: Fri, 26 Sep 2025 20:06:54 +0400 Subject: [PATCH] optimized BitArray.equals --- lynglib/src/commonMain/kotlin/net/sergeych/lyng/Script.kt | 2 +- .../kotlin/net/sergeych/lynon/MemoryBitOutput.kt | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lynglib/src/commonMain/kotlin/net/sergeych/lyng/Script.kt b/lynglib/src/commonMain/kotlin/net/sergeych/lyng/Script.kt index 4f89647..bdad379 100644 --- a/lynglib/src/commonMain/kotlin/net/sergeych/lyng/Script.kt +++ b/lynglib/src/commonMain/kotlin/net/sergeych/lyng/Script.kt @@ -183,7 +183,7 @@ class Script( catch( e: ExecutionError ) { e.errorObject } - catch (e: ScriptError) { + catch (_: ScriptError) { ObjNull } result ?: raiseError(ObjAssertionFailedException(this,"Expected exception but nothing was thrown")) diff --git a/lynglib/src/commonMain/kotlin/net/sergeych/lynon/MemoryBitOutput.kt b/lynglib/src/commonMain/kotlin/net/sergeych/lynon/MemoryBitOutput.kt index 7dd8080..efb4ab3 100644 --- a/lynglib/src/commonMain/kotlin/net/sergeych/lynon/MemoryBitOutput.kt +++ b/lynglib/src/commonMain/kotlin/net/sergeych/lynon/MemoryBitOutput.kt @@ -81,7 +81,13 @@ class BitArray(val bytes: UByteArray, val lastByteBits: Int) : BitList { fun asUByteArray(): UByteArray = bytes override fun equals(other: Any?): Boolean { - return other is BitList && this.compareTo(other) == 0 + return when(other) { + is BitArray -> + // important: size must match as trailing zero bits will generate false eq otherwise: + size == other.size && bytes contentEquals other.bytes + is BitList -> compareTo(other) == 0 + else -> false + } } override fun hashCode(): Int {